• axios配置


    import axios, { isCancel } from 'axios'
    import { md5 } from 'vux'
    import util from '@/libs/util'
    import store from '../../store'
    // 创建一个错误
    function errorCreate (e) {
      let err
      if (typeof e === 'string') {
        err = errorCreate(new Error(e))
      } else {
        err = {
          name: e.name,
          message: e.message,
          stack: e.stack,
          isCancel: isCancel(e)
        }
      }
      return err
    }
    
    // 组合conifg
    function formarConfig (config) {
      // 在请求发送之前做一些处理
      const token = util.cookies.get('token')
      const uid = util.cookies.get('uuid')
      const { data, url } = config
      return {
        ...config,
        url: ((token && uid) && `${url}?uid=${uid}&token=${token}`) || url,
        data: {
          data,
          apisign: md5(util.MD5_KEY + JSON.stringify(data))
        }
      }
    }
    
    export const checkLogin = () => {
      const token = util.cookies.get('token')
      if (!token || token === 'undefined') {
        return false
      }
      return true
    }
    
    function createService (url) {
      // 创建一个 axios 实例
      const service = axios.create({
        baseURL: url || process.env.VUE_BASE_URL,
        timeout: 30000 // 请求超时时间,
      })
    
      // 请求拦截器
      service.interceptors.request.use(
        config => {
          if (!config.forbidLoading) {
          
          }
    
          // 在请求发送之前做一些处理
          config = formarConfig(config)
          return config
        },
        error => {
          Promise.reject(error)
        }
      )
    
      // 响应拦截器
      service.interceptors.response.use(
        response => {
          // dataAxios 是 axios 返回数据中的 data
          const dataAxios = response.data
          // 这个状态码是和后端约定的
          const { status } = dataAxios
    
          store.dispatch('cptwebsite/loading/closeLoading')
          // 根据 code 进行判断
          if (status === '1' || status === '1027') {
            return [dataAxios.data, null, response]
          } else {
            if (status === '1010' || status === '1011') {
              
            }
            return [dataAxios.data, errorCreate(`${dataAxios.info}`), dataAxios]
          }
        },
        error => {
          
          if (error && error.response) {
            switch (error.response.status) {
              case 400:
                error.message = '请求错误'
                break
              case 401:
                error.message = '未授权,请登录'
                break
              case 403:
                error.message = '拒绝访问'
                break
              case 404:
                error.message = `请求地址出错: ${error.response.config.url}`
                break
              case 408:
                error.message = '请求超时'
                break
              case 500:
                error.message = '服务器内部错误'
                break
              case 501:
                error.message = '服务未实现'
                break
              case 502:
                error.message = '网关错误'
                break
              case 503:
                error.message = '服务不可用'
                break
              case 504:
                error.message = '网关超时'
                break
              case 505:
                error.message = 'HTTP版本不受支持'
                break
              default:
                break
            }
          }
          Promise.reject(error)
          return Promise.resolve([null, errorCreate(error), error.response])
        }
      )
    
      return service
    }
    export default createService
    

      

  • 相关阅读:
    [React] Cleaning up Functional Components with Custom Hooks
    [React] useCallback + useMemo to avoid re-render
    [React] Replacing Instance Variables with the useRef Hook
    [Cloud Architect] 4. Introduction to Design for Cost, Performance, & Scalability
    [Cloud Architect] 3. Monitor, React, and Recover
    [Cloud Architect] 3. Business Objectives
    1314:【例3.6】过河卒(Noip2002)
    1313:【例3.5】位数问题
    rk3568 linux sdk Buildroot 开发
    AcWing 873. 欧拉函数
  • 原文地址:https://www.cnblogs.com/dhsz/p/11641419.html
Copyright © 2020-2023  润新知