qa-prevention-xgf-app/utils/request.js

138 lines
3.1 KiB
JavaScript
Raw Normal View History

2024-09-03 17:21:31 +08:00
export const requestPath = "http://192.168.0.112:8088";
// const requestPath = 'http://192.168.0.31:8992/qa-prevention-xgf/'; // 后台请求地址
// const requestPath = 'https://skqhdg.porthebei.com:9006/qa-prevention-xgf/'; // 后台请求地址
// const requestPath = 'https://qgxgf.qhdsafety.com/qa-prevention-xgf/'; // 外网地址
2024-08-08 11:08:55 +08:00
import store from "../store/index";
2023-11-07 10:24:08 +08:00
function post(url, data) {
2024-08-08 11:08:55 +08:00
return new Promise((resolve, reject) => {
if (data && data.loading !== false) {
uni.showLoading({
title: "加载中",
});
}
uni.request({
url: requestPath + url,
data: {
USER_ID: store.state.userInfo.USER_ID || "",
...data,
},
method: "POST",
header: {
"Content-type": "application/x-www-form-urlencoded",
},
success: (res) => {
if (res.statusCode !== 200) {
2024-08-08 11:08:55 +08:00
uni.showToast({
title: "网络错误请重试," + res.statusCode,
icon: "error",
duration: 2000,
});
reject(err);
2023-11-07 10:24:08 +08:00
}
2024-08-08 11:08:55 +08:00
if (data && data.loading !== false) {
uni.hideLoading();
}
if (res.data.result === "success") {
resolve(res.data);
} else {
uni.showToast({
title: res.data.msg || "系统开小差了",
icon: "none",
duration: 2000,
});
reject(res.data);
}
},
fail: (err) => {
if (data && data.loading !== false) {
uni.hideLoading();
}
uni.showToast({
title: "网络错误请重试",
icon: "none",
duration: 2000,
});
reject(err);
},
});
});
2023-11-07 10:24:08 +08:00
}
function upload(url, data) {
2024-08-08 11:08:55 +08:00
return new Promise((resolve, reject) => {
if (data && data.loading !== false) {
uni.showLoading({
title: "加载中",
});
}
uni.uploadFile({
url: requestPath + url,
filePath: data.filePath,
name: data.name || "file",
formData: data.formData || {},
success: (res) => {
uni.hideLoading();
if (JSON.parse(res.data).result === "success") {
resolve(JSON.parse(res.data));
} else {
uni.showToast({
title: JSON.parse(res.data).msg || "系统开小差了",
icon: "none",
duration: 2000,
});
reject(JSON.parse(res.data));
}
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: "网络错误请重试",
icon: "none",
duration: 2000,
});
reject(err);
},
});
});
2023-11-07 10:24:08 +08:00
}
function uploads(url, data) {
2024-08-08 11:08:55 +08:00
return new Promise((resolve, reject) => {
if (data && data.loading !== false) {
uni.showLoading({
title: "加载中",
});
}
uni.uploadFile({
url: requestPath + url,
files: data.files,
formData: data.formData || {},
success: (res) => {
uni.hideLoading();
if (JSON.parse(res.data).result === "success") {
resolve(JSON.parse(res.data));
} else {
uni.showToast({
title: JSON.parse(res.data).msg || "系统开小差了",
icon: "none",
duration: 2000,
});
reject(JSON.parse(res.data));
}
},
fail: (err) => {
uni.hideLoading();
uni.showToast({
title: "网络错误请重试",
icon: "none",
duration: 2000,
});
reject(err);
},
});
});
2023-11-07 10:24:08 +08:00
}
2024-08-08 11:08:55 +08:00
export { post, upload, uploads };