vue 的base64加密解密


1.新建一個文件

加入代碼

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) {
         return decodeURIComponent(atob(str).split('').map(function (c) {
             return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
         }).join(''));
     }
 }
 export default Base64
 
2.在主體文件main.js中進行設置(引入和暴露出來)
import Base64 from "./assets/js/base64.js"  路徑寫法:

Vue.prototype.$Base64 = Base64;   暴露:
 
3.在需要加密的頁面中進行加密:
加密的方式有兩種:
3-1單個數據加密
this.$Base64.encode()
3-2.多數據加密(對象)
this.$Base64.encode(JSON.stringify(params))
 
4.在對應的文件中解密
解密也分兩種:
4-1.單個數據解密
this.$Base64.decode()
4-2.多個數據解密(對象)
JSON.parse(this.$Base64.decode(this.$route.query.id))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM