Showing
2 changed files
with
68 additions
and
109 deletions
| ... | @@ -10,6 +10,62 @@ import { ref } from 'vue' | ... | @@ -10,6 +10,62 @@ import { ref } from 'vue' |
| 10 | import { getUserInfoAPI } from '@/api/users' | 10 | import { getUserInfoAPI } from '@/api/users' |
| 11 | 11 | ||
| 12 | /** | 12 | /** |
| 13 | + * 构建当前用户对象 | ||
| 14 | + * @param {Object} data - 包含用户信息的响应数据 | ||
| 15 | + * @returns {Object|null} 合并后的当前用户对象,或 null 如果无法构建 | ||
| 16 | + */ | ||
| 17 | + | ||
| 18 | +export const build_current_user = (data) => { | ||
| 19 | + if (!data || typeof data !== 'object') return null | ||
| 20 | + | ||
| 21 | + const merged_user = { | ||
| 22 | + ...(data.user && typeof data.user === 'object' ? data.user : {}), | ||
| 23 | + ...(data.checkin && typeof data.checkin === 'object' ? data.checkin : {}), | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + const unread_candidates = [ | ||
| 27 | + data?.unread_msg_count, | ||
| 28 | + data?.user?.unread_msg_count, | ||
| 29 | + data?.user?.unread, | ||
| 30 | + data?.unread_msg, | ||
| 31 | + data?.unread, | ||
| 32 | + ] | ||
| 33 | + | ||
| 34 | + for (const value of unread_candidates) { | ||
| 35 | + const num = Number(value) | ||
| 36 | + if (Number.isFinite(num)) { | ||
| 37 | + merged_user.unread_msg_count = num | ||
| 38 | + break | ||
| 39 | + } | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + const teacher_candidates = [ | ||
| 43 | + data?.is_teacher, | ||
| 44 | + data?.user?.is_teacher, | ||
| 45 | + data?.user?.teacher, | ||
| 46 | + ] | ||
| 47 | + | ||
| 48 | + for (const value of teacher_candidates) { | ||
| 49 | + if (value === null || value === undefined) continue | ||
| 50 | + if (value === true || value === 'true') { | ||
| 51 | + merged_user.is_teacher = 1 | ||
| 52 | + break | ||
| 53 | + } | ||
| 54 | + if (value === false || value === 'false') { | ||
| 55 | + merged_user.is_teacher = 0 | ||
| 56 | + break | ||
| 57 | + } | ||
| 58 | + const num = Number(value) | ||
| 59 | + if (Number.isFinite(num)) { | ||
| 60 | + merged_user.is_teacher = num ? 1 : 0 | ||
| 61 | + break | ||
| 62 | + } | ||
| 63 | + } | ||
| 64 | + | ||
| 65 | + return merged_user | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +/** | ||
| 13 | * @function useUserInfo | 69 | * @function useUserInfo |
| 14 | * @description 提供用户信息获取功能 | 70 | * @description 提供用户信息获取功能 |
| 15 | * @returns {Object} 包含用户信息状态和方法的对象 | 71 | * @returns {Object} 包含用户信息状态和方法的对象 |
| ... | @@ -21,47 +77,6 @@ export function useUserInfo() { | ... | @@ -21,47 +77,6 @@ export function useUserInfo() { |
| 21 | const error = ref(null) | 77 | const error = ref(null) |
| 22 | 78 | ||
| 23 | /** | 79 | /** |
| 24 | - * 从数据中提取未读消息数量 | ||
| 25 | - * @param {Object} data - 包含用户信息的响应数据 | ||
| 26 | - * @returns {number|null} 未读消息数量,或 null 如果无法提取 | ||
| 27 | - */ | ||
| 28 | - | ||
| 29 | - const get_unread_msg_count = (data) => { | ||
| 30 | - const candidates = [ | ||
| 31 | - data?.unread_msg_count, | ||
| 32 | - data?.user?.unread_msg_count, | ||
| 33 | - data?.user?.unread, | ||
| 34 | - data?.unread_msg, | ||
| 35 | - data?.unread, | ||
| 36 | - ] | ||
| 37 | - | ||
| 38 | - for (const value of candidates) { | ||
| 39 | - const num = Number(value) | ||
| 40 | - if (Number.isFinite(num)) return num | ||
| 41 | - } | ||
| 42 | - | ||
| 43 | - return null | ||
| 44 | - } | ||
| 45 | - | ||
| 46 | - const get_is_teacher = (data) => { | ||
| 47 | - const candidates = [ | ||
| 48 | - data?.is_teacher, | ||
| 49 | - data?.user?.is_teacher, | ||
| 50 | - data?.user?.teacher, | ||
| 51 | - ] | ||
| 52 | - | ||
| 53 | - for (const value of candidates) { | ||
| 54 | - if (value === null || value === undefined) continue | ||
| 55 | - if (value === true || value === 'true') return 1 | ||
| 56 | - if (value === false || value === 'false') return 0 | ||
| 57 | - const num = Number(value) | ||
| 58 | - if (Number.isFinite(num)) return num ? 1 : 0 | ||
| 59 | - } | ||
| 60 | - | ||
| 61 | - return null | ||
| 62 | - } | ||
| 63 | - | ||
| 64 | - /** | ||
| 65 | * 刷新用户信息 | 80 | * 刷新用户信息 |
| 66 | * @returns {Promise<Object>} 用户信息对象 | 81 | * @returns {Promise<Object>} 用户信息对象 |
| 67 | * @throws {Error} 获取失败时抛出错误 | 82 | * @throws {Error} 获取失败时抛出错误 |
| ... | @@ -75,24 +90,12 @@ export function useUserInfo() { | ... | @@ -75,24 +90,12 @@ export function useUserInfo() { |
| 75 | 90 | ||
| 76 | if (code === 1) { | 91 | if (code === 1) { |
| 77 | userInfo.value = data | 92 | userInfo.value = data |
| 78 | - // 合并用户和打卡数据 | 93 | + const mergedUser = build_current_user(data) |
| 79 | - const mergedUser = { | ||
| 80 | - ...data.user, | ||
| 81 | - ...data.checkin | ||
| 82 | - } | ||
| 83 | - | ||
| 84 | - const unread = get_unread_msg_count(data) | ||
| 85 | - if (unread !== null) { | ||
| 86 | - mergedUser.unread_msg_count = unread | ||
| 87 | - } | ||
| 88 | - | ||
| 89 | - const is_teacher = get_is_teacher(data) | ||
| 90 | - if (is_teacher !== null) { | ||
| 91 | - mergedUser.is_teacher = is_teacher | ||
| 92 | - } | ||
| 93 | 94 | ||
| 94 | // 更新本地存储 | 95 | // 更新本地存储 |
| 95 | - localStorage.setItem('currentUser', JSON.stringify(mergedUser)) | 96 | + if (mergedUser) { |
| 97 | + localStorage.setItem('currentUser', JSON.stringify(mergedUser)) | ||
| 98 | + } | ||
| 96 | 99 | ||
| 97 | loading.value = false | 100 | loading.value = false |
| 98 | return mergedUser | 101 | return mergedUser | ... | ... |
| ... | @@ -12,6 +12,7 @@ import { logoutAPI, getUserInfoAPI } from '@/api/users' | ... | @@ -12,6 +12,7 @@ import { logoutAPI, getUserInfoAPI } from '@/api/users' |
| 12 | import { getAuthInfoAPI } from '@/api/auth' | 12 | import { getAuthInfoAPI } from '@/api/auth' |
| 13 | import { clearAuthHeaders, setAuthHeaders } from '@/utils/axios' | 13 | import { clearAuthHeaders, setAuthHeaders } from '@/utils/axios' |
| 14 | import { applyUserInfoAuth, getUserInfoFromStorage, removeUserInfoFromStorage } from '@/utils/auth_user_info' | 14 | import { applyUserInfoAuth, getUserInfoFromStorage, removeUserInfoFromStorage } from '@/utils/auth_user_info' |
| 15 | +import { build_current_user } from '@/composables/useUserInfo' | ||
| 15 | 16 | ||
| 16 | // 创建认证上下文的Symbol key,用于provide/inject依赖注入 | 17 | // 创建认证上下文的Symbol key,用于provide/inject依赖注入 |
| 17 | // 使用Symbol确保key的唯一性,避免命名冲突 | 18 | // 使用Symbol确保key的唯一性,避免命名冲突 |
| ... | @@ -31,41 +32,6 @@ export function provideAuth() { | ... | @@ -31,41 +32,6 @@ export function provideAuth() { |
| 31 | // 加载状态标志,用于控制加载动画等UI展示 | 32 | // 加载状态标志,用于控制加载动画等UI展示 |
| 32 | const loading = ref(true) | 33 | const loading = ref(true) |
| 33 | 34 | ||
| 34 | - const get_unread_msg_count = (data) => { | ||
| 35 | - const candidates = [ | ||
| 36 | - data?.unread_msg_count, | ||
| 37 | - data?.user?.unread_msg_count, | ||
| 38 | - data?.user?.unread, | ||
| 39 | - data?.unread_msg, | ||
| 40 | - data?.unread, | ||
| 41 | - ] | ||
| 42 | - | ||
| 43 | - for (const value of candidates) { | ||
| 44 | - const num = Number(value) | ||
| 45 | - if (Number.isFinite(num)) return num | ||
| 46 | - } | ||
| 47 | - | ||
| 48 | - return null | ||
| 49 | - } | ||
| 50 | - | ||
| 51 | - const get_is_teacher = (data) => { | ||
| 52 | - const candidates = [ | ||
| 53 | - data?.is_teacher, | ||
| 54 | - data?.user?.is_teacher, | ||
| 55 | - data?.user?.teacher, | ||
| 56 | - ] | ||
| 57 | - | ||
| 58 | - for (const value of candidates) { | ||
| 59 | - if (value === null || value === undefined) continue | ||
| 60 | - if (value === true || value === 'true') return 1 | ||
| 61 | - if (value === false || value === 'false') return 0 | ||
| 62 | - const num = Number(value) | ||
| 63 | - if (Number.isFinite(num)) return num ? 1 : 0 | ||
| 64 | - } | ||
| 65 | - | ||
| 66 | - return null | ||
| 67 | - } | ||
| 68 | - | ||
| 69 | /** | 35 | /** |
| 70 | * 检查登录token是否过期 | 36 | * 检查登录token是否过期 |
| 71 | * @returns {boolean} true表示token有效,false表示token已过期 | 37 | * @returns {boolean} true表示token有效,false表示token已过期 |
| ... | @@ -101,15 +67,10 @@ export function provideAuth() { | ... | @@ -101,15 +67,10 @@ export function provideAuth() { |
| 101 | // 从服务器获取用户信息并更新本地存储 | 67 | // 从服务器获取用户信息并更新本地存储 |
| 102 | const { code, data } = await getUserInfoAPI(); | 68 | const { code, data } = await getUserInfoAPI(); |
| 103 | if (code === 1) { | 69 | if (code === 1) { |
| 104 | - const unread = get_unread_msg_count(data) | 70 | + currentUser.value = build_current_user(data) |
| 105 | - const is_teacher = get_is_teacher(data) | 71 | + if (currentUser.value) { |
| 106 | - currentUser.value = { | 72 | + localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) |
| 107 | - ...data.user, | ||
| 108 | - ...data.checkin, | ||
| 109 | - ...(unread !== null ? { unread_msg_count: unread } : {}), | ||
| 110 | - ...(is_teacher !== null ? { is_teacher } : {}), | ||
| 111 | } | 73 | } |
| 112 | - localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) | ||
| 113 | // 重新设置认证头 | 74 | // 重新设置认证头 |
| 114 | const userInfo = getUserInfoFromStorage() | 75 | const userInfo = getUserInfoFromStorage() |
| 115 | if (userInfo && userInfo.user_id && userInfo.HTTP_USER_TOKEN) { | 76 | if (userInfo && userInfo.user_id && userInfo.HTTP_USER_TOKEN) { |
| ... | @@ -138,14 +99,7 @@ export function provideAuth() { | ... | @@ -138,14 +99,7 @@ export function provideAuth() { |
| 138 | try { | 99 | try { |
| 139 | const userRes = await getUserInfoAPI(); | 100 | const userRes = await getUserInfoAPI(); |
| 140 | if (userRes.code === 1) { | 101 | if (userRes.code === 1) { |
| 141 | - const unread = get_unread_msg_count(userRes.data) | 102 | + currentUser.value = build_current_user(userRes.data) |
| 142 | - const is_teacher = get_is_teacher(userRes.data) | ||
| 143 | - currentUser.value = { | ||
| 144 | - ...userRes.data.user, | ||
| 145 | - ...userRes.data.checkin, | ||
| 146 | - ...(unread !== null ? { unread_msg_count: unread } : {}), | ||
| 147 | - ...(is_teacher !== null ? { is_teacher } : {}), | ||
| 148 | - } | ||
| 149 | } else { | 103 | } else { |
| 150 | currentUser.value = { ...data.user, ...data.checkin } | 104 | currentUser.value = { ...data.user, ...data.checkin } |
| 151 | } | 105 | } |
| ... | @@ -154,7 +108,9 @@ export function provideAuth() { | ... | @@ -154,7 +108,9 @@ export function provideAuth() { |
| 154 | currentUser.value = { ...data.user, ...data.checkin } | 108 | currentUser.value = { ...data.user, ...data.checkin } |
| 155 | } | 109 | } |
| 156 | 110 | ||
| 157 | - localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) | 111 | + if (currentUser.value) { |
| 112 | + localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) | ||
| 113 | + } | ||
| 158 | } | 114 | } |
| 159 | } | 115 | } |
| 160 | } | 116 | } | ... | ... |
-
Please register or login to post a comment