|  |  |  | 
|---|
|  |  |  | import { defineStore } from 'pinia'; | 
|---|
|  |  |  | import { store } from '@/store'; | 
|---|
|  |  |  | import { getToken, setToken, removeToken, removeUserInfo, setUserInfo, getUserInfo } from '@/utils'; | 
|---|
|  |  |  | import { | 
|---|
|  |  |  | getToken, | 
|---|
|  |  |  | setToken, | 
|---|
|  |  |  | removeToken, | 
|---|
|  |  |  | removeUserInfo, | 
|---|
|  |  |  | setUserInfo, | 
|---|
|  |  |  | getUserInfo, | 
|---|
|  |  |  | md5Encrypt, | 
|---|
|  |  |  | getUserDetail, | 
|---|
|  |  |  | setUserDetail, | 
|---|
|  |  |  | removeUserDetail, | 
|---|
|  |  |  | } from '@/utils'; | 
|---|
|  |  |  | import { resetRouter, router } from '@/router'; | 
|---|
|  |  |  | import { useTagsViewStoreHook } from './tagsView'; | 
|---|
|  |  |  | import * as accountServices from '@/services/api/Account'; | 
|---|
|  |  |  | import * as userServices from '@/services/api/User'; | 
|---|
|  |  |  | import { usePermissionStoreHook } from './permission'; | 
|---|
|  |  |  | import { getAccountInfoFromAccessToken, AccountInfo } from '@bole-core/core'; | 
|---|
|  |  |  | import { useClearSubModule } from '@/hooks'; | 
|---|
|  |  |  | import { myClient } from '@/constants/query'; | 
|---|
|  |  |  | import * as authServices from '@/services/api/auth'; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | export interface UserState { | 
|---|
|  |  |  | token: string; | 
|---|
|  |  |  | userInfo: API.IdentityModelTokenCacheItem; | 
|---|
|  |  |  | accountInfo: Partial<AccountInfo>; | 
|---|
|  |  |  | name: string; | 
|---|
|  |  |  | userInfo: API.LoginCommandCallback; | 
|---|
|  |  |  | userDetail?: Nullable<API.GetEnterpriseLoginInfoQueryResult>; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | function getDefaultState() { | 
|---|
|  |  |  | const accessToken = getToken(); | 
|---|
|  |  |  | const userInfo = getUserInfo(); | 
|---|
|  |  |  | const accountInfo = getAccountInfoFromAccessToken(accessToken); | 
|---|
|  |  |  | const userDetail = getUserDetail(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return { | 
|---|
|  |  |  | token: accessToken, | 
|---|
|  |  |  | name: accountInfo.name, | 
|---|
|  |  |  | accountInfo: accountInfo || {}, | 
|---|
|  |  |  | userInfo: userInfo || {}, | 
|---|
|  |  |  | userDetail: userDetail, | 
|---|
|  |  |  | } as UserState; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | userId(state) { | 
|---|
|  |  |  | return state.accountInfo.sub; | 
|---|
|  |  |  | return state.userDetail?.id; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | actions: { | 
|---|
|  |  |  | 
|---|
|  |  |  | this.token = token; | 
|---|
|  |  |  | setToken(token); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | setName(name: string) { | 
|---|
|  |  |  | this.name = name; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | setUserInfo(userInfo: API.IdentityModelTokenCacheItem) { | 
|---|
|  |  |  | setUserInfo(userInfo: API.LoginCommandCallback) { | 
|---|
|  |  |  | this.userInfo = userInfo; | 
|---|
|  |  |  | setUserInfo(userInfo); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | setAccountInfo(accountInfo: Partial<AccountInfo>) { | 
|---|
|  |  |  | this.accountInfo = accountInfo; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | setUserDetail(detail: API.GetEnterpriseLoginInfoQueryResult) { | 
|---|
|  |  |  | this.userDetail = detail; | 
|---|
|  |  |  | setUserDetail(detail); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 用户登入 | 
|---|
|  |  |  | loginByUsername(data: API.AccessRequestDto) { | 
|---|
|  |  |  | return new Promise<void>((resolve, reject) => { | 
|---|
|  |  |  | userServices | 
|---|
|  |  |  | .passwordLogin( | 
|---|
|  |  |  | { | 
|---|
|  |  |  | loginName: data.userName, | 
|---|
|  |  |  | password: data.userPassword, | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | { showLoading: false } | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | .then((res) => { | 
|---|
|  |  |  | if (res) { | 
|---|
|  |  |  | console.log('res: ', res); | 
|---|
|  |  |  | this.setToken(res.accessToken); | 
|---|
|  |  |  | async loginByUsername(params: API.PasswordLoginCommand) { | 
|---|
|  |  |  | let res = await authServices.passwordLogin( | 
|---|
|  |  |  | { | 
|---|
|  |  |  | ...params, | 
|---|
|  |  |  | password: params.password, | 
|---|
|  |  |  | // password: md5Encrypt(params.password), | 
|---|
|  |  |  | type: AppLocalConfig.userType, | 
|---|
|  |  |  | clientType: AppLocalConfig.clientType, | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | { | 
|---|
|  |  |  | showLoading: false, | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | if (res) { | 
|---|
|  |  |  | this.setToken(res.accessToken); | 
|---|
|  |  |  | this.setUserInfo(res); | 
|---|
|  |  |  | this.getCurrentUserInfo(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | const accountInfo = getAccountInfoFromAccessToken(res.accessToken); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | this.setName(accountInfo.name); | 
|---|
|  |  |  | this.setAccountInfo(accountInfo); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 获取用户信息 | 
|---|
|  |  |  | this.setUserInfo(res); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | resolve(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }) | 
|---|
|  |  |  | .catch((error) => { | 
|---|
|  |  |  | reject(error); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | async getCurrentUserInfo() { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | let res = await authServices.getEnterpriseLoginInfo({}, { showLoading: false }); | 
|---|
|  |  |  | if (res) { | 
|---|
|  |  |  | // res.frontStatus = getUserCertificationFrontStatusAdapter( | 
|---|
|  |  |  | //   res.userCertificationStatus, | 
|---|
|  |  |  | //   res.userCertificationAuditStatus | 
|---|
|  |  |  | // ); | 
|---|
|  |  |  | this.setUserDetail(res); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (error) {} | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 登出 清空缓存 | 
|---|
|  |  |  | 
|---|
|  |  |  | return new Promise(async (resolve) => { | 
|---|
|  |  |  | removeToken(); | 
|---|
|  |  |  | removeUserInfo(); | 
|---|
|  |  |  | removeUserDetail(); | 
|---|
|  |  |  | this.resetState(); | 
|---|
|  |  |  | resetRouter(); | 
|---|
|  |  |  | myClient.removeQueries(); | 
|---|
|  |  |  | 
|---|
|  |  |  | Object.assign(this, getDefaultState()); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | refreshToken(params: API.AccessRefreshToken) { | 
|---|
|  |  |  | return new Promise<API.IdentityModelTokenCacheItem>( | 
|---|
|  |  |  | // eslint-disable-next-line no-async-promise-executor | 
|---|
|  |  |  | async (resolve, reject) => { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | const res = await accountServices.getTokenByRefreshToken(params, { | 
|---|
|  |  |  | showLoading: false, | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | if (res) { | 
|---|
|  |  |  | this.setToken(res.accessToken); | 
|---|
|  |  |  | this.setUserInfo(res); | 
|---|
|  |  |  | resolve(res); | 
|---|
|  |  |  | return; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | reject('出错了'); | 
|---|
|  |  |  | } catch (error) { | 
|---|
|  |  |  | reject(error); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | async refreshToken(params: any) { | 
|---|
|  |  |  | return Promise.resolve({ | 
|---|
|  |  |  | accessToken: '1222', | 
|---|
|  |  |  | expiresIn: 1000 * 60 * 24 * 60, | 
|---|
|  |  |  | creationTime: '2025-08-04 00:00:00', | 
|---|
|  |  |  | refreshToken: '1222', | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | }); | 
|---|