wujingjing
2024-12-12 bd52da253561e68d9ab31a260a9c23c2745334a7
src/utils/util.ts
@@ -1,11 +1,11 @@
import { ElMessage, ElMessageBox } from 'element-plus';
import JSONbig from 'json-bigint';
import { storeToRefs } from 'pinia';
import { unref, type Ref } from 'vue';
import { useTagsViewRoutes } from '/@/stores/tagsViewRoutes';
import { useThemeConfig } from '/@/stores/themeConfig';
import request from '/@/utils/request';
/**
 * @description 当碰到 JSON 中存在过长的数字时,使用 JSONbigString 解析,数字会转为字符串处理
 * 用法:JSONbigString.parse(jsonStr))
@@ -110,8 +110,8 @@
export const getImg = (name) => `src/assets/images/${name}`;
export const downloadJSON = (jsonData: Object, fileName: String) => {
   const dataStr = 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(jsonData));
export const downloadJSON = (jsonData: string, fileName: String) => {
   const dataStr = 'data:text/json;charset=utf-8,' + encodeURIComponent(jsonData);
   const downloadAnchorNode = document.createElement('a');
   downloadAnchorNode.setAttribute('href', dataStr);
   downloadAnchorNode.setAttribute('download', fileName + '.json');
@@ -168,9 +168,9 @@
export const convertListToTree = (
   data: any[],
   defaultProps = {
      ID: 'ID',
      Children: 'Children',
      ParentID: 'ParentID',
      ID: 'id',
      Children: 'children',
      ParentID: 'parent',
   }
) => {
   if (!data || data?.length === 0) return [];
@@ -210,11 +210,10 @@
   callback: (value: T, index?, array?, parent?) => any,
   parent: any = null,
   markParent = false,
   childrenKey = 'Children'
   childrenKey = 'children'
) => {
   if (!treeData || treeData.length === 0) return;
   if (!parent) parent = treeData;
   for (let index = 0; index < treeData.length; index++) {
      const value = treeData[index] as any;
      if (markParent) {
@@ -233,7 +232,7 @@
      if (value[childrenKey] && value[childrenKey].length !== 0) {
         // 递归跳出
         const callResult = travelTree(value[childrenKey], callback, value, markParent);
         const callResult = travelTree(value[childrenKey], callback, value, markParent, childrenKey);
         if (callResult) {
            return true;
         }
@@ -258,7 +257,7 @@
 * @param tableData
 * @returns
 */
export const flatten = (tableData: any[], removeChild?: boolean, children = 'Children'): any[] => {
export const flatten = (tableData: any[], removeChild?: boolean, children = 'children'): any[] => {
   const flattenedData: any[] = [];
   for (const item of tableData) {
@@ -413,8 +412,8 @@
 */
export const deleteCurrentRow = (
   row: {
      ID: string;
      Name: string;
      id: string;
      title: string;
      [key: string]: any;
   },
   label: String,
@@ -424,7 +423,7 @@
   showRowName = true,
   extraParams: Record<string, any> = {}
) => {
   const tip = showRowName ? `确定删除${label}:【${row.Name}】?` : `确定删除当前${label}?`;
   const tip = showRowName ? `确定删除${label}:【${row.title}】?` : `确定删除当前${label}?`;
   ElMessageBox.confirm(tip, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
@@ -436,17 +435,13 @@
      }
      const res = await deleteApi(
         {
            ID: row.ID,
            id: row.id,
            ...rawExtraParams,
         },
         req
      );
      if (res.Data) {
         ElMessage.success(`删除${label}成功`);
         callback(row.ID);
      } else {
         ElMessage.error(`删除${label}失败`);
      }
      ElMessage.success(`删除${label}成功`);
      callback(row.id);
   });
};
@@ -468,12 +463,14 @@
 * 最近 n 天的 startDate、endDate
 * @param dates
 */
export const getRecentDateRange = (dates: number) => {
export const getRecentDateRange = (dates: number, includesCurrent = true) => {
   dates = includesCurrent ? dates - 1 : dates;
   // 获取当前日期
   const endDate = new Date();
   const startDate = new Date();
   startDate.setTime(startDate.getTime() - 3600 * 1000 * 24 * dates);
   startDate.setHours(0, 0, 0, 0);
   endDate.setHours(23,59,59,59)
   return [startDate, endDate];
};
@@ -579,7 +576,23 @@
      }, wait);
   };
};
/**
 * 文件大小字节转换为XXX
 * @param size 字节大小
 * @returns {string|*}
 */
export const convertFileSize = (size) => {
   if (!size && size !== 0) return '';
   if (size < pow1024(1)) return size + ' B';
   if (size < pow1024(2)) return (size / pow1024(1)).toFixed(2) + ' KB';
   if (size < pow1024(3)) return (size / pow1024(2)).toFixed(2) + ' MB';
   if (size < pow1024(4)) return (size / pow1024(3)).toFixed(2) + ' GB';
   return (size / pow1024(4)).toFixed(2) + ' TB';
};
// 求次幂
function pow1024(num) {
   return Math.pow(1024, num);
}
/**
 *
 * @param {*} func 节流函数
@@ -688,11 +701,47 @@
/**
 * 保留指定精度小数位,且不补零
 * @param num
 * @param precision
 * @returns
 * @param num
 * @param precision
 * @returns
 */
export const toMyFixed = (num, precision) => {
   if (num == null) return '';
   return num.toFixed(precision).replace(/\.?0+$/, '');
   if (!precision) return num + '';
   const factor = Math.pow(10, precision);
   return Math.round(Number(num) * factor) / factor + '';
};
type GetTextWidthOption = {
   size?: string;
   family?: string;
};
export function getTextWidth(text: string, option: GetTextWidthOption) {
   if (!text) return 0;
   const { size = '14px', family = 'Microsoft YaHei' } = option;
   const spanEle = document.createElement('span');
   document.body.appendChild(spanEle);
   spanEle.style.font = 'times new roman';
   spanEle.style.fontSize = size;
   spanEle.style.height = 'auto';
   spanEle.style.width = 'auto';
   spanEle.style.position = 'absolute';
   spanEle.style.whiteSpace = 'no-wrap';
   spanEle.innerHTML = text;
   const width = spanEle.clientWidth;
   document.body.removeChild(spanEle);
   return width;
}
export function decodeFormData(formDataString) {
   const params = new URLSearchParams(formDataString);
   const decodedData = {};
   for (const [key, value] of params) {
      decodedData[key] = decodeURIComponent(value);
   }
   return decodedData;
}