user.js
6.25 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { login, logout, getInfo, loginByPhone, getInfoForPc, getBlack } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import defAva from '@/assets/images/profile.jpg'
import { getMyOwnMemberInfo } from '@/api/system/userInfo.js'
import { defineStore } from 'pinia'
import aes from '@/utils/aes'
import { loginZtx } from '@/apiPc/login.js'
import { getRemindCount } from '@/api/system/user'
import { ElMessageBox } from 'element-plus'
const useUserStore = defineStore(
'user',
{
state: () => ({
token: getToken(),
user: null,
name: '',
userId: '',
nickName: '',
avatar: '',
roles: [],
permissions: [],
memberInfo: {},
deptInfo: {},
authenticationStatus: '', // 是否已认证
deptType: '',
showPrice: false,
perId: '',
personInfo: '',
unit: '',
isExam: '1', // 是否为考点,0:是;1:否,
genFlag: '', // 是否是直属的协会
badge: {},
memId: '',
reLogin: false,
toRouter: '',
isBlack: null
}),
actions: {
// 登录
login(userInfo) {
const username = userInfo.username.trim()
const password = userInfo.password
const code = userInfo.code
const uuid = userInfo.uuid
return new Promise((resolve, reject) => {
login(username, password, code, uuid).then(res => {
setToken(res.data.token)
this.token = res.data.token
resolve()
}).catch(error => {
reject(error)
})
})
},
loginByPhone(userInfo) {
const phonenumber = userInfo.telNo.trim()
const code = userInfo.code
return new Promise((resolve, reject) => {
loginByPhone(phonenumber, code).then(res => {
setToken(res.data.token)
this.token = res.data.token
resolve()
}).catch(error => {
reject(error)
})
})
},
loginPc(userInfo) {
const phonenumber = userInfo.phonenumber
const captcha = userInfo.captcha
const uuid = userInfo.uuid
const code = userInfo.code
return new Promise((resolve, reject) => {
loginZtx(phonenumber, captcha, code, uuid).then(res => {
setToken(res.data.token)
this.token = res.data.token
resolve()
}).catch(error => {
reject(error)
})
})
},
// 获取用户信息
getInfo() {
return new Promise((resolve, reject) => {
getInfo().then(res => {
const user = res.data.user
const personInfo = res.data.personInfo
const avatar = (user.avatar == '' || user.avatar == null) ? defAva : user.avatar
if (res.data.roles && res.data.roles.length > 0) { // 验证返回的roles是否是一个非空数组
this.roles = res.data.roles
this.permissions = res.data.permissions
} else {
this.roles = ['ROLE_DEFAULT']
}
this.user = user
this.name = user.userName
this.nickName = user.nickName
this.deptType = user.dept.deptType
this.genFlag = user.dept.genFlag
this.showPrice = ['1', '2', '3'].indexOf(this.deptType) > -1
this.dept = user.dept
this.memId = user.memId
this.avatar = avatar
this.userId = user.userId
if (personInfo) {
this.perId = aes.encrypt(personInfo.perId)
this.personInfo = personInfo
}
resolve(res.data)
}).catch(error => {
reject(error)
})
})
},
getIsBlack() {
return new Promise((resolve, reject) => {
getBlack().then(res => {
this.isBlack = res.data
resolve(res.data)
}).catch(error => {
reject(error)
})
})
},
// 获取pc端用户信息
getInfoPc() {
return new Promise((resolve, reject) => {
getInfoForPc().then(res => {
const user = res.data.user
const perId = res.data.user.userId
const avatar = (user.avatar == '' || user.avatar == null) ? defAva : user.avatar
this.user = user
this.perId = aes.encrypt(perId)
this.name = user.userName
this.nickName = user.nickName
this.avatar = avatar
this.memId = user.memId
resolve(res.data)
}).catch(error => {
reject(error)
})
})
},
// 退出系统
logOut() {
return new Promise((resolve, reject) => {
logout(this.token).then(() => {
this.token = ''
this.roles = []
this.permissions = []
this.perId = ''
this.userId = ''
removeToken()
resolve()
}).catch(error => {
reject(error)
})
})
},
// 获取道馆信息
getMemInfo() {
return getMyOwnMemberInfo().then(res => {
this.authenticationStatus = res.data.authenticationStatus
this.memberInfo = res.data.memberInfo || {}
this.deptInfo = res.data.dept || {}
this.isExam = res.data?.dept?.isExam
})
},
getRemindInfo() {
getRemindCount().then((res) => {
this.badge = res.data ?? {}
})
},
setReLogin() {
this.reLogin = true
setTimeout(() => {
this.reLogin = false
}, 1000)
},
goToRouter(name) {
this.toRouter = name
},
checkAndLogin() {
if (!this.user || !this.user.userId) {
ElMessageBox.alert('请先登录账号', '系统提示')
.then(() => {
this.setReLogin()
})
return false
} else if (!this.user.perId) {
ElMessageBox.alert('您还未绑定个人会员身份,请先进行身份绑定', '系统提示')
.then(() => {
this.goToRouter('myInfo')
})
return false
} else {
return true
}
}
}
})
export default useUserStore