• ts封装axios


    import axios from "axios";
    import qs from "qs";

    export interface VictoryResult {
      CODE: string;
      MSG: string;
      DATA?: any;
      // 老接口的数据
      data?: any;
      message?: string;
      result?: number;
      resultCode?: string;
      msg?: string;
    }
    export interface VictoryResultOld {
      data?: any;
      message: string;
      result: number;
    }

    export function isResponseOK(result: VictoryResult) {
      return result && result.CODE === "00";
    }
    export function isResponseOKOld(result: VictoryResult) {
      return result && result.result === 1;
    }

    export function isBusinessError(result: any = {}) {
      return (
        result &&
        (Object.keys(result).some(n => n === "CODE") || // 新接口
          Object.keys(result).some(n => n === "result")) // 老接口
      );
    }

    //请求方式
    export enum RequestMethod {
      post = "post",
      get = "get"
    }

    export enum ContentType {
      JSON = "application/json;charset=UTF-8",
      XWWWFORMURLENCODED = "application/x-www-form-urlencoded"
    }

    /**
     * loading: 是否loading,默认true
     * requestConfig: 请求配置,设置http头之类的
     */
    export type optionsType = {
      loading?: boolean; // default true
      requestConfig?: any;
      displayErrorToast?: boolean; // default true
      requestMethod?: string; // default RequestMethod.post
      handleResponseOk?: Function; // 处理responseOK 默认是responseOK
      "content-type"?: ContentType;
    };

    function handleResponse(
      res: any,
      displayErrorToast: boolean,
      handleResponseOK: Function
    ) {
      const { data } = res;
      if (handleResponseOK(data)) {
        return data as VictoryResult;
      } else {
        if (displayErrorToast) {
          alert(data.MSG || "服务异常");
        }
        throw data as VictoryResult;
      }
    }

    function handleError(error: any, displayErrorToast: boolean) {
      console.warn(error);
      if (displayErrorToast && !isBusinessError(error)) {
        alert("网络异常");
      }
    }

    export default function fetchData(
      urlConfig: string,
      requestData?: Record<string, any>,
      options?: optionsType,
      isMock = false
    ): Promise<VictoryResult> | never {
      const defaultOptions = {
        loading: true,
        requestConfig: { timeout: 20000 },
        displayErrorToast: true
      };
      options = { ...defaultOptions, ...options };
      const displayErrorToast = options.displayErrorToast || false;
      const loading = options.loading;

      const contentType = options["content-type"]
        ? options["content-type"]
        : ContentType.XWWWFORMURLENCODED;
      const realRequestData =
        contentType === ContentType.JSON
          ? requestData
          : qs.stringify(requestData || {});

      // 构造请求 header
      const requestConfig = options.requestConfig || {};
      const defaultHeaders = {
        "content-type": contentType
      };
      const realRequestConfig = {
        ...requestConfig,
        ...{ headers: { ...defaultHeaders, ...requestConfig.headers } }
      };
      const requestMethod = options.requestMethod || RequestMethod.post;
      const handleResponseOK = options.handleResponseOk || isResponseOK;
      if (requestMethod === RequestMethod.get) {
        return axios
          .get(urlConfig, {
            headers: { ...defaultHeaders, ...requestConfig.headers },
            params: requestData,
            timeout: 20000
          })
          .then(res => {
            return handleResponse(res, displayErrorToast, handleResponseOK);
          })
          .catch(error => {
            handleError(error, displayErrorToast);
            throw error;
          });
      } else {
        return axios
          .post(urlConfig, realRequestData, realRequestConfig)
          .then(res => {
            return handleResponse(res, displayErrorToast, handleResponseOK);
          })
          .catch(error => {
            handleError(error, displayErrorToast);
            throw error;
          });
      }
    }
  • 相关阅读:
    switch case 变量初始化问题
    GDB 调试 ---转 比较全的东东
    mount不是很熟悉 转载文章了解下 转自http://forum.ubuntu.org.cn/viewtopic.php?f=120&t=257333
    转 strace
    Mysql 漏洞利用(越权读取文件,实战怎么从低权限拿到root密码)[转]
    echo,die(),print(),print_r(),var_dump()的区别
    iis7.5加fck解析漏洞后台拿shell
    Php发送post请求方法
    分享PHP小马一枚,完美绕过安全狗检测。
    性能测试-Gatling(一)
  • 原文地址:https://www.cnblogs.com/xk-one/p/14155634.html
Copyright © 2020-2023  润新知