From fef089a8b5223e40996eb1796fbba9148089b156 Mon Sep 17 00:00:00 2001 From: chenghx Date: Mon, 13 Aug 2018 11:38:29 +0800 Subject: [PATCH] feat: add account state --- src/components/dashboard/WorkPlace.vue | 15 +++++---------- src/components/layout/HeaderlAvatar.vue | 22 +++------------------- src/components/login/Login.vue | 2 ++ src/main.js | 4 ++-- src/mock/user/login.js | 11 +++++++++++ src/store/index.js | 11 +++++++++++ src/store/modules/account.js | 11 +++++++++++ 7 files changed, 45 insertions(+), 31 deletions(-) create mode 100644 src/store/index.js create mode 100644 src/store/modules/account.js diff --git a/src/components/dashboard/WorkPlace.vue b/src/components/dashboard/WorkPlace.vue index beec3f0..b415a02 100644 --- a/src/components/dashboard/WorkPlace.vue +++ b/src/components/dashboard/WorkPlace.vue @@ -124,28 +124,23 @@ export default { PageHeader}, data () { return { - currUser: {}, projects: [], loading: true, activities: [], teams: [] } }, + computed: { + currUser () { + return this.$store.state.account.user + } + }, mounted () { - this.currentUser() this.getProjectList() this.getActivites() this.getTeams() }, methods: { - currentUser () { - this.$axios({ - method: 'get', - url: '/user/current' - }).then(res => { - this.currUser = res.data - }) - }, getProjectList () { this.$axios({ method: 'get', diff --git a/src/components/layout/HeaderlAvatar.vue b/src/components/layout/HeaderlAvatar.vue index 837cb01..24b71bf 100644 --- a/src/components/layout/HeaderlAvatar.vue +++ b/src/components/layout/HeaderlAvatar.vue @@ -36,25 +36,9 @@ const AMenuDivider = AMenu.Divider export default { name: 'HeaderAvatar', components: {AMenu, AMenuItem, AMenuDivider, AIcon, AAvatar, ADropdown}, - data () { - return { - currUser: { - name: 'XXXXX', - avatar: '' - } - } - }, - mounted () { - this.currentUser() - }, - methods: { - currentUser () { - this.$axios({ - method: 'get', - url: '/user/current' - }).then(res => { - this.currUser = res.data - }) + computed: { + currUser () { + return this.$store.state.account.user } } } diff --git a/src/components/login/Login.vue b/src/components/login/Login.vue index c82d48d..4c21325 100644 --- a/src/components/login/Login.vue +++ b/src/components/login/Login.vue @@ -120,7 +120,9 @@ export default { this.logging = false const result = res.data if (result.code >= 0) { + const user = result.data.user this.$router.push('/dashboard/workplace') + this.$store.commit('account/setuser', user) this.$message.success(result.message, 3) } else { this.error = result.message diff --git a/src/main.js b/src/main.js index 0ac459b..42c45de 100644 --- a/src/main.js +++ b/src/main.js @@ -8,18 +8,18 @@ import Viser from 'viser-vue' import axios from 'axios' import message from 'ant-design-vue/es/message' import '@/mock' -import Vuex from 'vuex' +import store from './store' Vue.prototype.$axios = axios Vue.prototype.$message = message Vue.config.productionTip = false Vue.use(Viser) -Vue.use(Vuex) /* eslint-disable no-new */ new Vue({ el: '#app', router, + store, components: { App }, template: '' }) diff --git a/src/mock/user/login.js b/src/mock/user/login.js index 6471dcd..014287d 100644 --- a/src/mock/user/login.js +++ b/src/mock/user/login.js @@ -1,6 +1,15 @@ import Mock from 'mockjs' import '@/mock/extend' +const user = Mock.mock({ + name: '@ADMIN', + avatar: '@AVATAR', + address: '@CITY', + welcome: '@WELCOME', + timefix: '@TIMEFIX', + position: '@POSITION' +}) + Mock.mock('/login', 'post', ({body}) => { let result = {} const {name, password} = JSON.parse(body) @@ -11,6 +20,8 @@ Mock.mock('/login', 'post', ({body}) => { } else { result.code = 0 result.message = Mock.mock('@TIMEFIX') + ',欢迎回来' + result.data = {} + result.data.user = user } return result }) diff --git a/src/store/index.js b/src/store/index.js new file mode 100644 index 0000000..04274a0 --- /dev/null +++ b/src/store/index.js @@ -0,0 +1,11 @@ +import Vue from 'vue' +import Vuex from 'vuex' +import account from './modules/account' + +Vue.use(Vuex) + +export default new Vuex.Store({ + modules: { + account + } +}) diff --git a/src/store/modules/account.js b/src/store/modules/account.js new file mode 100644 index 0000000..b1259a9 --- /dev/null +++ b/src/store/modules/account.js @@ -0,0 +1,11 @@ +export default { + namespaced: true, + state: { + user: {} + }, + mutations: { + setuser (state, user) { + state.user = user + } + } +}