乐趣区

关于前端:开发笔记记录实用小工具

计算价格 精度计算解决 decimal.js

import Decimal from 'decimal.js'

const calculation = (data: any, type: number) => {const { priceProductShow, priceProductDiscount, skuNum} = data
    const money = new Decimal(priceProductShow)
    const count = new Decimal(priceProductDiscount)
    const num = new Decimal(skuNum)
    const add = money.mul(num).add(count).toNumber()
    let temp = 0
    if (type === 1) {temp = new Decimal(totalMoney).add(new Decimal(add)).toNumber()} else {temp = new Decimal(totalMoney).sub(new Decimal(add)).toNumber()}
    return temp
  }

加密工具 crypto-js

// ts 依赖包 @types/crypto-js
// js 依赖包 crypto-js
import CryptoJS from 'crypto-js';

const aesPassword = (password:any) => {const p = password.split('');
  if (p.length !== 16) {for (let i = p.length; i < 16; i++) {p[i] = '';
    }
  }
  const txt = CryptoJS.enc.Utf8.parse(p.join(''));
  const key = CryptoJS.enc.Utf8.parse('test');
  const iv = CryptoJS.enc.Utf8.parse('test2');
  const aesPassword = CryptoJS.AES.encrypt(txt, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.ZeroPadding,
  });
  return aesPassword.ciphertext.toString(CryptoJS.enc.Base64);
};
退出移动版