50d4471c by yangwei

token修改

1 parent 640a4a57
1 /* 1 /*
2 * @Description: 此文件主要创建 axios 实例,然后添加请求拦截器和响应拦截器 2 * @Description: 此文件主要创建 axios 实例,然后添加请求拦截器和响应拦截器
3 * @Autor: renchao 3 * @Autor: renchao
4 * @LastEditTime: 2023-06-08 09:02:53 4 * @LastEditTime: 2023-06-26 09:24:00
5 */ 5 */
6 import Vue from 'vue' 6 import Vue from 'vue'
7 import axios from 'axios' 7 import axios from 'axios'
8 import Router from '@/router' 8 import Router from '@/router'
9 import Cookies from 'js-cookie'
10 import { Message } from 'element-ui' 9 import { Message } from 'element-ui'
11 import { endLoadingSubCount } from './requestLoading' 10 import { endLoadingSubCount } from './requestLoading'
11 import {getToken, setToken} from "@/utils/util";
12 12
13 // create an axios instance 13 // create an axios instance
14 const service = axios.create({ 14 const service = axios.create({
...@@ -27,15 +27,7 @@ const service = axios.create({ ...@@ -27,15 +27,7 @@ const service = axios.create({
27 service.interceptors.request.use( 27 service.interceptors.request.use(
28 config => { 28 config => {
29 if (process.env.NODE_ENV === 'development') { 29 if (process.env.NODE_ENV === 'development') {
30 const token = localStorage.getItem('token') 30 const token = getToken()
31 // 添加请求头
32 if (token) {
33 config.headers['Authorization'] = 'Bearer ' + token
34 } else {
35 config.headers.delete('Authorization')
36 }
37 } else {
38 const token = Cookies.get('token')
39 // 添加请求头 31 // 添加请求头
40 if (token) { 32 if (token) {
41 config.headers['Authorization'] = 'Bearer ' + token 33 config.headers['Authorization'] = 'Bearer ' + token
...@@ -75,12 +67,8 @@ service.interceptors.response.use( ...@@ -75,12 +67,8 @@ service.interceptors.response.use(
75 window.__isNeedLogin = false 67 window.__isNeedLogin = false
76 Message.error('token失效,请重新登录'); 68 Message.error('token失效,请重新登录');
77 let locationUrl = window.location.protocol + '//' + window.location.host + window.location.pathname; 69 let locationUrl = window.location.protocol + '//' + window.location.host + window.location.pathname;
78 if (process.env.NODE_ENV === 'development') { 70 setToken(undefined)
79 localStorage.removeItem('token') 71 sessionStorage.removeItem('token')
80 } else {
81 Cookies.remove('token')
82 }
83
84 if (window._config.casEnable) { 72 if (window._config.casEnable) {
85 window.location.href = window._config.casBaseURL + '/logout?service=' + encodeURIComponent(locationUrl); 73 window.location.href = window._config.casBaseURL + '/logout?service=' + encodeURIComponent(locationUrl);
86 } else { 74 } else {
......