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

64 lines
1.5 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-03-30 10:22:01
* @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' : '',
'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 Promise.reject({message:error.errMsg,code:error.statusCode})
}
// 不需要token的接口封装
const Request = new MsbUniRequest();
Request.baseUrl = BASE_URL[ENV];
Request.use('request', (option) => {
$store.state.token && (option.header = {...option.header, Authorization:$store.state.token});
return option;
})
Request.use('success', successIntercept);
Request.use('error', errorIntercept);
// 需要token的接口
const RequestTk = new MsbUniRequest();
RequestTk.baseUrl = BASE_URL[ENV];
RequestTk.use('request', (option) => {
return option;
})
RequestTk.use('success', successIntercept);
RequestTk.use('error', errorIntercept);
export {
// 不需要Token的接口请求用这个实例
Request,
// 需要token的请求
RequestTk
}