You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shop-app/common/utils/requset.js

134 lines
3.1 KiB

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