1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import axios from 'axios'
- import Cookies from 'js-cookie'
- import router from '@/router'
- import qs from 'qs'
- import { clearLoginInfo } from '@/utils'
- import isPlainObject from 'lodash/isPlainObject'
- import {MessageBox} from "element-ui";
- const http = axios.create({
- baseURL: window.SITE_CONFIG['apiURL'],
- timeout: 1000 * 180,
- withCredentials: true
- })
- /**
- * 请求拦截
- */
- http.interceptors.request.use(config => {
- // 临时免登陆
- // Cookies.set('token', '49e42af404cbb94096920626f01fb6f6')
- config.headers['Accept-Language'] = Cookies.get('language') || 'zh-CN'
- config.headers['sessionToken'] = Cookies.get('token') || ''
- // 默认参数
- var defaults = {}
- // 防止缓存,GET请求默认带_t参数
- if (config.method === 'get') {
- config.params = {
- ...config.params,
- ...{ '_t': new Date().getTime() }
- }
- }
- if (isPlainObject(config.params)) {
- config.params = {
- ...defaults,
- ...config.params
- }
- }
- if (isPlainObject(config.data)) {
- config.data = {
- ...defaults,
- ...config.data
- }
- if (/^application\/x-www-form-urlencoded/.test(config.headers['content-type'])) {
- config.data = qs.stringify(config.data)
- }
- }
- return config
- }, error => {
- return Promise.reject(error)
- })
- /**
- * 响应拦截
- */
- http.interceptors.response.use(response => {
- if (response.data.code === 401 || response.data.code === 402 || response.data.code === 403 || response.data.code === 10001) {
- if(localStorage.getItem('pathCopy')) {
- localStorage.setItem('pathCopyshow',true)
- }
- clearLoginInfo()
- MessageBox.confirm(response.data.msg, '提示',{
- showClose: false,
- showCancelButton: false,
- callback: () => {
- router.replace({ name: 'login' })
- }
- })
- return Promise.reject(response.data.msg)
- }
- return response
- }, error => {
- console.error(error)
- return Promise.reject(error)
- })
- export default http
|