派生自 wuyushui/SewerAndRainNetwork

陈泽平
2021-05-30 0110b78419d98e114c59fa9fb6f69663fbdc3c98
src/utils/axios.js
New file
@@ -0,0 +1,216 @@
import Vue from 'vue'
import axios from 'axios'
import * as $CONST from '@/utils/constant'
import * as $T from '@/utils/tools'
// import $store from '@/store'
/**
 * 定义传入参数数据格式
 *
 * === 删除数据 ===
 * {
 *    key:['dataId1','dataId2']
 * }
 *
 */
/**
 * 定义返回的json数据格式
 *
 * === 分页数据 ===
 * {
 *    code:200,            --- 目前框架认为0和200都是成功状态
 *    message:'按业务自定义',
 *    data:{
 *      records:[{...},{...}],   --- 可按自己业务替换属性
 *      total:xx,          --- 可按自己业务替换属性
 *      size:xx,           --- 可按自己业务替换属性
 *      current:xx,        --- 可按自己业务替换属性
 *      pages:xx           --- 可按自己业务替换属性
 *    },
 * }
 *
 * === 列表数据 ===
 *
 * {
 *    code:200,
 *    message:'按业务自定义',
 *    data:[{..},{..}...]
 * }
 *
 * === 一条数据 ===
 *
 * {
 *    code:200,
 *    message:'按业务自定义',
 *    data:{...}
 * }
 *
 * === 无返回数据 ===
 *
 * {
 *    code:200,
 *    message:'按业务自定义',
 *    data: null
 * }
 *
 * === 树形结构 ===
 *
 * {
 *    code:200,
 *    message:'按业务自定义'
 *    data:[{
 *      prop:xx,
 *      ...
 *      children: [
 *         prop:xx,
 *         ...
 *         children:[...]
 *      ]
 *    },
 *    {
 *      prop:xx,
 *      ...
 *      children: [
 *         prop:xx,
 *         ...
 *         children:[...]
 *      ]
 *    }]
 * }
 *
 */
// 创建axios实例
const Service = axios.create({
  timeout: 1000 * 30
})
const CancelToken = axios.CancelToken
/*
 function showDebugInfo(config) {
 console.group('%cMethod::' + config.method + '::Url::' + config.url, 'color:red;font-size:12px;')
 console.log('%c保存/更新/查看场合,传入的参数::', 'font-size:12px;')
 console.log(config.hasOwnProperty('params') ? JSON.stringify(config.params) : JSON.stringify(config.data))
 console.groupEnd()
 }
 function filterOauthData(config) {
 let url = config.url
 if (config.method === 'post' && $t.isExistOauthApi(url)) {
 // post请求的场合下,追加角色名称
 }
 if (config.method === 'get' && $t.isExistOauthApi(url)) {
 // get请求的场合下,追加角色名称
 if (config.hasOwnProperty('params')) {
 config.params['userCode'] = ''
 } else {
 config['params'] = {}
 config.params['userCode'] = ''
 }
 }
 return config
 } */
// request拦截器
Service.interceptors.request.use(
  config => {
    // 临时追加
    //   if ($store.state.user.account) {
    //     config.headers.account = $store.state.user.account
    //   }
    const copyConfig = Object.assign({}, config)
    // copyConfig = filterOauthData(copyConfig)
    // showDebugInfo(copyConfig)
    copyConfig.cancelToken = new CancelToken((c) => {
      // 页面失效场景,取消padding中请求
      Vue.prototype.$cancels.push(c)
      // todo 快速双击取消重复请求
      // 重复请求场景,取消掉上一个未padding中请求
    })
    return copyConfig
  },
  error => {
    Promise.reject(error)
  })
// respone拦截器
Service.interceptors.response.use(
  response => {
    const res = response.data
    /* if (Number(res.code) !== 200 && Number(res.code) !== 0) {
      $T.warning(res.message)
      return Promise.reject(res.message)
    } else {
      return res
    } */
    return res
  },
  error => {
    if (error.message && error.message.includes('timeout')) {
      $T.fail($CONST.MSG_SYS_TIME_OUT)
      return Promise.reject(error)
    }
    if (!error.response) {
      $T.fail($CONST.MSG_SYS_ERR)
      return Promise.reject(error)
    }
    switch (error.response.status) {
      // http status handler
      case 404:
        $T.fail($CONST.MSG_SYS_404)
        break
      case 500:
        $T.fail($CONST.MSG_SYS_ERR)
        break
      case 503:
        $T.fail($CONST.MSG_SYS_503)
        break
    }
    return Promise.reject(error)
  }
)
const $http = {}
$http.$service = Service
$http.$axios = axios
$http.get = function (url, params = {}) {
  if ($T.isInlineParams(url)) {
    // 严格遵循restful标准的url的场景
    url = $T.replaceUrlParams(url, params)
    params = {}
  }
  return Service({
    url: url,
    method: 'get',
    params: $T.filterParams(params)
  })
}
$http.post = function (url, data = {}, setHeaders) {
  return Service({
    url: url,
    method: 'post',
    data: data,
    headers: setHeaders || { 'Content-Type': 'application/json; charset=UTF-8' }
  })
}
$http.put = function (url, data = {}) {
  return Service({
    url: url,
    method: 'put',
    data: data
  })
}
$http.delete = function (url, data = {}) {
  return Service({
    url: url,
    method: 'delete',
    data: data
  })
}
export default $http