/**
 * 一切http辅助类的基类，用于调用axios
 * nh 2021.12.23
 */

import { axiosUtils } from "./axiosUtils";
import { toolUtils } from "@/utils/toolUtils";

export class httpUtils extends axiosUtils {
  protected constructor(baseUrl: string) {
    super();
    this.baseUrl = baseUrl;
  }

  //根域名地址
  private _baseUrl = "";
  private set baseUrl(val: string) {
    this._baseUrl = val;
  }
  private get baseUrl() {
    return this._baseUrl;
  }

  private async send(url: string, data: any, superFnName: string) {
    var newUrl = toolUtils.getBaseHttpUrl(this.baseUrl, url);
    return await super[superFnName](newUrl, data);
  }

  /**
   * get请求
   * @param url
   * @param data
   * @returns
   */
  public async getRequest(url: string, data: any) {
    return await this.send(url, data, "getRequest");
  }

  /**
   * post请求
   * @param url
   * @param data
   * @returns
   */
  public async postRequest(url: string, data: any) {
    return await this.send(url, data, "postRequest");
  }

  /**
   * 自定义请求
   * @param url
   * @param headers
   * @param data
   * @param method get | post  默认post
   * @returns
   */
  protected async customRequest(
    url: string,
    headers: any,
    data: any,
    method: string
  ) {
    return await super.customRequest(url, headers, data, method);
  }

  /**
   * 上传文件
   * @param _paramobj 格式如下：
   * {
   *    uploadProgressCall 上传进度回调函数，函数参数为object，格式如下:{uploadedTotal:1},uploadedTotal总共上传了多少字节,是所有文件上传的字节数总和
   *    oneUploadedCall 每上传完一个后的回调函数，函数参数为object，格式如下:{fileName:'文件名称'}
   *    userId:'不传时，默认会取vuex中user.id',
   *    files 数组或者object，示例
   *    { 
   *      groupCode:'不传时，默认会取vuex中selectProject.groupCode',
   *      projectId:'不传时，默认会取vuex中selectProject.id',
   *      fileName:'文件名称',
   *      fileBucket:'存储空间名称，可以不传',
   *      file:文件信息，假设有一个input type为file id为txtFile的标签，那么file为document.getElementById('txtFile').files[0]
   *    }
   * }
   */
  public async uploadFiles(_paramobj: any) {
    _paramobj.fileServiceUrl = this.baseUrl;
    return await super.uploadFiles(_paramobj);
  }
}
