zmc
2023-12-08 9c1d136e4f5ed9b5bce100147edbb52486da985a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import * as XLSX from 'xlsx/xlsx.mjs'
import dayjs from 'dayjs'
 
export function useCommonFunction() {
  /**
   * description:判断起始时间跨度是否超过1个月
   * @param: 开始时间,结束时间
   * @createTime:2023-08-18
   * @returns:超过一个月返回true,不超过一个月则返回false
   */
 
  function isExceedOneMonth(dateStr1, dateStr2) {
    // 超过一个月,返回True,否则返回False
    // 将日期字符串转为日期对象
    const date1 = new Date(dateStr1)
    const date2 = new Date(dateStr2)
 
    // 获取两个日期的年、月、日
    const year1 = date1.getFullYear()
    const month1 = date1.getMonth()
    const day1 = date1.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
        }
      }
    } else if (Math.abs(year1 - year2) === 1) {
      // 年份差值为1,比较月份和日期
      if (
        (year1 < year2 && month1 === 11 && month2 === 0 && day1 < day2) ||
        (year1 > year2 && month1 === 0 && month2 === 11 && day1 > day2)
      ) {
        return true
      }
    }
 
    // 默认返回false,表示两个日期字符串不相差一个月
    return false
  }
 
  /**
   * 百分号比较大小
   * @param: a是否大于b
   * @returns:大于,则返回true。否则返回false
   */
  function cmpp(a, b) {
    return Number(a.replace('%', '')) >= Number(b.replace('%', ''))
  }
 
  /**
   *导出为excel
   * @param: 表格数据,待导出的表格列,excel列,excel文件名
   * @returns
   */
  function exportToExcel(exportData, tableColumns, excelColumnsName, excelName = 'data.xlsx') {
    const itemsFormatted = exportData.map((item) => {
      const newItem = {}
      tableColumns.forEach((col) => {
        newItem[col] = item[col]
      })
      return newItem
    })
    // 创建xlsx对象
    const xls = XLSX.utils.json_to_sheet(itemsFormatted)
 
    // 编辑表头行       修改表头
    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')
    // 将workbook转为二进制xlsx文件并下载
    XLSX.writeFile(wb, excelName)
  }
 
  /**
   * description:返回时间数组,间隔15分钟。
   * @param: 异常的开始,异常结束时间
   * @createTime:2023-08-17
   * @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 = []
    if (begin == end) {
      time.push(begin)
      return time
    }
    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)
    return time
  }
 
  /**
   * 格式化为百分号
   * @param {*} v 
   * @returns 
   */
  function percentFormatter(v) {
    return Math.round(v * 100, 2) + '%'
  }
 
  return { isExceedOneMonth, cmpp, exportToExcel, descFiftyTime, percentFormatter }
}