提交 61ee6482 作者: 方治民

feat: 添加 UserStore 管理,优化持久化存储策略,使得在 APP 下数据不丢失

上级 432a3fe4
import { defineStore } from 'pinia'
import { store } from '/@/store'
import { getAuthCache, setAuthCache } from '@/utils/auth'
import { TOKEN_KEY, USER_INFO_KEY } from '@/enums/cacheEnum'
import type { defs } from '@/api/services'
import Navigate from '@/utils/page/navigate'
export type UserInfo = Partial<defs.UserInfo>
interface UserState {
userInfo: Nullable<UserInfo>
token?: string
loading?: boolean
}
export const useUserStore = defineStore({
id: 'app-user',
state: (): UserState => ({
token: null,
userInfo: null,
loading: false,
}),
getters: {
getToken(): Nullable<string> {
if (this.token) {
return this.token
}
const token = getAuthCache(TOKEN_KEY)
if (token) {
this.token = token
return token
}
},
getUserInfo(): Nullable<UserInfo> {
if (this.userInfo) {
return this.userInfo
}
const info = getAuthCache(USER_INFO_KEY)
if (info) {
const userInfo = JSON.parse(info) as UserInfo
this.userInfo = userInfo
return userInfo
}
return null
},
/**
* @returns 是否为审核账号
*/
isAuditMode(): boolean {
return this.getUserInfo?.mobile === '10000000001'
},
},
actions: {
setToken(token: string) {
this.token = token
setAuthCache(TOKEN_KEY, token)
},
setUserInfo(info: UserInfo) {
this.userInfo = info
setAuthCache(USER_INFO_KEY, info ? JSON.stringify(info) : null)
},
async logout() {
if (this.loading) {
return
}
this.loading = true
try {
await API.auth.logout.request()
} finally {
this.setToken(null)
this.setUserInfo(null)
await Navigate.to('/pages/login/login')
this.loading = false
}
},
async refreshUserInfo() {
if (this.loading) {
return
}
this.loading = true
try {
const userInfo = await API.userView.getUserInfo.request()
this.setUserInfo(userInfo)
} finally {
this.loading = false
}
},
},
})
// Need to be used outside the setup
export function useUserStoreWithOut() {
return useUserStore(store)
}
...@@ -5,24 +5,41 @@ export function getToken() { ...@@ -5,24 +5,41 @@ export function getToken() {
} }
export function getAuthCache(key: string): string | null { export function getAuthCache(key: string): string | null {
let value: string | null = null
// #ifdef APP-PLUS
value = plus.storage.getItem(key)
// #endif
// #ifndef APP-PLUS
try { try {
const value = uni.getStorageSync(key) value = uni.getStorageSync(key)
if (value) { if (value) {
return value return value
} }
} catch (e) {} } catch (e) {}
// #endif
return null return value
} }
export function setAuthCache(key: string, value: string) { export function setAuthCache(key: string, value: string) {
// #ifdef APP-PLUS
plus.storage.setItem(key, value)
// #endif
// #ifndef APP-PLUS
try { try {
uni.setStorageSync(key, value) uni.setStorageSync(key, value)
} catch (e) {} } catch (e) {}
// #endif
} }
export function clearAuthCache() { export function clearAuthCache() {
// #ifdef APP-PLUS
plus.storage.clear()
// #endif
// #ifndef APP-PLUS
try { try {
uni.clearStorageSync() uni.clearStorageSync()
} catch (e) {} } catch (e) {}
// #endif
} }
...@@ -30,8 +30,8 @@ export default { ...@@ -30,8 +30,8 @@ export default {
* 跳转到指定页面 * 跳转到指定页面
* @param page 页面全路径 * @param page 页面全路径
*/ */
to(page: string, options?: UniNamespace.NavigateToOptions) { async to(page: string, options?: UniNamespace.NavigateToOptions) {
uni.navigateTo({ await uni.navigateTo({
url: page, url: page,
...options, ...options,
}) })
......
...@@ -2,6 +2,8 @@ import type { AxiosRequestConfig } from 'axios' ...@@ -2,6 +2,8 @@ import type { AxiosRequestConfig } from 'axios'
declare global { declare global {
type Recordable<T = any> = Record<string, T> type Recordable<T = any> = Record<string, T>
type Nullable<T> = T | null
type NonNullable<T> = T extends null | undefined ? never : T
interface ImportMetaEnv extends ViteEnv { interface ImportMetaEnv extends ViteEnv {
__: unknown __: unknown
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论