动态el-form实列,小程序加密解密

获取动态form表单的ref实例

:ref="el => (items.el = el)"

Object.hasOwnProperty.call

Object.hasOwnProperty.call() - 简书

//判断一个对象,每个属性是否有值
let data = {
  code: "code",
  name: "name"
}

let flag = Object.values(data).every(item=>{
    return item != '' ? true : false
})
import CryptoJS from "crypto-js";
const {
	http
} = uni.$u;
import config from '@/common/config';

/**
 * AES加密 单数据加密
 * --方法需要异转同!!!!!
 * @param {String} plaintext 要加密的明文
 * @returns {obj} {encrypted:密文,redisKey:传给后端的参}
 */
export async function encryptByAES(plaintext) {

	return await http({
		url: "/memberApp/getFrontendKey",
		method: "GET"
	}).then(r => {
		if (r.data.code === 200) {
			//进行加密
			let key = CryptoJS.enc.Base64.parse(r.data.data.key);
			let encrypted = CryptoJS.AES.encrypt(plaintext, key, {
				mode: CryptoJS.mode.ECB,
				padding: CryptoJS.pad.Pkcs7,
			});
			return {
				encrypted: encodeURIComponent(encrypted.ciphertext.toString(CryptoJS.enc.Base64)),
				redisKey: r.data.data.redisKey
			}
		} else {
			uni.showToast(r.data.msg)
		}
	}).catch(() => {
		uni.showToast("网络连接失败!")
	})
}


/**
 * AES加密 多数据加密
 * --方法需要异转同!!!!!
 * @param {Array} plaintexts [{name:"键名",value:"明文"}]
 * @returns {obj} {...name:密文,redisKey:传给后端的参}
 */
export async function encryptByAESarr(plaintexts) {
	return await http.post(config.baseUrl + "/memberApp/getFrontendKey", {}, {
			method: "GET"
		}

	).then(r => {
		if (r.data.code === 200) {
			let encryptedObj = {};
			plaintexts.forEach(item => {
				let key = CryptoJS.enc.Base64.parse(r.data.data.key);
				let encrypted = CryptoJS.AES.encrypt(item.value, key, {
					mode: CryptoJS.mode.ECB,
					padding: CryptoJS.pad.Pkcs7,
				});
				encryptedObj[item.name] = encodeURIComponent(encrypted.ciphertext.toString(CryptoJS
					.enc.Base64));
			})
			encryptedObj.redisKey = r.data.data.redisKey;
			return encryptedObj
		} else {
			uni.showToast(r.data.msg)
		}
	}).catch(() => {
		uni.showToast("网络连接失败!")
	})
}


/**
 * AES解密
 * 方法不用转同
 * @param {String} cipherText 密文
 * @return {String} 明文
 */
export async function decryptByAES(cipherText) {
	// let key = CryptoJS.enc.Base64.parse(keyInBase64Str);
	// // 返回的是一个Word Array Object,其实就是Java里的字节数组
	// let decrypted = CryptoJS.AES.decrypt(cipherText, key, {
	//     mode: CryptoJS.mode.ECB,
	//     padding: CryptoJS.pad.Pkcs7,
	// });
	return await http.post(config.baseUrl + "/memberApp/memberName/decrypt?encryptMessage=" + cipherText,
	{},{method:"GET"}
	).then(r => {
		if (r.data.code === 200) {
			return r.data.data;
		} else {
			uni.showToast(r.data.msg)
		}
	}).catch(() => {
		uni.showToast("网络连接失败!")
	})
	// return decrypted.toString(CryptoJS.enc.Utf8);
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值