summaryrefslogtreecommitdiff
path: root/src/utils/http.js
blob: 95e3ca2aec18420526eb7cb7b707a977d18a7c90 (plain)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import axios from 'axios'
import { storageKey } from '@/utils/constants'
import store from '@/store'
import _ from 'lodash'

const CancelToken = axios.CancelToken

axios.interceptors.request.use(config => {
  const token = localStorage.getItem(storageKey.token)
  // 添加http请求终止方法
  const arr = []
  const cancelToken = new CancelToken(function executor (c) {
    arr.push(c)
    store.commit('setHttpCancel', arr)
  })
  // 登录超时,将参数q的+变为空格
  if (config.params && config.params.q && config.params.q.length > 0) {
    let q = config.params.q
    if (q.indexOf('+') > -1) {
      q = q.replace(/\+/g, ' ')
    }
    config.params.q = q
  }

  config.cancelToken = cancelToken
  if (token) {
    config.headers['Cn-Authorization'] = token // 请求头token
  }
  return config
},
err => Promise.reject(err)
)
const accountErrorCode = [518003, 518004, 518005, 518006, 518007, 518008] // 账号锁定等
const licenceErrorCode = [715001]

// 若get请求的url中带问号,则将url上的参数截取,改为对象形式传参
axios.interceptors.request.use(
  config => {
    if (config.method === 'get') {
      const url = config.url
      const index = url.indexOf('?')
      if (index > -1) {
        const pre = url.substring(0, index)
        const suf = url.substring(index + 1, url.length)
        const paramsArr = suf.split('&')
        const params = {}
        paramsArr.forEach(p => {
          const i = p.indexOf('=')
          if (i > -1) {
            params[p.substring(0, i)] = p.substring(i + 1, p.length)
          } else {
            params[p] = ''
          }
        })
        config = { ...config, url: pre, params: params }
      }
    }
    return config
  }
)
axios.interceptors.response.use(
  response => {
    return response
  },
  error => {
    try {
      if (_.get(error, 'response.config.url') !== '/sys/login') {
        if (error.response && licenceErrorCode.indexOf(error.response.data.code) !== -1) {
          localStorage.removeItem(storageKey.token)
          window.location.href = '/'
        } else if (error.response && accountErrorCode.indexOf(error.response.data.code) !== -1) {
          localStorage.removeItem(storageKey.token)
          window.location.href = '/'
        }
      }
    } catch (e) {
      console.error(e)
    }
    return Promise.reject(error)
  }
)
export function getForDebug (url, params) {
  return new Promise((resolve) => {
    axios.get(url, {
      params: params
    }).then(response => {
      resolve(response)
    }).catch(err => {
      if (err.response) {
        resolve(err.response)
      } else if (err.message) {
        resolve(err.message)
      }
    })
  })
}

export function postForDebug (url, params, headers) {
  return new Promise(resolve => {
    axios.post(url, params, { headers: headers }).then(response => {
      resolve(response, response)
    }).catch(err => {
      if (err.response) {
        resolve(err.response)
      } else if (err.message) {
        resolve(err.message)
      }
    })
  })
}