|  |  |  | 
|---|
|  |  |  | setUserDetail, | 
|---|
|  |  |  | getUserDetail, | 
|---|
|  |  |  | removeUserDetail, | 
|---|
|  |  |  | removeMatchMakingIdentity, | 
|---|
|  |  |  | getMatchMakingIdentity, | 
|---|
|  |  |  | setMatchMakingIdentity, | 
|---|
|  |  |  | } from '@/utils/storage/auth'; | 
|---|
|  |  |  | import * as accountServices from '@12333/services/api/Account'; | 
|---|
|  |  |  | import * as userServices from '@12333/services/api/User'; | 
|---|
|  |  |  | import Taro, { useRouter } from '@tarojs/taro'; | 
|---|
|  |  |  | import { ButtonProps } from '@tarojs/components'; | 
|---|
|  |  |  | import { debounce } from 'lodash'; | 
|---|
|  |  |  | 
|---|
|  |  |  | setOSSLink, | 
|---|
|  |  |  | getUserCertificationFrontStatusAdapter, | 
|---|
|  |  |  | LocationUtils, | 
|---|
|  |  |  | md5Encrypt, | 
|---|
|  |  |  | } from '@12333/utils'; | 
|---|
|  |  |  | import DefaultAvatar from '@/assets/components/icon-default-avatar.png'; | 
|---|
|  |  |  | import { MatchMakingIdentityEnum, WxMiniAppEnum } from '@12333/constants'; | 
|---|
|  |  |  | import { myClient } from '@/constants/query'; | 
|---|
|  |  |  | import { globalEventEmitter } from '@12333/hooks'; | 
|---|
|  |  |  | import * as authServices from '@12333/services/apiV2/auth'; | 
|---|
|  |  |  | import { AppLocalConfig } from '@/constants'; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | interface UserState { | 
|---|
|  |  |  | userInfo?: Nullable<API.IdentityModelTokenCacheItem>; | 
|---|
|  |  |  | userInfo?: Nullable<API.LoginCommandCallback>; | 
|---|
|  |  |  | token?: Nullable<string>; | 
|---|
|  |  |  | refreshToken?: Nullable<string>; | 
|---|
|  |  |  | userDetail?: Nullable<API.UserInfoV2>; | 
|---|
|  |  |  | userDetail?: Nullable<API.GetPersonalLoginInfoQueryResult>; | 
|---|
|  |  |  | firstGetUserDetail?: boolean; | 
|---|
|  |  |  | matchMakingIdentity?: MatchMakingIdentityEnum; | 
|---|
|  |  |  | /**是否有设置过身份 */ | 
|---|
|  |  |  | isSetMatchMakingIdentity?: boolean; | 
|---|
|  |  |  | firstLaunch?: boolean; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | locationCity?: string; | 
|---|
|  |  |  | locationProvince?: string; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | ); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | function initMatchMakingIdentity(userDetail: API.UserInfoV2): MatchMakingIdentityEnum { | 
|---|
|  |  |  | if (!userDetail) { | 
|---|
|  |  |  | const cacheMatchMakingIdentity = getMatchMakingIdentity(); | 
|---|
|  |  |  | return cacheMatchMakingIdentity | 
|---|
|  |  |  | ? cacheMatchMakingIdentity | 
|---|
|  |  |  | : MatchMakingIdentityEnum.Contributors; | 
|---|
|  |  |  | } else if (!userDetail?.matchMakingIdentity) { | 
|---|
|  |  |  | return MatchMakingIdentityEnum.Contributors; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return userDetail?.matchMakingIdentity; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | export const useUserStore = defineStore({ | 
|---|
|  |  |  | id: 'app-user', | 
|---|
|  |  |  | state: (): UserState => { | 
|---|
|  |  |  | 
|---|
|  |  |  | refreshToken: userInfo?.refreshToken ?? '', | 
|---|
|  |  |  | userDetail: userDetail, | 
|---|
|  |  |  | firstGetUserDetail: true, | 
|---|
|  |  |  | matchMakingIdentity: initMatchMakingIdentity(userDetail), | 
|---|
|  |  |  | isSetMatchMakingIdentity: userDetail | 
|---|
|  |  |  | ? !!userDetail.matchMakingIdentity || !!getMatchMakingIdentity() | 
|---|
|  |  |  | : !!getMatchMakingIdentity(), | 
|---|
|  |  |  | firstLaunch: true, | 
|---|
|  |  |  | locationCity: storageLocation?.city ?? '北京市', | 
|---|
|  |  |  | locationProvince: storageLocation?.province ?? '北京', | 
|---|
|  |  |  | firstSetLocation: true, | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | getters: { | 
|---|
|  |  |  | cacheToken: (state) => { | 
|---|
|  |  |  | if (!state.token) { | 
|---|
|  |  |  | const storageToken = getCacheToken() as API.IdentityModelTokenCacheItem; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | state.token = storageToken.accessToken; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return state.token || null; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | cacheRefreshToken: (state) => { | 
|---|
|  |  |  | return state.refreshToken; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | 
|---|
|  |  |  | return getAccountInfoFromAccessToken(this.userInfo?.accessToken); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // matchMakingIdentity(state): MatchMakingIdentityEnum { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // }, | 
|---|
|  |  |  | userId: (state) => { | 
|---|
|  |  |  | return state.userDetail?.id ?? ''; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | actions: { | 
|---|
|  |  |  | // 手机号授权Code登录 | 
|---|
|  |  |  | async getTokenByPhone( | 
|---|
|  |  |  | detail: ButtonProps.onGetPhoneNumberEventDetail, | 
|---|
|  |  |  | wxMiniAppUserLoginRes: any | 
|---|
|  |  |  | wxMiniAppUserLoginRes: API.LoginCommandCallback | 
|---|
|  |  |  | ) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | let res: API.IdentityModelTokenCacheItem; | 
|---|
|  |  |  | if (!wxMiniAppUserLoginRes.accessToken) { | 
|---|
|  |  |  | let params: API.WxMiniAppPhoneLoginInput = { | 
|---|
|  |  |  | openId: wxMiniAppUserLoginRes.openId, | 
|---|
|  |  |  | sessionKey: wxMiniAppUserLoginRes.sessionKey, | 
|---|
|  |  |  | let res: API.LoginCommandCallback = wxMiniAppUserLoginRes; | 
|---|
|  |  |  | if (!wxMiniAppUserLoginRes?.isBindPhoneNumber) { | 
|---|
|  |  |  | let bindRes = await authServices.bindWxmpUserInfo({ | 
|---|
|  |  |  | encryptedData: detail.encryptedData, | 
|---|
|  |  |  | iv: detail.iv, | 
|---|
|  |  |  | wxMiniApp: WxMiniAppEnum.人单合一, | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | res = await accountServices.wxMiniAppPhoneAuthLogin(params); | 
|---|
|  |  |  | sessionKey: wxMiniAppUserLoginRes.sessionKey, | 
|---|
|  |  |  | accessToken: wxMiniAppUserLoginRes.accessToken, | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | res.accessToken = bindRes.accessToken; | 
|---|
|  |  |  | this.loginSuccess(res); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 用户手机验证码登入 | 
|---|
|  |  |  | async loginByUsername(data: API.PhoneMesssageCodeLoginInput) { | 
|---|
|  |  |  | let res = await accountServices.phoneMesssageCodeLogin( | 
|---|
|  |  |  | async loginByUsername(data: API.SmsLoginCommand) { | 
|---|
|  |  |  | let res = await authServices.smsLogin( | 
|---|
|  |  |  | { | 
|---|
|  |  |  | phoneNumber: data.phoneNumber, | 
|---|
|  |  |  | code: data.code, | 
|---|
|  |  |  | verifyCode: data.verifyCode, | 
|---|
|  |  |  | type: AppLocalConfig.userType, | 
|---|
|  |  |  | clientType: AppLocalConfig.clientType, | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | { showLoading: false } | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 用户账号密码登入 | 
|---|
|  |  |  | async loginByPassword(data: API.AccessRequestDto) { | 
|---|
|  |  |  | let res = await accountServices.passwordLogin( | 
|---|
|  |  |  | async loginByPassword(params: API.PasswordLoginCommand) { | 
|---|
|  |  |  | let res = await authServices.passwordLogin( | 
|---|
|  |  |  | { | 
|---|
|  |  |  | loginName: data.userName, | 
|---|
|  |  |  | password: data.userPassword, | 
|---|
|  |  |  | userName: params.userName, | 
|---|
|  |  |  | // password: md5Encrypt(params.password), | 
|---|
|  |  |  | password: params.password, | 
|---|
|  |  |  | type: AppLocalConfig.userType, | 
|---|
|  |  |  | clientType: AppLocalConfig.clientType, | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | { showLoading: false } | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async loginSuccess(res: API.IdentityModelTokenCacheItem) { | 
|---|
|  |  |  | async loginSuccess(res: API.LoginCommandCallback) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | this.setUserInfoAction(res); | 
|---|
|  |  |  | this.setTokenAction(res); | 
|---|
|  |  |  | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async wxMiniAppUserLoginFromScan(wxIndentityRes: API.WxMiniAppIndentityInfo, uuid: string) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | let res = await accountServices.wxMiniAppUserLoginFromScan({ | 
|---|
|  |  |  | uId: uuid, | 
|---|
|  |  |  | userName: wxIndentityRes.userName, | 
|---|
|  |  |  | openId: wxIndentityRes.openId, | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | this.loginSuccess(res); | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | } catch (error) {} | 
|---|
|  |  |  | // try { | 
|---|
|  |  |  | //   let res = await accountServices.wxMiniAppUserLoginFromScan({ | 
|---|
|  |  |  | //     uId: uuid, | 
|---|
|  |  |  | //     userName: wxIndentityRes.userName, | 
|---|
|  |  |  | //     openId: wxIndentityRes.openId, | 
|---|
|  |  |  | //   }); | 
|---|
|  |  |  | //   this.loginSuccess(res); | 
|---|
|  |  |  | //   return res; | 
|---|
|  |  |  | // } catch (error) {} | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async wxMiniAppPhoneAuthLoginFromScan( | 
|---|
|  |  |  | 
|---|
|  |  |  | wxIndentityRes: API.WxMiniAppIndentityInfo, | 
|---|
|  |  |  | uuid: string | 
|---|
|  |  |  | ) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | let res = await accountServices.wxMiniAppPhoneAuthLoginFromScan({ | 
|---|
|  |  |  | uId: uuid, | 
|---|
|  |  |  | openId: wxIndentityRes.openId, | 
|---|
|  |  |  | sessionKey: wxIndentityRes.sessionKey, | 
|---|
|  |  |  | encryptedData: detail.encryptedData, | 
|---|
|  |  |  | iv: detail.iv, | 
|---|
|  |  |  | wxMiniApp: WxMiniAppEnum.人单合一, | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | this.loginSuccess(res); | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | } catch (error) {} | 
|---|
|  |  |  | // try { | 
|---|
|  |  |  | //   let res = await accountServices.wxMiniAppPhoneAuthLoginFromScan({ | 
|---|
|  |  |  | //     uId: uuid, | 
|---|
|  |  |  | //     openId: wxIndentityRes.openId, | 
|---|
|  |  |  | //     sessionKey: wxIndentityRes.sessionKey, | 
|---|
|  |  |  | //     encryptedData: detail.encryptedData, | 
|---|
|  |  |  | //     iv: detail.iv, | 
|---|
|  |  |  | //     wxMiniApp: WxMiniAppEnum.C端小程序, | 
|---|
|  |  |  | //   }); | 
|---|
|  |  |  | //   this.loginSuccess(res); | 
|---|
|  |  |  | //   return res; | 
|---|
|  |  |  | // } catch (error) {} | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async getCurrentUserInfo() { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | let res = await userServices.getUserInfo({ showLoading: false }); | 
|---|
|  |  |  | let res = await authServices.getPersonalLoginInfo({}, { showLoading: false }); | 
|---|
|  |  |  | if (res) { | 
|---|
|  |  |  | res.frontStatus = getUserCertificationFrontStatusAdapter( | 
|---|
|  |  |  | res.userCertificationStatus, | 
|---|
|  |  |  | res.userCertificationAuditStatus | 
|---|
|  |  |  | ); | 
|---|
|  |  |  | res.originalAvatarUrl = res.avatarUrl; | 
|---|
|  |  |  | res.avatarUrl = res.avatarUrl ? setOSSLink(res.avatarUrl) : DefaultAvatar; | 
|---|
|  |  |  | // res.frontStatus = getUserCertificationFrontStatusAdapter( | 
|---|
|  |  |  | //   res.userCertificationStatus, | 
|---|
|  |  |  | //   res.userCertificationAuditStatus | 
|---|
|  |  |  | // ); | 
|---|
|  |  |  | res.originalAvatar = res.avatar; | 
|---|
|  |  |  | res.avatar = res.avatar ? setOSSLink(res.avatar) : DefaultAvatar; | 
|---|
|  |  |  | this.setUserDetail(res); | 
|---|
|  |  |  | this.firstGetUserDetail = false; | 
|---|
|  |  |  | this.matchMakingIdentity = initMatchMakingIdentity(res); | 
|---|
|  |  |  | this.isSetMatchMakingIdentity = true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (error) {} | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | setTokenAction(tokenInfo: API.IdentityModelTokenCacheItem) { | 
|---|
|  |  |  | setTokenAction(tokenInfo: API.LoginCommandCallback) { | 
|---|
|  |  |  | this.token = tokenInfo?.accessToken; | 
|---|
|  |  |  | this.refreshToken = tokenInfo.refreshToken ?? ''; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | setUserInfoAction(info: API.IdentityModelTokenCacheItem) { | 
|---|
|  |  |  | this.userInfo = info; | 
|---|
|  |  |  | setUserInfo(info); | 
|---|
|  |  |  | setUserInfoAction(info: API.LoginCommandCallback) { | 
|---|
|  |  |  | this.userInfo = { | 
|---|
|  |  |  | ...this.userInfo, | 
|---|
|  |  |  | ...info, | 
|---|
|  |  |  | }; | 
|---|
|  |  |  | setUserInfo(this.userInfo); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | setUserDetail(detail: API.UserInfoV2) { | 
|---|
|  |  |  | setUserDetail(detail: API.GetPersonalLoginInfoQueryResult) { | 
|---|
|  |  |  | this.userDetail = detail; | 
|---|
|  |  |  | setUserDetail(detail); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | 
|---|
|  |  |  | this.userDetail = null; | 
|---|
|  |  |  | removeUserInfo(); | 
|---|
|  |  |  | removeUserDetail(); | 
|---|
|  |  |  | removeMatchMakingIdentity(); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | logout() { | 
|---|
|  |  |  | this.resetState(); | 
|---|
|  |  |  | myClient.removeQueries(); | 
|---|
|  |  |  | // myClient.removeQueries(); | 
|---|
|  |  |  | goAuthorization(); | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async getTokenByRefreshToken(params: API.AccessRefreshToken) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | const res = await accountServices.getTokenByRefreshToken(params, { | 
|---|
|  |  |  | showLoading: false, | 
|---|
|  |  |  | }); | 
|---|
|  |  |  | if (res) { | 
|---|
|  |  |  | this.setTokenAction(res); | 
|---|
|  |  |  | this.setUserInfoAction(res); | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | throw new Error('刷新token失败'); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (error) { | 
|---|
|  |  |  | throw new Error('error'); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async setMatchMakingIdentity(params: API.SetMatchMakingIdentityInput) { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | let res = 1; | 
|---|
|  |  |  | if (this.userDetail) { | 
|---|
|  |  |  | res = await userServices.setMatchMakingIdentity(params); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (res) { | 
|---|
|  |  |  | if (!this.userDetail) { | 
|---|
|  |  |  | setMatchMakingIdentity(params.matchMakingIdentity); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | this.matchMakingIdentity = params.matchMakingIdentity; | 
|---|
|  |  |  | this.isSetMatchMakingIdentity = true; | 
|---|
|  |  |  | return res; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (error) { | 
|---|
|  |  |  | throw new Error('error'); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | setLocationCity(cityName: string, provinceName: string) { | 
|---|
|  |  |  | this.locationCity = cityName; | 
|---|
|  |  |  | this.locationProvince = provinceName; | 
|---|
|  |  |  | this.firstSetLocation = false; | 
|---|
|  |  |  | setStorageLocationCity({ city: cityName, province: provinceName }); | 
|---|
|  |  |  | if (LocationUtils.isProvinceChange(provinceName)) { | 
|---|
|  |  |  | this.resetState(); | 
|---|
|  |  |  | myClient.removeQueries(); | 
|---|
|  |  |  | globalEventEmitter.trigger('logout/refresh'); | 
|---|
|  |  |  | // myClient.invalidateQueries({ | 
|---|
|  |  |  | //   queryKey: ['logout/refresh'], | 
|---|
|  |  |  | // }); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | // if (LocationUtils.isProvinceChange(provinceName)) { | 
|---|
|  |  |  | //   this.resetState(); | 
|---|
|  |  |  | //   myClient.removeQueries(); | 
|---|
|  |  |  | //   globalEventEmitter.trigger('logout/refresh'); | 
|---|
|  |  |  | // } | 
|---|
|  |  |  | LocationUtils.currentProvinceName = provinceName; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  |  | 
|---|
|  |  |  | setFirstLaunch(firstLaunch: boolean) { | 
|---|
|  |  |  | this.firstLaunch = firstLaunch; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | }); | 
|---|
|  |  |  |  | 
|---|