From 348d29c1cd601e269eae92e6ec55d31e77b3ecd0 Mon Sep 17 00:00:00 2001 From: riku <risaku@163.com> Date: 星期四, 23 十一月 2023 13:21:46 +0800 Subject: [PATCH] 修改指标排序界面相关逻辑 --- src/utils/common.js | 169 ++++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 114 insertions(+), 55 deletions(-) diff --git a/src/utils/common.js b/src/utils/common.js index 62bd9f7..54f6dae 100644 --- a/src/utils/common.js +++ b/src/utils/common.js @@ -1,60 +1,119 @@ -export function useCommonFunction(){ - /** - * description锛氬垽鏂捣濮嬫椂闂磋法搴︽槸鍚﹁秴杩�1涓湀 - * @param锛� 寮�濮嬫椂闂达紝缁撴潫鏃堕棿 - * @createTime:2023-08-18 - * @returns锛氳秴杩囦竴涓湀杩斿洖true,涓嶈秴杩囦竴涓湀鍒欒繑鍥瀎alse - */ - function isExceedOneMonth(dateStr1, dateStr2) { - // 瓒呰繃涓�涓湀锛岃繑鍥濼rue锛屽惁鍒欒繑鍥濬alse - // 灏嗘棩鏈熷瓧绗︿覆杞负鏃ユ湡瀵硅薄 - 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(); - console.log(month1, month2); - - // 鍒ゆ柇涓や釜鏃ユ湡鏄惁鐩稿樊涓�涓湀 - 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; - } +import * as XLSX from 'xlsx/xlsx.mjs' +import dayjs from 'dayjs' + +export function useCommonFunction() { + /** + * description锛氬垽鏂捣濮嬫椂闂磋法搴︽槸鍚﹁秴杩�1涓湀 + * @param锛� 寮�濮嬫椂闂达紝缁撴潫鏃堕棿 + * @createTime:2023-08-18 + * @returns锛氳秴杩囦竴涓湀杩斿洖true,涓嶈秴杩囦竴涓湀鍒欒繑鍥瀎alse + */ + + function isExceedOneMonth(dateStr1, dateStr2) { + // 瓒呰繃涓�涓湀锛岃繑鍥濼rue锛屽惁鍒欒繑鍥濬alse + // 灏嗘棩鏈熷瓧绗︿覆杞负鏃ユ湡瀵硅薄 + 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 } - - // 榛樿杩斿洖false锛岃〃绀轰袱涓棩鏈熷瓧绗︿覆涓嶇浉宸竴涓湀 - return false; } - - /** - * description锛氱櫨鍒嗗彿姣旇緝澶у皬 - * @param锛� a鏄惁澶т簬b - * @returns锛氬ぇ浜庯紝鍒欒繑鍥瀟rue銆傚惁鍒欒繑鍥瀎alse - */ - function cmpp(a, b) { - return Number(a.replace('%', '')) > Number(b.replace('%', '')); + } 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 } + } - return {isExceedOneMonth,cmpp} + // 榛樿杩斿洖false锛岃〃绀轰袱涓棩鏈熷瓧绗︿覆涓嶇浉宸竴涓湀 + return false + } + + /** + * 鐧惧垎鍙锋瘮杈冨ぇ灏� + * @param锛� a鏄惁澶т簬b + * @returns锛氬ぇ浜庯紝鍒欒繑鍥瀟rue銆傚惁鍒欒繑鍥瀎alse + */ + function cmpp(a, b) { + return Number(a.replace('%', '')) >= Number(b.replace('%', '')) + } + + /** + *瀵煎嚭涓篹xcel + * @param锛� 琛ㄦ牸鏁版嵁锛屽緟瀵煎嚭鐨勮〃鏍煎垪锛宔xcel鍒楋紝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锛屽苟鎶妔heet娣诲姞杩涘幓 + const wb = XLSX.utils.book_new() + XLSX.utils.book_append_sheet(wb, xls, 'Sheet1') + // 灏唚orkbook杞负浜岃繘鍒秞lsx鏂囦欢骞朵笅杞� + 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 v * 100 + '%' + } + + return { isExceedOneMonth, cmpp, exportToExcel, descFiftyTime, percentFormatter } } -- Gitblit v1.9.3