|  |  |  | 
|---|
|  |  |  | LocationUtils, | 
|---|
|  |  |  | } from '@12333/utils'; | 
|---|
|  |  |  | import DefaultAvatar from '@/assets/components/icon-default-avatar.png'; | 
|---|
|  |  |  | import { MatchMakingIdentityEnum, WxMiniAppEnum } from '@12333/constants'; | 
|---|
|  |  |  | import { WxMiniAppEnum } from '@12333/constants'; | 
|---|
|  |  |  | import { myClient } from '@/constants/query'; | 
|---|
|  |  |  | import { globalEventEmitter } from '@12333/hooks'; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | refreshToken?: Nullable<string>; | 
|---|
|  |  |  | userDetail?: Nullable<API.UserInfoV2>; | 
|---|
|  |  |  | firstGetUserDetail?: boolean; | 
|---|
|  |  |  | matchMakingIdentity?: MatchMakingIdentityEnum; | 
|---|
|  |  |  | /**是否有设置过身份 */ | 
|---|
|  |  |  | isSetMatchMakingIdentity?: 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(), | 
|---|
|  |  |  | locationCity: storageLocation?.city ?? '北京市', | 
|---|
|  |  |  | locationProvince: storageLocation?.province ?? '北京', | 
|---|
|  |  |  | firstSetLocation: true, | 
|---|
|  |  |  | 
|---|
|  |  |  | res.avatarUrl = res.avatarUrl ? setOSSLink(res.avatarUrl) : DefaultAvatar; | 
|---|
|  |  |  | this.setUserDetail(res); | 
|---|
|  |  |  | this.firstGetUserDetail = false; | 
|---|
|  |  |  | this.matchMakingIdentity = initMatchMakingIdentity(res); | 
|---|
|  |  |  | this.isSetMatchMakingIdentity = true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (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; | 
|---|
|  |  |  | }, | 
|---|
|  |  |  | }, | 
|---|