Skip to content

Commit

Permalink
提供重置登录信息状态方法。修复登出或替换账号还保留之前账号操作信息和痕迹bug
Browse files Browse the repository at this point in the history
  • Loading branch information
daxiongYang committed Jul 23, 2018
1 parent 9785ad9 commit 288e539
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import '@/element-ui-theme'
import '@/assets/scss/index.scss'
import httpRequest from '@/utils/httpRequest' // api: https://github.com/axios/axios
import { isAuth } from '@/utils'
import cloneDeep from 'lodash/cloneDeep'

Vue.use(VueCookie)
Vue.config.productionTip = false
Expand All @@ -22,6 +23,9 @@ if (process.env.NODE_ENV !== 'production') {
Vue.prototype.$http = httpRequest // ajax请求方法
Vue.prototype.isAuth = isAuth // 权限方法

// 保存整站vuex本地储存初始状态
window.SITE_CONFIG['storeState'] = cloneDeep(store.state)

/* eslint-disable no-new */
new Vue({
el: '#app',
Expand Down
2 changes: 2 additions & 0 deletions src/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Vue from 'vue'
import Router from 'vue-router'
import http from '@/utils/httpRequest'
import { isURL } from '@/utils/validate'
import { clearLoginInfo } from '@/utils'

Vue.use(Router)

Expand Down Expand Up @@ -40,6 +41,7 @@ const mainRoutes = {
beforeEnter (to, from, next) {
let token = Vue.cookie.get('token')
if (!token || !/\S/.test(token)) {
clearLoginInfo()
next({ name: 'login' })
}
next()
Expand Down
9 changes: 9 additions & 0 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Vue from 'vue'
import Vuex from 'vuex'
import cloneDeep from 'lodash/cloneDeep'
import common from './modules/common'
import user from './modules/user'

Expand All @@ -10,5 +11,13 @@ export default new Vuex.Store({
common,
user
},
mutations: {
// 重置vuex本地储存状态
resetStore (state) {
Object.keys(state).forEach((key) => {
state[key] = cloneDeep(window.SITE_CONFIG['storeState'][key])
})
}
},
strict: process.env.NODE_ENV !== 'production'
})
4 changes: 2 additions & 2 deletions src/utils/httpRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import axios from 'axios'
import router from '@/router'
import qs from 'qs'
import merge from 'lodash/merge'
import { clearLoginInfo } from '@/utils'

const http = axios.create({
timeout: 1000 * 30,
Expand All @@ -27,8 +28,7 @@ http.interceptors.request.use(config => {
*/
http.interceptors.response.use(response => {
if (response.data && response.data.code === 401) { // 401, token失效
Vue.cookie.delete('token')
router.options.isAddDynamicMenuRoutes = false
clearLoginInfo()
router.push({ name: 'login' })
}
return response
Expand Down
13 changes: 13 additions & 0 deletions src/utils/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import Vue from 'vue'
import router from '@/router'
import store from '@/store'

/**
* 获取uuid
*/
Expand Down Expand Up @@ -43,3 +47,12 @@ export function treeDataTranslate (data, id = 'id', pid = 'parentId') {
}
return res
}

/**
* 清除登录信息
*/
export function clearLoginInfo () {
Vue.cookie.delete('token')
store.commit('resetStore')
router.options.isAddDynamicMenuRoutes = false
}
3 changes: 2 additions & 1 deletion src/views/main-navbar-update-password.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
</template>

<script>
import { clearLoginInfo } from '@/utils'
export default {
data () {
var validateConfirmPassword = (rule, value, callback) => {
Expand Down Expand Up @@ -93,7 +94,7 @@
this.visible = false
this.$nextTick(() => {
this.mainTabs = []
this.$cookie.delete('token')
clearLoginInfo()
this.$router.replace({ name: 'login' })
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/views/main-navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@

<script>
import UpdatePassword from './main-navbar-update-password'
import { clearLoginInfo } from '@/utils'
export default {
data () {
return {
Expand Down Expand Up @@ -101,8 +102,7 @@
data: this.$http.adornData()
}).then(({data}) => {
if (data && data.code === 0) {
this.$cookie.delete('token')
this.$router.options.isAddDynamicMenuRoutes = false
clearLoginInfo()
this.$router.push({ name: 'login' })
}
})
Expand Down

0 comments on commit 288e539

Please sign in to comment.