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

74 lines
1.7 KiB
TypeScript

3 years ago
import {Module} from "vuex";
import api from "../api";
import util from '../libs/util'
import {Base64} from 'js-base64'
import {State} from './index'
import router from "../router";
export interface UserState {
id: number
username: string
nickname: string
phone: string
icon: string
email: string
ready: boolean
auth: [auth?]
[key: string]: any
}
interface auth {
rid: string
ruid: string
level: number
}
export const User: Module<UserState, State> = {
namespaced: true,
state: {
id: 0,
username: '',
nickname: '',
phone: '',
icon: '',
email: '',
auth: [],
ready: false
},
mutations: {
setBase(state: UserState, data: any) {
state.id = data.id
state.icon = data.icon
state.username = data.username
state.nickname = data.nickname
state.phone = data.phone
state.email = data.email
state.ready = true
},
setAuth(state: UserState, data: any) {
state.auth = data
},
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]))
if (data.id > 0) {
context.commit('setAuth', data.auth)
api.user.get(data.id).Start(e => {
context.commit('setBase', e)
})
}
}
}
}