import request from "@/utils/request";
const baseURL = "/account/admin/common";

const CommonApi = {
  /**
   * 上传文件
   *
   * @param formData
   */
  upload(formData: FormData) {
    return request<any, uploadResponse>({
      url: `${baseURL}/upload`,
      method: "post",
      data: formData,
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  /**
   * 上传文件
   */
  uploadFile(file: File) {
    const formData = new FormData();
    formData.append("file", file);
    return request<any, FileInfo>({
      url: `${baseURL}/upload`,
      method: "post",
      data: formData,
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
  },
  /**
   * 下载文件
   * @param url
   * @param fileName
   */
  download(url: string, fileName?: string) {
    return request({
      url: url,
      method: "get",
      responseType: "blob",
    }).then((res) => {
      const blob = new Blob([res.data]);
      const a = document.createElement("a");
      const url = window.URL.createObjectURL(blob);
      a.href = url;
      a.download = fileName || "下载文件";
      a.click();
      window.URL.revokeObjectURL(url);
    });
  },
  /**
   * 发送验证码
   */
  sms(params: smsRequest) {
    return request<any, smsResponse>({
      url: `${baseURL}/sms`,
      method: "get",
      params
    });
  },
};

export default CommonApi;

export interface smsRequest {
  /**
   * 验证码类型
   */
  type: string;
  [property: string]: any;
}
/**
 * 是否成功
 *
 * CzgResult«Boolean»
 */
export interface smsResponse {
  code?: number | null;
  data?: boolean | null;
  msg?: null | string;
  [property: string]: any;
}

/**
 * 文件API类型声明
 */
export interface FileInfo {
  /** 文件名 */
  name: string;
  /** 文件路径 */
  url: string;
}

/**
 * 文件地址
 *
 * CzgResult«String»
 */
export interface uploadResponse {
  code?: number | null;
  data?: null | string;
  msg?: null | string;
  [property: string]: any;
}
