SmartStorage/apis/request.js

184 lines
6.2 KiB
JavaScript
Raw Normal View History

2024-08-23 16:20:04 +08:00
class Http {
2024-09-19 08:30:55 +08:00
get(baseUrl = "", url, data = {}, header = {}) {
return new Promise((resolve, reject) => {
uni.request({
url: baseUrl + url,
data: data,
method: "GET",
dataType: "json",
header: {
"content-type": "application/json",
Authorization: uni.getStorageSync("token"),
...header,
},
// 成功的回调
success(res) {
if (res.data.code == 401) {
uni.showToast({
icon: "none",
title: "登录状态过期,请重新登录!",
success: () => {
uni.reLaunch({
2024-09-23 08:28:02 +08:00
url: "/pages/login/login",
// url: "/pages/nwLogin/index",
2024-09-19 08:30:55 +08:00
});
},
});
} else {
resolve(res);
}
},
fail(err) {
reject(err);
},
});
});
}
2024-08-23 16:20:04 +08:00
2024-09-19 08:30:55 +08:00
post(baseUrl = "", url, data = {}, header = {}) {
return new Promise((resolve, reject) => {
uni.request({
url: baseUrl + url,
data: data,
method: "POST",
dataType: "json",
header: {
"content-type": "application/json",
Authorization: uni.getStorageSync("token"),
...header,
},
// 成功的回调
success(res) {
// console.log(res);
if (res.data.code == 401) {
uni.showToast({
icon: "none",
title: "登录状态过期,请重新登录!",
success: () => {
uni.reLaunch({
2024-09-23 08:28:02 +08:00
url: "/pages/login/login",
// url: "/pages/nwLogin/index",
2024-09-19 08:30:55 +08:00
});
},
});
} else {
resolve(res);
}
},
fail(err) {
// console.log(err);
reject(err);
},
});
});
}
2024-08-23 16:20:04 +08:00
2024-09-19 08:30:55 +08:00
put(baseUrl = "", url, data = {}, header = {}) {
return new Promise((resolve, reject) => {
uni.request({
url: baseUrl + url,
data: data,
method: "PUT",
dataType: "json",
header: {
"content-type": "application/json",
Authorization: uni.getStorageSync("token"),
...header,
},
// 成功的回调
success(res) {
if (res.data.code == 401) {
uni.showToast({
icon: "none",
title: "登录状态过期,请重新登录!",
success: () => {
uni.reLaunch({
2024-09-23 08:28:02 +08:00
url: "/pages/login/login",
// url: "/pages/nwLogin/index",
2024-09-19 08:30:55 +08:00
});
},
});
} else {
resolve(res);
}
},
fail(err) {
reject(err);
},
});
});
}
2024-08-23 16:20:04 +08:00
2024-09-19 08:30:55 +08:00
delete(baseUrl = "", url, data = {}, header = {}) {
return new Promise((resolve, reject) => {
uni.request({
url: baseUrl + url,
data: data,
method: "DELETE",
dataType: "json",
header: {
"content-type": "application/json",
Authorization: uni.getStorageSync("token"),
...header,
},
// 成功的回调
success(res) {
if (res.data.code == 401) {
uni.showToast({
icon: "none",
title: "登录状态过期,请重新登录!",
success: () => {
uni.reLaunch({
2024-09-23 08:28:02 +08:00
url: "/pages/login/login",
// url: "/pages/nwLogin/index",
2024-09-19 08:30:55 +08:00
});
},
});
} else {
resolve(res);
}
},
fail(err) {
reject(err);
},
});
});
}
2024-08-23 16:20:04 +08:00
2024-09-19 08:30:55 +08:00
upload(baseUrl = "", url, data = {}, header = {}) {
return new Promise((resolve, reject) => {
uni.uploadFile({
url: baseUrl + url,
filePath: data,
header: {
Authorization: uni.getStorageSync("token"),
...header,
},
name: "file",
// 成功的回调
success(res) {
if (res.data.code == 401) {
uni.showToast({
icon: "none",
title: "登录状态过期,请重新登录!",
success: () => {
uni.reLaunch({
2024-09-23 08:28:02 +08:00
url: "/pages/login/login",
// url: "/pages/nwLogin/index",
2024-09-19 08:30:55 +08:00
});
},
});
} else {
resolve(res);
}
},
fail(err) {
reject(err);
},
});
});
}
2023-12-20 15:15:23 +08:00
}
2024-09-19 08:30:55 +08:00
export default new Http();