封装路由跳转方法,对query传参进行加密


前言

众所周知,vue路由跳转可以通过params和query进行传递参数,我们可以把这两种方式简单理解为POST和GET请求方法,params传递的参数会放在body中,虽然用户看不到具体参数,但是在跳转后的页面刷新会造成参数丢失的问题。query传递的参数会拼在url的后面,在跳转后的页面刷新不会造成参数丢失。但通过query传递的参数会在地址栏显示出来,很不安全。下面我们封装一个全局的路由跳转方法,来对query参数进行加密。

1.引入库crypto-js

npm install crypto-js

2.封装加密、解密方法

import CryptoJS from "crypto-js";
const key = CryptoJS.enc.Utf8.parse("reddatehongzao20"); // 十六位十六进制数作为密钥
const iv = CryptoJS.enc.Utf8.parse("hongzao20reddate"); // 十六位十六进制数作为密钥偏移量
// 解密方法
export function decryption(word: any): string {
  const encryptedHexStr = CryptoJS.enc.Hex.parse(word);
  const srcs = CryptoJS.enc.Base64.stringify(encryptedHexStr);
  const decrypt = CryptoJS.AES.decrypt(srcs, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  const decryptedStr = decrypt.toString(CryptoJS.enc.Utf8);
  return decryptedStr.toString();
}

// 加密方法
export function encryption(word: any): string {
  const srcs = CryptoJS.enc.Utf8.parse(word);
  const encrypted = CryptoJS.AES.encrypt(srcs, key, {
    iv,
    mode: CryptoJS.mode.CBC,
    padding: CryptoJS.pad.Pkcs7
  });
  return encrypted.ciphertext.toString().toUpperCase();
}

3.封装全局路由跳转方法

/**
* 打开新路由
* @param params 路由参数
* @param _type 打开方式 1:replace 0或不传:push
*/
$routerPush(
 params:
   | string
   | {
       name?: string;
       path?: string;
       query?: any;
     },
 _type) {
	if (typeof params == "object") {
	  if (params.hasOwnProperty("query")) {
	    for (const key in params.query) {
	      if (params.query.hasOwnProperty(key)) {
	      	// encryption 为封装的加密方法
	        const paramsKey = encryption(params.query[key]);
	        params.query[key] = paramsKey;
	      }
	    }
	    // 无论是使用query或params,都可以使用name;
	    // 如果是path的话,params传参无效
	    params.name = params.path;
	    delete params.path;
	  }
	}
	if (_type == 1) {
	  this.$router.replace(params);
	} else {
	  this.$router.push(params);
	}
}

总结

以上就是今天的分享!!!

// utils/navigation.js 使/** * 路由跳转公共方法封装 * 功能:统一路由管理、参数处理、错误拦截、扩展功能 */ // 路由配置白名单(无需登录可访问的页面) const whiteList = ['/pages/login/login', '/pages/index/index']; // 全局前置守卫钩子(可选) let beforeEach = (to, from, next) => { next() }; // 设置前置守卫 export const setBeforeEach = (fn) => { beforeEach = fn; }; /** * 处理跳转参数 * @param {string} url * @param {Object} params * @returns {string} */ function handleParams(url, params = {}) { if (!params || Object.keys(params).length === 0) return url; const query = Object.keys(params) .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`) .join('&'); return url.includes('?') ? `${url}&${query}` : `${url}?${query}`; } /** * 统一跳转方法 * @param {Object} options * @param {Function} method */ async function navigate(options, method) { try { const fullUrl = handleParams(options.url, options.params); // 执行全局前置守卫 await beforeEach({ url: fullUrl, type: method.name }, getCurrentPageUrl(), (next) => { if (next === false) return; }); method({ url: fullUrl, success: (res) => { options.success && options.success(res); }, fail: (err) => { console.error(`[路由错误] ${method.name} 跳转失败:`, err); options.fail && options.fail(err); }, complete: options.complete }); } catch (error) { console.error('[路由异常]', error); } } // 获取当前页面路径 function getCurrentPageUrl() { const pages = getCurrentPages(); return pages[pages.length - 1]?.route || ''; } // 封装导航方法 export const nav = { /** * 保留当前页面跳转 * @param {Object} options {url, params, success, fail, complete} */ navigateTo(options) { navigate(options, uni.navigateTo); }, /** * 关闭当前页面跳转 * @param {Object} options */ redirectTo(options) { navigate(options, uni.redirectTo); }, /** * 关闭所有页面跳转 * @param {Object} options */ reLaunch(options) { navigate(options, uni.reLaunch); }, /** *
最新发布
03-30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值