Blame view

antis-ncc-admin/src/utils/request.js 3.98 KB
96009bc9   hexiaodong   hxd
1
  import axios from 'axios'
2d733a6a   李宇   最新
2
3
4
  import {
    message
  } from '@/utils/message'
96009bc9   hexiaodong   hxd
5
  import store from '@/store'
2d733a6a   李宇   最新
6
7
8
  import {
    getToken
  } from '@/utils/auth'
96009bc9   hexiaodong   hxd
9
10
11
12
13
14
15
16
  import define from '@/utils/define'
  
  
  // create an axios instance
  const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    withCredentials: false, // send cookies when cross-domain requests
    timeout: define.timeout, // request timeout
36493279   李宇   最新
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
    // 配置参数序列化,使数组参数符合 ASP.NET Core 的格式(key=value1&key=value2)
    paramsSerializer: function(params) {
      const parts = []
      for (const key in params) {
        if (params.hasOwnProperty(key)) {
          const value = params[key]
          if (value === null || value === undefined) {
            continue
          }
          if (Array.isArray(value)) {
            // 数组参数:key=value1&key=value2
            value.forEach(item => {
              if (item !== null && item !== undefined) {
                parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(item))
              }
            })
          } else {
            parts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value))
          }
        }
      }
      return parts.join('&')
    }
96009bc9   hexiaodong   hxd
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
  })
  
  // request interceptor
  service.interceptors.request.use(
    config => {
      if (config.url.indexOf('http') > -1) config.baseURL = ''
      // 部分接口timeout时间单独处理
      if (config.url.indexOf('SynThirdInfo') > -1 || config.url.indexOf('extend/Email/Receive') > -1 ||
        config.url.indexOf('Permission/Authority/Data') > -1 || config.url.indexOf('DataSync/Actions/Execute') > -1) {
        config.timeout = 100000
      }
      // do something before request is sent
      if (store.getters.token) {
        config.headers['Authorization'] = getToken()
      }
1fffa876   “wangming”   优化储扣列表接口:增加门店、时间、...
55
56
57
      // GET 请求时,如果传入了 data 但没有 params,则将其转换为 params
      // 如果已经有 params,则保留 params,不覆盖
      if (config.method == 'get' && config.data && !config.params) {
96009bc9   hexiaodong   hxd
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
        config.params = config.data
      }
      let timestamp = Date.parse(new Date()) / 1000
      if (config.url.indexOf('?') > -1) {
        config.url += `&n=${timestamp}`
      } else {
        config.url += `?n=${timestamp}`
      }
      return config
    },
    error => {
      // do something with request error
      if (process.env.NODE_ENV === 'development') {
        console.log(error) // for debug
      }
      return Promise.reject(error)
    }
  )
  
  // response interceptor
  service.interceptors.response.use(
    response => {
      const res = response.data
      let config = response.config
      let url = config.url
      // 特殊接口处理
2d733a6a   李宇   最新
84
85
      if (url.indexOf('/Base/DataSource/Actions/Test') > -1 || (url.indexOf('Model') > -1 && url.indexOf('Config') > -
        1) || url.indexOf('Bot/send-text') > -1) return res
25c153f3   “wangming”   Update database c...
86
87
88
89
      // 文件流下载:直接返回 Blob,避免按 JSON 的 code 判断
      if (config.responseType === 'blob' && res instanceof Blob) {
        return res
      }
96009bc9   hexiaodong   hxd
90
91
92
93
94
95
      if (res.code !== 200) {
        message({
          message: res.msg || '请求出错,请重试',
          type: 'error',
          duration: 1500,
          onClose: () => {
2d733a6a   李宇   最新
96
97
            if (url.indexOf('/api/oauth/Login') < 0 && url.indexOf('/api/oauth/LockScreen') < 0 && (res.code ===
                600 || res.code === 601 || res.code === 602)) {
96009bc9   hexiaodong   hxd
98
99
100
              // 600:登录过期,请重新登录  601: 您的帐号在其他地方已登录,被强制踢出 602: Token验证失败
              store.dispatch('user/resetToken').then(() => {
                if (window.location.pathname.indexOf('login') > -1) return
2d733a6a   李宇   最新
101
102
103
                setTimeout(() => {
                  location.reload()
                }, 100);
96009bc9   hexiaodong   hxd
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
              })
            }
          }
        })
        return Promise.reject(new Error(res.msg || 'Error'))
      } else {
        return res
      }
    },
    error => {
      if (process.env.NODE_ENV === 'development') {
        console.log(error) // for debug
      }
      message({
        message: '请求出错,请重试',
        type: 'error',
        duration: 1500
      })
      return Promise.reject(error)
    }
  )
  
2d733a6a   李宇   最新
126
  export default service