如何使用window.btoa base64对接口数组进行加密处理

大家好,我是孙叫兽,前端全栈工程师,uniapp技术交流群:1149933135

项目采用uniapp+uView开发;

uiapp官方文档:https://uniapp.dcloud.io/

uview官方文档:http://uviewui.com/components/intro.html

接口传参的操作步骤请参考我上一篇文章——>uniapp框架之如何修改接口传参的参数 

我这个显示的是加密过的,这里只演示过程。

window.btoa base64加密 一般格式是:window.btoa(‘123456’);

这个不支持数组直接加密的,因此我们需要将数组转换成字符串,如果后端处理了,我们就不用转成字符串啦,具体可以使用console.log(this.personId.attendees);打印一下。

这个不用转成字符串的

this.personId.attendees = window.btoa(this.personId.attendees);
this.personId.charge = window.btoa(this.personId.charge);

转成字符串的

this.personId.attendees = window.btoa(this.personId.attendees.toString());
this.personId.charge = window.btoa(this.personId.attendees.toString());

因为上面的对一个数组四个人一起加密处理的,后台解密需要解密四次,我们可以将数组拆分成4个分开加密(有多少人分割成多少个数组再加密)。

 var result = [];
	  for(var i=1;i<=this.personId.attendees.length;i++){
	  		  result.push(this.personId.attendees.slice(i,i+1));    
	  	     }    
return result;
	  // console.log(result);
//对参与人进行加密(可以多个人)
 this.personId.attendees = window.btoa(result);
	   // console.log(this.personId.attendees);
	   //对主责人进行加密处理(只能选一个人)
 this.personId.charge = window.btoa(this.personId.attendees.toString());

猜你喜欢

转载自blog.csdn.net/weixin_41937552/article/details/109307438
今日推荐