莳松crm管理系统
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
ss-crm-manage-web/src/store/modules/user.ts

90 lines
1.9 KiB

11 months ago
import { store } from '../index'
import { defineStore } from 'pinia'
10 months ago
import { getAccessToken, removeToken } from '@/utils/auth'
11 months ago
import { CACHE_KEY, useCache } from '@/hooks/web/useCache'
import { getInfo, loginOut } from '@/api/login'
const { wsCache } = useCache()
interface UserVO {
id: number
avatar: string
nickname: string
}
interface UserInfoVO {
permissions: string[]
roles: string[]
isSetUser: boolean
user: UserVO
}
export const useUserStore = defineStore('admin-user', {
state: (): UserInfoVO => ({
permissions: [],
roles: [],
isSetUser: false,
user: {
id: 0,
avatar: '',
nickname: ''
}
}),
getters: {
getPermissions(): string[] {
return this.permissions
},
getRoles(): string[] {
return this.roles
},
getIsSetUser(): boolean {
return this.isSetUser
},
getUser(): UserVO {
return this.user
}
},
actions: {
async setUserInfoAction() {
if (!getAccessToken()) {
this.resetState()
return null
}
let userInfo = wsCache.get(CACHE_KEY.USER)
if (!userInfo) {
10 months ago
userInfo = await getInfo({})
11 months ago
}
this.permissions = userInfo.permissions
this.roles = userInfo.roles
this.user = userInfo.user
this.isSetUser = true
wsCache.set(CACHE_KEY.USER, userInfo)
10 months ago
wsCache.set(CACHE_KEY.ROLE_ROUTERS, userInfo.menus)
11 months ago
},
async loginOut() {
await loginOut()
removeToken()
wsCache.clear()
this.resetState()
},
resetState() {
this.permissions = []
this.roles = []
this.isSetUser = false
this.user = {
id: 0,
avatar: '',
nickname: ''
}
10 months ago
},
refresh() {
10 months ago
wsCache.delete(CACHE_KEY.USER)
10 months ago
this.resetState()
window.location.href = ''
11 months ago
}
}
})
export const useUserStoreWithOut = () => {
return useUserStore(store)
}