/**
* 获取唯一随机数
* @type {*[]}
*/
let randomArr = []
function getRandomNum(length = 32) {
let num = parseInt(Math.random() * 20000);
if (randomArr.indexOf(num) === -1) {
let arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
let num = '';
for (let i = 0; i < length; i++) {
num += arr[parseInt(Math.random() * arr.length)];
}
randomArr.push(num);
return num
} else {
return getRandomNum()
}
}
/**
* 地址带参:参数转码
* 添加12位随机数后,使用base64转码
*/
export function setParam(param) {
let result = ""
let tag = 0
for (let i in param) {
result += ((!tag ? "&" : "") + i + "=" + getRandomNum(12) + '' + param[i]);
}
return window.btoa(result)
}
/**
* 地址带参:参数解码
* 解码 base64 后, 截取数据
* @param value
*/
export function getParam(value) {
//这里是直接封装获取当前页面的url截取的 根据需求来 如果你的url是变量或者在外面这一步要提出去
let queryParams = new URLSearchParams(window.location.search);
//这个sn是我加密时给定的key值 可自定义
let sn = queryParams.get('sn')
const str = unescape(sn)
const arr = (window.atob(str) || "").split("&");
let param = {}
arr.map(item => {
if (item) {
const [key, value] = item.split("=")
param[key] = value.substring(12)
}
})
return param
}
使用:
import { setParam, getParam } from "../../../../utils/Crypto.js";
//加密
let that = this;
let obj = {
appId: id,
editMode: "update",
type: "template",
};
let urlAddressParams = setParam(obj);
let url = "manage.html?sn=" + urlAddressParams;
window.location.href = url;
//解密
let { appId,editMode,type } = getParam()
this.baseboardId = appId