let baseUrl = "https://wcweb.gigadevice.com"; //用于拼接之后请求的url
let bUrl = "https://wcweb.gigadevice.com";
if (['adminui2.bju-pro.com','zhaoyi.test3.yongsy.net'].includes(locations.host)) {
baseUrl = "https://zycxweb.bju-pro.com"; //用于拼接之后请求的url
bUrl = "https://zhaoyi.test3.yongsy.net"
}
export default (params) => {
const { url, data, method = 'GET', type='wc' } = params;
let requestData = data;
let headers = {
'X-TOKEN': $.cookie('token'), // 自定义请求头
'X-LANG': 'zh', // 中文zh,英文en
"Content-Type": "application/json;charset=utf8",
};
let dataType = "json";
let newUrl = baseUrl+url;
if(type === 'wc') {
// 根据请求方法处理数据
requestData = method === 'GET' ? data : JSON.stringify(data);
}
if(type === 'yongxi') {
newUrl = bUrl+url;
headers = {
"Content-Type": "application/x-www-form-urlencoded;charset=utf8",
};
// 根据请求方法处理数据
requestData = data;
}
return new Promise((resolve, reject) => {
$.ajax({
url: newUrl,
type: method,
async: true, // 始终异步
dataType,
data: requestData,
headers,
success: function (ret) {
if (ret.code === 200) {
resolve(ret);
} else if(ret.code === 401) {
// 处理未登录
setTimeout(() => {
$.removeCookie('token', { path: '/' });
$.removeCookie('loginEmail', { path: '/' });
$.removeCookie('userName', { path: '/' });
$.removeCookie('token');
$.removeCookie('loginEmail');
window.locations.href = '/mygd/login.html';
}, 1500);
reject(new Error('请去登录!'));
} else {
resolve(ret);
// reject(new Error('服务器返回错误代码: ' + ret.code));
}
},
error: function (jqXHR, textStatus, errorThrown) {
// 处理请求错误
if (jqXHR.status === 0) {
// 通常表示网络错误或CORS问题
reject(new Error('网络错误或CORS问题'));
} else {
// 其他HTTP错误
reject(new Error('HTTP错误: ' + jqXHR.status + ' - ' + errorThrown));
}
},
});
});
}