Fix user menu bug

pull/9/head
johnniang 2019-05-07 21:33:18 +08:00
parent 926007c45b
commit 47699b38a2
7 changed files with 45 additions and 18 deletions

View File

@ -18,12 +18,16 @@
</a> </a>
<header-comment class="action" /> <header-comment class="action" />
<a-dropdown> <a-dropdown>
<span class="action ant-dropdown-link user-dropdown-menu"> <span
class="action ant-dropdown-link user-dropdown-menu"
v-if="user"
>
<a-avatar <a-avatar
class="avatar" class="avatar"
size="small" size="small"
:src="avatar" :src="user.avatar"
/> />
{{ user.nickname }}
</span> </span>
<a-menu <a-menu
slot="overlay" slot="overlay"
@ -55,7 +59,6 @@
import HeaderComment from './HeaderComment' import HeaderComment from './HeaderComment'
import SettingDrawer from '@/components/SettingDrawer/SettingDrawer' import SettingDrawer from '@/components/SettingDrawer/SettingDrawer'
import { mapActions, mapGetters } from 'vuex' import { mapActions, mapGetters } from 'vuex'
import userApi from '@/api/user'
import optionApi from '@/api/option' import optionApi from '@/api/option'
export default { export default {
@ -67,7 +70,6 @@ export default {
data() { data() {
return { return {
optionVisible: true, optionVisible: true,
user: {},
options: [], options: [],
keys: ['blog_url'] keys: ['blog_url']
} }
@ -76,11 +78,10 @@ export default {
this.optionVisible = this.$refs.drawer.visible this.optionVisible = this.$refs.drawer.visible
}, },
created() { created() {
this.loadUser()
this.loadOptions() this.loadOptions()
}, },
computed: { computed: {
...mapGetters(['nickname', 'avatar']) ...mapGetters(['user'])
}, },
methods: { methods: {
...mapActions(['logout']), ...mapActions(['logout']),
@ -110,11 +111,6 @@ export default {
this.optionVisible = this.$refs.drawer.visible this.optionVisible = this.$refs.drawer.visible
this.$refs.drawer.toggle() this.$refs.drawer.toggle()
}, },
loadUser() {
userApi.getProfile().then(response => {
this.user = response.data.data
})
},
loadOptions() { loadOptions() {
optionApi.listAll(this.keys).then(response => { optionApi.listAll(this.keys).then(response => {
this.options = response.data.data this.options = response.data.data
@ -123,3 +119,9 @@ export default {
} }
} }
</script> </script>
<style lang="less" scoped>
.avatar {
margin-right: 0.3rem;
}
</style>

View File

@ -9,7 +9,8 @@ import {
DEFAULT_FIXED_HEADER, DEFAULT_FIXED_HEADER,
DEFAULT_FIXED_HEADER_HIDDEN, DEFAULT_FIXED_HEADER_HIDDEN,
DEFAULT_FIXED_SIDEMENU, DEFAULT_FIXED_SIDEMENU,
DEFAULT_CONTENT_WIDTH_TYPE DEFAULT_CONTENT_WIDTH_TYPE,
USER
} from '@/store/mutation-types' } from '@/store/mutation-types'
import config from '@/config/defaultSettings' 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_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('TOGGLE_COLOR', Vue.ls.get(DEFAULT_COLOR, config.primaryColor))
store.commit('SET_TOKEN', Vue.ls.get(ACCESS_TOKEN)) store.commit('SET_TOKEN', Vue.ls.get(ACCESS_TOKEN))
store.commit('SET_USER', Vue.ls.get(USER))
// last step // last step
} }

View File

@ -3,6 +3,7 @@ const getters = {
theme: state => state.app.theme, theme: state => state.app.theme,
color: state => state.app.color, color: state => state.app.color,
token: state => state.user.token, token: state => state.user.token,
user: state => state.user.user,
avatar: state => state.user.avatar, avatar: state => state.user.avatar,
nickname: state => state.user.name, nickname: state => state.user.name,
roles: state => state.user.roles, roles: state => state.user.roles,

View File

@ -1,6 +1,7 @@
import Vue from 'vue' 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 adminApi from '@/api/admin'
import userApi from '@/api/user'
const user = { const user = {
state: { state: {
@ -8,7 +9,8 @@ const user = {
name: '', name: '',
avatar: '', avatar: '',
roles: [], roles: [],
info: {} info: {},
user: {}
}, },
mutations: { mutations: {
SET_TOKEN: (state, token) => { SET_TOKEN: (state, token) => {
@ -30,9 +32,26 @@ const user = {
CLEAR_TOKEN: state => { CLEAR_TOKEN: state => {
Vue.ls.remove(ACCESS_TOKEN) Vue.ls.remove(ACCESS_TOKEN)
state.token = null state.token = null
},
SET_USER: (state, user) => {
Vue.ls.set(USER, user)
state.user = user
} }
}, },
actions: { 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 }) { login({ commit }, { username, password }) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
adminApi adminApi

View File

@ -7,6 +7,7 @@ export const DEFAULT_FIXED_HEADER = 'DEFAULT_FIXED_HEADER'
export const DEFAULT_FIXED_SIDEMENU = 'DEFAULT_FIXED_SIDEMENU' export const DEFAULT_FIXED_SIDEMENU = 'DEFAULT_FIXED_SIDEMENU'
export const DEFAULT_FIXED_HEADER_HIDDEN = 'DEFAULT_FIXED_HEADER_HIDDEN' export const DEFAULT_FIXED_HEADER_HIDDEN = 'DEFAULT_FIXED_HEADER_HIDDEN'
export const DEFAULT_CONTENT_WIDTH_TYPE = 'DEFAULT_CONTENT_WIDTH_TYPE' export const DEFAULT_CONTENT_WIDTH_TYPE = 'DEFAULT_CONTENT_WIDTH_TYPE'
export const USER = 'USER'
export const CONTENT_WIDTH_TYPE = { export const CONTENT_WIDTH_TYPE = {
Fluid: 'Fluid', Fluid: 'Fluid',

View File

@ -64,7 +64,7 @@ export default {
} }
}, },
methods: { methods: {
...mapActions(['login']), ...mapActions(['login', 'loadUser']),
handleLogin() { handleLogin() {
if (!this.username) { if (!this.username) {
this.$message.warn('用户名不能为空') this.$message.warn('用户名不能为空')
@ -82,6 +82,8 @@ export default {
}) })
}, },
loginSuccess() { loginSuccess() {
// Cache the user info
this.loadUser()
if (this.$route.query.redirect) { if (this.$route.query.redirect) {
this.$router.replace(this.$route.query.redirect) this.$router.replace(this.$route.query.redirect)
} else { } else {
@ -104,7 +106,7 @@ body {
margin: -160px 0 0 -160px; margin: -160px 0 0 -160px;
width: 320px; width: 320px;
padding: 16px 32px 32px 32px; 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 { .loginLogo {
margin-bottom: 20px; margin-bottom: 20px;

View File

@ -181,7 +181,7 @@ export default {
this.loadOptions() this.loadOptions()
}, },
methods: { methods: {
...mapMutations({ setAvatar: 'SET_AVATAR' }), ...mapMutations({ setUser: 'SET_USER' }),
handleShowAttachDrawer() { handleShowAttachDrawer() {
this.attachmentDrawerVisible = true this.attachmentDrawerVisible = true
}, },
@ -214,7 +214,7 @@ export default {
handleUpdateProfile() { handleUpdateProfile() {
userApi.updateProfile(this.user).then(response => { userApi.updateProfile(this.user).then(response => {
this.user = response.data.data this.user = response.data.data
this.setAvatar(this.user.avatar) this.setUser(Object.assign({}, this.user))
this.$message.success('资料更新成功!') this.$message.success('资料更新成功!')
}) })
}, },