hlsHttp.js 4.43 KB
Newer Older
JingChao's avatar
JingChao committed
1 2
// 引入axios
import axios from 'axios'
李晓兵's avatar
李晓兵 committed
3
import hlsPopup from './hlsPopup'
JingChao's avatar
JingChao committed
4 5 6 7 8
import router from '../router/index'
let promiseArr = {}
let cancel = {}
const CancelToken = axios.CancelToken
// 请求拦截器
李晓兵's avatar
李晓兵 committed
9
axios.interceptors.request.use(config => {
JingChao's avatar
JingChao committed
10 11 12 13
  // 发起请求时,取消掉当前正在进行的相同请求
  config.cancelToken = new CancelToken(c => {
    cancel = c
  })
李晓兵's avatar
李晓兵 committed
14 15 16 17 18 19 20 21 22 23
  if (promiseArr[config.url]) {
    promiseArr[config.url]('操作取消')
    promiseArr[config.url] = cancel
  } else {
    promiseArr[config.url] = cancel
  }
  return config
}, error => {
  return Promise.reject(error)
})
JingChao's avatar
JingChao committed
24
// 响应拦截器即异常处理
李晓兵's avatar
李晓兵 committed
25
axios.interceptors.response.use(response => {
JingChao's avatar
JingChao committed
26 27 28 29 30
  if ($config.debug) {
    let postName = 'post'
    console.log(postName + ' success')
    console.log(postName + ' response ' + JSON.stringify(response.data, '', 2))
    console.log(postName + ' End!')
李晓兵's avatar
李晓兵 committed
31 32
  }
  if (response.data.result === 'E' || response.data.code === 'E') {
JingChao's avatar
JingChao committed
33 34 35 36
    hlsPopup.hideLoading()
    const err = {}
    err.message = response.data.message
    hlsPopup.showError(err.message)
李晓兵's avatar
李晓兵 committed
37
    return Promise.resolve(err)
JingChao's avatar
JingChao committed
38 39 40
  } else if (response.data.code && response.data.code !== 200) {
    hlsPopup.showError(response.data.message)
    return Promise.resolve(response.data)
JingChao's avatar
JingChao committed
41
  } else if (response.data.success && response.data.success === false) {
JingChao's avatar
JingChao committed
42
    hlsPopup.showError(response.data.error.message)
JingChao's avatar
JingChao committed
43
    return Promise.resolve(response.data)
李晓兵's avatar
李晓兵 committed
44
  } else {
JingChao's avatar
JingChao committed
45
    return response.data
李晓兵's avatar
李晓兵 committed
46 47 48
  }
}, err => {
  if (err && err.response) {
李晓兵's avatar
李晓兵 committed
49
    debugger
李晓兵's avatar
李晓兵 committed
50 51 52
    switch (err.response.status) {
      case 400:
        err.message = '错误请求'
JingChao's avatar
JingChao committed
53
        break
李晓兵's avatar
李晓兵 committed
54 55
      case 401:
        err.message = '登录已失效,请重新登录'
JingChao's avatar
JingChao committed
56
        break
李晓兵's avatar
李晓兵 committed
57 58
      case 403:
        err.message = '拒绝访问'
JingChao's avatar
JingChao committed
59
        break
李晓兵's avatar
李晓兵 committed
60 61
      case 404:
        err.message = '请求错误,未找到该资源'
JingChao's avatar
JingChao committed
62
        break
李晓兵's avatar
李晓兵 committed
63 64
      case 405:
        err.message = '不支持的请求类型'
JingChao's avatar
JingChao committed
65
        break
李晓兵's avatar
李晓兵 committed
66 67
      case 408:
        err.message = '请求超时'
JingChao's avatar
JingChao committed
68
        break
李晓兵's avatar
李晓兵 committed
69 70
      case 500:
        err.message = '服务器端出错'
JingChao's avatar
JingChao committed
71
        break
李晓兵's avatar
李晓兵 committed
72 73
      case 501:
        err.message = '网络未实现'
JingChao's avatar
JingChao committed
74
        break
李晓兵's avatar
李晓兵 committed
75 76
      case 502:
        err.message = '网络错误'
JingChao's avatar
JingChao committed
77
        break
李晓兵's avatar
李晓兵 committed
78 79
      case 503:
        err.message = '服务不可用'
JingChao's avatar
JingChao committed
80
        break
李晓兵's avatar
李晓兵 committed
81 82
      case 504:
        err.message = '网络超时'
JingChao's avatar
JingChao committed
83
        break
李晓兵's avatar
李晓兵 committed
84 85
      case 505:
        err.message = 'http版本不支持该请求'
JingChao's avatar
JingChao committed
86
        break
李晓兵's avatar
李晓兵 committed
87 88 89 90
      default:
        err.message = `连接错误${err.response.status}`
    }
  } else {
JingChao's avatar
JingChao committed
91
    err.message = '连接到服务器失败'
李晓兵's avatar
李晓兵 committed
92
  }
JingChao's avatar
JingChao committed
93 94
  if (err.response && err.response.status === 401) {
    hlsPopup.hideLoading()
李晓兵's avatar
李晓兵 committed
95 96 97
    hlsPopup.showPopup({
      title: '登录失效,重新登录',
      onConfirm: () => {
JingChao's avatar
JingChao committed
98 99 100
        router.push({name: 'Login'})
      },
    })
李晓兵's avatar
李晓兵 committed
101
  } else {
JingChao's avatar
JingChao committed
102 103
    hlsPopup.hideLoading()
    hlsPopup.showError(err.message)
李晓兵's avatar
李晓兵 committed
104 105 106
  }
  return Promise.resolve(err)
})
JingChao's avatar
JingChao committed
107 108 109
axios.defaults.baseURL = ''
axios.defaults.timeout = 100000
// get请求
李晓兵's avatar
李晓兵 committed
110 111
export function get (url) {
  let param = {}
JingChao's avatar
JingChao committed
112
  let headers = {}
李晓兵's avatar
李晓兵 committed
113 114 115
  if (window.localStorage.access_token) {
    headers = {
      'Content-Type': 'application/json',
JingChao's avatar
JingChao committed
116
      'Authorization': 'Bearer ' + window.localStorage.access_token,
李晓兵's avatar
李晓兵 committed
117
    }
JingChao's avatar
JingChao committed
118
  } else {
李晓兵's avatar
李晓兵 committed
119
    headers = {
JingChao's avatar
JingChao committed
120
      'Content-Type': 'application/json',
李晓兵's avatar
李晓兵 committed
121 122
    }
  }
JingChao's avatar
JingChao committed
123 124 125 126 127
  if ($config.debug) {
    let postName = 'GET'
    console.log(postName + ' Start!')
    console.log(postName + ' url ' + url)
  }
李晓兵's avatar
李晓兵 committed
128 129 130 131 132 133 134 135 136 137 138 139 140
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url,
      headers: headers,
      params: param,
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}
JingChao's avatar
JingChao committed
141 142
// post请求
export function post (url, param) {
李晓兵's avatar
李晓兵 committed
143
  // param.user_id = window.localStorage.user_id
JingChao's avatar
JingChao committed
144 145
  param.access_token = window.localStorage.access_token
  let headers = {}
李晓兵's avatar
李晓兵 committed
146 147 148
  if (window.localStorage.access_token) {
    headers = {
      'Content-Type': 'application/json',
JingChao's avatar
JingChao committed
149
      'Authorization': 'Bearer ' + window.localStorage.access_token,
李晓兵's avatar
李晓兵 committed
150
    }
JingChao's avatar
JingChao committed
151
  } else {
李晓兵's avatar
李晓兵 committed
152
    headers = {
JingChao's avatar
JingChao committed
153
      'Content-Type': 'application/json',
李晓兵's avatar
李晓兵 committed
154 155
    }
  }
JingChao's avatar
JingChao committed
156
  if ($config.debug) {
李晓兵's avatar
李晓兵 committed
157
    let postName = 'POST'
JingChao's avatar
JingChao committed
158 159 160
    console.log(postName + ' Start!')
    console.log(postName + ' url ' + url)
    console.log(postName + ' parameter ' + JSON.stringify(param, '', 2))
李晓兵's avatar
李晓兵 committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174
  }
  return new Promise((resolve, reject) => {
    axios({
      method: 'post',
      headers: headers,
      url,
      data: param,
    }).then(res => {
      resolve(res)
    }).catch(err => {
      reject(err)
    })
  })
}