You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
OneAuth/oaf/src/store/user.ts

64 lines
1.7 KiB
TypeScript

3 years ago
import {Module} from "vuex";
import api from "@/api";
import util from '@/libs/util'
3 years ago
import {Base64} from 'js-base64'
import {State} from './index'
import router from "@/router";
import {Auths, NewAuths} from '@/auth'
import {modelsSimpleAuth, modelsUser} from '@/models'
3 years ago
import {Cfg} from '@/oaer'
3 years ago
export interface UserState {
id: number
local: modelsUser
3 years ago
ready: boolean
auth: Auths
3 years ago
}
export const User: Module<UserState, State> = {
namespaced: true,
state: {
id: 0,
local: {} as modelsUser,
auth: NewAuths([]),
3 years ago
ready: false
},
mutations: {
setBase(state: UserState, data: modelsUser) {
state.id = data.ID
state.local = data
3 years ago
state.ready = true
},
setAuth(state: UserState, data: modelsSimpleAuth[]) {
state.auth = NewAuths(data)
3 years ago
},
3 years ago
refreshToken(state: UserState, data: string) {
Cfg.token.value = util.getToken()
},
3 years ago
logout(state: UserState) {
state.ready = false
localStorage.removeItem('auth_token')
router.push({name: 'login'})
}
},
actions: {
fetchUserData(context) {
let token = util.getToken()?.split('.');
if (!token || token.length !== 3) {
return false
}
let data = JSON.parse(Base64.decode(token[1]))
console.log(data)
if (data.id) {
context.commit('setAuth', data.Auth)
api.user.get(data.id).Start(e => {
console.log(e)
3 years ago
context.commit('setBase', e)
},e=> {
context.commit('logout')
3 years ago
})
}
}
}
}