当前位置:网站首页>Flyio insensitive refresh token
Flyio insensitive refresh token
2022-07-22 09:34:00 【28 hours a day】
npm install flyio
var Fly=require('flyio/dist/npm/wx');
var fly=new Fly();
<!DOCTYPE html>
<html>
<head lang="zh-cmn-Hans">
<meta charset="UTF-8">
<title>Fly.js Demo</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
<meta name="viewport" content="width=device-width,initial-scale=0.5,user-scalable=no"/>
<!--[if lt IE 9]>
<script src="//cdn.bootcss.com/es5-shim/4.5.9/es5-shim.min.js"></script>
<script src="//cdn.bootcss.com/es5-shim/4.5.9/es5-sham.min.js"></script>
<script src="//cdn.bootcss.com/json3/3.3.2/json3.min.js"></script>
<![endif]-->
<!--[if lt IE 10]>
<script src="//cdn.bootcss.com/jquery-placeholder/2.3.1/jquery.placeholder.min.js"></script>
<![endif]-->
<!--<script src="https://cdn.bootcss.com/axios/0.16.2/axios.js"></script>-->
<script src="../dist/fly.js"></script>
</head>
<style>
html {
font-size: 20px;
-webkit-user-select: none;
}
</style>
<body>
<div style="background: teal; font-size: 38px; color: #ffef68; text-shadow: 2px 2px 5px #ffef68; width: 400px; height: 400px; text-align: center">
<div style="padding-top: 100px"> Fly.js</div>
</div>
open console panel to view log.
<script>
//import fly from "../index";
var csrfToken = "";
// Define public headers
fly.config.headers = {"x-tag": "flyio"}
fly.config.baseURL = "http://www.dtworkroom.com/doris/1/2.0.0/"
var newFly = new Fly;
newFly.config = fly.config;
var log = console.log
fly.interceptors.request.use(function (request) {
log(` Initiate request :path:${request.url},baseURL:${request.baseURL}`)
if (!csrfToken) {
log(" No, token, Ask first token...");
// Lock the instance of the day , Subsequent requests are queued outside the interceptor
fly.lock();
return newFly.get("/token").then((d) => {
request.headers["csrfToken"] = csrfToken = d.data.data.token;
log("token The request is successful , The value is : " + d.data.data.token);
log(` Continue to complete the request :path:${request.url},baseURL:${request.baseURL}`)
return request
}).finally(() => fly.unlock()) // After unlocking , The task in the request queue will continue to be initiated
} else {
request.headers["csrfToken"] = csrfToken;
}
})
// response interceptors
fly.interceptors.response.use(
function (response) {
log("interceptors.response", response)
// Validation failure
if (response.data.data.tokenExpired) {
log("token invalid , Re request token...");
this.lock(); // Lock response interceptor
return newFly.get("/token")
.then((d) => {
csrfToken = d.data.data.token;
log("token Updated , The value is : " + csrfToken);
})
.finally(() => this.unlock())
.then(() => {
log(` Re request :path:${response.request.url},baseURL:${response.request.baseURL}`)
return fly.request(response.request);
})
} else {
return response.data.data;
}
},
function (err) {
log("error-interceptor", err)
}
)
fly.get("/test?tag=1")
.then(function (d) {
log(" The request is successful :", d)
}).catch(function (e) {
log(" request was aborted ", e)
})
fly.get("/test?tag=2")
.then(function (d) {
log(" The request is successful :", d)
}).catch(function (e) {
log(" request was aborted ", e)
})
fly.get("/test?tag=3")
.then(function (d) {
log(" The request is successful :", d)
}).catch(function (e) {
log(" request was aborted ", e)
})
</script>
</body>
</html>
new Promise()
import store from '@/store'
import config from './config.js'
import Base64 from './base64.js'
import {
refreshToken
} from '@/api/user'
// H5 edition
// #ifdef H5
import Fly from "flyio/dist/npm/fly"
// #endif
// Wechat applet and APP edition
// #ifndef H5
import Fly from "flyio/dist/npm/wx"
// #endif
const request = new Fly();
let base64 = new Base64();
request.interceptors.request.use(function(request) {
request.baseURL = config.baseURL;
const token = store.state.user.token;
request.headers['Authorization'] = `Basic ${
base64.encode(`${
config.clientId}:${
config.clientSecret}`)}`;
if (token) {
request.headers['Blade-Auth'] = 'bearer ' + token
} else {
request.headers['Tenant-Id'] = config.tenantId
}
if (request.method === 'POST' && request.headers['Content-Type'] !== 'multipart/form-data') {
uni.showLoading({
title: '',
mask: true
});
setTimeout(()=>{
uni.hideLoading()
},800)
request.body = {
...request.body,
_t: Date.parse(new Date()) / 1000
}
} else if (request.method === 'GET') {
request.params = {
_t: Date.parse(new Date()) / 1000,
...request.params
}
}
return request
})
request.interceptors.response.use(function(response) {
console.log(response,"responense")
// uni.hideLoading();
return response.data
}, function(error) {
console.log(error, "***********************************")
// uni.hideLoading();
let pages = getCurrentPages();
console.log(pages)
let route = pages[pages.length-1].route;
if(route.indexOf('registerLogin') != -1) return;
console.log(pages)
if (error.response.data.code == 401) {
// uni.hideLoading();
this.lock();
let _this = this;
setTimeout(()=>{
_this.unlock();
},0)
return store.dispatch('refreshToken')
.then(res => {
console.log(res,"SSSSSSSSSSS")
_this.unlock()
return request.request(error.request);
})
.finally(() => _this.unlock())
.then(() => {
console.log(` Re request :path:${
error.request.url},baseURL:${
error.response.baseURL}`)
return request.request(error.request);
})
// let request = error.request;
// let headers = {}
// headers['Authorization'] =
// `Basic ${base64.encode(`${config.clientId}:${config.clientSecret}`)}`;
// const token = store.state.user.token;
// if (token) {
// headers['Blade-Auth'] = 'bearer ' + token
// } else {
// headers['Tenant-Id'] = config.tenantId
// }
// let data = {...request.body}
// refreshToken(store.state.user.refreshToken, store.state.user.tenantId).then(res => {
// console.log(res, "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA")
// if (res.access_token) {
// store.commit('SET_TOKEN', res.access_token)
// headers['Blade-Auth'] = 'bearer ' + res.access_token
// uni.showLoading({
// mask: true
// })
// uni.request({
// url: request.baseURL + request.url,
// header: headers,
// method: request.method,
// data,
// success: res => {
// console.log(res, " Really? It's done ")
// console.log(store.state.user.token, "0000000000")
// uni.hideLoading();
// if (request.method === 'GET') {
// } else {
// uni.navigateBack({
// delta: 1
// })
// }
// },
// fail: err => {
// console.log(" failed ")
// }
// })
// } else {
// uni.reLaunch({
// url: '/pages/public/registerLogin/registerLogin'
// })
// setTimeout(() => {
// uni.showModal({
// content: ' The current login has expired , Please login again ',
// showCancel: false,
// confirmColor: '#FF6600',
// success: (res) => {
// if (res.confirm) {
// console.log(' The user clicks ok ');
// }
// }
// });
// }, 200)
// }
// })
}
})
export default request
export {
request
}
边栏推荐
猜你喜欢
How much do you know about the benefits of encouraging enterprise knowledge sharing?
Modify the hosts file to customize the local IP domain name
High frequency leetcode deep search part: 98 Validate binary search tree
嵌入式之网络问题总结(网卡丢包、网卡无法识别)
Activity启动(launchActivity/startActivity)_(1)_流程图之WMS
高频leetcode深搜部分:124. 二叉树中的最大路径和
Measured 11 remote video conferencing software, home work can also be as smooth as silk
各地級市-進出口與貿易差額(2000-2020)
The complement of position operation and shaping in C language
Programmers' wisdom and courage in those years!!!
随机推荐
oracle错误一览表
CONDA common function record
Redis原理之BitMap
高频leetcode深搜部分:733. 图像渲染
高频leetcode深搜部分:617. 合并二叉树
File 读写操作(指定文件目录下的文件)
Laravel installs the debugbar toolbar and configures the virtual host
(1) Principle analysis of Tiktok Kwai short video watermarking
MySQL安装配置-8.0版-Windows
飞机大战分析及代码
Array of C language
How much do you know about the benefits of encouraging enterprise knowledge sharing?
uni-app接入友盟统计
conda 常用功能记录
Record of problems encountered
嵌入式之SD卡/U盘只读问题解决方案(FAT只读修复方式)
4000+上市公司所属省份、城市、经营等多指标数据已更新至2022.1
Redis原理之HyperLogLog
嵌入式之SD卡异常问题分析
Review 后台管理系统实战:请求参数的 2 种封装风格