浪花 - 响应拦截器(强制登录)

news/2024/7/9 23:42:50 标签: vue, typescript

1. 配置响应拦截器

import axios from 'axios';

const myAxios = axios.create({
    baseURL: 'http://localhost:8080/api/',
});

myAxios.defaults.withCredentials = true;

// 请求拦截器
myAxios.interceptors.request.use(function (config) {
    // Do something before request is sent
    console.log("发送请求啦...");
    return config;
}, function (error) {
    // Do something with request error
    return Promise.reject(error);
});

// 响应拦截器
myAxios.interceptors.response.use(function (response) {
    // Do something with response data
    console.log("接收到请求啦...");
    return response.data;
}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});

export default myAxios;

2. 强制跳转到登录页面

  • 有些页面只允许用户登录后查看
  • 未登录用户没有权限,后端返回 40100
  • 前端接收到 40100 则跳转到登录页面,要求用户进行登录

// 响应拦截器
myAxios.interceptors.response.use(function (response) {
    // Do something with response data
    console.log("接收到请求啦...");
    // 未登录,强制跳转到登录页
    if (response?.data?.code === 40100) {
        const redirectUrl = window.location.href;
        window.location.href = `/user/login?redirect${redirectUrl}`;
    }
    return response.data;
}, function (error) {
    // Do something with response error
    return Promise.reject(error);
});

export default myAxios;

3. 登录成功跳转到登录之前的页面

  • 记录当前页面,拼接到 url 的 redirect 后面

  • 登录成功,取出 redirect,重定向到 redirect
// 提交登录表单信息
const onSubmit = async (values) => {
  const res = await myAxios.post('/user/login', {
    userAccount: userAccount.value,
    userPassword: userPassword.value,
  })
  console.log(res, "用户登录");
  if (res.code === 0 && res.data) {
    // 返回到之前的页面
    const redirectUrl = route.query?.redirect as string ?? '/';
    window.location.href=redirectUrl;
  } else {
    Toast.fail("登录失败");
  }
};

http://www.niftyadmin.cn/n/5348946.html

相关文章

Unity 责任链模式(实例详解)

文章目录 示例1:游戏事件处理系统示例2:UI消息处理链示例3:游戏内物理碰撞响应链示例4:AI决策链示例5:场景切换责任链示例6:输入命令处理链 责任链模式(Chain of Responsibility)在U…

Ubuntu(22.04):安装VNC

Ubuntu(20.04):安装VNC_ubuntu安装vnc-CSDN博客 Ubuntu20.04上安装VNC与Ubuntu22.04安装VNC略有不同,试了很久才终于成功。 1.在Ubuntu22.04的终端里安装tightvncserver sudo apt install tigervnc-standalone-server 2.在Ubuntu22.04的终端里安装gnome-panel sudo apt inst…

QT + opengl 环境搭建(glfw, glad),创建一个简单窗口

一.下载glfw,glad并编译 1.glfw个人理解就是对底层opengl的一些基本接口的封装,提供了一些渲染物体所需的最低限度的接口。它允许用户创建OpenGL上下文、定义窗口参数以及处理用户输入。glfw的下载地址:Download | GLFW,下载完成后…

Neo4j 国内镜像下载与安装

Neo4j 5.x 简体中文版指南 社区版:https://neo4j.com/download-center/#community 链接地址(Linux版):https://neo4j.com/artifact.php?nameneo4j-community-3.5.13-unix.tar.gz 链接地址(Windows)&#x…

快速搭建一个基于MVC架构的Spring Boot应用

提示:如果对 MVC 架构模式不熟悉可以看我的博客 > MVC架构模式与三层架构 快速搭建一个基于MVC架构的Spring Boot应用 一、Web 服务二、快速构建一个Spring Web MVC的 Web 应用1.使用脚手架快速的搭建环境:2.准备数据库:3.编写Dao层访问数…

Unity 解释器模式(实例详解)

文章目录 示例1:基础解释器结构示例2:小于表达式(LessThanExpression)示例3:逻辑或表达式(OrExpression)示例4:逻辑非表达式(NotExpression)示例5&#xff1a…

关于SQLite 的下载与使用。配合python

win系统下: SQLite Download Page Precompiled Binaries for Windows sqlite-tools-win-x64-3450000.zip (4.77 MiB) 解压后,找个位置。然后设置环境变量指定位置。 可以手动建立.db文件。 也可以通过代码建立: 如下代码就是建立一个db文件。…

Ubuntu 下进行系统备份与迁移

经常一个项目做到一半,结果系统崩溃了,每次都得重装系统,因此,就在想怎么才能够直接在ubuntu系统备份现有的系统呢?找了很多教程,发现都需要安装软件,这些方法都比较复杂,后来终于找到一种简单…