fix: send token when uploading user avatar

This commit is contained in:
hajimi
2026-08-03 17:47:50 +08:00
parent 51f6ef4695
commit 6758388f88
2 changed files with 38 additions and 15 deletions
+25 -15
View File
@@ -125,15 +125,21 @@
import { ref, shallowRef, computed } from 'vue'
import { onShow, onUnload } from '@dcloudio/uni-app'
import { getUserInfo, userEdit, userBindMobile, userMnpMobile } from '@/api/user'
import { getVerifyInfo } from '@/api/app'
import { getVerifyInfo, uploadImage } from '@/api/app'
import { FieldType } from '@/enums/appEnums'
import { useAppStore } from '@/stores/app'
import { getToken } from '@/utils/auth'
const appStore = useAppStore()
// 用户信息
const userInfo = ref<any>({})
const defaultAvatar = computed(() => {
const avatar = userInfo.value?.avatar
if (avatar) {
if (avatar.startsWith('http')) return avatar
return `${appStore.config?.domain || ''}${avatar}`
}
const logo = appStore.config?.website?.shop_logo
if (!logo) return '/static/images/avatar.png'
if (logo.startsWith('http')) return logo
@@ -145,22 +151,26 @@ const chooseAvatar = () => {
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
success: async (res) => {
const tempFilePath = res.tempFilePaths[0]
uni.uploadFile({
url: (appStore.config?.domain || '') + '/api/upload/image',
filePath: tempFilePath,
name: 'file',
header: {
token: uni.getStorageSync('token')
},
success: (uploadRes) => {
const data = JSON.parse(uploadRes.data)
if (data.code === 1 && data.data?.uri) {
handleAvatarChange(data.data.uri)
}
const token = getToken()
if (!token) {
uni.navigateTo({ url: '/pages/login/login' })
return
}
uni.showLoading({ title: '上传中...' })
try {
const data: any = await uploadImage(tempFilePath, token)
if (!data?.uri) {
uni.$u.toast('头像上传失败,请重试')
return
}
})
handleAvatarChange(data.uri)
} catch (error) {
uni.$u.toast('头像上传失败,请重试')
} finally {
uni.hideLoading()
}
}
})
}