const wxSafeUrl = (url: string = location.href) => { const replaceMap = { 'https://h5p-gw.duotest.cn': 'https://h5.gogpay.cn/h5-test', 'https://h5p-gw.gogpay.cn': 'https://h5.gogpay.cn/h5' };

url = url.replace(/(p=\w+&?)/g, '').replace(/(openId=\w+&?)/g, '');

for (const [key, value] of Object.entries(replaceMap)) { url = url.replace(key, value); }

return url; };

该函数使用一个 replaceMap 对象来存储需要替换的URL地址,并使用 Object.entries 方法循环遍历 replaceMap 对象,将每个键值对中的 key 和 value 分别作为 url.replace 方法的第一个和第二个参数进行替换。此外,该函数还使用正则表达式替换了URL中可能包含的 p 和 openId 参数,确保URL安全性。

例如,如果 url 为 https://h5p-gw.duotest.cn/test?p=123&openId=456,则经过该函数处理后的 url 将为 https://h5.gogpay.cn/h5-test/test。

注意事项:

  • 确保 replaceMap 对象中的 key 和 value 均为正确的URL地址。
  • 确保正则表达式能够匹配到所有需要替换的敏感信息。
  • 该函数仅用于示例,实际使用时需要根据实际情况进行调整。
微信安全URL处理函数:优化和代码示例

原文地址: https://www.cveoy.top/t/topic/pcDa 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录