refactor(用户信息): 重构用户信息获取逻辑,使用组合式函数替代直接API调用
将直接调用getUserInfoAPI改为使用useUserInfo组合式函数,统一用户信息获取逻辑 移除重复的图片错误处理代码,使用useImageLoader组合式函数
Showing
2 changed files
with
24 additions
and
21 deletions
| ... | @@ -43,15 +43,16 @@ import { ref, computed, onMounted } from 'vue' | ... | @@ -43,15 +43,16 @@ import { ref, computed, onMounted } from 'vue' |
| 43 | import { useRoute, useRouter } from 'vue-router' | 43 | import { useRoute, useRouter } from 'vue-router' |
| 44 | import { useAuth } from '@/contexts/auth' | 44 | import { useAuth } from '@/contexts/auth' |
| 45 | import { showToast } from 'vant' | 45 | import { showToast } from 'vant' |
| 46 | -import { getUserInfoAPI } from '@/api/users' | 46 | +import { useUserInfo } from '@/composables/useUserInfo' |
| 47 | 47 | ||
| 48 | const route = useRoute() | 48 | const route = useRoute() |
| 49 | const router = useRouter() | 49 | const router = useRouter() |
| 50 | const { currentUser } = useAuth() | 50 | const { currentUser } = useAuth() |
| 51 | +const { getUserInfoFromLocal } = useUserInfo() | ||
| 51 | 52 | ||
| 52 | /** | 53 | /** |
| 53 | * @var {import('vue').Ref<number>} unread_msg_count | 54 | * @var {import('vue').Ref<number>} unread_msg_count |
| 54 | - * @description 未读消息数量,来源于 getUserInfoAPI() 的返回值;用于控制“我的”图标右上角红点显示 | 55 | + * @description 未读消息数量,来源于 getUserInfoAPI() 的返回值;用于控制"我的"图标右上角红点显示 |
| 55 | */ | 56 | */ |
| 56 | const unread_msg_count = ref(0) | 57 | const unread_msg_count = ref(0) |
| 57 | 58 | ||
| ... | @@ -62,9 +63,9 @@ const unread_msg_count = ref(0) | ... | @@ -62,9 +63,9 @@ const unread_msg_count = ref(0) |
| 62 | */ | 63 | */ |
| 63 | async function load_unread_msg_count() { | 64 | async function load_unread_msg_count() { |
| 64 | try { | 65 | try { |
| 65 | - const { code, data } = await getUserInfoAPI() | 66 | + const userInfo = getUserInfoFromLocal() |
| 66 | - if (code === 1) { | 67 | + if (userInfo) { |
| 67 | - unread_msg_count.value = +data.unread_msg_count || 0 | 68 | + unread_msg_count.value = +userInfo.unread_msg_count || 0 |
| 68 | } else { | 69 | } else { |
| 69 | unread_msg_count.value = 0 | 70 | unread_msg_count.value = 0 |
| 70 | } | 71 | } | ... | ... |
| ... | @@ -141,13 +141,20 @@ import { useAuth } from "@/contexts/auth"; | ... | @@ -141,13 +141,20 @@ import { useAuth } from "@/contexts/auth"; |
| 141 | import CheckInDialog from "@/components/ui/CheckInDialog.vue"; | 141 | import CheckInDialog from "@/components/ui/CheckInDialog.vue"; |
| 142 | import { getUserInfoAPI } from "@/api/users"; | 142 | import { getUserInfoAPI } from "@/api/users"; |
| 143 | import { showToast } from "vant"; | 143 | import { showToast } from "vant"; |
| 144 | - | ||
| 145 | import { useTitle } from "@vueuse/core"; | 144 | import { useTitle } from "@vueuse/core"; |
| 145 | +import { useImageLoader } from "@/composables/useImageLoader"; | ||
| 146 | +import { useUserInfo } from "@/composables/useUserInfo"; | ||
| 146 | 147 | ||
| 147 | const router = useRouter(); | 148 | const router = useRouter(); |
| 148 | const $route = useRoute(); | 149 | const $route = useRoute(); |
| 149 | useTitle($route.meta.title); | 150 | useTitle($route.meta.title); |
| 150 | 151 | ||
| 152 | +// 图片加载错误处理 | ||
| 153 | +const { handleImageError } = useImageLoader() | ||
| 154 | + | ||
| 155 | +// 用户信息获取 | ||
| 156 | +const { userInfo, refreshUserInfo } = useUserInfo() | ||
| 157 | + | ||
| 151 | const profile = ref({}); | 158 | const profile = ref({}); |
| 152 | const checkIns = ref({}); | 159 | const checkIns = ref({}); |
| 153 | const isTeacher = ref(false); | 160 | const isTeacher = ref(false); |
| ... | @@ -158,18 +165,19 @@ const formatCheckInCount = (count) => { | ... | @@ -158,18 +165,19 @@ const formatCheckInCount = (count) => { |
| 158 | }; | 165 | }; |
| 159 | 166 | ||
| 160 | onMounted(async () => { | 167 | onMounted(async () => { |
| 161 | - const { code, data } = await getUserInfoAPI(); | 168 | + const userData = await refreshUserInfo(); |
| 162 | - if (code === 1) { | 169 | + if (userData) { |
| 163 | - profile.value = data.user; | 170 | + profile.value = userData; |
| 164 | - checkIns.value = data.checkin; | 171 | + checkIns.value = { |
| 165 | - checkIns.value.total_days = data.checkin.total_days || 0 | 172 | + total_days: userData.total_days || 0, |
| 166 | - checkIns.value.consecutive_days = data.checkin.consecutive_days || 0 | 173 | + consecutive_days: userData.consecutive_days || 0, |
| 167 | - checkIns.value.longest_consecutive_days = data.checkin.longest_consecutive_days || 0 | 174 | + longest_consecutive_days: userData.longest_consecutive_days || 0 |
| 168 | - isTeacher.value = data.is_teacher; | 175 | + }; |
| 176 | + isTeacher.value = userData.is_teacher; | ||
| 169 | // 处理消息中心的未读消息数量 | 177 | // 处理消息中心的未读消息数量 |
| 170 | menuItems2.value.forEach(item => { | 178 | menuItems2.value.forEach(item => { |
| 171 | if (item.path === '/profile/messages') { | 179 | if (item.path === '/profile/messages') { |
| 172 | - item.badge = +data.unread_msg_count || 0; | 180 | + item.badge = +(userData?.unread_msg_count || 0); |
| 173 | } | 181 | } |
| 174 | }); | 182 | }); |
| 175 | } | 183 | } |
| ... | @@ -217,12 +225,6 @@ const handleMenuClick = (path) => { | ... | @@ -217,12 +225,6 @@ const handleMenuClick = (path) => { |
| 217 | } | 225 | } |
| 218 | }; | 226 | }; |
| 219 | 227 | ||
| 220 | -// Handle image error | ||
| 221 | -const handleImageError = (e) => { | ||
| 222 | - e.target.onerror = null; | ||
| 223 | - e.target.src = "https://fastly.jsdelivr.net/npm/@vant/assets/cat.jpeg"; | ||
| 224 | -}; | ||
| 225 | - | ||
| 226 | // Handle check-in type click | 228 | // Handle check-in type click |
| 227 | const handleCheckInClick = (path) => { | 229 | const handleCheckInClick = (path) => { |
| 228 | router.push(path); | 230 | router.push(path); | ... | ... |
-
Please register or login to post a comment