Просмотр исходного кода

feat(user): 优化头像下载逻辑并统一ID类型为字符串

- 将 downloadAvatar 函数参数 userId 类型从 string | number 改为 string
- 统一 UserBindingResponse 中 patientUserId 和 boundUserId 类型为 string
- 在 my-doctor 页面中引入 downloadAvatar 方法用于下载医生头像
- 新增 downloadedAvatar 用于缓存已下载的本地头像路径
- 重构 doctorAvatar 计算属性优先使用本地已下载头像
- 在获取医生信息后尝试下载并缓存头像文件
- 添加下载失败时的错误捕获与警告日志输出
mcbaiyun 1 месяц назад
Родитель
Сommit
265cc5f3d2
3 измененных файлов с 33 добавлено и 3 удалено
  1. 1 1
      src/api/user.ts
  2. 2 2
      src/api/userBinding.ts
  3. 30 0
      src/pages/patient/profile/infos/my-doctor.vue

+ 1 - 1
src/api/user.ts

@@ -76,7 +76,7 @@ export function uploadAvatar(filePath: string) {
 }
 
 // 下载头像(封装 uni.downloadFile)
-export async function downloadAvatar(userId: string | number) {
+export async function downloadAvatar(userId: string) {
   const token = uni.getStorageSync('token')
   const res: any = await uni.downloadFile({
     url: `https://wx.baiyun.work/user/avatar/${userId}`,

+ 2 - 2
src/api/userBinding.ts

@@ -8,8 +8,8 @@ export interface BaseQueryRequest {
 export interface UserBindingResponse {
   id: string
   // Snowflake 64位ID 使用字符串保存以避免 JS Number 精度丢失
-  patientUserId: string | number
-  boundUserId: string | number
+  patientUserId: string 
+  boundUserId: string 
   bindingType: string
   status: number
   createTime: string

+ 30 - 0
src/pages/patient/profile/infos/my-doctor.vue

@@ -53,6 +53,7 @@ import { ref, computed } from 'vue'
 import { onLoad } from '@dcloudio/uni-app'
 import CustomNav from '@/components/custom-nav.vue'
 import { listUserBindingsByPatient, type UserBindingResponse, type UserBindingPageResponse } from '@/api/userBinding'
+import { downloadAvatar } from '@/api/user'
 import { getUserInfo } from '@/api/user'
 
 // 为避免与导入的类型冲突,重命名本地接口
@@ -79,7 +80,14 @@ const pageData = ref({
 
 const defaultAvatar = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0'
 
+// 下载后本地临时保存的图片路径(uni.downloadFile 会产出 tempFilePath)
+const downloadedAvatar = ref<string | null>(null)
+
+// 优先使用已下载的本地头像,若无则使用后端返回的 avatar 字段,再次 fallback 到默认头像
 const doctorAvatar = computed(() => {
+  if (downloadedAvatar.value) {
+    return downloadedAvatar.value
+  }
   if (doctorInfo.value?.avatar) {
     return doctorInfo.value.avatar
   }
@@ -157,6 +165,17 @@ const fetchDoctorInfo = async () => {
               introduction: userInfo.introduction || '暂无介绍',
               avatar: userInfo.avatar
             }
+            // 尝试从后端下载用户头像(如果后端未返回 avatar 或想替换为下载后的临时路径)
+            try {
+              if (boundDoctor.boundUserId) {
+                const dlRes: any = await downloadAvatar(String(boundDoctor.boundUserId))
+                if (dlRes && dlRes.statusCode === 200 && dlRes.tempFilePath) {
+                  downloadedAvatar.value = dlRes.tempFilePath
+                }
+              }
+            } catch (err) {
+              console.warn('下载医生头像失败:', err)
+            }
           } else {
             // 如果获取详细信息失败,使用绑定接口返回的基础信息
             doctorInfo.value = {
@@ -169,6 +188,17 @@ const fetchDoctorInfo = async () => {
               specialty: '未知',
               introduction: '暂无介绍'
             }
+            // 尝试下载头像(绑定接口返回的数据中可能没有 avatar)
+            try {
+              if (boundDoctor.boundUserId) {
+                const dlRes: any = await downloadAvatar(String(boundDoctor.boundUserId))
+                if (dlRes && dlRes.statusCode === 200 && dlRes.tempFilePath) {
+                  downloadedAvatar.value = dlRes.tempFilePath
+                }
+              }
+            } catch (err) {
+              console.warn('下载医生头像失败:', err)
+            }
           }
         } catch (error) {
           console.error('获取医生详细信息失败:', error)