/*
 * @Author: 倚楼听风雨 18408246387@163.com
 * @Date: 2023-02-02 13:47:20
 * @LastEditors: 倚楼听风雨 18408246387@163.com
 * @LastEditTime: 2023-02-13 11:22:07
 * @FilePath: \log-server-web\src\api\data_abnormal_rate.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import service from "@/utils/request";

//DataAbnormalRateRouter============================================================================
export const queryAbnormalRate = (data) => {
  return service({
    url: "/dataStatistics/queryAbnormalRate",
    method: "post",
    data,
  });
};
//WeChatScannerApiRouter============================================================================
export const getWeChatScannerApiList = (data) => {
  return service({
    url: "/dataStatistics/getWeChatScannerApiList",
    method: "post",
    data,
  });
};
//WeChatScannerDetailedRouter============================================================================
export const getWeChatScannerDetailedList = (data) => {
  return service({
    url: "/dataStatistics/getWeChatScannerDetailedList",
    method: "post",
    data,
  });
};
//WeChatScannerLedgerRouter=================================================================================
export const getWeChatScannerLedgerList = (data) => {
  return service({
    url: "/dataStatistics/getWeChatScannerLedgerList",
    method: "post",
    data,
  });
};
export const exportLedgerListExcel = (tableData, fileName) => {
  service({
    url: "/dataStatistics/exportLedgerListExcel",
    method: "post",
    data: {
      fileName: fileName,
      infoList: tableData,
    },
    responseType: "blob",
  }).then((res) => {
    handleFileError(res, fileName);
  });
};

const handleFileError = (res, fileName) => {
  if (typeof (res.data) !== 'undefined') {
    if (res.data.type === 'application/json') {
      const reader = new FileReader()
      reader.onload = function() {
        const message = JSON.parse(reader.result).msg
        ElMessage({
          showClose: true,
          message: message,
          type: 'error'
        })
      }
      reader.readAsText(new Blob([res.data]))
    }
  } else {
    var downloadUrl = window.URL.createObjectURL(new Blob([res]))
    var a = document.createElement('a')
    a.style.display = 'none'
    a.href = downloadUrl
    a.download = fileName
    var event = new MouseEvent('click')
    a.dispatchEvent(event)
  }
}
