axios拦截器只弹一次el-message实现方法

本文介绍了一种在多人共用账号的情况下,检测并优雅处理异地登录的技术方案。通过自定义消息组件,确保仅在首次检测到异地登录时,向用户发送警告信息,避免重复通知。此方法适用于前后端分离的现代Web应用。

场景说明

同一个账号,两个人使用,第二个人登录成功后,当第一个人再次操作界面时,所有接口返回特定的102码,这时前端知道这个账号异地登录了,现在要做的是让前端给第一个人一个提示,告诉他他的账号被别人使用了。

错误尝试

第一反应是在axios拦截器内实现message弹出操作,实际情况是,第一个人触发102码的时候并不是每次只有一个接口请求,可能会有好几个axios同时请求得到102码,这个时候就会出现下面的场景:

在这里插入图片描述

错误代码(简写)

import axios from 'axios'
import {Message} from 'element-ui'
const http = axios.create({
	timeout: 1000 * 60 * 2,
	withCredentials: true
})
// 当为post请求时设置其Content-Type为application/x-www-form-urlencoded
http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
// 响应拦截
http.interceptors.response.use(res => {
	if (res.data.code === 102) {
		Message({
			message: '当前账号已经在其他客户端登录',
			type: 'warning',
		})
		// 这里执行清除token和跳转到登录页等操作)

解决办法

如何解决弹窗多次弹出的问题呢?实际情况下我们只希望它弹一次!

新建一个messageOnce.js文件,如下:

import {Message} from 'element-ui'
// 私有属性,只在当前文件可用
const showMessage = Symbol('showMessage')
export default class domMessage {
	success (options, single = true) {
		this[showMessage]('success', options, single)
	}
	warning(options, single = true) {
		this[showMessage]('warning', options, single)
	}
	info(options, single = true) {
		this[showMessage]('info', options, single)
	}
	error(options, single = true) {
		this[showMessage]('error', options, single)
	}
	[showMessage] (type, options, single) {
		if (single) {
			// 关键代码,判断当前页是否有el-message标签,如果没有则执行弹窗操作
			if (document.getElementsByClassName('el-message').length === 0) {
				Message[type](options)
			}
		} else {
			Message[type](options)
		}
	}
}

在处理响应拦截的文件中引入messageOnce.js,如下:

import axios from 'axios'
import domMessage from './messageOnce'

// new 对象实例
const messageOnce = new domMessage()
const http = axios.create({
	timeout: 1000 * 60 * 2,
	withCredentials: true
})
// 当为post请求时设置其Content-Type为application/x-www-form-urlencoded
http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
// 响应拦截
http.interceptors.response.use(res => {
	if (res.data.code === 102) {
		messageOnce.warning({
			message: '当前账号已经在其他客户端登录',
			type: 'warning'
		})
		// 这里执行清除token和跳转到登录页等操作)

总结

一开始打算使用防抖函数来处理,发现没法解决这个问题,后来向朋友请教,提供了这个思路给我,很受用。如果觉得文章不错,点个赞再走吧!

<!-- 添加用户--> <el-dialog :title="title" v-model="openAddUser" width="90%" append-to-body fullscreen> <el-form :model="queryEmp" ref="queryDetRef" :inline="true" label-width="150px"> <el-form-item label="用户" prop="EMP_NAME_"> <el-input v-model="queryEmp.EMP_NAME_" placeholder="请输入用户名称" clearable style="width: 240px" @keyup.enter="handleQueryEmp" /> </el-form-item> <el-form-item> <el-button type="primary" icon="Search" @click="handleQueryEmp">搜索</el-button> <el-button icon="Refresh" @click="resetQueryDet">重置</el-button> </el-form-item> </el-form> <!-- 使用 el-row 和 el-col 实现左右布局 --> <el-row :gutter="20"> <!-- 左侧表格 --> <el-col :span="12"> <el-card> <el-table v-loading="loading" :data="empListLeft"> <el-table-column type="selection" width="50" align="center"/> <el-table-column label="用户编号" align="center" key="EMP_ID_" prop="EMP_CODE_"/> <el-table-column label="用户名称" align="center" key="userName" prop="EMP_NAME_"/> <el-table-column label="机构名称" align="center" key="ORG_NAME_" prop="ORG_NAME_"/> <el-table-column label="操作" align="center" class-name="small-padding fixed-width"> <template #default="scope"> <el-button link type="primary" @click="handleAddEmp(scope.row)">添加</el-button> </template> </el-table-column> </el-table> </el-card> </el-col> <!-- 右侧表格 --> <el-col :span="12"> <el-card> <el-table v-loading="loading" :data="empListRight"> <el-table-column type="selection" width="50" align="center"/> <el-table-column label="用户编号" align="center" key="EMP_ID_" prop="EMP_CODE_"/> <el-table-column label="用户名称" align="center" key="
03-11
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值