initHeader
This commit is contained in:
83
frontend/src/stores/modules/user/index.ts
Normal file
83
frontend/src/stores/modules/user/index.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { USER_INFO } from '@/constants/storeKey'
|
||||
import { TOKEN_PREFIX } from '@/constants/user'
|
||||
import type { UserInfo } from '@/types/user'
|
||||
import { getUserById } from '@/api/user'
|
||||
import { createPersistedState } from 'pinia-plugin-persistedstate'
|
||||
|
||||
export const useUserInfoStore = defineStore('userInfo', {
|
||||
// 行为
|
||||
actions: {
|
||||
dataFill(state: UserInfo) {
|
||||
this.$state = { ...this.$state, ...state }
|
||||
},
|
||||
async initUserInfo() {
|
||||
this.dataFill(await getUserById())
|
||||
},
|
||||
removeToken() {
|
||||
this.access_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') {
|
||||
if (type === 'auth') {
|
||||
if (this.access_token) {
|
||||
return `${TOKEN_PREFIX}${this.access_token}`
|
||||
} else {
|
||||
return ''
|
||||
}
|
||||
} else {
|
||||
return this.refresh_token
|
||||
}
|
||||
},
|
||||
},
|
||||
// 状态
|
||||
state: (): UserInfo => {
|
||||
return {
|
||||
access_token: '',
|
||||
refresh_token: '',
|
||||
expires_in: 0,
|
||||
scope: '',
|
||||
nickname: '',
|
||||
userType: 0,
|
||||
deptIndex: '',
|
||||
userIndex: '',
|
||||
client_id: '',
|
||||
headSculpture: '',
|
||||
jti: '',
|
||||
name: '',
|
||||
deptId: '',
|
||||
phone: '',
|
||||
email: '',
|
||||
limitIpStart: '',
|
||||
limitIpEnd: '',
|
||||
limitTime: '',
|
||||
casualUser: 0,
|
||||
type: 0,
|
||||
smsNotice: 0,
|
||||
emailNotice: 0,
|
||||
role: [],
|
||||
devCode: '',
|
||||
id: '',
|
||||
loginName: '',
|
||||
state: 0,
|
||||
registerTime: '',
|
||||
loginTime: '',
|
||||
deptName: '',
|
||||
areaId: '',
|
||||
areaName: '',
|
||||
deptLevel: 0,
|
||||
roleList: [],
|
||||
roleCode: [],
|
||||
}
|
||||
},
|
||||
plugins: [
|
||||
createPersistedState({
|
||||
key: USER_INFO,
|
||||
storage: window.localStorage
|
||||
}),
|
||||
],
|
||||
})
|
||||
Reference in New Issue
Block a user