Files
webman-buildadmin-mall/web/src/stores/adminInfo.ts
2026-03-30 14:45:09 +08:00

58 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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,
},
})