From e401afa5a470f543234f352810c371f8aa9c0c37 Mon Sep 17 00:00:00 2001 From: Bowen Date: Sat, 6 Oct 2018 23:28:29 +0800 Subject: [PATCH] feat: fetch userInfo logic --- mock/server/config/routes.js | 6 +++--- src/services/index.js | 8 ++++---- src/store/modules/login/actions.js | 16 +++++++++++++--- src/store/modules/login/getters.js | 9 +++++++++ src/store/modules/login/index.js | 6 ++++++ src/store/modules/login/mutations/index.js | 13 +++++++++++++ src/store/modules/login/mutations/types.js | 5 +++++ src/store/modules/login/state.js | 5 +++++ src/store/modules/workspace/actions.js | 4 ++-- src/store/mutations/index.js | 3 --- src/store/mutations/types.js | 3 +-- src/store/state.js | 3 +-- 12 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 src/store/modules/login/getters.js create mode 100644 src/store/modules/login/mutations/index.js create mode 100644 src/store/modules/login/mutations/types.js create mode 100644 src/store/modules/login/state.js diff --git a/mock/server/config/routes.js b/mock/server/config/routes.js index 39fb03d..7992adc 100644 --- a/mock/server/config/routes.js +++ b/mock/server/config/routes.js @@ -4,12 +4,12 @@ const api = require('../../api') module.exports = { 'GET /api/user': { + errno: 0, name: 'Bowen Liu', - avatar: '', - token: Math.random().toString(16).slice(2), + avatar: 'https://assets-cdn.github.com/apple-touch-icon-120x120.png', notify: 12, position: '前端开发', - department: '蚂蚁金服-某某某事业群-某某平台部-某某技术部-UED' + department: '蚂蚁金服-某某某事业群-某某平台部-某某技术部' }, 'GET /api/analysis': analysis, diff --git a/src/services/index.js b/src/services/index.js index 7b7cf5c..4df8bc4 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -14,12 +14,12 @@ export function pushBasicForm (formData) { return baseRequest.post(routes.FORM_BASIC, formData) } -export function fetchAllAnalysis () { - return baseRequest.get(routes.ANALYSIS) +export function fetchUserInfo () { + return baseRequest.get(routes.CURRENT_USER) } -export function fetchSpaceUser () { - return baseRequest.get(routes.CURRENT_USER) +export function fetchAllAnalysis () { + return baseRequest.get(routes.ANALYSIS) } export function fetchSpaceProjects () { diff --git a/src/store/modules/login/actions.js b/src/store/modules/login/actions.js index 15dc67a..733eec8 100644 --- a/src/store/modules/login/actions.js +++ b/src/store/modules/login/actions.js @@ -1,6 +1,6 @@ -import { pushLogin } from 'SERVICES' +import { pushLogin, fetchUserInfo } from 'SERVICES' import { setTokenToLocal } from 'AUTH' -import types from 'STORE/mutations/types' +import types from './mutations/types' import { Notification } from 'element-ui' export default { @@ -24,7 +24,7 @@ export default { }) .then(data => { if (!data.token) throw new Error(`[pushLogin]: token's error`) - commit(types.SET_TOKEN, data.token, { root: true }) + commit(types.SET_TOKEN, data.token) setTokenToLocal({ token: data.token }) replace('/dashboard/analysis') }) @@ -35,5 +35,15 @@ export default { }) console.error(e) }) + }, + fetchUserInfo ({ commit }) { + return fetchUserInfo({ commit }) + .then(res => res.data) + .then(data => { + if (data.errno !== 0) throw new Error(`[fetchUserInfo]: Token error`) + commit(types.SET_USERNAME, data.name) + commit(types.SET_ROLE, data.role) + }) + .catch(e => console.error(e)) } } diff --git a/src/store/modules/login/getters.js b/src/store/modules/login/getters.js new file mode 100644 index 0000000..9fa4c1f --- /dev/null +++ b/src/store/modules/login/getters.js @@ -0,0 +1,9 @@ +// We do not use `state` directly for strong scalability. +export default { + username (state) { + return state.username + }, + role (state) { + return state.role + } +} diff --git a/src/store/modules/login/index.js b/src/store/modules/login/index.js index ac867fe..ecdebef 100644 --- a/src/store/modules/login/index.js +++ b/src/store/modules/login/index.js @@ -1,5 +1,11 @@ +import state from './state' +import getters from './getters' +import mutations from './mutations' import actions from './actions' export default { + state, + getters, + mutations, actions } diff --git a/src/store/modules/login/mutations/index.js b/src/store/modules/login/mutations/index.js new file mode 100644 index 0000000..2c2eb11 --- /dev/null +++ b/src/store/modules/login/mutations/index.js @@ -0,0 +1,13 @@ +import types from './types' + +export default { + [types.SET_USERNAME] (state, username) { + state.username = username + }, + [types.SET_ROLE] (state, role) { + state.role = role + }, + [types.SET_TOKEN] (state, token) { + state.token = token + } +} diff --git a/src/store/modules/login/mutations/types.js b/src/store/modules/login/mutations/types.js new file mode 100644 index 0000000..5f2d0c5 --- /dev/null +++ b/src/store/modules/login/mutations/types.js @@ -0,0 +1,5 @@ +export default { + SET_USERNAME: 'SET_USERNAME', + SET_ROLE: 'SET_ROLE', + SET_TOKEN: 'SET_TOKEN' +} diff --git a/src/store/modules/login/state.js b/src/store/modules/login/state.js new file mode 100644 index 0000000..93a777c --- /dev/null +++ b/src/store/modules/login/state.js @@ -0,0 +1,5 @@ +export default { + username: '', + role: [], + token: '' +} diff --git a/src/store/modules/workspace/actions.js b/src/store/modules/workspace/actions.js index d885f5c..a93dc03 100644 --- a/src/store/modules/workspace/actions.js +++ b/src/store/modules/workspace/actions.js @@ -1,5 +1,5 @@ import { - fetchSpaceUser, + fetchUserInfo, fetchSpaceProjects, fetchSpaceActivities, fetchSpaceRadar, @@ -10,7 +10,7 @@ import types from './mutations/types' export default { fetchWorkspace ({ commit }) { Promise.all([ - fetchSpaceUser(), + fetchUserInfo(), fetchSpaceProjects(), fetchSpaceActivities(), fetchSpaceRadar(), diff --git a/src/store/mutations/index.js b/src/store/mutations/index.js index 87c2366..b45e55d 100644 --- a/src/store/mutations/index.js +++ b/src/store/mutations/index.js @@ -3,8 +3,5 @@ import types from './types' export default { [types.TOGGLE_ASIDE] (state) { state.isCollapse = !state.isCollapse - }, - [types.SET_TOKEN] (state, token) { - state.token = token } } diff --git a/src/store/mutations/types.js b/src/store/mutations/types.js index 0860867..b9ef882 100644 --- a/src/store/mutations/types.js +++ b/src/store/mutations/types.js @@ -1,4 +1,3 @@ export default { - TOGGLE_ASIDE: 'TOGGLE_ASIDE', - SET_TOKEN: 'SET_TOKEN' + TOGGLE_ASIDE: 'TOGGLE_ASIDE' } diff --git a/src/store/state.js b/src/store/state.js index 25012c1..aa0d60d 100644 --- a/src/store/state.js +++ b/src/store/state.js @@ -1,4 +1,3 @@ export default { - isCollapse: false, - token: '' + isCollapse: false }