import { Modal } from 'ant-design-vue' import { defineStore } from 'pinia' import { h } from 'vue' import HttpHandler from '@/utils/gener/HttpHandler' import { router } from '@/router' import { UserLogin } from '@/api/user' export interface TokenInfo { Token: string UserNo: string OrgNo: string } export interface User { No: string Name: string OrgNo: string OrgName: string FK_Dept: string FK_DeptName: string SysLang: string CCBPMRunModel: number IsAdmin: boolean | number Token: string homePath?: string avatar?: string } export interface LoginParams { username: string password: string OrgNo?: string //SAAS版 登录需要组织编号 verifyCode?: string } export const useUserStore = defineStore({ id: 'app-user', state: (): { webUser: User } => ({ webUser: { No: '', Name: '', OrgNo: '', OrgName: '', FK_Dept: '', FK_DeptName: '', SysLang: '', CCBPMRunModel: -1, IsAdmin: 0, Token: '', homePath: '/', avatar: '' } }), getters: { getToken(): string { return this.webUser.Token || localStorage.getItem('Token') || '' }, getWebUser(): User { return this.webUser || JSON.parse(localStorage.getItem('userInfo') || '') || '' } }, actions: { setUserInfo(user: User | null) { if (!user) { this.webUser = { No: '', Name: '', OrgNo: '', OrgName: '', FK_Dept: '', FK_DeptName: '', SysLang: '', CCBPMRunModel: -1, IsAdmin: 0, Token: '', homePath: '/', avatar: '' } localStorage.removeItem('userInfo') return } this.webUser = user localStorage.setItem('userInfo', JSON.stringify(user)) }, setToken(token: string) { this.webUser.Token = token // for null or undefined value localStorage.setItem('Token', token) }, async fetchUserInfo() { const userHandler = new HttpHandler('') return await userHandler.CustomRequest({ DoType: 'WebUser_Init', Token: this.getToken, t: Date.now() }) }, async login( params: LoginParams & { goHome?: boolean type?: string } ) { // const handler = new HttpHandler('BP.WF.HttpHandler.WF_Portal') // handler.AddPara('TB_No', params.username) // handler.AddPara('TB_PW', params.password) // handler.AddPara('VerifyCode', params.verifyCode) // const data = await handler.DoMethodReturnJson('Login_Submit') const data = UserLogin(params.username, params.OrgNo || '') if (!data.Token) { Modal.error({ title: () => h('span', '错误'), content: () => h('span', '未获取到登录凭证') }) return null } this.setToken(data.Token) const userInfo = await this.fetchUserInfo() this.setUserInfo(userInfo) router.replace('/') }, async logout() { if (this.getToken) { try { const handler = new HttpHandler('BP.WF.HttpHandler.WF_Portal') await handler.DoMethodReturnString('Default_LogOut') } catch { console.log('注销Token失败') } } this.setToken('') this.setUserInfo(null) router.push('/login') }, confirmLoginOut() { Modal.confirm({ title: () => h('span', '提示'), content: () => h('span', '确定要退出登录吗?'), cancelText: '取消', okText: '确定', onOk: async () => { await this.logout() } }) } } })