瀏覽代碼

提供重置登录信息状态方法。修复登出或替换账号还保留之前账号操作信息和痕迹bug

daxiongYang 6 年之前
父節點
當前提交
288e539c81
共有 7 個文件被更改,包括 34 次插入5 次删除
  1. 4 0
      src/main.js
  2. 2 0
      src/router/index.js
  3. 9 0
      src/store/index.js
  4. 2 2
      src/utils/httpRequest.js
  5. 13 0
      src/utils/index.js
  6. 2 1
      src/views/main-navbar-update-password.vue
  7. 2 2
      src/views/main-navbar.vue

+ 4 - 0
src/main.js

9
import '@/assets/scss/index.scss'
9
import '@/assets/scss/index.scss'
10
import httpRequest from '@/utils/httpRequest' // api: https://github.com/axios/axios
10
import httpRequest from '@/utils/httpRequest' // api: https://github.com/axios/axios
11
import { isAuth } from '@/utils'
11
import { isAuth } from '@/utils'
12
import cloneDeep from 'lodash/cloneDeep'
12
13
13
Vue.use(VueCookie)
14
Vue.use(VueCookie)
14
Vue.config.productionTip = false
15
Vue.config.productionTip = false
22
Vue.prototype.$http = httpRequest // ajax请求方法
23
Vue.prototype.$http = httpRequest // ajax请求方法
23
Vue.prototype.isAuth = isAuth     // 权限方法
24
Vue.prototype.isAuth = isAuth     // 权限方法
24
25
26
// 保存整站vuex本地储存初始状态
27
window.SITE_CONFIG['storeState'] = cloneDeep(store.state)
28
25
/* eslint-disable no-new */
29
/* eslint-disable no-new */
26
new Vue({
30
new Vue({
27
  el: '#app',
31
  el: '#app',

+ 2 - 0
src/router/index.js

8
import Router from 'vue-router'
8
import Router from 'vue-router'
9
import http from '@/utils/httpRequest'
9
import http from '@/utils/httpRequest'
10
import { isURL } from '@/utils/validate'
10
import { isURL } from '@/utils/validate'
11
import { clearLoginInfo } from '@/utils'
11
12
12
Vue.use(Router)
13
Vue.use(Router)
13
14
40
  beforeEnter (to, from, next) {
41
  beforeEnter (to, from, next) {
41
    let token = Vue.cookie.get('token')
42
    let token = Vue.cookie.get('token')
42
    if (!token || !/\S/.test(token)) {
43
    if (!token || !/\S/.test(token)) {
44
      clearLoginInfo()
43
      next({ name: 'login' })
45
      next({ name: 'login' })
44
    }
46
    }
45
    next()
47
    next()

+ 9 - 0
src/store/index.js

1
import Vue from 'vue'
1
import Vue from 'vue'
2
import Vuex from 'vuex'
2
import Vuex from 'vuex'
3
import cloneDeep from 'lodash/cloneDeep'
3
import common from './modules/common'
4
import common from './modules/common'
4
import user from './modules/user'
5
import user from './modules/user'
5
6
10
    common,
11
    common,
11
    user
12
    user
12
  },
13
  },
14
  mutations: {
15
    // 重置vuex本地储存状态
16
    resetStore (state) {
17
      Object.keys(state).forEach((key) => {
18
        state[key] = cloneDeep(window.SITE_CONFIG['storeState'][key])
19
      })
20
    }
21
  },
13
  strict: process.env.NODE_ENV !== 'production'
22
  strict: process.env.NODE_ENV !== 'production'
14
})
23
})

+ 2 - 2
src/utils/httpRequest.js

3
import router from '@/router'
3
import router from '@/router'
4
import qs from 'qs'
4
import qs from 'qs'
5
import merge from 'lodash/merge'
5
import merge from 'lodash/merge'
6
import { clearLoginInfo } from '@/utils'
6
7
7
const http = axios.create({
8
const http = axios.create({
8
  timeout: 1000 * 30,
9
  timeout: 1000 * 30,
27
 */
28
 */
28
http.interceptors.response.use(response => {
29
http.interceptors.response.use(response => {
29
  if (response.data && response.data.code === 401) { // 401, token失效
30
  if (response.data && response.data.code === 401) { // 401, token失效
30
    Vue.cookie.delete('token')
31
    router.options.isAddDynamicMenuRoutes = false
31
    clearLoginInfo()
32
    router.push({ name: 'login' })
32
    router.push({ name: 'login' })
33
  }
33
  }
34
  return response
34
  return response

+ 13 - 0
src/utils/index.js

1
import Vue from 'vue'
2
import router from '@/router'
3
import store from '@/store'
4
1
/**
5
/**
2
 * 获取uuid
6
 * 获取uuid
3
 */
7
 */
43
  }
47
  }
44
  return res
48
  return res
45
}
49
}
50
51
/**
52
 * 清除登录信息
53
 */
54
export function clearLoginInfo () {
55
  Vue.cookie.delete('token')
56
  store.commit('resetStore')
57
  router.options.isAddDynamicMenuRoutes = false
58
}

+ 2 - 1
src/views/main-navbar-update-password.vue

25
</template>
25
</template>
26
26
27
<script>
27
<script>
28
  import { clearLoginInfo } from '@/utils'
28
  export default {
29
  export default {
29
    data () {
30
    data () {
30
      var validateConfirmPassword = (rule, value, callback) => {
31
      var validateConfirmPassword = (rule, value, callback) => {
93
                    this.visible = false
94
                    this.visible = false
94
                    this.$nextTick(() => {
95
                    this.$nextTick(() => {
95
                      this.mainTabs = []
96
                      this.mainTabs = []
96
                      this.$cookie.delete('token')
97
                      clearLoginInfo()
97
                      this.$router.replace({ name: 'login' })
98
                      this.$router.replace({ name: 'login' })
98
                    })
99
                    })
99
                  }
100
                  }

+ 2 - 2
src/views/main-navbar.vue

55
55
56
<script>
56
<script>
57
  import UpdatePassword from './main-navbar-update-password'
57
  import UpdatePassword from './main-navbar-update-password'
58
  import { clearLoginInfo } from '@/utils'
58
  export default {
59
  export default {
59
    data () {
60
    data () {
60
      return {
61
      return {
101
            data: this.$http.adornData()
102
            data: this.$http.adornData()
102
          }).then(({data}) => {
103
          }).then(({data}) => {
103
            if (data && data.code === 0) {
104
            if (data && data.code === 0) {
104
              this.$cookie.delete('token')
105
              this.$router.options.isAddDynamicMenuRoutes = false
105
              clearLoginInfo()
106
              this.$router.push({ name: 'login' })
106
              this.$router.push({ name: 'login' })
107
            }
107
            }
108
          })
108
          })