jinlin
2024-02-01 2df883fcbed176f83d8d144fd007e7f72fcb54d6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import Vue from 'vue'
import axios from 'axios'
import Cookies from 'js-cookie'
import qs from 'qs'
import {clearLoginInfo} from '../utils'
import tip from '../utils/tips'
import isPlainObject from 'lodash/isPlainObject'
 
const http = axios.create({
  baseURL: window.SITE_CONFIG['apiURL'],
  timeout: 1000 * 180,
  withCredentials: true
})
 
/**
 * 请求拦截
 */
http.interceptors.request.use(config => {
  config.headers['Accept-Language'] = Cookies.get('language') || 'zh-CN'
  config.headers['token'] = 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 => {
  //console.log(error,'2222')
  return Promise.reject(error)
})
 
/**
 * 响应拦截
 */
http.interceptors.response.use(response => {
  Vue.prototype.$EventBus.$emit('singleSignVerify')
  if ((response.data.code === 401 || response.data.code === 10001)) {
    //clearLoginInfo()
    Vue.prototype.$EventBus.$emit('reLogin')
    return Promise.reject(response.data.msg)
  }
  let data = response.data
  data.success = data.code === 0
  if (!data.success) {
    if (data.msg) {
      console.log(data,'后台异常,没有提示信息')
      tip.error(data.msg)
    }
  }
  return data
}, error => {
  //console.log(error,'333')
  return Promise.reject(error)
})
 
export default http