|
|
/*
|
|
|
* @Author: ch
|
|
|
* @Date: 2022-03-17 17:42:32
|
|
|
* @LastEditors: ch
|
|
|
* @LastEditTime: 2022-04-12 09:19:05
|
|
|
* @Description: 项目接口请求统一处理器,返回一个需要token和不需要token的请求封装方法
|
|
|
*/
|
|
|
|
|
|
import MsbUniRequest from '@/common/plugins/msbUniRequest';
|
|
|
import $store from '@/common/store';
|
|
|
|
|
|
const ENV = 'test';
|
|
|
const BASE_URL = {
|
|
|
// 'test' : 'http://39.103.236.147/api',
|
|
|
'test' : 'http://k8s-horse-gateway.mashibing.cn',
|
|
|
'release' : '',
|
|
|
'prod' : ''
|
|
|
};
|
|
|
const successIntercept = (response) =>{
|
|
|
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;
|
|
|
}
|
|
|
const errorIntercept = (error) =>{
|
|
|
return {message:error.errMsg,code:error.statusCode}
|
|
|
}
|
|
|
// 不需要token的接口封装
|
|
|
const MsbRequest = new MsbUniRequest();
|
|
|
MsbRequest.baseUrl = BASE_URL[ENV];
|
|
|
|
|
|
|
|
|
MsbRequest.use('request', (option) => {
|
|
|
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){
|
|
|
const pages = getCurrentPages();
|
|
|
const page = pages[pages.length - 1];
|
|
|
page.$Router.replace({path : '/login',query:{h : page.__page__.path}})
|
|
|
return Promise.reject({message:'要先登录才能操作哦~'});
|
|
|
}else{
|
|
|
option.header = {...option.header, Authorization:$store.state.token}
|
|
|
return option;
|
|
|
}
|
|
|
|
|
|
})
|
|
|
MsbRequestTk.use('success', successIntercept);
|
|
|
MsbRequestTk.use('error', errorIntercept);
|
|
|
export {
|
|
|
MsbRequest,
|
|
|
MsbRequestTk
|
|
|
}
|
|
|
|