当前位置:网站首页>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.搞定,也不报错了!!
边栏推荐
- PlatoFarm几大创新经济模型,给予当下元宇宙市场的启发
- Seektiger eco pass STI new progress, log in to ZB on April 14
- The victory of Dao community, tiger Dao VC wins in governance and consensus
- Database advanced learning notes -- storage functions
- ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
- MySQL master-slave architecture. After the master database is suspended and restarted, how can the slave database automatically connect to the master database
- Use xposed to crack the software
- 能够遍历一个文件夹下的所有文件和子文件夹
- Extreme deflation and perpetual motion machine model will promote the outbreak of platofarm
- 房地产数字化转型方案:全方位数智化系统运营,助力房企管控实效提升
猜你喜欢

NTU Lin Xuantian's "machine learning cornerstone" problem solving and code implementation | [you deserve it]
![[ESP32][esp-idf] esp32s3快速搭建LVGLV7.9](/img/39/8efef047d0a9223b97819a54b5edf8.png)
[ESP32][esp-idf] esp32s3快速搭建LVGLV7.9

TCP Basics

JS提升:实现flat平铺的底层原理

Data can't lie. Plato farm is the leader of the meta universe

C# 读写文件从用户态切到内核态,到底是个什么流程?

The blind box of super primitive series will be launched soon, and platofarm will enable more rights and interests

WPF布局之控件随着窗口等比放大缩小,适应多分辨率满屏填充应用

OSPF的LSA及优化

Installing MySQL for Linux operating system (centos7)
随机推荐
备受关注的Bit.Store,最新动态一览
极致通缩和永动机模型,将推动 PlatoFarm 爆发
In php7?? And?: Differences between
哪些字符串会被FastJson解析为null呢?
Basic knowledge of redis
Introduction to consoleappender
[ESP32][esp-idf][LVGL7.9] 使用oled iic编译失败
PlatoFarm进展不断,接连上线正式版以及推出超级原始人NFT
【MySQL】MySQL错误“ERROR 2006 (HY000):MySQL server has gone away”
ThresholdFilter简介说明
2021.07.13 我们是这样崩的
Have you ever seen this kind of dynamic programming -- the stock problem of state machine dynamic programming (Part 2)
Array method of J S, loop
Weekly report on July 27, 2022
Winform 生成随机验证码
The victory of Dao community, tiger Dao VC wins in governance and consensus
pkg打包node工程
【FPGA教程案例41】图像案例1——通过verilog读取图片
Introduction to thresholdfilter
3分钟带你了解微信小程序开发