utils.ts 3.9 KB
Newer Older
周成波's avatar
周成波 committed
1 2
import _cryptoJs from 'crypto-js';

周成波's avatar
周成波 committed
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
export default class utils {
  // 格式化为json字符串
  static formatJson(jsonString: string) {
    try {
      return JSON.stringify(JSON.parse(jsonString), null, 2); // 对JSON字符串进行格式化处理
    } catch (error) {
      return 'Invalid JSON'
    }
  }

  // 格式化为json
  static formatJsonObj(jsonString: string) {
    try {
      return JSON.parse(jsonString) // 对JSON字符串进行格式化处理
    } catch (error) {
      return JSON.parse('{"error": "Invalid JSON"}')
    }
  }

  // 生成年月日时分秒毫秒字符串
  static genDateTimeStr() {
    const now = new Date();
    const year = now.getFullYear();
    const month = (now.getMonth() + 1).toString().padStart(2, '0');
    const day = now.getDate().toString().padStart(2, '0');
    const hours = now.getHours().toString().padStart(2, '0');
    const minutes = now.getMinutes().toString().padStart(2, '0');
    const seconds = now.getSeconds().toString().padStart(2, '0');
    const milliseconds = now.getMilliseconds().toString().padStart(3, '0');
    const formattedDateTime = `${year}${month}${day}${hours}${minutes}${seconds}${milliseconds}`;
    // console.log(formattedDateTime); // 输出类似:20221231120530123
    return formattedDateTime
  }

37 38 39 40
  // 对一句话再次拆分文本
  static splitDetailText(sentences: string[]) {
    // console.log(sentences)
    let result_sentences: string[] = [];
Administrator's avatar
Administrator committed
41 42 43 44 45 46
    let currentSentence = '';
    for (let i = 0; i < sentences.length; i++) {
      const str = sentences[i];
      currentSentence += str + ',';
      if (i < sentences.length - 1 && (currentSentence + sentences[i + 1]).length <= 15) {
        continue;
47
      }
Administrator's avatar
Administrator committed
48 49 50 51
      result_sentences.push(currentSentence.endsWith(",") ? currentSentence.slice(0, -1) : currentSentence);
      currentSentence = '';
    }
    // console.log('result_sentences=', result_sentences);
52 53 54 55
    return result_sentences;
  }


56
  // 拆分文本
Administrator's avatar
Administrator committed
57
  static splitText(str: string, type: string = 'default') {
58
    str = str.replaceAll('“','').replaceAll('”','')
59
    // 使用正则表达式拆分文本
Administrator's avatar
Administrator committed
60 61 62 63 64 65
    let sentences: string[] = [];
    if(type == 'default'){
      sentences = str.split(/[!|?|。|!|?]/);
    } else {
      sentences = str.split(/[!|?|。|!|?|,|,]/);
    }
66 67
    // 过滤掉长度为 0 的句子
    sentences = sentences.filter(s => s.length > 0);
周成波's avatar
周成波 committed
68 69
    // 过滤掉只包含标点符号的句子
    sentences = sentences.filter(s => !utils.containsOnlyPunctuation(s));
Administrator's avatar
Administrator committed
70 71 72 73 74 75
    if(type == 'default'){
      return sentences
    } else {
      let detailSplit = utils.splitDetailText(sentences);
      return detailSplit;
    }
76 77
  }

周成波's avatar
周成波 committed
78 79 80 81 82 83 84 85 86 87 88
  // 拆分英文文本
  static splitTextEn(str: string) {
    str = str.replaceAll('"','').replaceAll('"','')
    // 使用正则表达式拆分文本
    let sentences = str.split(/[!|?|.]/);
    // 过滤掉长度为 0 的句子
    sentences = sentences.filter(s => s.length > 0);
    // console.log(sentences)
    return sentences
  }

周成波's avatar
周成波 committed
89 90 91 92 93 94 95
  // 过滤掉中文字符
  static filterChineseAndPunctuation(inputString: string) {
    return inputString.replace(/[\u4E00-\u9FA5\u3000-\u303F\uff00-\uffef]/g, '') // 过滤中文字符
                      .replace(/[^\w\s]|_/g, '') // 过滤标点符号
                      .replace(/\s+/g, ' '); // 连续多个空格替换为一个空格
  }

周成波's avatar
周成波 committed
96 97 98 99 100 101
  // 检查该字符串是否只包含中文标点符号和英文标点符号
  static containsOnlyPunctuation(str: string) {
    // 使用正则表达式匹配是否只包含标点符号(包括中文标点)
    return /^[!-\/:-@\[-`{-~\p{P}\p{S}\s]*$/u.test(str);
  }

周成波's avatar
周成波 committed
102 103 104 105 106 107 108 109 110 111 112
  // 加密
  static aesEncrypt(word: string) {
    var key = _cryptoJs.enc.Utf8.parse('e6ef616dc57343248f6b3e98a07e1dde');
    var srcs = _cryptoJs.enc.Utf8.parse(word);
    var encrypted = _cryptoJs.AES.encrypt(srcs, key, {
      mode: _cryptoJs.mode.ECB,
      padding: _cryptoJs.pad.Pkcs7
    });
    return encrypted.toString();
  }

周成波's avatar
周成波 committed
113
}