HttpService.ts 4.71 KB
import {AxiosInstance, AxiosRequestConfig} from 'axios';
import qs from 'qs';

/**
 * @title   http请求封装
 * @desc
 */
export class HttpService {

    constructor(public axios: AxiosInstance) {
    }

    /**
     * get请求, json形式返回
     * @param url    接口地址
     * @param paramMap  参数
     */
    public get(url: string, paramMap?: Record<string, unknown>): Promise<unknown> {
        const request: AxiosRequestConfig = {
            params: paramMap,
        };
        return this.axios.get(url, request);
    }

    /**
     * post请求
     * @param url    接口地址
     * @param body  参数
     */
    public post(url: string, body: string | Record<string, unknown>): Promise<unknown> {
        const requestConfig = {
            withCredentials: true,
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
            },
        };
        return this.axios.post(url, body, requestConfig);
    }

    /**
     * 以表单方式提交
     * @ param {string} url
     * @ param body
     * @ returns {Promise<any>}
     */
    public postForm(url: string, body: string | Record<string, unknown>): Promise<unknown> {
        const options = {
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
            },
        };
        return this.axios.post(url, qs.stringify(body), options);
    }

    /**
     * 以参数和json一起提交数据
     * @param url
     * @param body
     * @param paramMap
     */
    public postParams(url: string, body: string | Record<string, unknown> | null, paramMap: string | Record<string, unknown>): Promise<any> {
        const options = {
            params: paramMap,
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
            },
        };
        return this.axios.post(url, body, options);
    }

    /**
     * put方式提交
     * @param url
     * @param body
     */
    public put(url: string, body: Record<string, unknown> | string): Promise<any> {
        const options = {};
        return this.axios.put(url, body, options);
    }

    /**
     * delete方式提交
     * @param url
     * @param paramMap
     */
    public delete(url: string, paramMap?: Record<string, unknown> | string): Promise<any> {
        const options = {
            params: paramMap,
        };
        return this.axios.delete(url, options);
    }

    /**
     * patch方式提交
     * @param url
     * @param body
     */
    public patch(url: string, body: Record<string, unknown> | string): Promise<any> {
        const options = {};
        return this.axios.patch(url, body, options);
    }

    /**
     * post请求下载文件
     * @param url
     * @param body
     * @param fileName
     * @param contentType
     */
    public postDownload(url: string, body = null, fileName: string, contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
        this.axios.post(url, body, {
            responseType: 'blob',
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
            },
        }).then(data => {
            HttpService.downloadFile(data, fileName, contentType);
        });
    }

    /**
     * get方式请求下载文件
     * @param url
     * @param paramMap
     * @param fileName
     * @param contentType
     */
    public getDownload(url: string, paramMap = null, fileName: string, contentType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') {
        this.axios.get(url, {
            responseType: 'blob',
            headers: {
                'Content-Type': 'application/json; charset=UTF-8',
            },
            params: paramMap,
        }).then(data => {
            HttpService.downloadFile(data, fileName, contentType);
        });
    }

    /**
     * 将文件下载下来
     * @param data
     * @param fileName
     * @param contentType
     */
    private static downloadFile(data: any, fileName: string, contentType: string) {
        const blob = new Blob([data], {type: contentType});
        const url = window.URL.createObjectURL(blob);
        // 以动态创建a标签进行下载
        const a = document.createElement('a');
        a.href = url;
        a.download = fileName;
        a.click();
        window.URL.revokeObjectURL(url);
    }

    /**
     * 以formData方式提交
     * @ param {string} url
     * @ param body
     * @ returns {Promise<any>}
     */
    public postFormData(url: string, formData: any): Promise<any> {
        const options = {
            headers: {
                'Content-Type': 'multipart/form-data; charset=UTF-8',
            },
        };
        return this.axios.post(url, formData, options);
    }
}