feat(用户信息): 添加教师身份判断逻辑
在用户信息和认证上下文中添加get_is_teacher函数,用于从不同字段中判断教师身份并统一返回0或1
Showing
2 changed files
with
45 additions
and
0 deletions
| ... | @@ -43,6 +43,24 @@ export function useUserInfo() { | ... | @@ -43,6 +43,24 @@ export function useUserInfo() { |
| 43 | return null | 43 | return null |
| 44 | } | 44 | } |
| 45 | 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 | + | ||
| 46 | /** | 64 | /** |
| 47 | * 刷新用户信息 | 65 | * 刷新用户信息 |
| 48 | * @returns {Promise<Object>} 用户信息对象 | 66 | * @returns {Promise<Object>} 用户信息对象 |
| ... | @@ -68,6 +86,11 @@ export function useUserInfo() { | ... | @@ -68,6 +86,11 @@ export function useUserInfo() { |
| 68 | mergedUser.unread_msg_count = unread | 86 | mergedUser.unread_msg_count = unread |
| 69 | } | 87 | } |
| 70 | 88 | ||
| 89 | + const is_teacher = get_is_teacher(data) | ||
| 90 | + if (is_teacher !== null) { | ||
| 91 | + mergedUser.is_teacher = is_teacher | ||
| 92 | + } | ||
| 93 | + | ||
| 71 | // 更新本地存储 | 94 | // 更新本地存储 |
| 72 | localStorage.setItem('currentUser', JSON.stringify(mergedUser)) | 95 | localStorage.setItem('currentUser', JSON.stringify(mergedUser)) |
| 73 | 96 | ... | ... |
| ... | @@ -48,6 +48,24 @@ export function provideAuth() { | ... | @@ -48,6 +48,24 @@ export function provideAuth() { |
| 48 | return null | 48 | return null |
| 49 | } | 49 | } |
| 50 | 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 | + | ||
| 51 | /** | 69 | /** |
| 52 | * 检查登录token是否过期 | 70 | * 检查登录token是否过期 |
| 53 | * @returns {boolean} true表示token有效,false表示token已过期 | 71 | * @returns {boolean} true表示token有效,false表示token已过期 |
| ... | @@ -84,10 +102,12 @@ export function provideAuth() { | ... | @@ -84,10 +102,12 @@ export function provideAuth() { |
| 84 | const { code, data } = await getUserInfoAPI(); | 102 | const { code, data } = await getUserInfoAPI(); |
| 85 | if (code === 1) { | 103 | if (code === 1) { |
| 86 | const unread = get_unread_msg_count(data) | 104 | const unread = get_unread_msg_count(data) |
| 105 | + const is_teacher = get_is_teacher(data) | ||
| 87 | currentUser.value = { | 106 | currentUser.value = { |
| 88 | ...data.user, | 107 | ...data.user, |
| 89 | ...data.checkin, | 108 | ...data.checkin, |
| 90 | ...(unread !== null ? { unread_msg_count: unread } : {}), | 109 | ...(unread !== null ? { unread_msg_count: unread } : {}), |
| 110 | + ...(is_teacher !== null ? { is_teacher } : {}), | ||
| 91 | } | 111 | } |
| 92 | localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) | 112 | localStorage.setItem('currentUser', JSON.stringify(currentUser.value)) |
| 93 | // 重新设置认证头 | 113 | // 重新设置认证头 |
| ... | @@ -119,10 +139,12 @@ export function provideAuth() { | ... | @@ -119,10 +139,12 @@ export function provideAuth() { |
| 119 | const userRes = await getUserInfoAPI(); | 139 | const userRes = await getUserInfoAPI(); |
| 120 | if (userRes.code === 1) { | 140 | if (userRes.code === 1) { |
| 121 | const unread = get_unread_msg_count(userRes.data) | 141 | const unread = get_unread_msg_count(userRes.data) |
| 142 | + const is_teacher = get_is_teacher(userRes.data) | ||
| 122 | currentUser.value = { | 143 | currentUser.value = { |
| 123 | ...userRes.data.user, | 144 | ...userRes.data.user, |
| 124 | ...userRes.data.checkin, | 145 | ...userRes.data.checkin, |
| 125 | ...(unread !== null ? { unread_msg_count: unread } : {}), | 146 | ...(unread !== null ? { unread_msg_count: unread } : {}), |
| 147 | + ...(is_teacher !== null ? { is_teacher } : {}), | ||
| 126 | } | 148 | } |
| 127 | } else { | 149 | } else { |
| 128 | currentUser.value = { ...data.user, ...data.checkin } | 150 | currentUser.value = { ...data.user, ...data.checkin } | ... | ... |
-
Please register or login to post a comment