ludc
2025-01-03 7577e0d40354021d70b78d58b712ff8c2e8ad485
Source/plt-web/plt-web-ui/src/util/func.js
@@ -1,7 +1,92 @@
/**
 * 通用工具类
 */
import CryptoJS from 'crypto-js'
import {validatenull} from "@/util/validate";
import {getStore} from "@/util/store";
import {getIcons} from "@/api/UI/Icons";
import store from "@/store/index"
export default class func {
  static getSvgList() {
    const fs = require('fs');
    const path = require('path');
    // 指定文件夹路径
    const directoryPath = "../config/svg"; // 修改为你的文件夹路径
    // 存储 .svg 文件路径的数组
    let svgFiles = [];
    // 读取文件夹
    fs.readdir(directoryPath, (err, files) => {
      if (err) {
        return console.error('无法读取目录: ' + err);
      }
      // 遍历文件
      files.forEach(file => {
        // 检查文件后缀名
        if (path.extname(file).toLowerCase() === '.svg') {
          // 构建文件的完整路径
          const filePath = path.join(directoryPath, file);
          svgFiles.push(filePath);
        }
      });
      // 输出结果
      console.log(JSON.stringify(svgFiles, null, 2));
    });
  }
  /**
   * 单选表格行
   * 时间戳格式化
   */
  static formattedDate(val) {
    // 创建一个 Date 对象
    const date = new Date(val);
    // 格式化时间的辅助函数
    const formatNumber = (number) => String(number).padStart(2, '0');
    // 提取并格式化年、月、日、小时、分钟和秒
    const year = date.getFullYear();
    const month = formatNumber(date.getMonth() + 1); // 月份从0开始,需要+1
    const day = formatNumber(date.getDate());
    const hours = formatNumber(date.getHours());
    const minutes = formatNumber(date.getMinutes());
    const seconds = formatNumber(date.getSeconds());
    // 格式化为 YYYY-MM-DD HH:MM:SS
    return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  }
  /**
   * 单选表格行
   * row 当前行数据
   * CrudRef 表格ref绑定值
   * lastIndex 判断二次点击index是否和第一次点击一致
   * setLastIndex 更新lastIndex值
   * setSelectList 将下拉框保存数组置空
   */
  static rowClickHandler(row, CrudRef, lastIndex, setLastIndex, setSelectList) {
    /*if (lastIndex === row.$index) {
      setSelectList();
      CrudRef.clearSelection();
    } else {
      setSelectList();
      CrudRef.clearSelection();
      CrudRef.toggleRowSelection(row);
    }*/
    setSelectList();
    CrudRef.toggleSelection();
    CrudRef.toggleRowSelection(row);//选中当前行
    setLastIndex(row.$index);
  }
  /**
   * 不为空
   * @param val
@@ -36,6 +121,18 @@
      return true;
    }
    return false;
  }
  /**
   * 判断对象为空
   */
  static isEmptyObject(obj) {
    if (obj) {
      return Object.keys(obj).length === 0;
    } else {
      return true;
    }
  }
  /**
@@ -176,5 +273,92 @@
      return item[attr] !== array[0][attr];
    });
  }
  /**
   * 3des加密
   * @param message
   * @param key
   * @returns {string}
   */
  encryptByDES(message, key) {
    const keyHex = CryptoJS.enc.Utf8.parse(key);
    const encrypted = CryptoJS.DES.encrypt(message, keyHex, {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
  }
  /**
   * HmacMD5加密
   * @param message
   * @param key
   * @returns {string}
   * @constructor
   */
  static HmacMD5(message, key) {
    const encrypted = CryptoJS.HmacMD5(message, key);
    return encrypted.toString();
  }
  /**
   * aes加密
   * @param data
   * @param secretKey
   * @returns {string}
   */
  static encryptData(data, secretKey) {
    const key = CryptoJS.enc.Utf8.parse(secretKey);
    const iv = CryptoJS.enc.Utf8.parse(secretKey.substr(0, 16)); // AES block size is 128 bits (16 bytes)
    const encrypted = CryptoJS.AES.encrypt(data, key, {
      iv: iv,
      mode: CryptoJS.mode.CBC,
      padding: CryptoJS.pad.Pkcs7
    });
    return encrypted.toString();
  }
  /* 根据name获取svg图标
  * @param name
  * @returns {string}
  */
  static getSVGByName(name) {
    let svgHtml = '';
    if(name && name.indexOf(':')!=-1) {
      let iconList = getStore({name: 'icons'});
      if (iconList && iconList.length > 0) {
        let libName = name.split(':')[0];
        let currentLic = iconList.find(item => item.lable == libName);
        if (currentLic && currentLic.list) {
          currentLic.list.find(item => {
            if (item.name == name) {
              svgHtml = item.content;
            }
            return item.name == name;
          })
        }
      }else {
        getIcons().then(res => {
          store.dispatch("setIcons", res.data.data);
          let iconList = res.data.data;
          if (iconList && iconList.length > 0) {
            let libName = name.split(':')[0];
            let currentLic = iconList.find(item => item.lable == libName);
            if (currentLic.list) {
              currentLic.list.find(item => {
                if (item.name == name) {
                  svgHtml = item.content;
                }
                return item.name == name;
              })
            }
          }
          return svgHtml;
        })
      }
    }
    return svgHtml;
  }
}