feat: 新增userStore
parent
30e4214387
commit
77c962ea91
@ -0,0 +1,85 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { store } from '../index'
|
||||
import { UserType } from '@/api/login/types'
|
||||
import { ElMessageBox } from 'element-plus'
|
||||
import { useI18n } from '@/hooks/web/useI18n'
|
||||
import { loginOutApi } from '@/api/login'
|
||||
import { useTagsViewStore } from './tagsView'
|
||||
import router from '@/router'
|
||||
|
||||
interface UserState {
|
||||
userInfo?: UserType
|
||||
tokenKey: string
|
||||
token: string
|
||||
roleRouters?: string[] | AppCustomRouteRecordRaw[]
|
||||
}
|
||||
|
||||
export const useUserStore = defineStore('user', {
|
||||
state: (): UserState => {
|
||||
return {
|
||||
userInfo: undefined,
|
||||
tokenKey: 'Token',
|
||||
token: '',
|
||||
roleRouters: undefined
|
||||
}
|
||||
},
|
||||
getters: {
|
||||
getTokenKey(): string {
|
||||
return this.tokenKey
|
||||
},
|
||||
getToken(): string {
|
||||
return this.token
|
||||
},
|
||||
getUserInfo(): UserType | undefined {
|
||||
return this.userInfo
|
||||
},
|
||||
getRoleRouters(): string[] | AppCustomRouteRecordRaw[] | undefined {
|
||||
return this.roleRouters
|
||||
}
|
||||
},
|
||||
actions: {
|
||||
setTokenKey(tokenKey: string) {
|
||||
this.tokenKey = tokenKey
|
||||
},
|
||||
setToken(token: string) {
|
||||
this.token = token
|
||||
},
|
||||
setUserInfo(userInfo?: UserType) {
|
||||
this.userInfo = userInfo
|
||||
},
|
||||
setRoleRouters(roleRouters: string[] | AppCustomRouteRecordRaw[]) {
|
||||
this.roleRouters = roleRouters
|
||||
},
|
||||
logoutConfirm() {
|
||||
const { t } = useI18n()
|
||||
ElMessageBox.confirm(t('common.loginOutMessage'), t('common.reminder'), {
|
||||
confirmButtonText: t('common.ok'),
|
||||
cancelButtonText: t('common.cancel'),
|
||||
type: 'warning'
|
||||
})
|
||||
.then(async () => {
|
||||
const res = await loginOutApi().catch(() => {})
|
||||
if (res) {
|
||||
this.reset()
|
||||
}
|
||||
})
|
||||
.catch(() => {})
|
||||
},
|
||||
reset() {
|
||||
const tagsViewStore = useTagsViewStore()
|
||||
tagsViewStore.delAllViews()
|
||||
this.setToken('')
|
||||
this.setUserInfo(undefined)
|
||||
this.setRoleRouters([])
|
||||
router.replace('/login')
|
||||
},
|
||||
logout() {
|
||||
this.reset()
|
||||
}
|
||||
},
|
||||
persist: true
|
||||
})
|
||||
|
||||
export const useUserStoreWithOut = () => {
|
||||
return useUserStore(store)
|
||||
}
|
||||
Loading…
Reference in New Issue