From 47699b38a240811424d4845987d96af30f9e10f4 Mon Sep 17 00:00:00 2001 From: johnniang Date: Tue, 7 May 2019 21:33:18 +0800 Subject: [PATCH] Fix user menu bug --- src/components/Tools/UserMenu.vue | 24 +++++++++++++----------- src/core/bootstrap.js | 4 +++- src/store/getters.js | 1 + src/store/modules/user.js | 23 +++++++++++++++++++++-- src/store/mutation-types.js | 1 + src/views/user/Login.vue | 6 ++++-- src/views/user/Profile.vue | 4 ++-- 7 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/components/Tools/UserMenu.vue b/src/components/Tools/UserMenu.vue index d515eb72..c494a45e 100644 --- a/src/components/Tools/UserMenu.vue +++ b/src/components/Tools/UserMenu.vue @@ -18,12 +18,16 @@ - + + {{ user.nickname }} { - this.user = response.data.data - }) - }, loadOptions() { optionApi.listAll(this.keys).then(response => { this.options = response.data.data @@ -123,3 +119,9 @@ export default { } } + + diff --git a/src/core/bootstrap.js b/src/core/bootstrap.js index 32ee2532..10e743f6 100644 --- a/src/core/bootstrap.js +++ b/src/core/bootstrap.js @@ -9,7 +9,8 @@ import { DEFAULT_FIXED_HEADER, DEFAULT_FIXED_HEADER_HIDDEN, DEFAULT_FIXED_SIDEMENU, - DEFAULT_CONTENT_WIDTH_TYPE + DEFAULT_CONTENT_WIDTH_TYPE, + USER } from '@/store/mutation-types' import config from '@/config/defaultSettings' @@ -23,6 +24,7 @@ export default function Initializer() { store.commit('TOGGLE_FIXED_HEADER_HIDDEN', Vue.ls.get(DEFAULT_FIXED_HEADER_HIDDEN, config.autoHideHeader)) store.commit('TOGGLE_COLOR', Vue.ls.get(DEFAULT_COLOR, config.primaryColor)) store.commit('SET_TOKEN', Vue.ls.get(ACCESS_TOKEN)) + store.commit('SET_USER', Vue.ls.get(USER)) // last step } diff --git a/src/store/getters.js b/src/store/getters.js index 1bcb90a4..5ba11085 100644 --- a/src/store/getters.js +++ b/src/store/getters.js @@ -3,6 +3,7 @@ const getters = { theme: state => state.app.theme, color: state => state.app.color, token: state => state.user.token, + user: state => state.user.user, avatar: state => state.user.avatar, nickname: state => state.user.name, roles: state => state.user.roles, diff --git a/src/store/modules/user.js b/src/store/modules/user.js index 7582f7f3..ee43b841 100644 --- a/src/store/modules/user.js +++ b/src/store/modules/user.js @@ -1,6 +1,7 @@ import Vue from 'vue' -import { ACCESS_TOKEN } from '@/store/mutation-types' +import { ACCESS_TOKEN, USER } from '@/store/mutation-types' import adminApi from '@/api/admin' +import userApi from '@/api/user' const user = { state: { @@ -8,7 +9,8 @@ const user = { name: '', avatar: '', roles: [], - info: {} + info: {}, + user: {} }, mutations: { SET_TOKEN: (state, token) => { @@ -30,9 +32,26 @@ const user = { CLEAR_TOKEN: state => { Vue.ls.remove(ACCESS_TOKEN) state.token = null + }, + SET_USER: (state, user) => { + Vue.ls.set(USER, user) + state.user = user } }, actions: { + loadUser({ commit }) { + return Promise((resolve, reject) => { + userApi + .getProfile() + .then(response => { + commit('SET_USER', response.data.data) + resolve(response) + }) + .catch(error => { + reject(error) + }) + }) + }, login({ commit }, { username, password }) { return new Promise((resolve, reject) => { adminApi diff --git a/src/store/mutation-types.js b/src/store/mutation-types.js index a0917a9c..9db404fb 100644 --- a/src/store/mutation-types.js +++ b/src/store/mutation-types.js @@ -7,6 +7,7 @@ export const DEFAULT_FIXED_HEADER = 'DEFAULT_FIXED_HEADER' export const DEFAULT_FIXED_SIDEMENU = 'DEFAULT_FIXED_SIDEMENU' export const DEFAULT_FIXED_HEADER_HIDDEN = 'DEFAULT_FIXED_HEADER_HIDDEN' export const DEFAULT_CONTENT_WIDTH_TYPE = 'DEFAULT_CONTENT_WIDTH_TYPE' +export const USER = 'USER' export const CONTENT_WIDTH_TYPE = { Fluid: 'Fluid', diff --git a/src/views/user/Login.vue b/src/views/user/Login.vue index ae156d4e..bad6fbd7 100644 --- a/src/views/user/Login.vue +++ b/src/views/user/Login.vue @@ -64,7 +64,7 @@ export default { } }, methods: { - ...mapActions(['login']), + ...mapActions(['login', 'loadUser']), handleLogin() { if (!this.username) { this.$message.warn('用户名不能为空') @@ -82,6 +82,8 @@ export default { }) }, loginSuccess() { + // Cache the user info + this.loadUser() if (this.$route.query.redirect) { this.$router.replace(this.$route.query.redirect) } else { @@ -104,7 +106,7 @@ body { margin: -160px 0 0 -160px; width: 320px; padding: 16px 32px 32px 32px; - box-shadow: -4px 7px 46px 2px rgba(0,0,0,.1); + box-shadow: -4px 7px 46px 2px rgba(0, 0, 0, 0.1); } .loginLogo { margin-bottom: 20px; diff --git a/src/views/user/Profile.vue b/src/views/user/Profile.vue index 37ef603b..72acf11c 100644 --- a/src/views/user/Profile.vue +++ b/src/views/user/Profile.vue @@ -181,7 +181,7 @@ export default { this.loadOptions() }, methods: { - ...mapMutations({ setAvatar: 'SET_AVATAR' }), + ...mapMutations({ setUser: 'SET_USER' }), handleShowAttachDrawer() { this.attachmentDrawerVisible = true }, @@ -214,7 +214,7 @@ export default { handleUpdateProfile() { userApi.updateProfile(this.user).then(response => { this.user = response.data.data - this.setAvatar(this.user.avatar) + this.setUser(Object.assign({}, this.user)) this.$message.success('资料更新成功!') }) },