user.js
4.89 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
import { login, logout, getInfo, loginByPhone } 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: '',
nickName: '',
avatar: '',
roles: [],
permissions: [],
memberInfo: {},
authenticationStatus: '', // 是否已认证
deptType: '',
showPrice: false,
perId: '',
personInfo: '',
unit: '',
isExam: '1', // 是否为考点,0:是;1:否,
genFlag: '', // 是否是自动的协会
badge: {},
reLogin: false,
language: 0
}),
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 username = userInfo.username
const password = userInfo.password
const uuid = userInfo.uuid
const code = userInfo.code
return new Promise((resolve, reject) => {
loginZtx(username, password, 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.avatar = avatar
if (personInfo) {
this.perId = aes.encrypt(personInfo.perId)
this.personInfo = personInfo
}
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 = ''
removeToken()
resolve()
}).catch(error => {
reject(error)
})
})
},
// 获取道馆信息
getMemInfo() {
return getMyOwnMemberInfo().then(res => {
this.authenticationStatus = res.data.authenticationStatus
this.memberInfo = res.data.memberInfo
this.isExam = res.data?.dept?.isExam
})
},
getRemindInfo() {
getRemindCount().then((res) => {
this.badge = res.data
})
},
setReLogin() {
this.reLogin = true
setTimeout(() => {
this.reLogin = false
}, 1000)
},
checkAndLogin() {
if (!this.perId) {
ElMessageBox.alert('请先登录账号', '系统提示')
.then(() => {
this.setReLogin()
})
return false
} else {
return true
}
}
}
})
export default useUserStore