import { defineStore } from 'pinia' import { getWXLoginCode } from '@/lib/wechatService' import { checkBlacklistApi, getTokenBySmsApi, getUserInfoByCodeApi } from '@/services/modules/login' import type { CheckBlacklistRequest, LoginActionResult, LoginRequest, } from '@/services/modules/login/type' import type { UserInfoItem } from '@/services/modules/login/userInfo' import { resetUnauthorizedHandled } from '@/services/request' import { pinia } from '@/stores/index' import { uniStorage } from '@/utils/uniStorage' interface UserState { access_token?: string currentUserInfoIndex?: number userInfoList: UserInfoItem[] isBlacklisted?: boolean /** * 活体验证提醒:本次登录是否已经展示过 * 不持久化,避免变成永久只展示一次 */ hasShownLivenessReminder: boolean } export const useUserStore = defineStore('user', { state: (): UserState => ({ access_token: '', currentUserInfoIndex: 0, userInfoList: [], isBlacklisted: false, hasShownLivenessReminder: false, }), getters: { isLoggedIn(state): boolean { return Boolean(state.access_token) }, hasBlacklistRisk(state): boolean { return Boolean(state.isBlacklisted) }, currentUserInfo(state): UserInfoItem | undefined { const index = state.currentUserInfoIndex if (index == null) return undefined return state.userInfoList[index] }, }, actions: { // 登录 async login(payload: LoginRequest): Promise { const res = await getTokenBySmsApi(payload) if (!res.access_token) { return { success: false, message: res.msg || '登录失败', } } this.access_token = res.access_token // 登录成功后立即重置,保证本次登录重新具备弹窗资格 this.resetLivenessReminderShown() resetUnauthorizedHandled() await this.getUserInfoByCode() return { success: true, } }, async getUserInfoByCode(): Promise { const wxRes = await getWXLoginCode() if (!wxRes?.code) return undefined const userInfoResult = await getUserInfoByCodeApi(wxRes.code) if (userInfoResult.code !== 0 || !userInfoResult.data) { return undefined } const userInfoList = userInfoResult.data.userInfo || [] this.userInfoList = userInfoList console.log('userInfoList', userInfoList[0]) const index = this.currentUserInfoIndex ?? 0 const currentUserInfo = userInfoList[index] if (!currentUserInfo) { return undefined } const { phone, idCardNumber } = currentUserInfo if (phone && idCardNumber) { this.fetchUserBlacklistStatus({ phoneNumber: phone, idCardNumber, }) } return currentUserInfo }, async fetchUserBlacklistStatus(payload: CheckBlacklistRequest) { const res = await checkBlacklistApi(payload) if (res.code === 0) { const { idCardNumberStatus, phoneNumberStatus } = res.data this.isBlacklisted = idCardNumberStatus || phoneNumberStatus } }, setCurrentUserInfoIndex(index: number) { if (this.currentUserInfoIndex === index) return this.currentUserInfoIndex = index // 切换身份后,活体状态需要重新判断 this.resetLivenessReminderShown() }, markLivenessReminderShown() { this.hasShownLivenessReminder = true }, resetLivenessReminderShown() { this.hasShownLivenessReminder = false }, logout() { this.$reset() }, }, persist: { key: 'user-store', storage: uniStorage, pick: ['access_token', 'currentUserInfoIndex', 'userInfoList', 'isBlacklisted'], }, }) export function useUserStoreWithOut() { return useUserStore(pinia) }