riku
2023-11-23 348d29c1cd601e269eae92e6ec55d31e77b3ecd0
src/utils/common.js
@@ -1,4 +1,6 @@
import * as XLSX from 'xlsx/xlsx.mjs';
import * as XLSX from 'xlsx/xlsx.mjs'
import dayjs from 'dayjs'
export function useCommonFunction(){
    /**
     * description:判断起始时间跨度是否超过1个月
@@ -10,28 +12,25 @@
    function isExceedOneMonth(dateStr1, dateStr2) {
        // 超过一个月,返回True,否则返回False
        // 将日期字符串转为日期对象
        const date1 = new Date(dateStr1);
        const date2 = new Date(dateStr2);
    const date1 = new Date(dateStr1)
    const date2 = new Date(dateStr2)
  
        // 获取两个日期的年、月、日
        const year1 = date1.getFullYear();
        const month1 = date1.getMonth();
        const day1 = date1.getDate();
    const year1 = date1.getFullYear()
    const month1 = date1.getMonth()
    const day1 = date1.getDate()
  
        const year2 = date2.getFullYear();
        const month2 = date2.getMonth();
        const day2 = date2.getDate();
    const year2 = date2.getFullYear()
    const month2 = date2.getMonth()
    const day2 = date2.getDate()
  
        // 判断两个日期是否相差一个月
        if (year1 === year2) {
          // 年份相等,比较月份差值
          if (Math.abs(month1 - month2) === 1) {
            // 月份差值为1,还需要判断具体日期
            if (
              (month1 < month2 && day1 < day2) ||
              (month1 > month2 && day1 > day2)
            ) {
              return true;
        if ((month1 < month2 && day1 < day2) || (month1 > month2 && day1 > day2)) {
          return true
            }
          }
        } else if (Math.abs(year1 - year2) === 1) {
@@ -40,12 +39,12 @@
            (year1 < year2 && month1 === 11 && month2 === 0 && day1 < day2) ||
            (year1 > year2 && month1 === 0 && month2 === 11 && day1 > day2)
          ) {
            return true;
        return true
          }
        }
  
        // 默认返回false,表示两个日期字符串不相差一个月
        return false;
    return false
      }
      /**
@@ -54,7 +53,7 @@
     * @returns:大于,则返回true。否则返回false
     */
      function cmpp(a, b) {
        return Number(a.replace('%', '')) >= Number(b.replace('%', ''));
    return Number(a.replace('%', '')) >= Number(b.replace('%', ''))
      }
      /**
@@ -64,25 +63,24 @@
     */
     function exportToExcel(exportData,tableColumns,excelColumnsName,excelName='data.xlsx'){
      const itemsFormatted = exportData.map((item) => {
        const newItem = {};
      const newItem = {}
        tableColumns.forEach((col) => {
          newItem[col] = item[col];
        });
        return newItem;
      });
        newItem[col] = item[col]
      })
      return newItem
    })
      // 创建xlsx对象
      const xls = XLSX.utils.json_to_sheet(itemsFormatted);
    const xls = XLSX.utils.json_to_sheet(itemsFormatted)
     
     // 编辑表头行       修改表头
      excelColumnsName.forEach(item =>{
    excelColumnsName.forEach((item) => {
        xls[item[0]].v = item[1]
      })
      // 创建workbook,并把sheet添加进去
      const wb = XLSX.utils.book_new();
      XLSX.utils.book_append_sheet(wb, xls, 'Sheet1');
    const wb = XLSX.utils.book_new()
    XLSX.utils.book_append_sheet(wb, xls, 'Sheet1')
      // 将workbook转为二进制xlsx文件并下载
      XLSX.writeFile(wb, excelName);
    XLSX.writeFile(wb, excelName)
    }
     /**
@@ -92,22 +90,30 @@
     * @returns:比如12:00:00-13:00:00 所以返回的数组元素是 12:00:00 ,12:15:00,12:30:00,12:45:00,13:00:00
     */
     function  descFiftyTime(begin, end) {
      let time = [];
    let time = []
      if (begin == end) {
        time.push(begin);
        return time;
      time.push(begin)
      return time
      }
      time.push(begin);
      let temp = dayjs(begin).add(15, 'minute').format('YYYY-MM-DD HH:mm:ss');
    time.push(begin)
    let temp = dayjs(begin).add(15, 'minute').format('YYYY-MM-DD HH:mm:ss')
      while (temp != end) {
        time.push(temp);
        temp = dayjs(temp).add(15, 'minute').format('YYYY-MM-DD HH:mm:ss');
      time.push(temp)
      temp = dayjs(temp).add(15, 'minute').format('YYYY-MM-DD HH:mm:ss')
      }
      // 加上异常的结束时间
      time.push(temp);
      return time;
    time.push(temp)
    return time
    }
    return {isExceedOneMonth,cmpp,exportToExcel,descFiftyTime}
  /**
   * 格式化为百分号
   * @param {*} v
   * @returns
   */
  function percentFormatter(v) {
    return v * 100 + '%'
}
  return { isExceedOneMonth, cmpp, exportToExcel, descFiftyTime, percentFormatter }
}