From e667d069b4c137511a24ec073ea565aef494565e Mon Sep 17 00:00:00 2001 From: Bowen Date: Tue, 9 Oct 2018 20:21:55 +0800 Subject: [PATCH] feat: adjust set login/SET_ROLE strategy --- mock/server/config/routes.js | 9 ++++----- src/router/index.js | 11 ++--------- src/services/index.js | 9 ++------- src/store/modules/login/actions.js | 18 ++++++++++++------ src/store/modules/login/getters.js | 4 ++-- src/store/modules/login/mutations/index.js | 3 --- src/store/modules/login/mutations/types.js | 1 - src/store/modules/login/state.js | 1 - src/view/Login.vue | 2 +- 9 files changed, 23 insertions(+), 35 deletions(-) diff --git a/mock/server/config/routes.js b/mock/server/config/routes.js index 792683b..d945d64 100644 --- a/mock/server/config/routes.js +++ b/mock/server/config/routes.js @@ -10,8 +10,7 @@ module.exports = { avatar: 'https://assets-cdn.github.com/favicon.ico', notify: 12, position: '前端开发', - department: '蚂蚁金服-某某某事业群-某某平台部-某某技术部', - role: [req.query.role || 'admin'] + department: '蚂蚁金服-某某某事业群-某某平台部-某某技术部' }) }, @@ -27,19 +26,19 @@ module.exports = { 'POST /api/login': (req, res) => { const { username, password, token } = req.body - if (username === 'admin' && password === 'pro') { + if ((username === 'admin' || username === 'user') && password === 'pro') { res.send({ errno: 0, status: 'ok', token: token ? token : Math.random().toString(16).slice(2), - currentAuthority: 'admin' + role: [username] }) return } res.send({ errno: 1, status: 'error', - currentAuthority: 'guest' + role: ['user'] }) }, diff --git a/src/router/index.js b/src/router/index.js index 86e984a..bc8e395 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -24,15 +24,8 @@ router.beforeEach((to, from, next) => { // get token from sessionStorage if (getTokenFromLocal()) { // fetch user info - if (!store.getters['login/role'].length && to.path !== '/') { - store.dispatch('login/fetchUserInfo') - .then((userInfo) => { - // Preset dynamic routes is used to create new global routes map, - // filtered by `role` variable. - store.dispatch('login/createExtraRoutes', userInfo) - .then(() => router.addRoutes(store.getters['login/addRoutes'])) - }) - .catch(console.error) + if (!store.getters['login/userInfo'].name && to.path !== '/') { + store.dispatch('login/fetchUserInfo').catch(console.error) } next() } else { diff --git a/src/services/index.js b/src/services/index.js index fddc55a..4df8bc4 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -1,6 +1,5 @@ import baseRequest from './base' import * as routes from './routes' -import qs from 'querystring' // `POST` methods should be throttled in production mode export function pushLogin (userInfo) { @@ -15,12 +14,8 @@ export function pushBasicForm (formData) { return baseRequest.post(routes.FORM_BASIC, formData) } -export function fetchUserInfo (query) { - const url = query - ? `${routes.CURRENT_USER}?${qs.stringify(query)}` - : routes.CURRENT_USER - - return baseRequest.get(url) +export function fetchUserInfo () { + return baseRequest.get(routes.CURRENT_USER) } export function fetchAllAnalysis () { diff --git a/src/store/modules/login/actions.js b/src/store/modules/login/actions.js index b2a2439..27988d1 100644 --- a/src/store/modules/login/actions.js +++ b/src/store/modules/login/actions.js @@ -1,5 +1,6 @@ import { pushLogin, fetchUserInfo } from 'SERVICES' import { setTokenToLocal, removeToken } from 'AUTH' +import router from 'ROUTER' import { dynamicRoutes } from 'ROUTER/routes' import types from './mutations/types' import { Notification } from 'element-ui' @@ -8,12 +9,13 @@ import { Notification } from 'element-ui' const ADMINISTRATOR = 'admin' export default { - pushLogin ({ commit, dispatch }, { userInfo, replace }) { + pushLogin ({ commit, dispatch, getters }, { userInfo, replace }) { return pushLogin(userInfo) .then(res => { const data = res.data if (data.errno !== 0) throw new Error(`用户名或密码错误`) - if (!data.token) throw new Error(`[Token]: empty token`) + if (!data.token) throw new Error(`[Token]: Wrong token response`) + if (!Array.isArray(data.role)) throw new Error(`[Role]: Wrong role response`) return data }) .then(data => { @@ -21,7 +23,15 @@ export default { title: 'Success', message: '登陆成功,正在跳转...' }) + commit(types.SET_ROLE, data.role) commit(types.SET_TOKEN, data.token) + + // Preset dynamic routes is used to create new global routes map, + // filtered by `role` variable. + // action named createExtraRoutes should follow mutation named SET_ROLE + dispatch('createExtraRoutes', { role: data.role }) + .then(() => router.addRoutes(getters.addRoutes)) + setTokenToLocal({ token: data.token }) replace('/dashboard/analysis') }) @@ -38,10 +48,7 @@ export default { .then(res => res.data) .then(data => { if (data.errno !== 0) throw new Error(`[errno]: ${data.errno}`) - if (!Array.isArray(data.role)) throw new Error(`[role]: ${data.role}`) commit(types.SET_USER_INFO, data) - commit(types.SET_USERNAME, data.name) - commit(types.SET_ROLE, data.role) return data }) .catch(console.error) @@ -65,7 +72,6 @@ export default { }, logout ({ commit }, replace) { commit(types.SET_USER_INFO, {}) - commit(types.SET_USERNAME, '') commit(types.SET_ROLE, []) commit(types.SET_TOKEN, '') removeToken() diff --git a/src/store/modules/login/getters.js b/src/store/modules/login/getters.js index 7069ec2..df40f97 100644 --- a/src/store/modules/login/getters.js +++ b/src/store/modules/login/getters.js @@ -1,7 +1,7 @@ // We do not use `state` directly for strong scalability. export default { - username (state) { - return state.username + userInfo (state) { + return state.userInfo }, role (state) { return state.role diff --git a/src/store/modules/login/mutations/index.js b/src/store/modules/login/mutations/index.js index ed4d1b6..4e434f3 100644 --- a/src/store/modules/login/mutations/index.js +++ b/src/store/modules/login/mutations/index.js @@ -5,9 +5,6 @@ export default { [types.SET_USER_INFO] (state, userInfo) { state.userInfo = userInfo }, - [types.SET_USERNAME] (state, username) { - state.username = username - }, [types.SET_ROLE] (state, role) { state.role = role }, diff --git a/src/store/modules/login/mutations/types.js b/src/store/modules/login/mutations/types.js index 1b98e88..9dcb375 100644 --- a/src/store/modules/login/mutations/types.js +++ b/src/store/modules/login/mutations/types.js @@ -1,6 +1,5 @@ export default { SET_USER_INFO: 'SET_USER_INFO', - SET_USERNAME: 'SET_USERNAME', SET_ROLE: 'SET_ROLE', SET_TOKEN: 'SET_TOKEN', SET_ROUTES: 'SET_ROUTES' diff --git a/src/store/modules/login/state.js b/src/store/modules/login/state.js index aace555..540a09a 100644 --- a/src/store/modules/login/state.js +++ b/src/store/modules/login/state.js @@ -2,7 +2,6 @@ import { commonRoutes } from 'ROUTER/routes' export default { userInfo: {}, - username: '', role: [], token: '', routes: commonRoutes, // store current global routes map diff --git a/src/view/Login.vue b/src/view/Login.vue index 6cc5f14..cafb13b 100644 --- a/src/view/Login.vue +++ b/src/view/Login.vue @@ -16,7 +16,7 @@ >