|
@@ -1,13 +1,17 @@
|
|
|
-import { login, logout, getInfo } from '@/api/user'
|
|
|
|
|
|
|
+import { login, logout, getUserInfo } from '@/api/user'
|
|
|
import { getToken, setToken, removeToken } from '@/utils/auth'
|
|
import { getToken, setToken, removeToken } from '@/utils/auth'
|
|
|
|
|
+import usersInfo from '@/utils/usersInfo'
|
|
|
import router, { resetRouter } from '@/router'
|
|
import router, { resetRouter } from '@/router'
|
|
|
|
|
+import { Message } from 'element-ui'
|
|
|
|
|
+import { getBaseInfo } from '@/api/site'
|
|
|
|
|
+import baseInfo from '@/utils/baseInfo'
|
|
|
|
|
|
|
|
const state = {
|
|
const state = {
|
|
|
token: getToken(),
|
|
token: getToken(),
|
|
|
name: '',
|
|
name: '',
|
|
|
avatar: '',
|
|
avatar: '',
|
|
|
introduction: '',
|
|
introduction: '',
|
|
|
- roles: []
|
|
|
|
|
|
|
+ roles: ['admin']
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
const mutations = {
|
|
const mutations = {
|
|
@@ -31,67 +35,75 @@ const mutations = {
|
|
|
const actions = {
|
|
const actions = {
|
|
|
// user login
|
|
// user login
|
|
|
login({ commit }, userInfo) {
|
|
login({ commit }, userInfo) {
|
|
|
- const { username, password } = userInfo
|
|
|
|
|
|
|
+ const requestData = {
|
|
|
|
|
+ adminName: userInfo.username,
|
|
|
|
|
+ password: userInfo.password,
|
|
|
|
|
+ verifyCode: userInfo.verifyCode
|
|
|
|
|
+ }
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
- login({ username: username.trim(), password: password }).then(response => {
|
|
|
|
|
- const { data } = response
|
|
|
|
|
- commit('SET_TOKEN', data.token)
|
|
|
|
|
- setToken(data.token)
|
|
|
|
|
|
|
+ login({ LoginForm: requestData }, userInfo.pageId).then(response => {
|
|
|
|
|
+ // 更新本地accessToken
|
|
|
|
|
+ usersInfo.updateLoginAllInfo(response.data)
|
|
|
|
|
+
|
|
|
|
|
+ commit('SET_TOKEN', response.data.accessToken)
|
|
|
|
|
+ commit('SET_ROLES', ['admin'])
|
|
|
|
|
+ commit('SET_NAME', 'userName')
|
|
|
|
|
+ commit('SET_AVATAR', 'avatar')
|
|
|
|
|
+ commit('SET_INTRODUCTION', 'introduction')
|
|
|
resolve()
|
|
resolve()
|
|
|
}).catch(error => {
|
|
}).catch(error => {
|
|
|
|
|
+ Message({
|
|
|
|
|
+ message: error.message,
|
|
|
|
|
+ type: 'error',
|
|
|
|
|
+ duration: 5 * 1000
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ usersInfo.clear()
|
|
|
reject(error)
|
|
reject(error)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
- // get user info
|
|
|
|
|
- getInfo({ commit, state }) {
|
|
|
|
|
|
|
+ getUserInfo({ commit }, data) {
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
- getInfo(state.token).then(response => {
|
|
|
|
|
- const { data } = response
|
|
|
|
|
-
|
|
|
|
|
- if (!data) {
|
|
|
|
|
- reject('Verification failed, please Login again.')
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- const { roles, name, avatar, introduction } = data
|
|
|
|
|
-
|
|
|
|
|
- // roles must be a non-empty array
|
|
|
|
|
- if (!roles || roles.length <= 0) {
|
|
|
|
|
- reject('getInfo: roles must be a non-null array!')
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- commit('SET_ROLES', roles)
|
|
|
|
|
- commit('SET_NAME', name)
|
|
|
|
|
- commit('SET_AVATAR', avatar)
|
|
|
|
|
- commit('SET_INTRODUCTION', introduction)
|
|
|
|
|
- resolve(data)
|
|
|
|
|
|
|
+ getUserInfo(data).then(response => {
|
|
|
|
|
+ const result = response.data
|
|
|
|
|
+ // 更新本地userInfo
|
|
|
|
|
+ usersInfo.userId(result.ID)
|
|
|
|
|
+ usersInfo.adminName(result.ADMIN_NAME)
|
|
|
|
|
+ usersInfo.baseData({ roleId: result.ROLE_ID })
|
|
|
|
|
+ resolve(result)
|
|
|
}).catch(error => {
|
|
}).catch(error => {
|
|
|
reject(error)
|
|
reject(error)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
- // user logout
|
|
|
|
|
- logout({ commit, state, dispatch }) {
|
|
|
|
|
|
|
+ getBaseInfo({ commit }, data) {
|
|
|
return new Promise((resolve, reject) => {
|
|
return new Promise((resolve, reject) => {
|
|
|
- logout(state.token).then(() => {
|
|
|
|
|
- commit('SET_TOKEN', '')
|
|
|
|
|
- commit('SET_ROLES', [])
|
|
|
|
|
- removeToken()
|
|
|
|
|
- resetRouter()
|
|
|
|
|
-
|
|
|
|
|
- // reset visited views and cached views
|
|
|
|
|
- // to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
|
|
|
|
|
- dispatch('tagsView/delAllViews', null, { root: true })
|
|
|
|
|
-
|
|
|
|
|
- resolve()
|
|
|
|
|
|
|
+ getBaseInfo().then(response => {
|
|
|
|
|
+ baseInfo.set(response.data)
|
|
|
|
|
+ resolve(response)
|
|
|
}).catch(error => {
|
|
}).catch(error => {
|
|
|
reject(error)
|
|
reject(error)
|
|
|
})
|
|
})
|
|
|
})
|
|
})
|
|
|
},
|
|
},
|
|
|
|
|
|
|
|
|
|
+ // user logout
|
|
|
|
|
+ logout({ commit, state, dispatch }) {
|
|
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
|
|
+ commit('SET_TOKEN', '')
|
|
|
|
|
+ commit('SET_ROLES', [])
|
|
|
|
|
+ usersInfo.clear()
|
|
|
|
|
+ usersInfo.baseData()
|
|
|
|
|
+ removeToken()
|
|
|
|
|
+ resetRouter()
|
|
|
|
|
+ dispatch('tagsView/delAllViews', null, { root: true })
|
|
|
|
|
+ resolve()
|
|
|
|
|
+ })
|
|
|
|
|
+ },
|
|
|
|
|
+
|
|
|
// remove token
|
|
// remove token
|
|
|
resetToken({ commit }) {
|
|
resetToken({ commit }) {
|
|
|
return new Promise(resolve => {
|
|
return new Promise(resolve => {
|
|
@@ -100,26 +112,6 @@ const actions = {
|
|
|
removeToken()
|
|
removeToken()
|
|
|
resolve()
|
|
resolve()
|
|
|
})
|
|
})
|
|
|
- },
|
|
|
|
|
-
|
|
|
|
|
- // dynamically modify permissions
|
|
|
|
|
- async changeRoles({ commit, dispatch }, role) {
|
|
|
|
|
- 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 })
|
|
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|