/* * @Author: ch * @Date: 2022-03-17 17:42:32 * @LastEditors: ch * @LastEditTime: 2022-03-28 18:15:37 * @Description: 项目接口请求统一处理器,返回一个需要token和不需要token的请求封装方法 */ const { default: MsbUniRequest } = require("../plugins/msbUniRequest"); const ENV = 'test'; const BASE_URL = { // 'test' : 'http://39.103.236.147/api', 'test' : '', 'release' : '', 'prod' : '' }; const successIntercept = (response) =>{ console.log(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) => { return option; }) Request.use('success', successIntercept); Request.use('error', errorIntercept); // 需要token的接口 const RequestTk = new MsbUniRequest(); RequestTk.baseUrl = BASE_URL[ENV]; Request.use('request', (option) => { return option; }) Request.use('success', successIntercept); Request.use('error', errorIntercept); export { // 不需要Token的接口请求用这个实例 Request, // 需要token的请求 RequestTk }