uniapp MD5加密
安装:
npm install js-md5 -D
引入:
import Md5 from 'js-md5'
需求加密一个对象,
login_form: {openId: '123456789',phone: '',scenario: '656677',phoneSessionKey: '',openIdSessionKey: '',timeStamp: '',
},
//10位时间戳(秒)
const currentDate = new Date();
this.login_form.timeStamp = Math.floor(currentDate.getTime() / 1000);
//新的对象添加一个字段,用来生成签名
const newlogin_form = {...this.login_form,secretKey: "secretKey_key", //密钥
};
const sortedEntries = Object.entries(newlogin_form).filter(([key, value]) => value) // 过滤掉值为空的字段.sort(([keyA], [keyB]) => keyA.localeCompare(keyB)); // 按键字母顺序排序
const data = sortedEntries.map(([key, value]) => `${key}=${value}`) // 将键值对转换为 "key=value" 格式.join('&'); // 用 '&' 连接
console.log(data, "加密前的");
let password_md5 = Md5(data);
console.log(password_md5, "加密后的数据");
this.login_form.signature = password_md5