一般的には情報をコピーしたり、データを深くコピーしたり、暗号化したり、復号したりします.


import CryptoJS from "crypto-js";

const CRYPTOJSKEY = "zhouzhenrongqq12";

class PublicMethod {
  /**
   *     
   * @param text      
   */
  static copy(text) {
    const oInput = document.createElement("input");
    oInput.value = text;
    document.body.appendChild(oInput);
    oInput.select(); //     
    document.execCommand("Copy"); //          
    oInput.className = "oInput";
    oInput.style.display = "none";
  }
  /***
   *   
   * @param plaintText      
   * @returns {string}
   */
  static encrypt(plaintText) {
    const options = {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    };
    const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
    const encryptedData = CryptoJS.AES.encrypt(plaintText, key, options);
    let encryptedBase64Str = encryptedData.toString().replace(/\//g, "_");
    encryptedBase64Str = encryptedBase64Str.replace(/\+/g, "-");
    return encryptedBase64Str;
  }

  /***
   *   
   * @param encryptedBase64Str       
   * @returns {string}
   */
  static decrypt(encryptedBase64Str) {
    if (!encryptedBase64Str) {
      return null;
    }
    const vals = encryptedBase64Str.replace(/\-/g, '+').replace(/_/g, '/');
    const options = {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    };
    const key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
    const decryptedData = CryptoJS.AES.decrypt(vals, key, options);
    return CryptoJS.enc.Utf8.stringify(decryptedData);
  }

  /**
   *    
   * @param obj          
   * @returns {[]}
   */
  static deepCopy(obj) {
    const result = Array.isArray(obj) ? [] : {};
    for (const key in obj) {
      if (obj.hasOwnProperty(key)) {
        if (typeof obj[key] === 'object' && obj[key] !== null) {
          result[key] = this.deepCopy(obj[key]);
        } else {
          result[key] = obj[key];
        }
      }
    }
    return result;
  }
}

export default PublicMethod;