diff --git a/src/api/login/index.ts b/src/api/login/index.ts index ab552ec..1acfc8a 100644 --- a/src/api/login/index.ts +++ b/src/api/login/index.ts @@ -1,5 +1,12 @@ import { post } from '@/utils/request'; +import type { LoginResult } from './types'; + +export type { LoginResult } from './types'; export const login = (params: { phone: string; password: string }) => { - return post(`/op/login`, params); + return post(`/op/login`, params) as Promise<{ + code: number; + msg: string; + data: LoginResult; + }>; }; diff --git a/src/api/login/types.ts b/src/api/login/types.ts new file mode 100644 index 0000000..d152867 --- /dev/null +++ b/src/api/login/types.ts @@ -0,0 +1,8 @@ +export interface LoginResult { + token: string; + phone?: string; + nickName?: string; + realName?: string; + userName?: string; + name?: string; +} diff --git a/src/api/menu.ts b/src/api/menu.ts index 7c5d73c..f9be5b1 100644 --- a/src/api/menu.ts +++ b/src/api/menu.ts @@ -3,6 +3,7 @@ import type { MenuNode, PermissionCode } from '@/types'; import { FALLBACK_MENU_NODES } from '@/config/fallbackRoutes'; import { getAuthDataPromise, setAuthDataPromise, clearAuthDataCache } from '@/api/authCache'; import { forceReLogin } from '@/hooks/useAuth'; +import { useUserStore } from '@/stores/userStore'; export { clearAuthDataCache } from '@/api/authCache'; @@ -12,6 +13,8 @@ export { clearAuthDataCache } from '@/api/authCache'; export interface AuthData { roleList: string[]; permsList: string[]; + nickname: string; + phone: string; } const AUTH_FETCH_FAIL_MSG = '账户权限获取失败,请重新登录'; @@ -21,14 +24,26 @@ function normalizeAuthData(res: any): AuthData { throw new Error(res.msg || AUTH_FETCH_FAIL_MSG); } - const roleList = res?.data?.roleList || []; - const permsList = res?.data?.permsList || []; + const data = res?.data || {}; + const roleList = data.roleList || []; + const permsList = data.permsList || []; if (permsList.length === 0) { throw new Error(AUTH_FETCH_FAIL_MSG); } - return { roleList, permsList }; + const nickname = data.nickName || ''; + const phone = typeof data.phone === 'string' ? data.phone : ''; + + if (nickname || phone) { + const { setUser } = useUserStore(); + setUser({ + ...(nickname ? { nickname } : {}), + ...(phone ? { phone } : {}), + }); + } + + return { roleList, permsList, nickname, phone }; } function handleAuthFetchFailure() { diff --git a/src/layouts/BasicLayout.module.less b/src/layouts/BasicLayout.module.less index 2a25135..bf59f0e 100644 --- a/src/layouts/BasicLayout.module.less +++ b/src/layouts/BasicLayout.module.less @@ -62,7 +62,7 @@ align-items: center; } - .userPhone { + .displayName { font-size: 14px; color: rgba(0, 0, 0, 0.65); } diff --git a/src/layouts/BasicLayout.tsx b/src/layouts/BasicLayout.tsx index cd73a31..bcbd633 100644 --- a/src/layouts/BasicLayout.tsx +++ b/src/layouts/BasicLayout.tsx @@ -79,6 +79,7 @@ export default defineComponent({ const { menuItems } = useMenuStore(); const { phone, nickname } = useUserStore(); + const displayName = computed(() => nickname.value || phone.value); const { tabs, cachedViews, @@ -232,7 +233,7 @@ export default defineComponent({ {/* 右侧: 账号信息 + 退出 */}
- {nickname.value} + {displayName.value} {{ default: () => , diff --git a/src/pages/login/index.tsx b/src/pages/login/index.tsx index 481fd37..8f1059c 100644 --- a/src/pages/login/index.tsx +++ b/src/pages/login/index.tsx @@ -60,7 +60,11 @@ export default defineComponent({ return; } - setUser({ phone: form.phone, nickname: res.data.nickname, token: res.data.token }); + setUser({ + phone: res.data.phone || form.phone, + nickname: res.data.nickName || '', + token: res.data.token, + }); if (rememberMe.value) { localStorage.setItem( diff --git a/src/stores/userStore.ts b/src/stores/userStore.ts index 1fc5fc1..6d8074d 100644 --- a/src/stores/userStore.ts +++ b/src/stores/userStore.ts @@ -9,9 +9,32 @@ interface UserState { token: string; } +const USER_PROFILE_KEY = 'cpms_user_profile'; + +function loadUserProfile(): Pick { + try { + const raw = window.localStorage.getItem(USER_PROFILE_KEY); + if (!raw) return { phone: '', nickname: '' }; + const data = JSON.parse(raw); + return { + phone: typeof data.phone === 'string' ? data.phone : '', + nickname: typeof data.nickname === 'string' ? data.nickname : '', + }; + } catch { + window.localStorage.removeItem(USER_PROFILE_KEY); + return { phone: '', nickname: '' }; + } +} + +function persistUserProfile(profile: Pick) { + window.localStorage.setItem(USER_PROFILE_KEY, JSON.stringify(profile)); +} + +const savedProfile = loadUserProfile(); + const state = reactive({ - phone: '', - nickname: '', + phone: savedProfile.phone, + nickname: savedProfile.nickname, token: '', }); @@ -20,12 +43,15 @@ export function useUserStore() { if (user.phone !== undefined) state.phone = user.phone; if (user.nickname !== undefined) state.nickname = user.nickname; if (user.token !== undefined) state.token = user.token; + + persistUserProfile({ phone: state.phone, nickname: state.nickname }); }; const clearUser = () => { state.phone = ''; state.nickname = ''; state.token = ''; + window.localStorage.removeItem(USER_PROFILE_KEY); }; return {