58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import { defineStore } from 'pinia'
|
||
import { ADMIN_INFO } from '/@/stores/constant/cacheKey'
|
||
import type { AdminInfo } from '/@/stores/interface'
|
||
|
||
export const useAdminInfo = defineStore('adminInfo', {
|
||
state: (): AdminInfo => {
|
||
return {
|
||
id: 0,
|
||
username: '',
|
||
nickname: '',
|
||
avatar: '',
|
||
last_login_time: '',
|
||
token: '',
|
||
refresh_token: '',
|
||
super: false,
|
||
}
|
||
},
|
||
actions: {
|
||
/**
|
||
* 状态批量填充
|
||
* @param state 新状态数据
|
||
* @param [exclude=true] 是否排除某些字段(忽略填充),默认值 true 排除 token 和 refresh_token,传递 false 则不排除,还可传递 string[] 指定排除字段列表
|
||
*/
|
||
dataFill(state: Partial<AdminInfo>, exclude: boolean | string[] = true) {
|
||
if (exclude === true) {
|
||
exclude = ['token', 'refresh_token']
|
||
} else if (exclude === false) {
|
||
exclude = []
|
||
}
|
||
|
||
if (Array.isArray(exclude)) {
|
||
exclude.forEach((item) => {
|
||
delete state[item as keyof AdminInfo]
|
||
})
|
||
}
|
||
|
||
this.$patch(state)
|
||
},
|
||
removeToken() {
|
||
this.token = ''
|
||
this.refresh_token = ''
|
||
},
|
||
setToken(token: string, type: 'auth' | 'refresh') {
|
||
const field = type == 'auth' ? 'token' : 'refresh_token'
|
||
this[field] = token
|
||
},
|
||
getToken(type: 'auth' | 'refresh' = 'auth') {
|
||
return type === 'auth' ? this.token : this.refresh_token
|
||
},
|
||
setSuper(val: boolean) {
|
||
this.super = val
|
||
},
|
||
},
|
||
persist: {
|
||
key: ADMIN_INFO,
|
||
},
|
||
})
|