当前位置:网站首页>uniapp全局拦截401跳转到登录页
uniapp全局拦截401跳转到登录页
2022-04-22 05:57:00 【herry-弟弟】
1.使用uniapp开发小程序时,有时候后端接口返回的状态码是401,也就是未登录状态,这时候我们需要拦截401并让它跳到登录页,但是在封装的请求中直接判断状态码为401时弹出uni.showModal点击确定跳转时微信开发工具显示正常,但是真机上就重复弹出了很多个(假如你当前页面请求了5个接口,那就会有5个窗口,你需要点击5下才能关闭),这时我们的想法是只让他显示一次,解决方法如下:
//在main.js中加入以下代码
uni.addInterceptor('request', {
success(args) {
let isLogin = uni.getStorageSync('isLogin') //isLogin是你调用登录接口成功后保存的一个状态,我在这里调用成功后设置成了isLogin="yes"
if (args.statusCode == 401 && isLogin == 'yes') {
uni.showModal({
title: "温馨提示",
content: '登录已过期,重新登录',
cancelText: "取消",
confirmText: "确定",
confirmColor: "#815EC9",
showCancel: false,
success: (res) => {
if (res.confirm) {
uni.redirectTo({
url: '/pages/login/login' //跳到登录页
})
} else if (res.cancel) {
}
}
});
//这句代码很重要,是为了防止401后再继续调用接口
uni.setStorageSync('isLogin', 'no')
}
},
fail(err) {
console.log('失败')
}
})
亲测有效,也是目前自己写的最好的办法,帮到你了就关注点赞哦,分享每一点每一滴,哦里给!!!!
版权声明
本文为[herry-弟弟]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_43123560/article/details/124191692
边栏推荐
- Copu helped Peking University Graduate open source public elective course - Open Source PostgreSQL R & D course successfully enter Peking University
- A set of SQL statements supports both Oracle and MySQL?
- Shumei technology was invited to participate in the content governance label seminar of ICT Institute
- webService接口编写并发布与webService接口的调用(一)
- 数美科技荣获《银行家》“十佳智能风控管理创新奖”
- 使用Navicat 备份mysql数据库
- Mvcc transaction isolation in PostgreSQL
- 滚动条的多种样式
- Iframe子父级传参
- 使用pgBackRest并行归档解决wal堆积问题
猜你喜欢
随机推荐
小程序调用扫描二维码功能并跳转到二维码指定的路径
报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column
TiDB分表唯一主键ID——sequence 与gorm无法获取主键的解决
Pixel系列无痛升级
Escort · the future 𞓜 digital risk control summit of digital America 2022 officially set off
kubectl命令自动补齐
事务不生效之this调用
Promise
COM in Wine(1)——COM基本概念
Uncaught (in promise) NavigationDuplicated: Avoided redundant navigation to current location: “/?k=“
Shumei technology has obtained the international certification of ISO / IEC 27701 privacy information management system
剑指offer:对称的二叉树(递归 迭代leetcode 101)
Pgbackrest practice
Shumei technology was honored as the top 20 of the new enterprise service track of "real 100 innovators" in the interface news
Flink理论基础
POM文件浅析
如何设置窗口函数中窗口的大小
8张图让你一步步看清 async/await 和 promise 的执行顺序
Sort sort
一个三目表达式,引起的空指针









