Files
GX.O3.WMS/APK/APP_Report/common/requestx.js
T

102 lines
2.5 KiB
JavaScript
Executable File

import axios from '@/js_sdk/gangdiedao-uni-axios'
import global from '@/common/global.js'
//axios.default.baseURL = '/api'
// 创建自定义接口服务实例
const servicex = axios.create({
//baseURL: '',
timeout: 30000, // 不可超过 manifest.json 中配置 networkTimeout的超时时间
withCredentials: true,
headers: {
// 'Content-Type': 'application/json',
'Content-Type': 'application/x-www-form-urlencoded',
},
})
// 拦截器 在请求之前拦截
servicex.interceptors.request.use(config => {
config.baseURL = global.globalData.requestUrl;
let token = uni.getStorageSync('token');
if (token) {
config.headers.Authorization = "Bearer " + token;
}
console.log("config", config);
return config
})
// 响应后的拦截
servicex.interceptors.response.use(response => {
let returnType;
if (response.config.url) {
returnType = global.getReturnType(response.config.url);
global.deleteReturnType(response.config.url);
}
if (!returnType) {
returnType = "Data";
}
console.log("response", response || 'Error');
let data = response.data;
if (data.ErrCode == 0||data.StatusCode == 200) {
console.log("data.Data", data.Data);
// if (response.config.url=="http://192.168.0.3:3003/feeder/GetProductionLines"){
// return data;
// } else {
if (returnType) {
if (returnType == "all") {
return data;
}
return data[returnType];
}
return data.Data;
// }
}
else {
return Promise.reject(new Error(response.message||data.Data||data.Message))
}
}, error => {
if (error.config && error.config.url) {
global.deleteReturnType(error.config.url);
}
if (error && error.response && error.response.status) {
switch (error.response.status) {
case 401: //认证失败,清除token,重新登录
uni.setStorageSync('token', '');
uni.redirectTo({
url: '/pages/index/index'
});
break;
}
}
return Promise.reject(error.message);
})
servicex.localPost = function(url, parmas, returnType){
let myType = "data"
if (url) {
let myUrl = global.globalData.requestUrl + url;
if (returnType) {
myType = returnType;
}
global.setReturnType(myUrl, myType);
return servicex.post(myUrl, parmas);
}
}
servicex.localGet = function(url, parmas, returnType){
let myType = "data"
if (url) {
let myUrl = global.globalData.requestUrl + url;
if (returnType) {
myType = returnType;
}
global.setReturnType(myUrl, myType);
return servicex.get(myUrl, parmas);
}
}
export default servicex