|
|
|
|
/*
|
|
|
|
|
* @Author: ch
|
|
|
|
|
* @Date: 2022-03-17 17:42:32
|
|
|
|
|
* @LastEditors: ch
|
|
|
|
|
* @LastEditTime: 2022-04-22 16:37:06
|
|
|
|
|
* @Description: 项目接口请求统一处理器,返回一个需要token和不需要token的请求封装方法
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import MsbUniRequest from '@/common/plugins/msbUniRequest';
|
|
|
|
|
import $store from '@/common/store';
|
|
|
|
|
|
|
|
|
|
const ENV = 'test';
|
|
|
|
|
const BASE_URL = {
|
|
|
|
|
'test' : 'https://k8s-horse-gateway.mashibing.cn',
|
|
|
|
|
// 'test' : '',
|
|
|
|
|
'release' : '',
|
|
|
|
|
'prod' : ''
|
|
|
|
|
};
|
|
|
|
|
/**
|
|
|
|
|
* 接口返回成功结果统一处理
|
|
|
|
|
* @param {*} response
|
|
|
|
|
* @param {*} option
|
|
|
|
|
*/
|
|
|
|
|
const successIntercept = (response, option) =>{
|
|
|
|
|
clearRepeat(option)
|
|
|
|
|
if(response.statusCode === 200){
|
|
|
|
|
const result = response.data;
|
|
|
|
|
if(result.code === 'SUCCESS'){
|
|
|
|
|
return result.data;
|
|
|
|
|
}
|
|
|
|
|
if(result.code === 'TOKEN_EXPIRE'){
|
|
|
|
|
uni.navigateTo({url : '/login'})
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return Promise.reject(result);
|
|
|
|
|
}
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 接口返回错误结果统一处理
|
|
|
|
|
* @param {*} error
|
|
|
|
|
* @param {*} option
|
|
|
|
|
*/
|
|
|
|
|
const errorIntercept = (error, option) =>{
|
|
|
|
|
clearRepeat(option)
|
|
|
|
|
return {message:error.errMsg,code:error.statusCode}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//正在执行的请求标识
|
|
|
|
|
let repeatFlag = [];
|
|
|
|
|
/**
|
|
|
|
|
* 验证是否重复请求,没有则添加一条到标记
|
|
|
|
|
* @param {*} option
|
|
|
|
|
*/
|
|
|
|
|
const repeatVerify = (option)=>{
|
|
|
|
|
let flag = {
|
|
|
|
|
url : option.url,
|
|
|
|
|
method : option.method,
|
|
|
|
|
data : option.data
|
|
|
|
|
}
|
|
|
|
|
if(repeatFlag.includes(JSON.stringify(flag))){
|
|
|
|
|
return Promise.reject({message:'请勿频繁操作'});
|
|
|
|
|
}
|
|
|
|
|
repeatFlag.push(JSON.stringify(flag));
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
/**
|
|
|
|
|
* 清除请求标记
|
|
|
|
|
* @param {*} option
|
|
|
|
|
*/
|
|
|
|
|
const clearRepeat = (option) =>{
|
|
|
|
|
repeatFlag = repeatFlag.filter( i => {
|
|
|
|
|
return i !== JSON.stringify({
|
|
|
|
|
url : option.url,
|
|
|
|
|
method : option.method,
|
|
|
|
|
data : option.data
|
|
|
|
|
})
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 不需要token的接口封装
|
|
|
|
|
const MsbRequest = new MsbUniRequest();
|
|
|
|
|
MsbRequest.baseUrl = BASE_URL[ENV];
|
|
|
|
|
|
|
|
|
|
MsbRequest.use('request', (option) => {
|
|
|
|
|
if(option.header.repeat){
|
|
|
|
|
// 如果当前请求不允许重复调用,则检查重复请求,当前接口有正在请求则不发起请求
|
|
|
|
|
const isRepeatVerify = repeatVerify(option);
|
|
|
|
|
if(isRepeatVerify){
|
|
|
|
|
return isRepeatVerify;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return option;
|
|
|
|
|
})
|
|
|
|
|
MsbRequest.use('success', successIntercept);
|
|
|
|
|
MsbRequest.use('error', errorIntercept);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 需要token的接口封装
|
|
|
|
|
const MsbRequestTk = new MsbUniRequest();
|
|
|
|
|
MsbRequestTk.baseUrl = BASE_URL[ENV];
|
|
|
|
|
|
|
|
|
|
MsbRequestTk.use('request', (option) => {
|
|
|
|
|
const token = $store.state.token;
|
|
|
|
|
if(!token){
|
|
|
|
|
// 登录状态处理,没有token直接跳转至登录
|
|
|
|
|
const pages = getCurrentPages();
|
|
|
|
|
const page = pages[pages.length - 1];
|
|
|
|
|
page.$Router.replace('/login');
|
|
|
|
|
return Promise.reject({message:'要先登录才能操作哦~'});
|
|
|
|
|
}else{
|
|
|
|
|
option.header = {...option.header, Authorization:token};
|
|
|
|
|
|
|
|
|
|
if(option.header.repeat){
|
|
|
|
|
// 如果当前请求不允许重复调用,则检查重复请求,当前接口有正在请求则不发起请求
|
|
|
|
|
const isRepeatVerify = repeatVerify(option);
|
|
|
|
|
if(isRepeatVerify){
|
|
|
|
|
return isRepeatVerify;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return option;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
MsbRequestTk.use('success', successIntercept);
|
|
|
|
|
MsbRequestTk.use('error', errorIntercept);
|
|
|
|
|
|
|
|
|
|
export {
|
|
|
|
|
MsbRequest,
|
|
|
|
|
MsbRequestTk
|
|
|
|
|
}
|
|
|
|
|
|