26 lines
1.5 KiB
TypeScript
26 lines
1.5 KiB
TypeScript
import { defineStore } from 'pinia'
|
|
import type { RouteRecordRaw } from 'vue-router'
|
|
import request, { clearSession, getSessionMeta, setAllowedResources } from './transport'
|
|
import { projectResources } from './resources.mjs'
|
|
import { routes } from './screens'
|
|
// The actual bearer stays private to the direct HTTPS transport. No account data is persisted.
|
|
export default defineStore('liveReadUser', {
|
|
state:()=>({ready:false, token:'', userInfo:{} as Record<string,unknown>,perms:[] as string[],keys:[] as string[],routes:[] as RouteRecordRaw[]}),
|
|
actions:{
|
|
forget(){this.$reset();clearSession()},
|
|
async login(form:{account:string,password:string}) {
|
|
this.forget()
|
|
try {
|
|
await request.post({url:'/login/account',params:{account:form.account,password:form.password,terminal:1}})
|
|
const self=await request.get({url:'/auth.admin/mySelf'})
|
|
if(!getSessionMeta().authenticated || !self?.user || Number(self.user.is_paw)===0 || self.user.need_bind_work_wechat===true || Number(self.user.need_bind_work_wechat)===1)throw Error('请先在正式后台完成账号验证。')
|
|
const {keys,perms}=projectResources(self)
|
|
setAllowedResources(keys)
|
|
this.userInfo={name:String(self.user.name || '当前账号')};this.perms=perms;this.keys=keys
|
|
this.routes=routes.filter(r=>keys.includes(String(r.meta?.key))&&!r.meta?.hidden).map(r=>({path:r.path,name:r.name,meta:r.meta} as RouteRecordRaw));this.ready=true
|
|
return {is_paw:1,need_bind_work_wechat:false}
|
|
}catch(e){this.forget();throw e}
|
|
}
|
|
}
|
|
})
|