上传头像问题
Showing
6 changed files
with
163 additions
and
40 deletions
| ... | @@ -323,7 +323,7 @@ export function uploadImgCorpPhoto(tempFilePath) { | ... | @@ -323,7 +323,7 @@ export function uploadImgCorpPhoto(tempFilePath) { |
| 323 | title: '加载中' | 323 | title: '加载中' |
| 324 | }); | 324 | }); |
| 325 | return uni.uploadFile({ | 325 | return uni.uploadFile({ |
| 326 | url: config.baseUrl_api + '/upload/uploadImgToLocalServerCaiJian', | 326 | url: config.baseUrl_api + '/fileServer/uploadImgToMinioCaiJian', |
| 327 | header: { | 327 | header: { |
| 328 | 'Authorization': uni.getStorageSync('token'), | 328 | 'Authorization': uni.getStorageSync('token'), |
| 329 | }, | 329 | }, |
| ... | @@ -344,6 +344,14 @@ export function getInfo(perId) { | ... | @@ -344,6 +344,14 @@ export function getInfo(perId) { |
| 344 | }) | 344 | }) |
| 345 | } | 345 | } |
| 346 | 346 | ||
| 347 | export function updateInfo(data) { | ||
| 348 | return request({ | ||
| 349 | url: '/person/info', | ||
| 350 | method: 'put', | ||
| 351 | params: data | ||
| 352 | }) | ||
| 353 | } | ||
| 354 | |||
| 347 | // 查询个人会员信息列表 | 355 | // 查询个人会员信息列表 |
| 348 | export function selectPageList(query) { | 356 | export function selectPageList(query) { |
| 349 | return request({ | 357 | return request({ | ... | ... |
| 1 | import CryptoJS from 'crypto-js' | 1 | import CryptoJS from 'crypto-js' |
| 2 | import config from '@/config.js' | ||
| 2 | export function szToHz(num) { | 3 | export function szToHz(num) { |
| 3 | const hzArr = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'] | 4 | const hzArr = ['〇', '一', '二', '三', '四', '五', '六', '七', '八', '九', '十'] |
| 4 | return hzArr[parseInt(num)] | 5 | return hzArr[parseInt(num)] |
| ... | @@ -52,3 +53,24 @@ export function reLaunchHomeByRole() { | ... | @@ -52,3 +53,24 @@ export function reLaunchHomeByRole() { |
| 52 | url: isDaoGuanRole() ? '/pages/index/daoGuanPerson' : '/pages/index/home' | 53 | url: isDaoGuanRole() ? '/pages/index/daoGuanPerson' : '/pages/index/home' |
| 53 | }) | 54 | }) |
| 54 | } | 55 | } |
| 56 | |||
| 57 | export function fillImgUrl(url, prefix) { | ||
| 58 | if (!url) return '' | ||
| 59 | const trimmedUrl = String(url).trim() | ||
| 60 | if (!trimmedUrl) return '' | ||
| 61 | if (trimmedUrl.startsWith('msr:')) { | ||
| 62 | return `${trimBaseUrl(config.baseUrl_api)}/fileServer/download?file=${encodeURIComponent(trimmedUrl)}&downFlag=0` | ||
| 63 | } | ||
| 64 | if (/^(data:|https?:\/\/)/.test(trimmedUrl)) { | ||
| 65 | return trimmedUrl | ||
| 66 | } | ||
| 67 | const baseUrl = prefix | ||
| 68 | ? `${trimBaseUrl(config.baseUrl_api)}/${String(prefix).replace(/^\/+|\/+$/g, '')}` | ||
| 69 | : trimBaseUrl(config.baseUrl_api) | ||
| 70 | const path = trimmedUrl.startsWith('/') ? trimmedUrl : `/${trimmedUrl}` | ||
| 71 | return baseUrl + path | ||
| 72 | } | ||
| 73 | |||
| 74 | function trimBaseUrl(url) { | ||
| 75 | return String(url || '').replace(/\/+$/, '') | ||
| 76 | } | ... | ... |
| 1 | // dev | 1 | // dev |
| 2 | // const baseUrl_api = 'http://192.168.1.222:8787' | 2 | const baseUrl_api = 'http://192.168.1.159:8787' |
| 3 | // const baseUrl_api = 'http://47.98.186.233:8787' | 3 | // const baseUrl_api = 'http://47.98.186.233:8787' |
| 4 | const baseUrl_api = 'https://tk001.wxjylt.com/stage-api/' | 4 | // const baseUrl_api = 'https://tk001.wxjylt.com/stage-api/' |
| 5 | const loginImage_api = 'https://tk001.wxjylt.com/stage-api' | 5 | const loginImage_api = 'https://tk001.wxjylt.com/stage-api' |
| 6 | const payUrl = 'https://wxpay.cmbc.com.cn/mobilePlatform/appserver/lcbpPay.do' | 6 | const payUrl = 'https://wxpay.cmbc.com.cn/mobilePlatform/appserver/lcbpPay.do' |
| 7 | 7 | ... | ... |
| ... | @@ -23,12 +23,8 @@ | ... | @@ -23,12 +23,8 @@ |
| 23 | </view> | 23 | </view> |
| 24 | 24 | ||
| 25 | <view class="user-top"> | 25 | <view class="user-top"> |
| 26 | <view class="avatar-wrap" @click="showLogoutConfirm"> | 26 | <view class="avatar-wrap" @click="handleAvatarUpload"> |
| 27 | <!-- <image class="avatar" v-if="perInfo.photo" :src="config.baseUrl_api + perInfo.photo" | 27 | <image :src="avatarUrl" class="avatar" mode="aspectFill"> |
| 28 | mode="aspectFill"> | ||
| 29 | </image> --> | ||
| 30 | <image :src="config.baseUrl_api + '/fs/static/tx@2x.png'" class="avatar" | ||
| 31 | mode="aspectFill"> | ||
| 32 | </image> | 28 | </image> |
| 33 | </view> | 29 | </view> |
| 34 | <!-- <view class="member-id">{{ userInfo?.userName }}</view>--> | 30 | <!-- <view class="member-id">{{ userInfo?.userName }}</view>--> |
| ... | @@ -219,9 +215,13 @@ import to from 'await-to-js' | ... | @@ -219,9 +215,13 @@ import to from 'await-to-js' |
| 219 | import { | 215 | import { |
| 220 | bindUser, | 216 | bindUser, |
| 221 | unbindUser, | 217 | unbindUser, |
| 222 | downStuCertSingle | 218 | downStuCertSingle, |
| 219 | uploadImgCorpPhoto, | ||
| 220 | getInfo, | ||
| 221 | updateInfo | ||
| 223 | } from '@/common/api.js' | 222 | } from '@/common/api.js' |
| 224 | import {onShow} from "@dcloudio/uni-app"; | 223 | import {onShow} from "@dcloudio/uni-app"; |
| 224 | import { fillImgUrl } from '@/common/utils.js' | ||
| 225 | 225 | ||
| 226 | const userStore = useUserStore() | 226 | const userStore = useUserStore() |
| 227 | const userInfo = computed(() => userStore.user ?? {}) | 227 | const userInfo = computed(() => userStore.user ?? {}) |
| ... | @@ -229,6 +229,21 @@ const perInfo = computed(() => userStore.perInfo ?? {}) | ... | @@ -229,6 +229,21 @@ const perInfo = computed(() => userStore.perInfo ?? {}) |
| 229 | console.log(222, userInfo.value) | 229 | console.log(222, userInfo.value) |
| 230 | console.log(333, perInfo.value) | 230 | console.log(333, perInfo.value) |
| 231 | 231 | ||
| 232 | const isValidPhotoValue = (value) => { | ||
| 233 | if (value === undefined || value === null) return false | ||
| 234 | const photo = String(value).trim() | ||
| 235 | return photo !== '' && photo !== 'null' && photo !== 'undefined' | ||
| 236 | } | ||
| 237 | |||
| 238 | const getAvatarPhoto = (info = {}) => { | ||
| 239 | return [info.photo2, info.perPhoto2, info.photo, info.perPhoto].find(isValidPhotoValue) || '' | ||
| 240 | } | ||
| 241 | |||
| 242 | const avatarUrl = computed(() => { | ||
| 243 | const photo = getAvatarPhoto(perInfo.value) | ||
| 244 | return photo ? fillImgUrl(photo) : config.baseUrl_api + '/fs/static/tx@2x.png' | ||
| 245 | }) | ||
| 246 | |||
| 232 | // 是否已绑定学员:绑定非会员时只有姓名,没有会员卡号,也应视为已绑定,避免反复弹绑定框。 | 247 | // 是否已绑定学员:绑定非会员时只有姓名,没有会员卡号,也应视为已绑定,避免反复弹绑定框。 |
| 233 | const isBound = computed(() => { | 248 | const isBound = computed(() => { |
| 234 | const perName = perInfo.value?.perName | 249 | const perName = perInfo.value?.perName |
| ... | @@ -538,6 +553,89 @@ const goToPay = () => { | ... | @@ -538,6 +553,89 @@ const goToPay = () => { |
| 538 | }); | 553 | }); |
| 539 | }; | 554 | }; |
| 540 | 555 | ||
| 556 | const handleAvatarUpload = () => { | ||
| 557 | if (!isBound.value) { | ||
| 558 | uni.showToast({title: '请先绑定学员', icon: 'none'}) | ||
| 559 | return | ||
| 560 | } | ||
| 561 | uni.chooseImage({ | ||
| 562 | count: 1, | ||
| 563 | sizeType: ['compressed'], | ||
| 564 | sourceType: ['album', 'camera'], | ||
| 565 | success: (res) => { | ||
| 566 | const imgUrl = res.tempFilePaths?.[0] | ||
| 567 | if (!imgUrl) { | ||
| 568 | uni.showToast({title: '未获取到图片地址,请重新选择图片', icon: 'none'}) | ||
| 569 | return | ||
| 570 | } | ||
| 571 | if (typeof wx === 'undefined' || typeof wx.cropImage !== 'function') { | ||
| 572 | uploadAvatarPhoto(imgUrl) | ||
| 573 | return | ||
| 574 | } | ||
| 575 | wx.cropImage({ | ||
| 576 | src: imgUrl, | ||
| 577 | cropScale: '4:5', | ||
| 578 | success: (cropRes) => { | ||
| 579 | uploadAvatarPhoto(cropRes.tempFilePath || imgUrl) | ||
| 580 | }, | ||
| 581 | fail: () => { | ||
| 582 | uploadAvatarPhoto(imgUrl) | ||
| 583 | } | ||
| 584 | }) | ||
| 585 | } | ||
| 586 | }) | ||
| 587 | } | ||
| 588 | |||
| 589 | const uploadAvatarPhoto = async (path) => { | ||
| 590 | uni.showLoading({title: '上传中...', mask: true}) | ||
| 591 | const [uploadErr, uploadRes] = await to(uploadImgCorpPhoto(path)) | ||
| 592 | if (uploadErr || uploadRes?.code !== 200 || !uploadRes?.data) { | ||
| 593 | uni.hideLoading() | ||
| 594 | uni.showToast({title: uploadRes?.msg || '头像上传失败,请重新上传', icon: 'none'}) | ||
| 595 | return | ||
| 596 | } | ||
| 597 | |||
| 598 | const photo = uploadRes.data.fang || '' | ||
| 599 | const photo2 = uploadRes.data.yuan || '' | ||
| 600 | if (!photo || !photo2) { | ||
| 601 | uni.hideLoading() | ||
| 602 | uni.showToast({title: '头像上传返回数据异常', icon: 'none'}) | ||
| 603 | return | ||
| 604 | } | ||
| 605 | |||
| 606 | const currentPerInfo = userStore.perInfo || {} | ||
| 607 | const nextPerInfo = { | ||
| 608 | ...currentPerInfo, | ||
| 609 | photo, | ||
| 610 | photo2 | ||
| 611 | } | ||
| 612 | |||
| 613 | const perId = userInfo.value?.perId || currentPerInfo.perId | ||
| 614 | if (!perId) { | ||
| 615 | uni.hideLoading() | ||
| 616 | uni.showToast({title: '未获取到人员信息,请重新登录后再试', icon: 'none'}) | ||
| 617 | return | ||
| 618 | } | ||
| 619 | |||
| 620 | const [detailErr, detailRes] = await to(getInfo(perId)) | ||
| 621 | const saveParams = { | ||
| 622 | ...(detailErr ? currentPerInfo : (detailRes?.data || currentPerInfo)), | ||
| 623 | perId, | ||
| 624 | photo, | ||
| 625 | photo2 | ||
| 626 | } | ||
| 627 | const [saveErr] = await to(updateInfo(saveParams)) | ||
| 628 | uni.hideLoading() | ||
| 629 | |||
| 630 | if (saveErr) { | ||
| 631 | uni.showToast({title: '头像已上传,保存失败请稍后重试', icon: 'none'}) | ||
| 632 | return | ||
| 633 | } | ||
| 634 | userStore.setPerInfo(nextPerInfo) | ||
| 635 | uni.showToast({title: '头像上传成功', icon: 'success'}) | ||
| 636 | await getWebInfo().catch(() => {}) | ||
| 637 | } | ||
| 638 | |||
| 541 | // 下载电子会员证 | 639 | // 下载电子会员证 |
| 542 | const downCert = async () => { | 640 | const downCert = async () => { |
| 543 | if (!isBound.value) { | 641 | if (!isBound.value) { |
| ... | @@ -547,6 +645,11 @@ const downCert = async () => { | ... | @@ -547,6 +645,11 @@ const downCert = async () => { |
| 547 | const perId = userInfo.value?.perId | 645 | const perId = userInfo.value?.perId |
| 548 | if (!perId) return | 646 | if (!perId) return |
| 549 | 647 | ||
| 648 | if (!perInfo.value.perPhoto || !perInfo.value.perPhoto2) { | ||
| 649 | uni.hideLoading() | ||
| 650 | uni.showToast({title: '请先上传头像照片', icon: 'none'}) | ||
| 651 | return | ||
| 652 | } | ||
| 550 | uni.showLoading({title: '加载中...', mask: true}) | 653 | uni.showLoading({title: '加载中...', mask: true}) |
| 551 | const [err, res] = await to(downStuCertSingle(perId)) | 654 | const [err, res] = await to(downStuCertSingle(perId)) |
| 552 | uni.hideLoading() | 655 | uni.hideLoading() | ... | ... |
| ... | @@ -111,6 +111,7 @@ | ... | @@ -111,6 +111,7 @@ |
| 111 | } from '@dcloudio/uni-app' | 111 | } from '@dcloudio/uni-app' |
| 112 | import config from '@/config.js' | 112 | import config from '@/config.js' |
| 113 | import * as aes2 from '@/common/utils.js' | 113 | import * as aes2 from '@/common/utils.js' |
| 114 | import { fillImgUrl } from '@/common/utils.js' | ||
| 114 | const current = ref(0) | 115 | const current = ref(0) |
| 115 | const agree = ref(false) | 116 | const agree = ref(false) |
| 116 | const perId = ref() | 117 | const perId = ref() |
| ... | @@ -333,10 +334,11 @@ | ... | @@ -333,10 +334,11 @@ |
| 333 | }); | 334 | }); |
| 334 | api.uploadImgCorpPhoto(path).then(data => { | 335 | api.uploadImgCorpPhoto(path).then(data => { |
| 335 | console.log(data) | 336 | console.log(data) |
| 336 | baseFormData.value.photo = data.data.fang; | 337 | const photoData = data.data || {} |
| 337 | baseFormData.value.photo2 = data.data.yuan; | 338 | baseFormData.value.photo = photoData.fang || ''; |
| 339 | baseFormData.value.photo2 = photoData.yuan || ''; | ||
| 338 | photoArr.value = { | 340 | photoArr.value = { |
| 339 | url: config.baseUrl_api + baseFormData.value.photo, | 341 | url: fillImgUrl(baseFormData.value.photo), |
| 340 | name: '头像', | 342 | name: '头像', |
| 341 | extname: 'jpg' | 343 | extname: 'jpg' |
| 342 | } | 344 | } |
| ... | @@ -421,23 +423,13 @@ | ... | @@ -421,23 +423,13 @@ |
| 421 | baseFormData.value.validityDate = res.data.validityDate?.slice(0,10) //去掉时分秒 | 423 | baseFormData.value.validityDate = res.data.validityDate?.slice(0,10) //去掉时分秒 |
| 422 | if (res.data.photo) { | 424 | if (res.data.photo) { |
| 423 | console.log(res.data.photo) | 425 | console.log(res.data.photo) |
| 424 | if (res.data.photo.indexOf('http') == -1) { | ||
| 425 | baseFormData.value.photo = res.data.photo | 426 | baseFormData.value.photo = res.data.photo |
| 426 | let obj = { | 427 | let obj = { |
| 427 | url: config.baseUrl_api + res.data.photo, | 428 | url: fillImgUrl(res.data.photo), |
| 428 | name: '头像', | 429 | name: '头像', |
| 429 | extname: 'jpg' | 430 | extname: 'jpg' |
| 430 | } | 431 | } |
| 431 | photoArr.value = obj | 432 | photoArr.value = obj |
| 432 | } else { | ||
| 433 | baseFormData.value.photo = res.data.photo | ||
| 434 | let obj = { | ||
| 435 | url: res.data.photo, | ||
| 436 | name: '头像', | ||
| 437 | extname: 'jpg' | ||
| 438 | } | ||
| 439 | photoArr.value = obj | ||
| 440 | } | ||
| 441 | 433 | ||
| 442 | } | 434 | } |
| 443 | // baseFormData.value.name = res.data.name | 435 | // baseFormData.value.name = res.data.name |
| ... | @@ -494,7 +486,6 @@ | ... | @@ -494,7 +486,6 @@ |
| 494 | // } | 486 | // } |
| 495 | } | 487 | } |
| 496 | 488 | ||
| 497 | |||
| 498 | function changeIdcType(e) { | 489 | function changeIdcType(e) { |
| 499 | console.log(e) | 490 | console.log(e) |
| 500 | // 切换证件照类型把当前页面数据清空 | 491 | // 切换证件照类型把当前页面数据清空 | ... | ... |
| ... | @@ -56,7 +56,7 @@ | ... | @@ -56,7 +56,7 @@ |
| 56 | 56 | ||
| 57 | <script setup> | 57 | <script setup> |
| 58 | import * as api from '@/common/api.js' | 58 | import * as api from '@/common/api.js' |
| 59 | import config from '@/config.js' | 59 | import { fillImgUrl } from '@/common/utils.js' |
| 60 | import { | 60 | import { |
| 61 | onMounted, | 61 | onMounted, |
| 62 | ref | 62 | ref |
| ... | @@ -92,13 +92,9 @@ | ... | @@ -92,13 +92,9 @@ |
| 92 | query.value.showMyPersonFlag = 1 | 92 | query.value.showMyPersonFlag = 1 |
| 93 | } | 93 | } |
| 94 | api.selectPageList(query.value).then(res => { | 94 | api.selectPageList(query.value).then(res => { |
| 95 | for (var l of res.rows) { | 95 | const rows = formatRows(res.rows || []) |
| 96 | if (l.photo && l.photo.indexOf('http') == -1) { | 96 | list.value = rows |
| 97 | l.photo = config.baseUrl_api + l.photo | 97 | paging.value.complete(rows) |
| 98 | } | ||
| 99 | } | ||
| 100 | list.value = res.rows | ||
| 101 | paging.value.complete(res.rows) | ||
| 102 | total.value = res.total | 98 | total.value = res.total |
| 103 | }) | 99 | }) |
| 104 | } | 100 | } |
| ... | @@ -112,18 +108,21 @@ | ... | @@ -112,18 +108,21 @@ |
| 112 | query.value.showMyPersonFlag = 1 | 108 | query.value.showMyPersonFlag = 1 |
| 113 | } | 109 | } |
| 114 | api.selectPageList(query.value).then(res => { | 110 | api.selectPageList(query.value).then(res => { |
| 115 | for (var l of res.rows) { | 111 | const rows = formatRows(res.rows || []) |
| 116 | if (l.photo && l.photo.indexOf('http') == -1) { | 112 | list.value = rows |
| 117 | l.photo = config.baseUrl_api + l.photo | 113 | paging.value.complete(rows) |
| 118 | } | ||
| 119 | } | ||
| 120 | list.value = res.rows | ||
| 121 | paging.value.complete(res.rows) | ||
| 122 | total.value = res.total | 114 | total.value = res.total |
| 123 | uni.hideLoading() | 115 | uni.hideLoading() |
| 124 | }) | 116 | }) |
| 125 | } | 117 | } |
| 126 | 118 | ||
| 119 | function formatRows(rows) { | ||
| 120 | return rows.map(item => ({ | ||
| 121 | ...item, | ||
| 122 | photo: fillImgUrl(item.photo) | ||
| 123 | })) | ||
| 124 | } | ||
| 125 | |||
| 127 | function handleDelete(item) { | 126 | function handleDelete(item) { |
| 128 | uni.showModal({ | 127 | uni.showModal({ |
| 129 | content: `是否确认删除${item.name}`, | 128 | content: `是否确认删除${item.name}`, | ... | ... |
-
Please register or sign in to post a comment