当前位置:网站首页>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.搞定,也不报错了!!
边栏推荐
- Winform 生成随机验证码
- Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
- include 与 require include_once 与 require_once 的区别
- Which strings will be resolved to null by fastjason?
- How PHP gets the interface
- Symbolic operation of MATLAB
- Installing MySQL for Linux operating system (centos7)
- Use xposed to crack the software
- Edge team explains how to improve the comprehensive performance experience through disk cache compression technology
- ASP.NET Core 6框架揭秘实例演示[29]:搭建文件服务器
猜你喜欢
Edge团队详解如何通过磁盘缓存压缩技术提升综合性能体验

Flink - checkpoint Failure reason: Not all required tasks are currently running

设计一个支持百万用户的系统

JS promotion: the underlying principle of flat tiling

2021.07.13 我们是这样崩的

OSS direct upload rails service practice

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

Standing on the shoulders of big men, you can see further
![[ESP32][esp-idf][LVGL7.9] 使用oled iic编译失败](/img/16/e5aa43df6ef9bdbc173fa547c85559.png)
[ESP32][esp-idf][LVGL7.9] 使用oled iic编译失败

深度学习必懂的 13 种概率分布
随机推荐
Can multithreading optimize program performance?
Branches and loops (1)
Symbolic operation of MATLAB
【MySQL】Got an error reading communication packets
TimeBasedRollingPolicy简介说明
Fixedwindowrollingpolicy introduction
PHP 获取接口的方式
7.27 minimum spanning tree phased test problem solution
Domain events and integration events are not so big
博弈论 1.Introduction(组合游戏基本概念、对抗搜索、Bash游戏、Nim游戏)
MySQL master-slave architecture. After the master database is suspended and restarted, how can the slave database automatically connect to the master database
3 minutes to tell you how to become a hacker | zero foundation to hacker getting started guide, you only need to master these five abilities
Seektiger eco pass STI new progress, log in to ZB on April 14
【MySQL】MySQL错误“ERROR 2006 (HY000):MySQL server has gone away”
Seeing clearly is more important than walking fast, because you can go far only when you walk right
Data can't lie. Plato farm is the leader of the meta universe
一文读懂Plato Farm的ePLATO,以及其高溢价缘由
Openatom openharmony sub forum, see you today at 14:00! Wonderful release of memorabilia attached
Introduction to thresholdfilter
备受关注的Bit.Store,最新动态一览