当前位置:网站首页>Uniapp global interception 401 jumps to the login page
Uniapp global interception 401 jumps to the login page
2022-04-22 06:42:00 【Herry brother】
1. Use uniapp When developing applets , Sometimes the status code returned by the back-end interface is 401, That is, not logged in , At this time, we need to intercept 401 And let it jump to the login page , However, in the encapsulated request, it is directly judged that the status code is 401 When it pops up uni.showModal Click OK to jump. The wechat development tool displays normally , But there are many repeated pop ups on the real machine ( If your current page requests 5 Interface , Then there will be 5 A window , You need to click 5 Can't close until ), At this time, our idea is to let him show it only once , The solution is as follows :
// stay main.js Add the following code to
uni.addInterceptor('request', {
success(args) {
let isLogin = uni.getStorageSync('isLogin') //isLogin It is a status saved after you call the login interface successfully , After I call here successfully, it is set to isLogin="yes"
if (args.statusCode == 401 && isLogin == 'yes') {
uni.showModal({
title: " reminder ",
content: ' Login expired , Log back in ',
cancelText: " Cancel ",
confirmText: " determine ",
confirmColor: "#815EC9",
showCancel: false,
success: (res) => {
if (res.confirm) {
uni.redirectTo({
url: '/pages/login/login' // Jump to the landing page
})
} else if (res.cancel) {
}
}
});
// This code is very important , To prevent 401 Then continue to call the interface
uni.setStorageSync('isLogin', 'no')
}
},
fail(err) {
console.log(' Failure ')
}
})
Close test effectively , It's also the best way to write by yourself at present , If I can help you, I'll pay attention to it , Share every bit, every drop , Oh, here you are !!!!
版权声明
本文为[Herry brother]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220556437760.html
边栏推荐
- 小程序自定义原生底部导航
- 报错:In aggregated query without GROUP BY, expression #1 of SELECT list contains nonaggregated column
- Solutions for minors protection in the social industry of digital beauty technology: guarding the "social circle" of minors
- Sort sort
- Shumei technology and surging news jointly released the "network information content security insight report"
- MySQL basics 2
- Automatically add partitions for PostgreSQL tables
- 从零开始学安卓(kotlin)二——Activity
- Functions and methods in Scala, function creation, closure and corrilization
- 2021-07-07
猜你喜欢
随机推荐
uniapp自定义底部导航栏h5有效果小程序无效的解决方案
TCP和UDP区别详解
Pixel 5 5G解锁教程(含解锁BL,安装EdXposed与Root)
Mysql 根据某一列的值 循环添加序号
Flink理论基础
从零开始学安卓(kotlin)二——Activity
指纹支付相关的细节处理
创新实训(七)FBV视图&CBV视图
小程序调用扫描二维码功能并跳转到二维码指定的路径
The display of redis stored data is garbled
Open source database management systems are now more popular than commercial products
如何设置窗口函数中窗口的大小
Automatically add partitions for PostgreSQL tables
Pixel系列无痛升级
创新实训(九)整合
The minors protection solution of digital beauty technology is heavily launched, opening a new era of minors' network escort
Spent four days painstakingly writing all the notes of MySQL, which is very suitable for beginners.
知识点整理:es6
事务不生效之this调用
MYSQL事务之事务隔离级别








