Files
entityControl_mobile/api/request.js

223 lines
6.2 KiB
JavaScript
Raw Normal View History

2026-03-06 16:50:46 +08:00
//存放主站域名
// const BASE_URL = 'http://192.168.1.251:8084' // 测试环境接口单独自动盘点
// const BASE_URL = 'http://101.132.133.142:18086' // 正式环境接口
// const BASE_URL = 'http://192.168.1.251:8086' // 测试环境接口
// const BASE_URL = 'http://192.168.1.13:8087' // 雨欣接口
const BASE_URL = 'http://192.168.1.5:8087' // 温接口
const BASE_URL_IMG = BASE_URL + "/img/upload"; // 上传图片
// 存储请求记录
let requestRecords = {};
// 重复请求拦截时间(毫秒)
const INTERCEPT_DURATION = 2000;
const request = (url, data = {}, method = "GET", ContentType = "application/json") => {
const requestObj = {
data,
url,
time: Date.now(),
};
if (method !== "GET") {
if (Object.keys(requestRecords).length == 0) {
requestRecords = requestObj;
} else {
const s_url = requestRecords.url; // 请求地址
const s_data = requestRecords.data; // 请求数据
const s_time = requestRecords.time; // 请求时间
if (
s_data === requestObj.data &&
requestObj.time - s_time < INTERCEPT_DURATION &&
s_url === requestObj.url
) {
uni.showToast({
title: "数据正在处理,请勿重复提交",
icon: "none",
duration: 2000,
});
return;
}
requestRecords = requestObj;
}
}
return new Promise(function (resolve, reject) {
let header = {};
if (uni.getStorageSync("token")) {
header = {
"Content-Type": ContentType,
Authorization: uni.getStorageSync("token"),
};
} else {
header = {
"Content-Type": ContentType,
};
}
if (Object.keys(data).length && !data.showLoading) {
uni.showLoading({
title: "加载中",
mask: true,
});
}
console.log("请求参数", BASE_URL);
uni.request({
url: BASE_URL + url,
data,
method,
header,
success: function (res) {
// console.log("res", res);
if (res.data.code == 200) {
resolve(res.data);
} else if (res.data.code == 401) {
uni.navigateTo({
url: "/pages/login/login",
});
} else {
if (Object.keys(res.data).length && !data.showLoading) {
uni.showToast({
title: res.data.msg,
icon: "none",
duration: 2000,
});
}
reject(res);
}
},
fail: function (err) {
console.log("err", err);
uni.getNetworkType({
success: function (res) {
console.log("当前网络状态:", res.networkType);
if (res.networkType === "none") {
console.log("当前无网络");
uni.showToast({
title: "当前网络不可用,请检查网络连接",
icon: "none",
});
return;
} else {
uni.showToast({
title: "加载失败,请稍后重试!",
icon: "none",
duration: 2000,
});
}
},
});
reject(err);
},
complete: function () {
// console.log("结束");
if (!data.showLoading) {
uni.hideLoading();
}
},
});
});
};
// 图片
const Upload = (url, source, formData) => {
return new Promise(function (resolve, reject) {
let header = {};
if (uni.getStorageSync("token")) {
header = {
// 'Content-Type': ContentType,
authorization: uni.getStorageSync("token"),
};
}
uni.uploadFile({
url: BASE_URL + url,
filePath: source,
name: 'file',
formData,
// name,
header,
success: function (res) {
let obj1 = JSON.parse(res.data);
uni.hideLoading();
if (obj1.code !== 200) {
uni.showToast({
title: obj1.message,
icon: "none",
duration: 2000,
});
} else {
uni.showToast({
title: "上传成功",
icon: "success",
duration: 1000,
});
resolve(obj1)
}
},
fail: function (err) {
console.log(JSON.stringify(err), "失败999");
uni.hideLoading();
uni.showToast({
title: "加载失败, 请稍后再试!",
icon: "none",
duration: 2000,
});
},
complete: function () {},
});
});
};
// 文件上传
const UploadFile = (url, data = {}, source) => {
return new Promise(function (resolve, reject) {
// 返回选定照片的本地文件路径列表tempFilePath可以作为img标签的src属性显示图片
data["name"] = "upload_resource";
let time = Math.floor(new Date().getTime() / 1000);
let sign = "";
var params = Object.keys(data).sort();
for (var ki in params) {
sign +=
(sign.indexOf("=") !== -1 ? "&" : "") +
params[ki] +
"=" +
encodeURIComponent(data[params[ki]]);
}
data["sign"] = md5(md5(sign) + "e8aac119d38cee477e49d0155832b7f4" + time);
data["time"] = time;
uni.uploadFile({
url: BASE_URL + url, //仅为示例,非真实的接口地址
filePath: source.tempFiles[0].path,
name: "upload_resource",
formData: data,
success: function (res) {
res.data = JSON.parse(res.data);
uni.hideLoading();
// uni.showToast({
// title: '图片上传成功',
// icon: 'success',
// duration: 1000
// })
if (data.returnAll) {
// 是否返回所有信息(成功或者失败都返回)
return resolve(res.data);
}
if (res.data.state) {
resolve(res.data);
} else {
uni.showToast({
title: res.data.message,
icon: "none",
});
}
},
fail: function (err) {
uni.hideLoading();
uni.showToast({
title: "加载失败,请退出后重试!",
icon: "none",
duration: 2000,
});
},
complete: function () {},
});
});
};
// form表单
export { BASE_URL, BASE_URL_IMG, request, Upload, UploadFile };