import { apiLogout } from '@/components/Menu/api' import { apiLogin } from '@/pages/Login/api' import { iFromValues } from '@/types/login' import { iUserInfo } from '@/types/setting' import { delUserInfo, getUserInfo, saveUserInfo } from '@/utils/common/user' import consts from '@/utils/consts' import history from '@/utils/history' import { action, computed, observable } from 'mobx' class UserState { initUserState = { account: '', accountGroup: undefined, company: '', csrf: '', enable: 0, id: '', isAdmin: 0, mobile: '', name: '', password: '', position: '', projectId: '', role: '', telephone: '' } @observable userInfo: iUserInfo = this.initUserState @observable permission: [] = [] @computed get isLogin() { return !!this.userInfo.id } @computed get role() { return this.userInfo.isAdmin ? 'ADMIN' : 'USER' } @action login(values: iFromValues) { apiLogin(values).then(({ code = -1, data }) => { if (code === consts.RET_CODE.SUCCESS) { saveUserInfo(data) this.userInfo = data history.push('/') } }) } @action logout() { apiLogout().then(({ code = -1 }) => { if (code === consts.RET_CODE.SUCCESS) { delUserInfo() this.userInfo = this.initUserState history.push('/login') } }) } @action check() { const user: iUserInfo | null = getUserInfo() if (user) { this.userInfo = user } } } export default new UserState()