Vue?路由傳參加密的示例代碼
Vue 路由傳參加密
首先,創(chuàng)建一個(gè)base64.js
const Base64 = {
//加密
encode(str) {
return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
function toSolidBytes(match, p1) {
return String.fromCharCode('0x' + p1);
}));
},
//解密
decode(str) {
// Going backwards: from bytestream, to percent-encoding, to original string.
return decodeURIComponent(atob(str).split('').map(function (c) {
return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
}).join(''));
}
}
export default Base64在main.js里面引用
import Base64 from './assets/js/base64.js' Vue.prototype.$Base64 = Base64;
使用頁(yè)面:
this.$router.push({
path: "/user/Recommend",
query:{
info:this.$Base64.encode(XXXXX)//this.$Base64.encode(JSON.stringify(row))
}
});接受參數(shù)頁(yè)面:
let params = JSON.parse(this.$Base64.decode(this.$route.query.info))
Vue項(xiàng)目:路由跳轉(zhuǎn)時(shí)中文傳參被URL編碼,怎么解決?用js封裝Base64編碼解碼加密解密
1.在utils中封裝js方法,代碼如下:
var Base64 = {
?
// private property
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=", //密鑰(不能修改)
// public method for encoding
encode: function(input) {
var output = "";
var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
var i = 0;
input = Base64._utf8_encode(input);
while (i < input.length) {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);
enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;
if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}
output = output + this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) + this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);
}
return output;
},
// public method for decoding
decode: function(input) {
var output = "";
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
input = input.replace(/[^A-Za-z0-9+/=]/g, "");
while (i < input.length) {
enc1 = this._keyStr.indexOf(input.charAt(i++));
enc2 = this._keyStr.indexOf(input.charAt(i++));
enc3 = this._keyStr.indexOf(input.charAt(i++));
enc4 = this._keyStr.indexOf(input.charAt(i++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
output = output + String.fromCharCode(chr1);
if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}
}
output = Base64._utf8_decode(output);
return output;
},
// private method for UTF-8 encoding
_utf8_encode: function(string) {
string = string.replace(/ /g, " ");
var utftext = "";
for (var n = 0; n < string.length; n++) {
var c = string.charCodeAt(n);
if (c < 128) {
utftext += String.fromCharCode(c);
} else if ((c > 127) && (c < 2048)) {
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
} else {
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
},
// private method for UTF-8 decoding
_utf8_decode: function(utftext) {
var string = "";
var i = 0;
var c,c1,c2,c3 = 0;
while (i < utftext.length) {
c = utftext.charCodeAt(i);
if (c < 128) {
string += String.fromCharCode(c);
i++;
} else if ((c > 191) && (c < 224)) {
c2 = utftext.charCodeAt(i + 1);
string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
i += 2;
} else {
c2 = utftext.charCodeAt(i + 1);
c3 = utftext.charCodeAt(i + 2);
string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
i += 3;
}
}
return string;
}
}
?
export default Base642.在main.js中引入并掛載到vue原型對(duì)象上
//Base64加密 import Base64 from '@/utils/base64.js' Vue.prototype.$Base64 = Base64
3.在vue文件中使用
this.$Base64.encode(str) //加密 this.$Base64.decode(str) //解密
拓展:編碼解碼的其他方式
1.md5
常用于加密,可以暴力反向破解但是難度很大,一般來(lái)說(shuō),對(duì)md5加密的要求是現(xiàn)代算力最高的計(jì)算機(jī)需要花百年才能進(jìn)行解密
2.window對(duì)象自帶的編碼解碼
window.btoa(str) //對(duì)str進(jìn)行編碼
binary to ascii,用于將ascii字符串或二進(jìn)制數(shù)據(jù)轉(zhuǎn)換成一個(gè)base64編碼過的字符串表示,即Base64的編碼過程,常用于編碼字符串。btoa不能編輯Unicode字符
window.atob(str) //對(duì)str進(jìn)行解碼
ascii to binary,用于解碼一個(gè)已經(jīng)被base-64編碼過的數(shù)據(jù),即解碼Base64編碼的字符串。
3.Unicode字符編碼
encodeURI(str) //將URL地址編碼轉(zhuǎn)變成ASCII字符序列 decodeURI(str) //將ASCII字符序列解碼轉(zhuǎn)變成UTL地址 encodeURIComponent(str) //將中文字符編碼轉(zhuǎn)變成ASCII字符序列 decodeURIComponent(str) //將ASCII字符序列解碼轉(zhuǎn)變成中文字符
4.使用JS函數(shù)的escape()和unescape()
var escape1 =escape("我的名字是:");//編碼
console.log(escape1);
var unescape1 = unescape(escape1); //解碼
console.log(unescape1);
到此這篇關(guān)于Vue 路由傳參加密的文章就介紹到這了,更多相關(guān)Vue 路由傳參加密內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue+Element-ui日歷排班自定義實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Vue+Element-ui日歷排班自定義的相關(guān)資料,有現(xiàn)成的日歷插件但是不符合需求,所以項(xiàng)目中使用vue+element的表格組件自己實(shí)現(xiàn)一個(gè)日歷組件,需要的朋友可以參考下2023-09-09
Vue引入jquery實(shí)現(xiàn)平滑滾動(dòng)到指定位置
這篇文章主要介紹了Vue引入jquery實(shí)現(xiàn)平滑滾動(dòng)到指定位置的效果,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
Ant Design Vue日期組件的時(shí)間限制方式
這篇文章主要介紹了Ant Design Vue日期組件的時(shí)間限制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
Vue3之Mixin的使用方式(全局,局部,setup內(nèi)部使用)
這篇文章主要介紹了Vue3之Mixin的使用方式(全局,局部,setup內(nèi)部使用),具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10
axios+Vue實(shí)現(xiàn)上傳文件顯示進(jìn)度功能
這篇文章主要介紹了axios+Vue上傳文件顯示進(jìn)度效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-04-04
詳解Vue.js之視圖和數(shù)據(jù)的雙向綁定(v-model)
本篇文章主要介紹了Vue.js之視圖和數(shù)據(jù)的雙向綁定(v-model),使用v-model指令,使得視圖和數(shù)據(jù)實(shí)現(xiàn)雙向綁定,有興趣的可以了解一下2017-06-06
vue2.0 下拉框默認(rèn)標(biāo)題設(shè)置方法
今天小編就為大家分享一篇vue2.0 下拉框默認(rèn)標(biāo)題設(shè)置方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2018-08-08
Vuex中Store的簡(jiǎn)單實(shí)現(xiàn)
這篇文章主要介紹了Vuex中Store的簡(jiǎn)單實(shí)現(xiàn),為了在 Vue 組件中訪問 this.$store property,你需要為 Vue 實(shí)例提供創(chuàng)建好的 store,Vuex 提供了一個(gè)從根組件向所有子組件,以 store 選項(xiàng)的方式 注入 該 store 的機(jī)制,需要的朋友可以參考下2023-11-11
vue3組合式API獲取子組件屬性和方法的代碼實(shí)例
這篇文章主要為大家詳細(xì)介紹了vue3組合式API獲取子組件屬性和方法的代碼實(shí)例,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-01-01

