当前位置:网站首页>Encapsulation API, request interception, response interception, authentication timeliness
Encapsulation API, request interception, response interception, authentication timeliness
2022-07-19 08:59:00 【~Herding horses~】
encapsulation API
API encapsulation , Create a new one .js file
import axios from 'axios'
// var baseurl = 'http://***.***.**.**:8080'
var baseurl = sessionStorage.getItem('baseurl_4')
// var token = sessionStorage.getItem('token')
// Set alert :post Method
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 Intercept ,response Intercept , Timeliness of authentication
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') // Sign in
sessionStorage.setItem('baseurl_4','http://192.168.**.**:8080') // register
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 Interceptor , Used to add token
axios.interceptors.request.use( config => {
let token = sessionStorage.getItem('token');
if(token) {
//request Interception adds token
config.headers.common['Authorization'] = token
}
return config
}),error => {
Promise.reject(error)
}
// Response interceptors
axios.interceptors.response.use(res => {
// There are two kinds of interface response :res.data.code and res.status
const code = res.data.code
const status = res.status;
// Get error messages
const msg = res.data.msg || res.error || res.data.res;
// Get error messages
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 = ' Interface connection exception '
console.log(message);
Dialog.alert({
message: ' Interface access exception , Please exit and try again '
}).then( () => {
sessionStorage.removeItem('token')
Toast.loading({
message: ' Jumping ...',
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: ' Login validation expiration , Please login again '
}).then( () => {
sessionStorage.removeItem('token')
Toast.loading({
message: ' Jumping ...',
forbidClick: true,
loadingType: 'spinner',
},800);
// setTimeout(() => {
// router.push('/Login');
router.push({
path: '/Login',
query: {
id : '2'}
})
// },1200)
setTimeout(() => {
window.location.reload();
}, 500)
})
message = ' system interface ' + message.substr(message.length - 3) + ' abnormal ';
console.log(message);
}
return Promise.reject(error)
})
new Vue({
router,
store,
render: h => h(App)
}).$mount('#app')
边栏推荐
- Scratch reverse order output electronic society graphical programming scratch grade examination level 4 true questions and answers analysis June 2022
- OGG中token的使用
- SharePoint接入简要笔记
- Programming in the novel [serial 14] the moon bends in the yuan universe
- LeetCode 0115.不同的子序列
- 用户/账户/账号的理解
- Distributed transaction best effort notification scheme
- vscode下载历史版本
- QT串口通信
- 动态内存管理
猜你喜欢

shell-笔记

解决ApplicationEventMulticaster not initialized - call ‘refresh‘ before multicasting events异常

Markdown(5):锚链接

一文,教你实现单点登录

SQL优化
![[Hongke] Introduction to genicam protocol](/img/a5/91a1d24a47ea2e3265530778d6fc1f.png)
[Hongke] Introduction to genicam protocol

2022t elevator repair examination question bank and answers

idea按条件断点调试

LabVIEW用了多线程,程序是不是会跑的更快些

Scratch reverse order output electronic society graphical programming scratch grade examination level 4 true questions and answers analysis June 2022
随机推荐
如何在docker里搭建自己的云数据库
LeetCode 0116.填充每个节点的下一个右侧节点指针
小说里的编程 【连载之十三】元宇宙里月亮弯弯
[handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
ARM计算新应用,违规垂钓“一网打尽”
置顶随笔分类
Xgen hair guide history cleared solution
【Port 3000 is already in use,3000端口被占用问题解决方法】
【回归预测】基于粒子滤波实现锂离子电池寿命预测附matlab代码
L1-088 静静的推荐(测试点1)
Dynamic memory management
树状数组
不创建临时变量交换数组元素
Jsp+servlet+mysql案例
认真工作后,发现周围混事的真多
【CTF】pwn2_sctf_2016
Expanding hard disk in VMWare
CPU load of webgl - webgl comparison
Event loop, macro task, micro task
Example description of alternative writing of instanceof