npm install crypto-js

npm install js-md5

import CryptoJS from 'crypto-js';
export const appKey = 'cb6015828a44b5234ce9ed1c3bc7acb0';
import md5 from 'js-md5'
export const calcuMD5 = (pwd) => { return md5(pwd) }
export const SEEDMD5 = calcuMD5("U2FsdGVkX189N3VRCrUckSMoQM98v8PB"); //加密MD5 密钥
//加密
export const encryptBy =(plaintText,CRYPTOJSKEY) =>{
    var plaintText = plaintText;
    var options = {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    };
    var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
    var encryptedData = CryptoJS.DES.encrypt(plaintText, key, options);
    var encryptedBase64Str = encryptedData.toString();
    return encryptedBase64Str;
}
// 解密
export const decryptBy = (encryptedBase64Str,CRYPTOJSKEY, type) => {
    var encryptedBase64Str = encryptedBase64Str;
    var options = {
      mode: CryptoJS.mode.ECB,
      padding: CryptoJS.pad.Pkcs7
    };
    var key = CryptoJS.enc.Utf8.parse(CRYPTOJSKEY);
    // 解密
    var decryptedData = CryptoJS.DES.decrypt(encryptedBase64Str, key, options);
    // 解密后,需要按照Utf8的方式将明文转位字符串
    var decryptedStr = decryptedData.toString(CryptoJS.enc.Utf8);
    return decryptedStr;
}

//时间戳转日期
export function timestampToTime(timestamp,type) {
    var date = new Date(timestamp * 1000);//时间戳为10位需*1000,时间戳为13位的话不需乘1000
    var Y = date.getFullYear() + '-';
    var M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-';
    var D = date.getDate() <10? '0'+date.getDate()+ ' ' : date.getDate();
    var h = ' ' + date.getHours()<10? '0'+date.getHours()+ ':' : date.getHours() + ':';
    var m = date.getMinutes() <10? '0'+date.getMinutes()+ ':' : date.getMinutes() + ':';
    var s = date.getSeconds();
    if(type){
        return Y+M+D+h+m+s;
    }else{
        return Y+M+D;
    }
}

export const asyncCreatScript = (jsSrc) => {
    let script=document.createElement("script");
    script.type="text/javascript";
    script.src= jsSrc;
    document.getElementsByTagName('head')[0].appendChild(script); 
    script.onload = ()=>{}
}
export const asyncCreatCss = (cssSrc)=>{
    let link = document.createElement("link");
    link.setAttribute("rel", "stylesheet");
    link.setAttribute("type", "text/css");
    link.setAttribute("href", cssSrc);
    document.querySelector("head").appendChild(link);
}
// 压缩图片的方法BASE64
export const compress = (base64String, w, quality)=> {
    var getMimeType = function (urlData) {
        var arr = urlData.split(',');
        var mime = arr[0].match(/:(.*?);/)[1];
        // return mime.replace("image/", "");
        return mime;
    };
    var newImage = new Image();
    var imgWidth, imgHeight;
    var promise = new Promise(resolve => newImage.onload = resolve);
    newImage.src = base64String;
    return promise.then(() => {
        imgWidth = newImage.width;
        imgHeight = newImage.height;
        var canvas = document.createElement("canvas");
        var ctx = canvas.getContext("2d");
        if (Math.max(imgWidth, imgHeight) > w) {
            if (imgWidth > imgHeight) {
                canvas.width = w;
                canvas.height = w * imgHeight / imgWidth;
            } else {
                canvas.height = w;
                canvas.width = w * imgWidth / imgHeight;
            }
        } else {
            canvas.width = imgWidth;
            canvas.height = imgHeight;
        }
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.drawImage(newImage, 0, 0, canvas.width, canvas.height);
        var base64 = canvas.toDataURL(getMimeType(base64String), quality);
        return base64;
    });
}

/**
 * @name 判断iOS
 */
export const isiOS = ()=>{
    let u = navigator.userAgent;
    let iOs = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端\
    return iOs;
}
/**
 * @name 判断android
 */
export const isAndroid = ()=>{
    let u = navigator.userAgent;
    let android = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
    return android
}

/**
 * @name 判断微信浏览器
 */

export const isWeiXin = () => {
    let ua = window.navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        return true;
    }else{
        return false;
    }
}

import { encryptBy, decryptBy, appKey, calcuMD5, SEEDMD5 } from '@/config'

let params = {
  cons: 1
}
let slol = JSON.stringify(params);
let data = encryptBy(slol, SEEDMD5) 加密
console.log(data)
console.log(decryptBy(data, SEEDMD5)) 解密

 

Logo

前往低代码交流专区

更多推荐