user.js
4.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* @Date: 2025-01-08 18:00:00
* @LastEditors: hookehuyr hookehuyr@gmail.com
* @LastEditTime: 2025-07-09 11:22:03
* @FilePath: /jgdl/src/stores/user.js
* @Description: 用户状态管理
*/
import { defineStore } from 'pinia'
import { getProfileAPI } from '@/api/index'
import Taro from '@tarojs/taro'
export const useUserStore = defineStore('user', {
state: () => {
return {
userInfo: {
avatar: '',
nickname: '',
phone: '',
gender: '',
school: '',
birthday: '',
avatar_url: '',
follower_count: 0,
order_count: 0,
favorite_count: 0
},
isAuthenticated: false,
isLoading: false
}
},
getters: {
/**
* 检查用户是否已完善基本信息(主要是手机号)
*/
hasCompleteProfile: (state) => {
return !!(state.userInfo.phone && state.userInfo.phone.trim())
},
/**
* 检查用户是否已认证
*/
isUserAuthenticated: (state) => {
return state.isAuthenticated && state.hasCompleteProfile
},
/**
* 获取用户显示名称
*/
displayName: (state) => {
return state.userInfo.nickname || '用户'
}
},
actions: {
/**
* 获取用户信息
*/
async fetchUserInfo() {
this.isLoading = true
try {
const response = await getProfileAPI()
if (response.code) {
this.userInfo = { ...this.userInfo, ...response.data }
this.isAuthenticated = true
return true
} else {
this.isAuthenticated = false
return false
}
} catch (error) {
console.error('获取用户信息失败:', error)
this.isAuthenticated = false
return false
} finally {
this.isLoading = false
}
},
/**
* 更新用户信息
* @param {Object} newUserInfo - 新的用户信息
*/
updateUserInfo(newUserInfo) {
this.userInfo = { ...this.userInfo, ...newUserInfo }
},
/**
* 清除用户信息(登出)
*/
clearUserInfo() {
this.userInfo = {
avatar: '',
nickname: '',
phone: '',
gender: '',
school: '',
birthday: '',
avatar_url: '',
follower_count: 0,
order_count: 0,
favorite_count: 0
}
this.isAuthenticated = false
},
/**
* 检查并处理权限验证
* @param {Object} options - 配置选项
* @param {string} options.redirectUrl - 权限验证失败时的跳转地址
* @param {string} options.message - 提示消息
* @param {Function} options.onSuccess - 验证成功的回调
* @param {Function} options.onFail - 验证失败的回调
* @returns {Promise<boolean>} 是否通过权限验证
*/
async checkPermission(options = {}) {
const {
redirectUrl = '/pages/register/index',
message = '请先完善个人信息',
onSuccess,
onFail
} = options
// 如果用户信息为空,先尝试获取
if (!this.isAuthenticated) {
await this.fetchUserInfo()
}
// 检查是否有完整的用户信息
if (this.hasCompleteProfile) {
onSuccess && onSuccess()
return true
} else {
// 权限验证失败,显示提示并跳转
Taro.showToast({
title: message,
icon: 'none',
duration: 2000,
success: () => {
setTimeout(() => {
Taro.navigateTo({
url: redirectUrl
})
}, 2000)
}
})
onFail && onFail()
return false
}
}
}
})