2022-02-23 16:48:04 +08:00

191 lines
4.1 KiB
TypeScript

import {
Action,
getModule,
Module,
Mutation,
VuexModule
} from 'vuex-module-decorators'
import { getToken, removeToken, setToken } from '@/utils/cookies'
import { getNonce, getUserInfo, login } from '@/api/User'
import store from '@/store'
const EIP721_DOMAIN_DATA = [
{ name: 'name', type: 'string' },
{ name: 'version', type: 'string' }
]
export interface IUserState {
token: string
name: string
avatar: string
introduction: string
roles: string[]
email: string
permissions: string[][]
level: number
sex?: string
account: string
}
@Module({ dynamic: true, store, name: 'user' })
class User extends VuexModule implements IUserState {
public token = getToken() || ''
public name = ''
public avatar = ''
public introduction = ''
public roles: string[] = []
public permissions: string[][] = []
public email = ''
public level = 999
public sex = '0'
public account = ''
@Action
public async Login({ bcInstance, account, chainId }: {bcInstance: any, account: string, chainId: string | number}) {
chainId += ''
const preRequest: any = await getNonce({ account, net_id: chainId })
const tips = 'This signature is only used for verify your account'
const signMsg = {
tips,
nonce: preRequest.nonce + ''
}
const signObj = {
types: {
EIP712Domain: EIP721_DOMAIN_DATA,
set: [
{ name: 'tips', type: 'string' },
{ name: 'nonce', type: 'string' }
]
},
primaryType: 'set',
domain: {
name: 'Auth',
version: '1'
},
message: signMsg
}
const signature = await bcInstance.signData(signObj, account)
const authData = {
account,
nonce: preRequest.nonce,
signature,
tips,
net_id: chainId
}
console.log('login data: ', authData)
const res: any = await login(authData)
setToken(res.token)
this.SET_TOKEN(res.token)
}
@Action
public ResetToken() {
removeToken()
this.SET_TOKEN('')
this.SET_ROLES([])
this.SET_PERMISSIONS([])
}
@Action
public async GetUserInfo() {
if (this.token === '') {
throw Error('GetUserInfo: token is undefined!')
}
const { data } = await getUserInfo({ /* Your params here */ })
if (!data) {
throw Error('Verification failed, please Login again.')
}
const { showname, avatar, introduction, permissions, level } = data
this.SET_NAME(showname)
this.SET_AVATAR(avatar)
this.SET_INTRODUCTION(introduction)
this.SET_PERMISSIONS(permissions)
this.SET_LEVEL(level)
}
@Action
public async UpdateInfo(data: any) {
const { showname, avatar } = data
this.SET_NAME(showname)
this.SET_AVATAR(avatar)
}
@Action
public async LogOut() {
if (this.token === '') {
throw Error('LogOut: token is undefined!')
}
// await logout()
removeToken()
this.SET_TOKEN('')
this.SET_ROLES([])
this.SET_PERMISSIONS([])
}
@Mutation
private SET_TOKEN(token: string) {
this.token = token
}
@Mutation
private SET_ACCOUNT(account: string) {
this.account = account
}
@Action
public updateAccount(account: string) {
this.SET_ACCOUNT(account)
}
@Action
public async updatePageToken(token: string) {
this.SET_TOKEN(token)
}
@Mutation
private SET_NAME(name: string) {
this.name = name
}
@Mutation
private SET_AVATAR(avatar: string) {
this.avatar = avatar
}
@Mutation
private SET_INTRODUCTION(introduction: string) {
this.introduction = introduction
}
@Mutation
private SET_ROLES(roles: string[]) {
this.roles = roles
}
@Mutation
private SET_LEVEL(level: number) {
this.level = level
}
@Mutation
private SET_PERMISSIONS(permissions: string[]) {
const results: string[][] = []
for (const permission of permissions) {
if (permission === '*') {
results.push(['*', '*'])
} else {
results.push(permission.split(':'))
}
}
this.permissions = results
}
get encodeToken() {
return encodeURIComponent(this.token)
}
}
export const UserModule = getModule(User)