request.js 3.7 KB
Newer Older
何秀钢 已提交
1
import axios from 'axios' // 引入axios
2
import { ElMessage, ElMessageBox } from 'element-plus'
Mr.奇淼('s avatar
Mr.奇淼( 已提交
3
import { useUserStore } from '@/pinia/modules/user'
4
import { emitter } from '@/utils/bus.js'
5
import router from '@/router/index'
6

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

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

何秀钢 已提交
58
// http response 拦截器
Mr.奇淼('s avatar
Mr.奇淼( 已提交
59
service.interceptors.response.use(
Mr.奇淼('s avatar
Mr.奇淼( 已提交
60
  response => {
61
    const userStore = useUserStore()
Mr.奇淼('s avatar
Mr.奇淼( 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    closeLoading()
    if (response.headers['new-token']) {
      userStore.setToken(response.headers['new-token'])
    }
    if (response.data.code === 0 || response.headers.success === 'true') {
      if (response.headers.msg) {
        response.data.msg = decodeURI(response.headers.msg)
      }
      return response.data
    } else {
      ElMessage({
        showClose: true,
        message: response.data.msg || decodeURI(response.headers.msg),
        type: 'error'
      })
      if (response.data.data && response.data.data.reload) {
78 79 80
        userStore.token = ''
        localStorage.clear()
        router.push({ name: 'Login', replace: true })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
81 82 83 84 85 86
      }
      return response.data.msg ? response.data : response
    }
  },
  error => {
    closeLoading()
K
king 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100

    if(!error.response){
      ElMessageBox.confirm(`
        <p>检测到请求错误</p>
        <p>${error}</p>
        `, '请求报错', {
          dangerouslyUseHTMLString: true,
          distinguishCancelAndClose: true,
          confirmButtonText: '稍后重试',
          cancelButtonText: '取消'
        })
        return
    }

Mr.奇淼('s avatar
Mr.奇淼( 已提交
101 102 103
    switch (error.response.status) {
      case 500:
        ElMessageBox.confirm(`
K
krank 已提交
104 105 106
        <p>检测到接口错误${error}</p>
        <p>错误码<span style="color:red"> 500 </span>:此类错误内容常见于后台panic,请先查看后台日志,如果影响您正常使用可强制登出清理缓存</p>
        `, '接口报错', {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
107 108 109 110 111 112 113
          dangerouslyUseHTMLString: true,
          distinguishCancelAndClose: true,
          confirmButtonText: '清理缓存',
          cancelButtonText: '取消'
        })
          .then(() => {
            const userStore = useUserStore()
114 115 116
            userStore.token = ''
            localStorage.clear()
            router.push({ name: 'Login', replace: true })
Mr.奇淼('s avatar
Mr.奇淼( 已提交
117 118 119 120
          })
        break
      case 404:
        ElMessageBox.confirm(`
K
krank 已提交
121 122 123
          <p>检测到接口错误${error}</p>
          <p>错误码<span style="color:red"> 404 </span>:此类错误多为接口未注册(或未重启)或者请求路径(方法)与api路径(方法)不符--如果为自动化代码请检查是否存在空格</p>
          `, '接口报错', {
Mr.奇淼('s avatar
Mr.奇淼( 已提交
124 125 126 127 128 129
          dangerouslyUseHTMLString: true,
          distinguishCancelAndClose: true,
          confirmButtonText: '我知道了',
          cancelButtonText: '取消'
        })
        break
130
    }
Mr.奇淼('s avatar
Mr.奇淼( 已提交
131

Mr.奇淼('s avatar
Mr.奇淼( 已提交
132 133 134 135
    return error
  }
)
export default service