当前位置:网站首页>封装API,request拦截,response拦截,身份验证时效性
封装API,request拦截,response拦截,身份验证时效性
2022-07-17 08:54:00 【~牧马~】
封装API
API封装,新建一个.js文件
import axios from 'axios'
// var baseurl = 'http://***.***.**.**:8080'
var baseurl = sessionStorage.getItem('baseurl_4')
// var token = sessionStorage.getItem('token')
// 设置预警:post方法
export const PostWarn = function PostWarn(params) {
return new Promise((resolve, reject) => {
axios.post(baseurl+'/user/user_url_api/',params).then(res => {
resolve(res)
}).catch(err => {
console.log(err);
reject(err)
})
})
}
request拦截,response拦截,身份验证时效性
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import vant from 'vant'
import 'vant/lib/index.css';
import {
Dialog, Toast } from "vant";
Vue.config.productionTip = false
Vue.use(vant)
Vue.prototype.$http = axios
sessionStorage.setItem('baseurl_3','http://192.168.**.**:8090') // 登录
sessionStorage.setItem('baseurl_4','http://192.168.**.**:8080') // 注册
const base_url = ''
axios.defaults.baseURL = base_url
// axios.defaults.timeout = 1000 * 10
localStorage.setItem('base_url', base_url)
axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8'
// request拦截器,用于加token
axios.interceptors.request.use( config => {
let token = sessionStorage.getItem('token');
if(token) {
//request 拦截给所有的请求头加token
config.headers.common['Authorization'] = token
}
return config
}),error => {
Promise.reject(error)
}
// 响应拦截器
axios.interceptors.response.use(res => {
// 接口响应的情况有两种:res.data.code 和 res.status
const code = res.data.code
const status = res.status;
//获取错误信息
const msg = res.data.msg || res.error || res.data.res;
// 获取错误信息
const rec = res.data.code;
console.log(rec,msg);
if(code == 500 || status == 500) {
console.log(rec,msg);
return Promise.reject(new Error(res))
}else{
return res
}
},error => {
let {
message } = error;
if(message == 'Network Error') {
message = '接口连接异常'
console.log(message);
Dialog.alert({
message: '接口访问异常,请退出重试'
}).then( () => {
sessionStorage.removeItem('token')
Toast.loading({
message: '跳转中...',
forbidClick: true,
loadingType: 'spinner',
},800);
// setTimeout(() => {
// router.push('/Login');
router.push({
path: '/Login',
query: {
id : '2'}
})
// },1200)
setTimeout(() => {
window.location.reload();
}, 500)
})
}else if(message.includes('Request failed with status code')) {
Dialog.alert({
message: '登录验证时效过期,请重新登录'
}).then( () => {
sessionStorage.removeItem('token')
Toast.loading({
message: '跳转中...',
forbidClick: true,
loadingType: 'spinner',
},800);
// setTimeout(() => {
// router.push('/Login');
router.push({
path: '/Login',
query: {
id : '2'}
})
// },1200)
setTimeout(() => {
window.location.reload();
}, 500)
})
message = '系统接口' + message.substr(message.length - 3) + '异常';
console.log(message);
}
return Promise.reject(error)
})
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
边栏推荐
- Exchange array elements without creating temporary variables
- Distributed transaction reliable message final consistency solution
- VMware中扩展硬盘
- Dynamic memory management
- Solutions to license invalidation caused by MATLAB update
- Solutions to license invalidation caused by MATLAB update
- WPF 3D application building (Foundation)
- mysql 初始化修改密码问题
- LeetCode 0115.不同的子序列
- [face recognition] face recognition based on histogram histogram with matlab code
猜你喜欢

MySQL视图

Convex mirror 3D glass contour scanning

2022T电梯修理考试题库及答案

Basic steps for creating a static library

Graphite thickness measurement

ES6 learning function (strict mode, higher-order function, closure)

Introduction to deep learning exercises sorting in the first week of deep learning

gradle入门笔记

cut,sort,uniq,xargs

Bean的作用域和生命周期
随机推荐
Expanding hard disk in VMWare
OGG中token的使用
Introduction to deep learning exercises sorting in the first week of deep learning
BCG 使用之CBCGPEdit控件
Sorting out of neural network basics exercises in the second week of in-depth study
Collation of exercises of shallow neural networks in the third week of in-depth study
Solutions to license invalidation caused by MATLAB update
New application of arm computing, illegal fishing "catch all"
What is memory overflow
Redis
Difference and connection between structure and Consortium
【人脸识别】基于直方图Histogram实现人脸识别附matlab代码
46. IO model
TP5 wechat withdrawal merchants transfer to change (copying is available)
QR分解求矩阵逆--c工程实现
深度学习第二周Neural Network Basics习题整理
小说里的编程 【连载之十五】元宇宙里月亮弯弯
MySQL视图
Magic Usage of mongodb $symbol +mongo data type
gradle入门笔记