Browse Source

refactor(用户信息): 使用storeToRefs解构userInfo并添加调试日志

重构用户信息相关代码,使用storeToRefs解构userInfo以简化模板中的访问
在setUserAvatar方法中添加调试日志以便跟踪头像设置过程
feige996 11 tháng trước cách đây
mục cha
commit
a18880675b
2 tập tin đã thay đổi với 11 bổ sung8 xóa
  1. 9 8
      src/pages/mine/index.vue
  2. 2 0
      src/store/user.ts

+ 9 - 8
src/pages/mine/index.vue

@@ -8,17 +8,17 @@
 
 <template>
   <view class="profile-container">
-    {{ JSON.stringify(userStore.userInfo) }}
+    {{ JSON.stringify(userInfo) }}
     <!-- 用户信息区域 -->
     <view class="user-info-section">
       <!-- #ifdef MP-WEIXIN -->
       <button class="avatar-button" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
-        <wd-img :src="userStore.userInfo.avatar" width="80px" height="80px" radius="50%"></wd-img>
+        <wd-img :src="userInfo.avatar" width="80px" height="80px" radius="50%"></wd-img>
       </button>
       <!-- #endif -->
       <!-- #ifndef MP-WEIXIN -->
       <view class="avatar-wrapper" @click="run">
-        <wd-img :src="userStore.userInfo.avatar" width="100%" height="100%" radius="50%"></wd-img>
+        <wd-img :src="userInfo.avatar" width="100%" height="100%" radius="50%"></wd-img>
       </view>
       <!-- #endif -->
       <view class="user-details">
@@ -27,13 +27,13 @@
           type="nickname"
           class="weui-input"
           placeholder="请输入昵称"
-          v-model="userStore.userInfo.username"
+          v-model="userInfo.username"
         />
         <!-- #endif -->
         <!-- #ifndef MP-WEIXIN -->
-        <view class="username">{{ userStore.userInfo.username }}</view>
+        <view class="username">{{ userInfo.username }}</view>
         <!-- #endif -->
-        <view class="user-id">ID: {{ userStore.userInfo.id }}</view>
+        <view class="user-id">ID: {{ userInfo.id }}</view>
       </view>
     </view>
 
@@ -93,7 +93,8 @@ import { storeToRefs } from 'pinia'
 import { IUploadSuccessInfo } from '@/api/login.typings'
 
 const userStore = useUserStore()
-
+// 使用storeToRefs解构userInfo
+const { userInfo } = storeToRefs(userStore)
 const toast = useToast()
 const hasLogin = ref(false)
 
@@ -142,7 +143,7 @@ const onChooseAvatar = (e: any) => {
     {
       onSuccess: (res) => {
         console.log('头像上传成功', res)
-        // 更新用户信息
+        useUserStore().setUserAvatar(res)
       },
     },
     avatarUrl,

+ 2 - 0
src/store/user.ts

@@ -36,6 +36,8 @@ export const useUserStore = defineStore(
     }
     const setUserAvatar = (avatar: string) => {
       userInfo.value.avatar = avatar
+      console.log('设置用户头像', avatar)
+      console.log('userInfo', userInfo.value)
     }
     // 删除用户信息
     const removeUserInfo = () => {