当前位置:网站首页>2022-uni-app解析token标准的方式-使用jsrsasign-爬坑过了
2022-uni-app解析token标准的方式-使用jsrsasign-爬坑过了
2022-07-28 09:43:00 【itwangyang520】
1.直接使用这个decodeURIComponent(escape(window.atob(strings[1],在uini-app解析token是失败的
var token = data.token; //在请求头中获取token
let strings = token.split("."); //截取token,获取载体
var userinfo = JSON.parse(decodeURIComponent(escape(window.atob(strings[1].replace(
/-/g, "+").replace(/_/g, "/"))))); //解析,需要吧‘_’,'-'进行转换否则会无法解析
- 如果你要这样子去使用,直接是报错的

10:59:34.382 联机调试并非打包,调试基座 HBuilder 是默认的测试包,权限、图标都不可自定义。只有在点菜单"发行-发行为原生安装包"时才能自定义这些设置
10:59:34.404 iOS9.0及以上系统需要在"设置"-"通用"-"设备管理"(或"描述文件")中信任DCloud企业证书(Digital Heaven开头的证书)才可以正常使用
10:59:34.428 如手机上HBuilder调试基座未启动,请手动启动。如应用未更新,请在手机上杀掉基座进程重启
11:01:05.950 [JS Framework] Failed to execute the callback function:
11:01:05.975 TypeError: undefined is not an object (evaluating 'window.atob') __ERROR
- 直接挂了,所以我们不能使用pc端的方式来解析token
2. 我们现在看看如何正确的使用
2.1 下载依赖
npm install jsrsasign
2.2. 封装一个公共方法
import jsrsasign from 'jsrsasign'
export const decodeToken = (token) => {
let obj = null
if (token !== '') {
const payload = jsrsasign.KJUR.jws.JWS.parse(token)
if (payload.hasOwnProperty('payloadObj')) {
obj = payload.payloadObj
}
}
return obj
}
2.3. 导入,使用
import {
decodeToken} from '@/utils/jsrsasign.js'
onShow() {
let token = uni.getStorageSync('token')
// 解析token获取当前账号的权限数组
let userinfo = decodeToken(uni.getStorageSync('token'))
this.init_data = JSON.parse(userinfo.data)
// console.log(this.init_data, "解析数据")
this.init()
},

3.搞定,也不报错了!!
边栏推荐
- Experiment 4 uses fdisk to manage hard disk
- fastjson中@jsonType注解的功能简介说明
- Pycharm uses CONDA to call the remote server
- 判断字符串是不是回文
- Take you to wechat applet development in 3 minutes
- The blind box of super primitive series will be launched soon, and platofarm will enable more rights and interests
- Plato farm - a farm meta universe game with Plato as the goal
- Introduction to thresholdfilter
- [ESP32][esp-idf][LVGL7.9] 使用oled iic编译失败
- TimeBasedRollingPolicy简介说明
猜你喜欢

Net 3 lines of code to realize the function of text to speech

医药行业数字化建设,箭在弦上

学个锤子 | .Net零基础逆向教程 第三课(壳与作业)

二分、三分、01分数规划【第III弹】

房地产数字化转型方案:全方位数智化系统运营,助力房企管控实效提升

How to get more marks in the game under the new economic model of Plato farm

Installing MySQL for Linux operating system (centos7)

博弈论 1.Introduction(组合游戏基本概念、对抗搜索、Bash游戏、Nim游戏)

每天在岗不足8小时被辞?腾讯前员工追讨1300万加班费等,法院终审获赔9万

Can multithreading optimize program performance?
随机推荐
Symbolic operation of MATLAB
Seektiger eco pass STI new progress, log in to ZB on April 14
How to get more marks in the game under the new economic model of Plato farm
居家健康诊断时代下,Senzo打造增强侧向流测试产品
在Plato Farm新经济模型下,如何在游戏中获取更多MARK
How PHP gets the interface
How to learn so many conceptual things in database? Seeking method
Basic knowledge of redis
DAO社区的胜利,Tiger DAO VC胜在治理与共识
判断字符串是不是回文
老板:公司系统太多,能不能实现账号互通?
Time series analysis 41 - time series prediction tbats model
JS提升:实现flat平铺的底层原理
PHP connection MySQL native code
pkg打包node工程
Plato farm - a farm meta universe game with Plato as the goal
[collection] linear algebra let me think - Summary of chapter topics
设计一个支持百万用户的系统
2021.07.13 我们是这样崩的
[ESP32][esp-idf][LVGL7.9] 使用oled iic编译失败