Commit af0f6fd9 by wenyang

优化H5用支付宝支付时跳转浏览器打开的Token问题

parent 4d691bd4
// import {baseURL,apiURL,cffpURL} from "../environments/environment"; // import {baseURL,apiURL,cffpURL} from "../environments/environment";
// 白名单,不需要携带token就允许被访问的接口 // 白名单,不需要携带token就允许被访问的接口
const whiteApiList = [`/api/authorize/obtainToken`, `/api/authorize/checkToken`]; const whiteApiList = [`/api/authorize/obtainToken`, `/api/authorize/checkToken`];
export const interceptor = () => { export const interceptor = () => {
uni.addInterceptor('request', { uni.addInterceptor('request', {
// 请求拦截器 // 请求拦截器
invoke(args) { invoke(args) {
// console.log('请求拦截器',args); // console.log('请求拦截器',args);
// 加载loading // 加载loading
// uni.showLoading({ // uni.showLoading({
// title: '加载中...' // title: '加载中...'
// }); // });
// 当本地没有token,并且接口地址没在白名单内,需要重新获取token // 当本地没有token,并且接口地址没在白名单内,需要重新获取token
if (!uni.getStorageSync('uni-token') && !whiteApiList.includes(args.url)) { if (!uni.getStorageSync('uni-token') && !whiteApiList.includes(args.url)) {
uni.request({ const params = {
url: '/api/authorize/obtainToken', ticket:'uni-app',
method: 'POST', loginId:null
data: { ticket: 'uni-app' }, }
success: (res) => { let h5userId = uni.getStorageSync('cffp_userId');
if (res.statusCode === 200) { if (h5userId) {
uni.setStorageSync('uni-token', res.data['data']['token']); params.loginId = h5userId;
window.location.href = window.location.href + '?' + 't_reload=' + new Date().getTime(); }
} uni.request({
// console.log(res) url: '/api/authorize/obtainToken',
} method: 'POST',
}) data: params,
// uni.hideLoading(); success: (res) => {
} if (res.statusCode === 200) {
// request 触发前拼接 url uni.setStorageSync('uni-token', res.data['data']['token']);
window.location.href = window.location.href + '?' + 't_reload=' + new Date().getTime();
//设置请求头及token }
args.header = { }
'content-type': args.method === 'POST' ? 'application/json' : 'application/x-www-form-urlencoded', })
'X-Authorization': uni.getStorageSync('uni-token') ? uni.getStorageSync('uni-token') : '', }
// 'Access-Control-Allow-Headers': 'appId', // request 触发前拼接 url
// 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
// 'Access-Control-Max-Age': 86400, //设置请求头及token
} args.header = {
// console.log(args.header) 'content-type': args.method === 'POST' ? 'application/json' : 'application/x-www-form-urlencoded',
}, 'X-Authorization': uni.getStorageSync('uni-token') ? uni.getStorageSync('uni-token') : '',
// 'Access-Control-Allow-Headers': 'appId',
// 响应拦截器,可以对数据进行预处理 // 'Access-Control-Allow-Methods': 'POST, GET, OPTIONS',
success() { // 'Access-Control-Max-Age': 86400,
// uni.hideLoading() }
// console.log(args); // console.log(args.header)
}, },
fail() {
// console.log('interceptor-fail', err) // 响应拦截器,可以对数据进行预处理
// console.log('请求失败') success() {
// uni.hideLoading() // uni.hideLoading()
}, // console.log(args);
complete() { },
// uni.hideLoading() fail() {
} // console.log('interceptor-fail', err)
}) // console.log('请求失败')
} // uni.hideLoading()
},
complete() {
// uni.hideLoading()
}
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment