fix: send token when uploading user avatar
This commit is contained in:
@@ -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()
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user