• axios 封装


    axios.ts

    // 引入网络请求库 https://github.com/axios/axios
    
    import axios from 'axios'
    import { message } from 'choerodon-ui';
    // import store from '../store'
    // import router from '../router'
    
    // 请求列表
    const requestList: any = []
    // 取消列表
    const CancelToken = axios.CancelToken
    let sources: any = {}
    
    // axios.defaults.timeout = 10000
    axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8'
    
    axios.defaults.baseURL = process.env.BASE_URL || 'http://localhost:7001/'
    
    axios.interceptors.request.use((config) => {
      const request = JSON.stringify(config.url) + JSON.stringify(config.data)
    
      config.cancelToken = new CancelToken((cancel) => {
        sources[request] = cancel
      })
    
      if(requestList.includes(request)){
        sources[request]('取消重复请求')
      }else{
        requestList.push(request)
        // store.dispatch('changeGlobalState', {loading: true})
      }
    
      // const token = store.getters.userInfo.token
      // if (token) {
      //   config.headers.token = token
      // }
    
      return config
    }, function (error) {
      return Promise.reject(error)
    })
    
    axios.interceptors.response.use(function (response) {
      const request = JSON.stringify(response.config.url) + JSON.stringify(response.config.data)
      requestList.splice(requestList.findIndex((item: any) => item === request), 1)
      if (requestList.length === 0) {
        // store.dispatch('changeGlobalState', {loading: false})
      }
      if (response.data.code === 900401) {
        message.error('认证失效,请重新登录!', 1000)
        // router.push('/login')
      }
      return response
    }, function (error) {
      if (axios.isCancel(error)) {
        requestList.length = 0
        // store.dispatch('changeGlobalState', {loading: false})
        throw new axios.Cancel('cancel request')
      } else {
        message.error('网络请求失败', 1000)
      }
      return Promise.reject(error)
    })
    
    const request = function (url: any, params: any, config: any, method: string) {
      return new Promise((resolve, reject) => {
        // @ts-ignore
        axios[method](url, params, Object.assign({}, config)).then((response: any) => {
          resolve(response.data)
        }, (err: any) => {
          if (err.Cancel) {
            console.log(err)
          } else {
            reject(err)
          }
        }).catch((err: any) => {
          reject(err)
        })
      })
    }
    
    const post = (url: any, params: any, config = {}) => {
      return request(url, params, config, 'post')
    }
    
    const get = (url: any, params: any, config = {}) => {
      return request(url, params, config, 'get')
    }
    
    export {sources, post, get}
    

    .

  • 相关阅读:
    ActiveReports 报表应用教程 (3)---图表报表
    ActiveReports 报表应用教程 (4)---分栏报表
    ActiveReports 报表应用教程 (5)---解密电子商务领域首张电子发票的诞生(套打报表)
    hdu4467 Graph
    ActiveReports 报表应用教程 (6)---分组报表
    [leetcode]Search a 2D Matrix
    参加百度开放云编程马拉松后一点总结
    CAS服务器配置
    Tomcat 的 SSL 配置
    在windows server 2008 R2 64bit上面配置PI OPC Server的DCOM
  • 原文地址:https://www.cnblogs.com/crazycode2/p/16449127.html
Copyright © 2020-2023  润新知