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.8 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

/*
* @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
}