| | |
| | | import {getEnvObj} from "@/utils/index.js"; |
| | | import {getUserInfo, updateInfo} from '@/api/user/index.js'; |
| | | import {useUserStore} from "@/store/index.js"; |
| | | import {ref} from "vue"; |
| | | import {onShow} from "@dcloudio/uni-app"; |
| | | |
| | | const userInfo = ref({ |
| | | id: '', |
| | |
| | | if (!phone) return true |
| | | const phoneRegex = /^1[3-9]\d{9}$/ |
| | | if (!phoneRegex.test(phone)) { |
| | | |
| | | uni.showToast({ |
| | | title: '请输入正确的手机号码', |
| | | icon: 'none', |
| | |
| | | // 头像上传到服务器 |
| | | function uploadUtil(options) { |
| | | const {formData, filePath, url} = options; |
| | | |
| | | uni.showLoading({title: '上传中...'}) |
| | | return new Promise((resolve, reject) => { |
| | | let accessToken = useUserStore()?.$state?.userInfo?.access_token; |
| | | uni.uploadFile({ |
| | |
| | | }, |
| | | fail: (err) => { |
| | | reject(err) |
| | | } |
| | | }); |
| | | }) |
| | | } |
| | | |
| | | // 上传头像功能 |
| | | const uploadAvatar = () => { |
| | | // #ifdef MP-WEIXIN |
| | | uni.chooseMedia({ |
| | | count: 1, |
| | | mediaType: ['image'], |
| | | sourceType: ['album', 'camera'], |
| | | maxDuration: 30, |
| | | camera: 'back', |
| | | success(res) { |
| | | uploadUtil({ |
| | | filePath: res.tempFiles[0].tempFilePath, |
| | | formData: { |
| | | fileName: res.tempFiles[0].tempFilePath, |
| | | sn: 'avatar_upload' |
| | | }, |
| | | url: '/blade-resource/oss/endpoint/put-file' |
| | | }).then(res => { |
| | | const resData = JSON.parse(res.data); |
| | | if (resData.code === 200 || resData.code === 0) { |
| | | // 更新头像显示 |
| | | userInfo.value.avatar = resData.data.link || resData.data.url; |
| | | uni.hideLoading(); |
| | | uni.showToast({title: '头像上传成功', icon: 'success'}); |
| | | } else { |
| | | throw new Error(resData.message || '上传失败'); |
| | | complete: () => { |
| | | uni.hideLoading() |
| | | } |
| | | }); |
| | | }) |
| | | } |
| | | }) |
| | | // #endif |
| | | |
| | | // #ifndef MP-WEIXIN |
| | | uni.chooseImage({ |
| | | count: 1, |
| | | success: (res) => { |
| | | const tempFile = res.tempFiles[0]; // 获取文件对象 |
| | | const filePath = tempFile.path || tempFile.tempFilePath; |
| | | if (!filePath) { |
| | | uni.showToast({ |
| | | title: '获取文件路径失败', |
| | | icon: 'none' |
| | | }); |
| | | return; |
| | | } |
| | | let fileName = tempFile.name; |
| | | if (!fileName) { |
| | | const pathWithoutProtocol = filePath.replace(/^file:\/\//, ''); |
| | | fileName = pathWithoutProtocol.split('/').pop() || 'unknown.png'; |
| | | } |
| | | |
| | | // 显示加载中 |
| | | uni.showLoading({ |
| | | title: '上传中...' |
| | | }); |
| | | |
| | | // 上传文件 |
| | | const handleUploadResult = (filePath, fileName) => { |
| | | uploadUtil({ |
| | | filePath: filePath, |
| | | formData: { |
| | |
| | | if (resData.code === 200 || resData.code === 0) { |
| | | // 更新头像显示 |
| | | userInfo.value.avatar = resData.data.link || resData.data.url; |
| | | uni.hideLoading(); |
| | | uni.showToast({title: '头像上传成功', icon: 'success'}); |
| | | } else { |
| | | throw new Error(resData.message || '上传失败'); |
| | | } |
| | | }).catch(err => { |
| | | uni.hideLoading(); |
| | | uni.showToast({ |
| | | title: err.message || '上传失败', |
| | | icon: 'none' |
| | | uni.showToast({title: err.message || '上传失败', icon: 'none'}); |
| | | }); |
| | | }); |
| | | }; |
| | | |
| | | // 上传头像功能 |
| | | const uploadAvatar = () => { |
| | | // #ifdef MP-WEIXIN |
| | | uni.chooseMedia({ |
| | | count: 1, |
| | | mediaType: ['image'], |
| | | sourceType: ['album', 'camera'], |
| | | maxDuration: 30, |
| | | camera: 'back', |
| | | success(res) { |
| | | const filePath = res.tempFiles[0].tempFilePath; |
| | | handleUploadResult(filePath, filePath); |
| | | } |
| | | }) |
| | | // #endif |
| | | |
| | | // #ifndef MP-WEIXIN |
| | | uni.chooseImage({ |
| | | count: 1, |
| | | success: (res) => { |
| | | const tempFile = res.tempFiles[0]; // 获取文件对象 |
| | | const filePath = tempFile.path || tempFile.tempFilePath; |
| | | if (!filePath) { |
| | | uni.showToast({title: '获取文件路径失败', icon: 'none'}); |
| | | return; |
| | | } |
| | | let fileName = tempFile.name; |
| | | if (!fileName) { |
| | | const pathWithoutProtocol = filePath.replace(/^file:\/\//, ''); |
| | | fileName = pathWithoutProtocol.split('/').pop() || 'unknown.png'; |
| | | } |
| | | // 上传文件 |
| | | handleUploadResult(filePath, fileName); |
| | | } |
| | | }); |
| | | // #endif |