Skip to content

Commit

Permalink
feat: fetch userInfo logic
Browse files Browse the repository at this point in the history
  • Loading branch information
lbwa committed Oct 6, 2018
1 parent 42c51cb commit e401afa
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 19 deletions.
6 changes: 3 additions & 3 deletions mock/server/config/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions src/services/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down
16 changes: 13 additions & 3 deletions src/store/modules/login/actions.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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')
})
Expand All @@ -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))
}
}
9 changes: 9 additions & 0 deletions src/store/modules/login/getters.js
Original file line number Diff line number Diff line change
@@ -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
}
}
6 changes: 6 additions & 0 deletions src/store/modules/login/index.js
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 13 additions & 0 deletions src/store/modules/login/mutations/index.js
Original file line number Diff line number Diff line change
@@ -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
}
}
5 changes: 5 additions & 0 deletions src/store/modules/login/mutations/types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
SET_USERNAME: 'SET_USERNAME',
SET_ROLE: 'SET_ROLE',
SET_TOKEN: 'SET_TOKEN'
}
5 changes: 5 additions & 0 deletions src/store/modules/login/state.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export default {
username: '',
role: [],
token: ''
}
4 changes: 2 additions & 2 deletions src/store/modules/workspace/actions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
fetchSpaceUser,
fetchUserInfo,
fetchSpaceProjects,
fetchSpaceActivities,
fetchSpaceRadar,
Expand All @@ -10,7 +10,7 @@ import types from './mutations/types'
export default {
fetchWorkspace ({ commit }) {
Promise.all([
fetchSpaceUser(),
fetchUserInfo(),
fetchSpaceProjects(),
fetchSpaceActivities(),
fetchSpaceRadar(),
Expand Down
3 changes: 0 additions & 3 deletions src/store/mutations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
3 changes: 1 addition & 2 deletions src/store/mutations/types.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export default {
TOGGLE_ASIDE: 'TOGGLE_ASIDE',
SET_TOKEN: 'SET_TOKEN'
TOGGLE_ASIDE: 'TOGGLE_ASIDE'
}
3 changes: 1 addition & 2 deletions src/store/state.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export default {
isCollapse: false,
token: ''
isCollapse: false
}

0 comments on commit e401afa

Please sign in to comment.