request.js 2.5 KB
Newer Older
何秀钢 已提交
1 2 3 4
import axios from 'axios' // 引入axios
import { Message } from 'element-ui'
import { store } from '@/store'
import context from '@/main'
5
import { MessageBox } from 'element-ui'
6

Mr.奇淼('s avatar
Mr.奇淼( 已提交
7
const service = axios.create({
何秀钢 已提交
8 9
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 99999
Mr.奇淼('s avatar
Mr.奇淼( 已提交
10
})
Mr.奇淼('s avatar
Mr.奇淼( 已提交
11 12 13
let acitveAxios = 0
let timer
const showLoading = () => {
何秀钢 已提交
14 15 16 17 18 19 20
  acitveAxios++
  if (timer) {
    clearTimeout(timer)
  }
  timer = setTimeout(() => {
    if (acitveAxios > 0) {
      context.$bus.emit('showLoading')
Mr.奇淼('s avatar
Mr.奇淼( 已提交
21
    }
何秀钢 已提交
22
  }, 400)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
23
}
Mr.奇淼('s avatar
Mr.奇淼( 已提交
24

Mr.奇淼('s avatar
Mr.奇淼( 已提交
25
const closeLoading = () => {
何秀钢 已提交
26 27 28 29 30 31 32
  acitveAxios--
  if (acitveAxios <= 0) {
    clearTimeout(timer)
    context.$bus.emit('closeLoading')
  }
}
// http request 拦截器
Mr.奇淼('s avatar
Mr.奇淼( 已提交
33
service.interceptors.request.use(
何秀钢 已提交
34 35 36
  config => {
    if (!config.donNotShowLoading) {
      showLoading()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
37
    }
何秀钢 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
    const token = store.getters['user/token']
    const user = store.getters['user/userInfo']
    config.data = JSON.stringify(config.data)
    config.headers = {
      'Content-Type': 'application/json',
      'x-token': token,
      'x-user-id': user.ID
    }
    return config
  },
  error => {
    closeLoading()
    Message({
      showClose: true,
      message: error,
      type: 'error'
    })
    return error
  }
)
Mr.奇淼('s avatar
Mr.奇淼( 已提交
58

何秀钢 已提交
59
// http response 拦截器
Mr.奇淼('s avatar
Mr.奇淼( 已提交
60
service.interceptors.response.use(
何秀钢 已提交
61 62
  response => {
    closeLoading()
63

何秀钢 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    if (response.headers['new-token']) {
      store.commit('user/setToken', response.headers['new-token'])
    }
    if (response.data.code === 0 || response.headers.success === 'true') {
      return response.data
    } else {
      Message({
        showClose: true,
        message: response.data.msg || decodeURI(response.headers.msg),
        type: response.headers.msgtype || 'error'
      })
      if (response.data.data && response.data.data.reload) {
        store.commit('user/LoginOut')
      }
      return response.data.msg ? response.data : response
Mr.奇淼('s avatar
Mr.奇淼( 已提交
79
    }
何秀钢 已提交
80 81 82
  },
  error => {
    closeLoading()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
83 84 85 86 87 88
    MessageBox.confirm(`
    <p>检测到接口错误${error}</p>
    <p>错误码500:此类错误内容常见于后台panic,如果影响您正常使用可强制登出清理缓存</p>
    <p>错误码404:此类错误多为接口未注册(或未重启)或者请求路径(方法)与api路径(方法)不符</p>
    `, '接口报错', {
      dangerouslyUseHTMLString: true,
89 90 91
      distinguishCancelAndClose: true,
      confirmButtonText: '清理缓存',
      cancelButtonText: '取消'
何秀钢 已提交
92
    })
93 94 95
      .then(() => {
        store.commit('user/LoginOut')
      })
何秀钢 已提交
96 97
    return error
  }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
98 99
)

何秀钢 已提交
100
export default service