ktianc 06d118cad7 1. 新增接入高德地图API
2. 新增按钮级别权限
3. vue-element-plus-admin版本更新
2022-11-18 20:59:10 +08:00

86 lines
2.0 KiB
TypeScript

import { defineStore } from 'pinia'
import { store } from '../index'
import { UserLoginType } from '@/api/login/types'
import { loginApi } from '@/api/login'
import { useAppStore } from '@/store/modules/app'
import { useCache } from '@/hooks/web/useCache'
import { getCurrentUserInfo } from '@/api/vadmin/auth/user'
import { resetRouter } from '@/router'
import { useTagsViewStore } from '@/store/modules/tagsView'
const appStore = useAppStore()
const { wsCache } = useCache()
const tagsViewStore = useTagsViewStore()
export interface UserState {
id?: number
telephone?: string
name?: string
nickname?: string
avatar?: string
gender?: string
roles?: Recordable[]
create_datetime?: string
}
export interface AuthState {
user: UserState
isUser: boolean
}
export const useAuthStore = defineStore('auth', {
state: (): AuthState => {
return {
user: {},
isUser: false
}
},
persist: {
// 开启持久化存储
enabled: true
},
getters: {
getUser(): UserState {
return this.user
},
getIsUser(): boolean {
return this.isUser
}
},
actions: {
async login(formData: UserLoginType) {
const res = await loginApi(formData)
if (res) {
wsCache.set(appStore.getToken, `${res.data.token_type} ${res.data.access_token}`)
// 存储用户信息
const auth = useAuthStore()
await auth.getUserInfo()
}
return res
},
logout() {
wsCache.clear()
this.user = {}
this.isUser = false
tagsViewStore.delAllViews()
resetRouter()
},
updateUser(data: UserState) {
this.user.gender = data.gender
this.user.name = data.name
this.user.nickname = data.nickname
wsCache.set(appStore.getUserInfo, this.user)
},
async getUserInfo() {
const res = await getCurrentUserInfo()
wsCache.set(appStore.getUserInfo, res.data)
this.isUser = true
this.user = res.data
}
}
})
export const useAuthStoreWithOut = () => {
return useAuthStore(store)
}