Browse Source

修改退出登录 登录方式和tocken 集中管理

1
mo 3 years ago
parent
commit
7192d8f862
4 changed files with 16 additions and 10 deletions
  1. 1 1
      src/api/login.js
  2. 4 2
      src/permission.js
  3. 1 0
      src/store/modules/user.js
  4. 10 7
      src/utils/request.js

+ 1 - 1
src/api/login.js

@@ -20,7 +20,7 @@ export function getInfo() {
 export function logout() {
   return request({
     url: '/api-auth/exit',
-    method: 'post',
+    method: 'get',
     requestType: 'form'
   })
 }

+ 4 - 2
src/permission.js

@@ -8,7 +8,8 @@ import { getToken } from '@/utils/auth' // 验权
 const whiteList = ['/login'] // 不重定向白名单
 router.beforeEach((to, from, next) => {
   NProgress.start()
-  if (getToken()) {
+  let tocken = getToken()
+  if (tocken) {
     if (to.path === '/login') {
       next({ path: '/' })
       NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
@@ -23,7 +24,8 @@ router.beforeEach((to, from, next) => {
             next({ ...to, replace: true })
           })
         }).catch((err) => {
-          return
+          console.log(err)
+
           store.dispatch('FedLogOut').then(() => {
             Message.error(err || 'Verification failed, please login again')
             next({ path: '/' })

+ 1 - 0
src/store/modules/user.js

@@ -68,6 +68,7 @@ const user = {
     LogOut({ commit, state }) {
       return new Promise((resolve, reject) => {
         logout(state.token).then(() => {
+
           commit('SET_TOKEN', '')
           commit('SET_ROLES', [])
           removeToken()

+ 10 - 7
src/utils/request.js

@@ -13,8 +13,9 @@ const service = axios.create({
 
 // request拦截器
 service.interceptors.request.use(config => {
-  if (store.getters.token) {
-    config.headers['Authorization'] = getToken() // 让每个请求携带自定义token 请根据实际情况自行修改
+  let tocken =getToken()
+  if (tocken) {
+    config.headers['Authorization'] = tocken // 让每个请求携带自定义token 请根据实际情况自行修改
   }
   if(config.url.indexOf('/api-mall-admin')== -1){
     if(config.url.indexOf('/api-admin')== -1 && config.url.indexOf('/api-auth')== -1){
@@ -60,17 +61,19 @@ service.interceptors.response.use(
 
       // 401:未登录;
       if (res.code === 401 || res.code === 403) {
+        console.log('401/403')
         location = window.location.origin;
+
         // MessageBox.confirm('登录过期请重新登录', '确定登出', {
         //   confirmButtonText: '重新登录',
         //   cancelButtonText: '取消',
         //   type: 'warning'
         // }).then(() => {
-        //   // store.dispatch('FedLogOut').then(() => {
-        //   //   // location.reload()// 为了重新实例化vue-router对象 避免bug
-        //   //   location.href = window.location.hostname;
-        //   // })
-          location.replace(location.origin);
+        //   store.dispatch('FedLogOut').then(() => {
+        //      location.reload()// 为了重新实例化vue-router对象 避免bug
+        //     location.href = window.location.hostname;
+        //   })
+
         // })
       }
       return Promise.reject('error')