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

73 lines
1.7 KiB

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