Blame view
src/store/modules/user.js
3.16 KB
d7d9c38c2 auto commit the c... |
1 2 3 |
import { login, logout, getInfo } from '@/api/user' import { getToken, setToken, removeToken } from '@/utils/auth' import router, { resetRouter } from '@/router' |
80a28914e init |
4 |
|
d7d9c38c2 auto commit the c... |
5 6 7 8 9 10 11 |
const state = { token: getToken(), name: '', avatar: '', introduction: '', roles: [] } |
80a28914e init |
12 13 |
const mutations = { |
80a28914e init |
14 15 16 |
SET_TOKEN: (state, token) => { state.token = token }, |
d7d9c38c2 auto commit the c... |
17 18 19 |
SET_INTRODUCTION: (state, introduction) => { state.introduction = introduction }, |
80a28914e init |
20 21 22 23 24 |
SET_NAME: (state, name) => { state.name = name }, SET_AVATAR: (state, avatar) => { state.avatar = avatar |
cf56a6c30 init role permission |
25 26 27 |
}, SET_ROLES: (state, roles) => { state.roles = roles |
80a28914e init |
28 29 30 31 32 |
} } const actions = { // user login |
d7d9c38c2 auto commit the c... |
33 34 |
login({ commit }, userInfo) { const { username, password } = userInfo |
80a28914e init |
35 |
return new Promise((resolve, reject) => { |
d7d9c38c2 auto commit the c... |
36 37 38 39 |
login({ username: username.trim(), password: password }).then(response => { const { data } = response commit('SET_TOKEN', data.token) setToken(data.token) |
80a28914e init |
40 41 42 43 44 45 46 47 |
resolve() }).catch(error => { reject(error) }) }) }, // get user info |
d7d9c38c2 auto commit the c... |
48 |
getInfo({ commit, state }) { |
80a28914e init |
49 50 |
return new Promise((resolve, reject) => { getInfo(state.token).then(response => { |
d7d9c38c2 auto commit the c... |
51 52 53 |
const { data } = response if (!data) { |
80a28914e init |
54 55 |
reject('Verification failed, please Login again.') } |
d7d9c38c2 auto commit the c... |
56 57 |
const { roles, name, avatar, introduction } = data |
cf56a6c30 init role permission |
58 59 60 61 |
// roles must be a non-empty array if (!roles || roles.length <= 0) { reject('getInfo: roles must be a non-null array!') } |
d7d9c38c2 auto commit the c... |
62 |
|
cf56a6c30 init role permission |
63 |
commit('SET_ROLES', roles) |
80a28914e init |
64 65 |
commit('SET_NAME', name) commit('SET_AVATAR', avatar) |
d7d9c38c2 auto commit the c... |
66 67 |
commit('SET_INTRODUCTION', introduction) resolve(data) |
80a28914e init |
68 69 70 71 72 73 74 |
}).catch(error => { reject(error) }) }) }, // user logout |
d7d9c38c2 auto commit the c... |
75 |
logout({ commit, state, dispatch }) { |
80a28914e init |
76 77 |
return new Promise((resolve, reject) => { logout(state.token).then(() => { |
d7d9c38c2 auto commit the c... |
78 79 80 |
commit('SET_TOKEN', '') commit('SET_ROLES', []) removeToken() |
80a28914e init |
81 |
resetRouter() |
d7d9c38c2 auto commit the c... |
82 83 84 85 |
// reset visited views and cached views // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485 dispatch('tagsView/delAllViews', null, { root: true }) |
80a28914e init |
86 87 88 89 90 91 92 93 |
resolve() }).catch(error => { reject(error) }) }) }, // remove token |
d7d9c38c2 auto commit the c... |
94 |
resetToken({ commit }) { |
80a28914e init |
95 |
return new Promise(resolve => { |
d7d9c38c2 auto commit the c... |
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 |
commit('SET_TOKEN', '') commit('SET_ROLES', []) removeToken() resolve() }) }, // dynamically modify permissions changeRoles({ commit, dispatch }, role) { return new Promise(async resolve => { const token = role + '-token' commit('SET_TOKEN', token) setToken(token) const { roles } = await dispatch('getInfo') resetRouter() // generate accessible routes map based on roles const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true }) // dynamically add accessible routes router.addRoutes(accessRoutes) // reset visited views and cached views dispatch('tagsView/delAllViews', null, { root: true }) |
80a28914e init |
123 124 125 126 127 128 129 130 131 132 133 |
resolve() }) } } export default { namespaced: true, state, mutations, actions } |