|
|
@@ -1,44 +1,57 @@
|
|
|
-
|
|
|
import { defineStore } from 'pinia'
|
|
|
|
|
|
import { getWXLoginCode } from '@/lib/wechatService'
|
|
|
|
|
|
-import { getTokenBySmsApi, getUserInfoByCodeApi } from '@/services/modules/auth'
|
|
|
-import type { LoginRequest } from '@/services/modules/auth/type'
|
|
|
+import { checkBlacklistApi, getTokenBySmsApi, getUserInfoByCodeApi } from '@/services/modules/auth'
|
|
|
+import type {
|
|
|
+ CheckBlacklistRequest,
|
|
|
+ LoginActionResult,
|
|
|
+ LoginRequest,
|
|
|
+} from '@/services/modules/auth/type'
|
|
|
import type { UserInfoItem } from '@/services/modules/auth/userInfo'
|
|
|
|
|
|
import { uniStorage } from '@/utils/uniStorage'
|
|
|
|
|
|
-
|
|
|
interface UserState {
|
|
|
token: string
|
|
|
access_token?: string
|
|
|
-
|
|
|
-
|
|
|
+ currentUserInfoIndex?: number
|
|
|
userInfoList: UserInfoItem[]
|
|
|
+ isBlacklisted?: boolean
|
|
|
}
|
|
|
|
|
|
export const useUserStore = defineStore('user', {
|
|
|
-
|
|
|
-
|
|
|
state: (): UserState => ({
|
|
|
token: '',
|
|
|
access_token: '',
|
|
|
-
|
|
|
+ currentUserInfoIndex: 0,
|
|
|
userInfoList: [],
|
|
|
+ isBlacklisted: false,
|
|
|
}),
|
|
|
+ getters: {
|
|
|
+ currentUserInfo(state): UserInfoItem | undefined {
|
|
|
+ const currentUserInfo = state.userInfoList[state.currentUserInfoIndex || 0]
|
|
|
+ return currentUserInfo || undefined
|
|
|
+ },
|
|
|
+ },
|
|
|
actions: {
|
|
|
- setTokenInfo() {},
|
|
|
// 登录
|
|
|
- async login(payload: LoginRequest) {
|
|
|
+ async login(payload: LoginRequest): Promise<LoginActionResult> {
|
|
|
const res = await getTokenBySmsApi(payload)
|
|
|
- if (res.access_token) {
|
|
|
- this.access_token = res.access_token
|
|
|
- // 获取用户信息
|
|
|
- await this.getUserInfoByCode()
|
|
|
- return true
|
|
|
+ // 登录失败(业务失败)
|
|
|
+ if (!res.access_token) {
|
|
|
+ return {
|
|
|
+ success: false,
|
|
|
+ message: res.msg || '登录失败',
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // 登录成功
|
|
|
+ this.access_token = res.access_token
|
|
|
+ // 拉取用户信息(这里可以继续 try/catch)
|
|
|
+ await this.getUserInfoByCode()
|
|
|
+ return {
|
|
|
+ success: true,
|
|
|
}
|
|
|
- return res
|
|
|
},
|
|
|
async getUserInfoByCode() {
|
|
|
const res = await getWXLoginCode()
|
|
|
@@ -46,14 +59,27 @@ export const useUserStore = defineStore('user', {
|
|
|
const userInfoResult = await getUserInfoByCodeApi(res.code)
|
|
|
if (userInfoResult.code === 0 && userInfoResult.data) {
|
|
|
this.userInfoList = userInfoResult.data.userInfo
|
|
|
+ if (this.userInfoList[0]) {
|
|
|
+ const { phone, idCardNumber } = this.userInfoList[0]
|
|
|
+ this.fetchUserBlacklistStatus({
|
|
|
+ phoneNumber: phone,
|
|
|
+ idCardNumber,
|
|
|
+ })
|
|
|
+ }
|
|
|
}
|
|
|
- console.log('res', userInfoResult)
|
|
|
+ }
|
|
|
+ },
|
|
|
+ async fetchUserBlacklistStatus(payload: CheckBlacklistRequest) {
|
|
|
+ const res = await checkBlacklistApi(payload)
|
|
|
+ if (res.code == 0) {
|
|
|
+ const { idCardNumberStatus, phoneNumberStatus } = res.data
|
|
|
+ this.isBlacklisted = idCardNumberStatus || phoneNumberStatus
|
|
|
}
|
|
|
},
|
|
|
},
|
|
|
persist: {
|
|
|
key: 'user-store',
|
|
|
storage: uniStorage,
|
|
|
- pick: ['token'],
|
|
|
+ pick: ['token', 'access_token', 'currentUserInfoIndex', 'userInfoList', 'isBlacklisted'],
|
|
|
},
|
|
|
})
|