user.js
4.46 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
import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
const baseUrl = config.baseUrl
const user = {
state: {
token: getToken(),
name: storage.get(constant.name),
roles: storage.get(constant.roles),
permissions: storage.get(constant.permissions),
code: storage.get(constant.code),
deptPaths: storage.get(constant.deptPaths),
id: storage.get(constant.id),
username: storage.get(constant.username),
roleCodes: storage.get(constant.roleCodes),
telephone: storage.get(constant.telephone),
},
mutations: {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, name) => {
state.name = name
storage.set(constant.name, name)
},
SET_ROLES: (state, roles) => {
state.roles = roles
storage.set(constant.roles, roles)
},
SET_PERMISSIONS: (state, permissions) => {
state.permissions = permissions
storage.set(constant.permissions, permissions)
},
SET_CODE: (state, code) => {
state.code = code
storage.set(constant.code, code)
},
SET_DEPT_PATHS: (state, deptPaths) => {
state.deptPaths = deptPaths
storage.set(constant.deptPaths, deptPaths)
},
SET_ID: (state, id) => {
state.id = id
storage.set(constant.id, id)
},
SET_USERNAME: (state, username) => {
state.username = username
storage.set(constant.username, username)
},
SET_ROLE_CODES: (state, roleCodes) => {
state.roleCodes = roleCodes
storage.set(constant.roleCodes, roleCodes)
},
SET_TELEPHONE: (state, telephone) => {
state.telephone = telephone
storage.set(constant.telephone, telephone)
}
},
actions: {
// 登录
Login({ commit, dispatch }, userInfo) {
const username = userInfo.username.trim()
const password = userInfo.password.trim()
const captcha = userInfo.captcha.trim()
const sn = userInfo.sn
return new Promise((resolve, reject) => {
login(username, password, captcha, sn).then(res => {
console.log('login_res', res)
setToken(res.data.token)
commit('SET_TOKEN', res.data.token)
const name = res.data.user.name || '';
commit('SET_NAME', name)
if (res.data.roles && res.data.roles.length > 0) {
commit('SET_ROLES', res.data.roles)
} else {
commit('SET_ROLES', ['ROLE_DEFAULT'])
}
// 登录后拉取最新的用户信息,统一存入 store
dispatch('GetInfo')
.then(() => resolve())
.catch(() => {
// GetInfo 失败(token 异常或过期),执行登出并回到登录页
dispatch('LogOut')
.finally(() => {
try { uni.reLaunch({ url: '/pages/login' }) } catch(e) {}
resolve()
})
})
}).catch(error => {
reject(error)
})
})
},
// 获取用户信息
GetInfo({ commit, state }) {
return new Promise((resolve, reject) => {
getInfo().then(res => {
const _data = res.data || {};
console.log('center/info__data', _data)
// 写入基础信息
commit('SET_NAME', _data.name)
commit('SET_ID', _data.id || '')
commit('SET_USERNAME', _data.username || '')
commit('SET_CODE', _data.code || '')
commit('SET_TELEPHONE', _data.telephone || '')
commit('SET_ROLE_CODES', _data.roleCodes || [])
commit('SET_DEPT_PATHS', _data.deptPaths || [])
resolve(res)
}).catch(error => {
reject(error)
})
})
},
// 退出系统
LogOut({ commit, state }) {
return new Promise((resolve, reject) => {
logout(state.token).then(() => {
commit('SET_TOKEN', '')
commit('SET_ROLES', [])
commit('SET_PERMISSIONS', [])
commit('SET_NAME', '')
commit('SET_ID', '')
commit('SET_USERNAME', '')
commit('SET_CODE', '')
commit('SET_TELEPHONE', '')
commit('SET_ROLE_CODES', [])
commit('SET_DEPT_PATHS', [])
removeToken()
storage.clean()
resolve()
}).catch(error => {
reject(error)
})
})
}
}
}
export default user