当前位置: 首页 > news >正文

elementUI项目中,只弹一个【token过期提示】信息框的处理

关键代码 

let msgArr = document.querySelectorAll('.token401Message')if (!msgArr.length) {Message({customClass: 'token401Message',message: response.data.msg,type: 'error',onClose: () => {msgArr = []}})}

完整代码

import axios from 'axios'
import { getToken } from '@/libs/util'
import { Message, Loading } from 'element-ui'
import store from '@/store'
import router from '@/router'
let loadingInstance = null // 记录页面中存在的loading
let loadingCount = 0 // 记录当前正在请求的数量
function showLoading(data) {if (loadingCount === 0) {loadingInstance = Loading.service({lock: true,background: 'rgba(255,255,255,.6)',text: data || ''})}loadingCount++
}function hideLoading() {let timer = setTimeout(() => {loadingCount--if (loadingInstance && loadingCount === 0) {loadingInstance.close()loadingInstance = nullclearTimeout(timer)timer = null}}, 0)
}
const instance = axios.create({// baseURL: baseUrl,timeout: 0,headers: {'Content-Type': 'application/json; charset=utf-8'},withCredentials: true
})
const no_need_to_refresh_token = ['/xxx', // 双因子登录'/xxx', // 验证码登录'/xxx', // 验证码登录获取验证码'/xxx', // 登录接口'/xxx', // 获取配置'/xxx', // 获取策略'/xxx' // 刷新token
]
// 添加请求拦截器
instance.interceptors.request.use(async config => {if (config.loadingShow) {showLoading()}if (getToken()) {config.headers.Authorization = getToken()config.headers['Tenant-ID'] = localStorage.getItem('tenantId')config.headers['UserId'] = localStorage.getItem('uesrId')// 排除双因子 验证码登录 ....if (no_need_to_refresh_token.every(item => config.url.indexOf(item) === -1)) {await store.dispatch('refreshToken')}}return config},error => {if (error.config.loadingShow) {hideLoading()}// 对请求错误做些什么return Promise.reject(error)}
)instance.interceptors.response.use(async response => {if (response.config.loadingShow) {hideLoading()}// token失效if (response.data.code === '401 UNAUTHORIZED') {router.replace({name: 'login',params: {clear: true}})// 只弹一个【token过期提示】信息框的处理let msgArr = document.querySelectorAll('.token401Message')if (!msgArr.length) {Message({customClass: 'token401Message',message: response.data.msg,type: 'error',onClose: () => {msgArr = []}})}return Promise.reject(new Error(response.data.msg || '请求错误'))}// 接口报错if (response.data.code &&(response.data.code !== 'SUCCESS' && response.data.code !== 'S1A00000') &&response.data.code !== '401 UNAUTHORIZED') {if (!response.request.responseURL.includes('account/verification')) {Message({message: response.data.msg,type: 'error'})return Promise.reject(new Error(response.data.msg || '请求错误'))}}return response},async error => {if (error.config.loadingShow || error === undefined) {hideLoading()}Message({message: error,type: 'error'})// 对响应错误做点什么return Promise.reject(error)}
)const http = {get: (path, data, config) =>instance.get(path, {params: data,...config}),delete: (path, data, config) =>instance.delete(path, {data: data,...config}),post: (path, data, config) => instance.post(path, data, config),put: (path, data, config) => instance.put(path, data, config)
}export default http

http://www.lryc.cn/news/520736.html

相关文章:

  • SpringBoot开发—— SpringBoot中如何实现 HTTP 请求的线程隔离
  • 【LLM】25.1.11 Arxiv LLM论文速递
  • 单片机实物成品-012 酒精监测
  • 使用葡萄城+vue实现Excel
  • 【Uniapp-Vue3】@import导入css样式及scss变量用法与static目录
  • 跟我学C++中级篇——字节序
  • Linux网络编程5——多路IO转接
  • Redis常见
  • 提升 PHP 编码效率的 10 个实用函数
  • 设计模式 行为型 访问者模式(Visitor Pattern)与 常见技术框架应用 解析
  • golang之数据库操作
  • 对话新晋 Apache SeaTunnel Committer:张圣航的开源之路与技术洞察
  • Mac 删除ABC 输入法
  • 《机器学习》之K-means聚类
  • 日常工作之 Elasticsearch 常用查询语句汇总
  • WeakAuras NES Script(lua)
  • JVM 触发类加载的条件有哪些?
  • Android实战经验篇-增加系统分区
  • 深入学习 Python 量化编程
  • 机器学习笔记——特征工程
  • 4种革新性AI Agent工作流设计模式全解析
  • 【入门级】计算机网络学习
  • 安装 Jenkins 后无法访问用户名或密码且忘记这些凭证怎么办?
  • day08_Kafka
  • 安装conda 环境
  • 【dockerros2】ROS2节点通信:docker容器之间/docker容器与宿主机之间
  • 使用外网访问在群晖中搭建思源docker
  • 深度学习中的EMA技术:原理、实现与实验分析
  • win32汇编环境,窗口程序中对按钮控件常用操作的示例
  • CentOS 7.9 通过 yum 安装 Docker