Skip to content

Commit

Permalink
fix(sys-client): fix nav styles & user state;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Sep 8, 2021
1 parent b91ba2b commit 4885ecd
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
2 changes: 1 addition & 1 deletion packages/system-client/src/api/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function login(data) {
* @param {string} token
* @returns
*/
export function getInfo() {
export function getUserProfile() {
return request({
url: '/account/profile',
method: 'get'
Expand Down
5 changes: 5 additions & 0 deletions packages/system-client/src/layout/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export default {
},
async created() {
console.log('app layout created', this.$route)
if (!this.app) {
this.$router.push({
path: '/applications'
})
}
},
beforeDestroy() {
resetRouter()
Expand Down
14 changes: 5 additions & 9 deletions packages/system-client/src/layout/components/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@
<tags-view v-if="!hideTags" />
</div>
<div class="right-menu">
<template v-if="device!=='mobile'">
<screenfull id="screenfull" class="right-menu-item hover-effect" />
</template>
<screenfull id="screenfull" class="right-menu-item hover-effect" />
<div class="github right-menu-item">
<a target="_blank" href="https://github.com/Maslow/laf/">
GitHub
Expand All @@ -35,7 +33,6 @@
</template>

<script>
import { mapGetters } from 'vuex'
import Screenfull from '@/components/Screenfull'
import TagsView from './TagsView'
export default {
Expand All @@ -54,10 +51,10 @@ export default {
}
},
computed: {
...mapGetters([
'name',
'device'
])
name() {
const profile = this.$store.state.user.user_profile
return profile?.username || profile?.name
}
},
methods: {
async logout() {
Expand Down Expand Up @@ -118,7 +115,6 @@ export default {
.right-menu {
float: right;
height: 100%;
width: 200px;
line-height: 50px;
display: flex;
Expand Down
26 changes: 10 additions & 16 deletions packages/system-client/src/store/modules/user.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import { login, getInfo } from '@/api/user'
import { login, getUserProfile } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import { Message } from 'element-ui'

const state = {
token: getToken(),
name: '',
avatar: ''
user_profile: null
}

const mutations = {
SET_TOKEN: (state, token) => {
state.token = token
},
SET_NAME: (state, name) => {
state.name = name
},
SET_AVATAR: (state, avatar) => {
state.avatar = avatar
SET_USER_PROFILE: (state, user_profile) => {
state.user_profile = user_profile
}
}

const actions = {
// user login
async login({ commit }, userInfo) {
const { username, password } = userInfo
async login({ commit }, { username, password }) {
const res = await login({ username: username.trim(), password: password })
const { data } = res
if (res.error) {
Expand All @@ -38,29 +33,26 @@ const actions = {

// get user info
async getInfo({ commit, dispatch }) {
const res = await getInfo()
const res = await getUserProfile()
const { data } = res
if (!data) {
return dispatch('/user/logout')
}
const account = data
commit('SET_NAME', account.name || account.username)
commit('SET_AVATAR', account.avatar)
commit('SET_USER_PROFILE', account)
return data
},

// user logout
logout({ commit, state, dispatch }) {
return new Promise((resolve, reject) => {
commit('SET_TOKEN', '')
commit('SET_NAME', '')
commit('SET_AVATAR', '')
removeToken()
resetRouter()

// reset visited views and cached views
// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2485
dispatch('tagsView/delAllViews', null, { root: true })
dispatch('app/clearStates')
resolve()
})
},
Expand All @@ -69,6 +61,8 @@ const actions = {
resetToken({ commit }) {
return new Promise(resolve => {
commit('SET_TOKEN', '')
commit('SET_USER_PROFILE', null)
commit('SET_USER_PROFILE', null)
removeToken()
resolve()
})
Expand Down

0 comments on commit 4885ecd

Please sign in to comment.