|
|
@@ -76,6 +76,7 @@ import { ref } from 'vue'
|
|
|
const loading = ref(false)
|
|
|
import { onShow } from '@dcloudio/uni-app'
|
|
|
import CustomNav from '@/components/custom-nav.vue'
|
|
|
+import { fetchUserInfo as fetchUserInfoApi, downloadAvatar as downloadAvatarApi, uploadAvatar as uploadAvatarApi, updateUserInfo as updateUserInfoApi, geocodeNearest as geocodeNearestApi } from '@/api/user'
|
|
|
|
|
|
const form = ref({
|
|
|
avatar: '',
|
|
|
@@ -103,15 +104,7 @@ const fetchUserInfo = async () => {
|
|
|
loading.value = true
|
|
|
// 使用 mask: true 阻止用户在加载/下载时继续交互
|
|
|
uni.showLoading({ title: '加载中...', mask: true })
|
|
|
- const response = await uni.request({
|
|
|
- url: 'https://wx.baiyun.work/user_info',
|
|
|
- method: 'POST',
|
|
|
- header: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- 'Authorization': `Bearer ${token}`
|
|
|
- },
|
|
|
- data: {}
|
|
|
- })
|
|
|
+ const response = await fetchUserInfoApi()
|
|
|
|
|
|
console.log('fetchUserInfo response:', response)
|
|
|
if (response.statusCode === 401) {
|
|
|
@@ -129,12 +122,7 @@ const fetchUserInfo = async () => {
|
|
|
const userId = d.id || d.userId
|
|
|
if (userId) {
|
|
|
try {
|
|
|
- const downloadRes = await uni.downloadFile({
|
|
|
- url: `https://wx.baiyun.work/user/avatar/${userId}`,
|
|
|
- header: {
|
|
|
- Authorization: `Bearer ${token}`
|
|
|
- }
|
|
|
- })
|
|
|
+ const downloadRes = await downloadAvatarApi(userId)
|
|
|
if (downloadRes.statusCode === 200 && downloadRes.tempFilePath) {
|
|
|
d.avatar = downloadRes.tempFilePath
|
|
|
}
|
|
|
@@ -190,59 +178,32 @@ const setSex = (s: number) => {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 上传头像到 `user/avatar/upload`,返回上传后的 URL(如果服务器返回)
|
|
|
+ * 使用 `src/api/user.ts` 中的 `uploadAvatar` 封装上传并返回上传后的 URL
|
|
|
*/
|
|
|
-const uploadAvatar = (filePath: string) => {
|
|
|
- return new Promise<string>((resolve, reject) => {
|
|
|
- if (!filePath) {
|
|
|
- reject(new Error('no filePath'))
|
|
|
- return
|
|
|
+const uploadAvatar = async (filePath: string) => {
|
|
|
+ if (!filePath) throw new Error('no filePath')
|
|
|
+ avatarUploading.value = true
|
|
|
+ uni.showLoading({ title: '上传头像中...' })
|
|
|
+ try {
|
|
|
+ const resp: any = await uploadAvatarApi(filePath)
|
|
|
+ uni.hideLoading()
|
|
|
+ avatarUploading.value = false
|
|
|
+ if (resp && resp.code === 200) {
|
|
|
+ const uploadedUrl = resp.data?.url || resp.data?.path || resp.data?.fileUrl || ''
|
|
|
+ if (uploadedUrl) form.value.avatar = uploadedUrl
|
|
|
+ avatarNeedsUpload.value = false
|
|
|
+ uni.showToast({ title: '头像上传成功', icon: 'success' })
|
|
|
+ return uploadedUrl || ''
|
|
|
}
|
|
|
- avatarUploading.value = true
|
|
|
- uni.showLoading({ title: '上传头像中...' })
|
|
|
- const token = uni.getStorageSync('token')
|
|
|
- uni.uploadFile({
|
|
|
- url: 'https://wx.baiyun.work/user/avatar/upload',
|
|
|
- filePath,
|
|
|
- name: 'file',
|
|
|
- header: {
|
|
|
- Authorization: `Bearer ${token}`
|
|
|
- },
|
|
|
- success: (uploadRes: any) => {
|
|
|
- uni.hideLoading()
|
|
|
- try {
|
|
|
- const resp = JSON.parse(uploadRes.data || '{}') as any
|
|
|
- if (resp && resp.code === 200) {
|
|
|
- // 尝试从响应中取出 URL 字段(后端可能返回 data.url 或 data.path 等)
|
|
|
- const uploadedUrl = resp.data?.url || resp.data?.path || resp.data?.fileUrl || ''
|
|
|
- if (uploadedUrl) {
|
|
|
- form.value.avatar = uploadedUrl
|
|
|
- }
|
|
|
- // 标记为已上传
|
|
|
- avatarNeedsUpload.value = false
|
|
|
- uni.showToast({ title: '头像上传成功', icon: 'success' })
|
|
|
- resolve(uploadedUrl || '')
|
|
|
- } else {
|
|
|
- uni.showToast({ title: resp?.message || '上传失败', icon: 'none' })
|
|
|
- reject(new Error(resp?.message || 'upload failed'))
|
|
|
- }
|
|
|
- } catch (err) {
|
|
|
- console.error('upload parse error', err)
|
|
|
- uni.showToast({ title: '头像上传失败', icon: 'none' })
|
|
|
- reject(err)
|
|
|
- }
|
|
|
- },
|
|
|
- fail: (err: any) => {
|
|
|
- uni.hideLoading()
|
|
|
- console.error('upload fail', err)
|
|
|
- uni.showToast({ title: '头像上传失败', icon: 'none' })
|
|
|
- reject(err)
|
|
|
- },
|
|
|
- complete: () => {
|
|
|
- avatarUploading.value = false
|
|
|
- }
|
|
|
- })
|
|
|
- })
|
|
|
+ uni.showToast({ title: resp?.message || '上传失败', icon: 'none' })
|
|
|
+ throw new Error(resp?.message || 'upload failed')
|
|
|
+ } catch (err) {
|
|
|
+ uni.hideLoading()
|
|
|
+ avatarUploading.value = false
|
|
|
+ console.error('upload error', err)
|
|
|
+ uni.showToast({ title: '头像上传失败', icon: 'none' })
|
|
|
+ throw err
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -358,14 +319,7 @@ const getCurrentLocation = async () => {
|
|
|
|
|
|
// 调用用户自己的地理编码接口
|
|
|
const token = uni.getStorageSync('token')
|
|
|
- const geocodeRes = await uni.request({
|
|
|
- url: `https://wx.baiyun.work/geo/nearest?latitude=${latitude}&longitude=${longitude}`,
|
|
|
- method: 'GET',
|
|
|
- header: {
|
|
|
- 'Authorization': `Bearer ${token}`
|
|
|
- }
|
|
|
- })
|
|
|
-
|
|
|
+ const geocodeRes = await geocodeNearestApi(latitude, longitude)
|
|
|
const data = geocodeRes.data as any
|
|
|
if (data && data.code === 200 && data.data) {
|
|
|
// 解析 data 字段中的 JSON 字符串
|
|
|
@@ -459,15 +413,7 @@ const onSubmit = async () => {
|
|
|
const payload: any = JSON.parse(JSON.stringify(form.value))
|
|
|
if (payload.avatar !== undefined) delete payload.avatar
|
|
|
|
|
|
- const response = await uni.request({
|
|
|
- url: 'https://wx.baiyun.work/update_user_info',
|
|
|
- method: 'POST',
|
|
|
- header: {
|
|
|
- 'Content-Type': 'application/json',
|
|
|
- 'Authorization': `Bearer ${token}`
|
|
|
- },
|
|
|
- data: payload
|
|
|
- })
|
|
|
+ const response = await updateUserInfoApi(payload)
|
|
|
console.log('Update response:', response)
|
|
|
const resp = response.data as any
|
|
|
if (resp && resp.code === 200) {
|