Showing
2 changed files
with
52 additions
and
93 deletions
| ... | @@ -10,24 +10,20 @@ import { ref } from 'vue' | ... | @@ -10,24 +10,20 @@ import { ref } from 'vue' |
| 10 | import { getUserInfoAPI } from '@/api/users' | 10 | import { getUserInfoAPI } from '@/api/users' |
| 11 | 11 | ||
| 12 | /** | 12 | /** |
| 13 | - * @function useUserInfo | 13 | + * 构建当前用户对象 |
| 14 | - * @description 提供用户信息获取功能 | ||
| 15 | - * @returns {Object} 包含用户信息状态和方法的对象 | ||
| 16 | - */ | ||
| 17 | -export function useUserInfo() { | ||
| 18 | - // 用户信息状态 | ||
| 19 | - const userInfo = ref(null) | ||
| 20 | - const loading = ref(false) | ||
| 21 | - const error = ref(null) | ||
| 22 | - | ||
| 23 | - /** | ||
| 24 | - * 从数据中提取未读消息数量 | ||
| 25 | * @param {Object} data - 包含用户信息的响应数据 | 14 | * @param {Object} data - 包含用户信息的响应数据 |
| 26 | - * @returns {number|null} 未读消息数量,或 null 如果无法提取 | 15 | + * @returns {Object|null} 合并后的当前用户对象,或 null 如果无法构建 |
| 27 | */ | 16 | */ |
| 28 | 17 | ||
| 29 | - const get_unread_msg_count = (data) => { | 18 | +export const build_current_user = (data) => { |
| 30 | - const candidates = [ | 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 = [ | ||
| 31 | data?.unread_msg_count, | 27 | data?.unread_msg_count, |
| 32 | data?.user?.unread_msg_count, | 28 | data?.user?.unread_msg_count, |
| 33 | data?.user?.unread, | 29 | data?.user?.unread, |
| ... | @@ -35,32 +31,51 @@ export function useUserInfo() { | ... | @@ -35,32 +31,51 @@ export function useUserInfo() { |
| 35 | data?.unread, | 31 | data?.unread, |
| 36 | ] | 32 | ] |
| 37 | 33 | ||
| 38 | - for (const value of candidates) { | 34 | + for (const value of unread_candidates) { |
| 39 | const num = Number(value) | 35 | const num = Number(value) |
| 40 | - if (Number.isFinite(num)) return num | 36 | + if (Number.isFinite(num)) { |
| 37 | + merged_user.unread_msg_count = num | ||
| 38 | + break | ||
| 41 | } | 39 | } |
| 42 | - | ||
| 43 | - return null | ||
| 44 | } | 40 | } |
| 45 | 41 | ||
| 46 | - const get_is_teacher = (data) => { | 42 | + const teacher_candidates = [ |
| 47 | - const candidates = [ | ||
| 48 | data?.is_teacher, | 43 | data?.is_teacher, |
| 49 | data?.user?.is_teacher, | 44 | data?.user?.is_teacher, |
| 50 | data?.user?.teacher, | 45 | data?.user?.teacher, |
| 51 | ] | 46 | ] |
| 52 | 47 | ||
| 53 | - for (const value of candidates) { | 48 | + for (const value of teacher_candidates) { |
| 54 | if (value === null || value === undefined) continue | 49 | if (value === null || value === undefined) continue |
| 55 | - if (value === true || value === 'true') return 1 | 50 | + if (value === true || value === 'true') { |
| 56 | - if (value === false || value === 'false') return 0 | 51 | + merged_user.is_teacher = 1 |
| 52 | + break | ||
| 53 | + } | ||
| 54 | + if (value === false || value === 'false') { | ||
| 55 | + merged_user.is_teacher = 0 | ||
| 56 | + break | ||
| 57 | + } | ||
| 57 | const num = Number(value) | 58 | const num = Number(value) |
| 58 | - if (Number.isFinite(num)) return num ? 1 : 0 | 59 | + if (Number.isFinite(num)) { |
| 60 | + merged_user.is_teacher = num ? 1 : 0 | ||
| 61 | + break | ||
| 59 | } | 62 | } |
| 60 | - | ||
| 61 | - return null | ||
| 62 | } | 63 | } |
| 63 | 64 | ||
| 65 | + return merged_user | ||
| 66 | +} | ||
| 67 | + | ||
| 68 | +/** | ||
| 69 | + * @function useUserInfo | ||
| 70 | + * @description 提供用户信息获取功能 | ||
| 71 | + * @returns {Object} 包含用户信息状态和方法的对象 | ||
| 72 | + */ | ||
| 73 | +export function useUserInfo() { | ||
| 74 | + // 用户信息状态 | ||
| 75 | + const userInfo = ref(null) | ||
| 76 | + const loading = ref(false) | ||
| 77 | + const error = ref(null) | ||
| 78 | + | ||
| 64 | /** | 79 | /** |
| 65 | * 刷新用户信息 | 80 | * 刷新用户信息 |
| 66 | * @returns {Promise<Object>} 用户信息对象 | 81 | * @returns {Promise<Object>} 用户信息对象 |
| ... | @@ -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 | // 更新本地存储 |
| 96 | + if (mergedUser) { | ||
| 95 | localStorage.setItem('currentUser', JSON.stringify(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 = { | ||
| 107 | - ...data.user, | ||
| 108 | - ...data.checkin, | ||
| 109 | - ...(unread !== null ? { unread_msg_count: unread } : {}), | ||
| 110 | - ...(is_teacher !== null ? { is_teacher } : {}), | ||
| 111 | - } | ||
| 112 | localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) | 72 | localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) |
| 73 | + } | ||
| 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,10 +108,12 @@ export function provideAuth() { | ... | @@ -154,10 +108,12 @@ export function provideAuth() { |
| 154 | currentUser.value = { ...data.user, ...data.checkin } | 108 | currentUser.value = { ...data.user, ...data.checkin } |
| 155 | } | 109 | } |
| 156 | 110 | ||
| 111 | + if (currentUser.value) { | ||
| 157 | localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) | 112 | localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) |
| 158 | } | 113 | } |
| 159 | } | 114 | } |
| 160 | } | 115 | } |
| 116 | + } | ||
| 161 | // 初始化完成,关闭加载状态 | 117 | // 初始化完成,关闭加载状态 |
| 162 | loading.value = false | 118 | loading.value = false |
| 163 | }) | 119 | }) | ... | ... |
-
Please register or login to post a comment