提交 | 用户 | 时间
|
0ca254
|
1 |
import { login, logout, getInfo } from '@/api/login' |
A |
2 |
import { getToken, setToken, removeToken } from '@/utils/auth' |
|
3 |
|
|
4 |
const user = { |
|
5 |
state: { |
|
6 |
token: getToken(), |
|
7 |
id: '', |
|
8 |
name: '', |
|
9 |
avatar: '', |
|
10 |
roles: [], |
|
11 |
permissions: [] |
|
12 |
}, |
|
13 |
|
|
14 |
mutations: { |
|
15 |
SET_TOKEN: (state, token) => { |
|
16 |
state.token = token |
|
17 |
}, |
|
18 |
SET_ID: (state, id) => { |
|
19 |
state.id = id |
|
20 |
}, |
|
21 |
SET_NAME: (state, name) => { |
|
22 |
state.name = name |
|
23 |
}, |
|
24 |
SET_AVATAR: (state, avatar) => { |
|
25 |
state.avatar = avatar |
|
26 |
}, |
|
27 |
SET_ROLES: (state, roles) => { |
|
28 |
state.roles = roles |
|
29 |
}, |
|
30 |
SET_PERMISSIONS: (state, permissions) => { |
|
31 |
state.permissions = permissions |
|
32 |
} |
|
33 |
}, |
|
34 |
|
|
35 |
actions: { |
|
36 |
// 登录 |
|
37 |
Login({ commit }, userInfo) { |
|
38 |
const username = userInfo.username.trim() |
|
39 |
const password = userInfo.password |
|
40 |
const code = userInfo.code |
|
41 |
const uuid = userInfo.uuid |
|
42 |
return new Promise((resolve, reject) => { |
|
43 |
login(username, password, code, uuid).then(res => { |
|
44 |
setToken(res.token) |
|
45 |
commit('SET_TOKEN', res.token) |
|
46 |
resolve() |
|
47 |
}).catch(error => { |
|
48 |
reject(error) |
|
49 |
}) |
|
50 |
}) |
|
51 |
}, |
|
52 |
|
|
53 |
// 获取用户信息 |
|
54 |
GetInfo({ commit, state }) { |
|
55 |
return new Promise((resolve, reject) => { |
|
56 |
getInfo().then(res => { |
|
57 |
const user = res.user |
|
58 |
const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar; |
|
59 |
if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组 |
|
60 |
commit('SET_ROLES', res.roles) |
|
61 |
commit('SET_PERMISSIONS', res.permissions) |
|
62 |
} else { |
|
63 |
commit('SET_ROLES', ['ROLE_DEFAULT']) |
|
64 |
} |
|
65 |
commit('SET_ID', user.userId) |
|
66 |
commit('SET_NAME', user.userName) |
|
67 |
commit('SET_AVATAR', avatar) |
|
68 |
resolve(res) |
|
69 |
}).catch(error => { |
|
70 |
reject(error) |
|
71 |
}) |
|
72 |
}) |
|
73 |
}, |
|
74 |
|
|
75 |
// 退出系统 |
|
76 |
LogOut({ commit, state }) { |
|
77 |
return new Promise((resolve, reject) => { |
|
78 |
logout(state.token).then(() => { |
|
79 |
commit('SET_TOKEN', '') |
|
80 |
commit('SET_ROLES', []) |
|
81 |
commit('SET_PERMISSIONS', []) |
|
82 |
removeToken() |
|
83 |
resolve() |
|
84 |
}).catch(error => { |
|
85 |
reject(error) |
|
86 |
}) |
|
87 |
}) |
|
88 |
}, |
|
89 |
|
|
90 |
// 前端 登出 |
|
91 |
FedLogOut({ commit }) { |
|
92 |
return new Promise(resolve => { |
|
93 |
commit('SET_TOKEN', '') |
|
94 |
removeToken() |
|
95 |
resolve() |
|
96 |
}) |
|
97 |
} |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
export default user |