Files
tk-mini-program/components/request.js

98 lines
4.1 KiB
JavaScript
Raw Normal View History

2025-06-09 19:18:38 +08:00
import addPrefixToHeaderIcon from './addPrefixToHeaderIcon.js'
2025-05-15 22:24:39 +08:00
export default function request(urldata) {
2025-05-19 18:34:04 +08:00
const { url, data, method, header, userInfo } = urldata;
2025-08-06 20:48:39 +08:00
// const baseUrl = "http://192.168.1.174:8086/" + url;
const baseUrl = "https://pk.zhukeping.com/"+ url;
2025-07-21 22:10:59 +08:00
// const baseUrl = "http://120.26.251.180:8086/"+ url;
2025-06-19 22:27:00 +08:00
// const baseUrl = "http://192.168.1.218:8086/"+ url;
2025-05-15 22:24:39 +08:00
if (userInfo) {
2025-05-21 22:52:33 +08:00
return new Promise((resolve, reject) => {
uni.getStorage({
key: "userinfo",
success: (res) => {
if (res.data) {
if (res.data.nickName) {
2025-08-05 22:05:56 +08:00
uni.getStorage({
key: "token",
success: (res) => {
console.log("token", res.data);
uni.request({
url: baseUrl,
data: data,
method: method,
header: {
token: res.data,
...header
},
success: function (res) {
console.log("请求成功", res);
resolve(addPrefixToHeaderIcon(res.data));
},
fail: function (res) {
reject(res);
}
});
2025-05-15 22:24:39 +08:00
},
2025-08-05 22:05:56 +08:00
fail: (res) => {}
2025-05-15 22:24:39 +08:00
});
2025-05-21 22:52:33 +08:00
} else {
2025-05-22 22:05:55 +08:00
uni.setStorageSync("lastPage", getCurrentPages()[getCurrentPages().length - 1].route);
2025-05-21 22:52:33 +08:00
uni.reLaunch({ url: "/pages/UserInformation/UserInformation" })
}
2025-05-19 18:34:04 +08:00
} else {
2025-05-22 22:05:55 +08:00
uni.setStorageSync("lastPage", getCurrentPages()[getCurrentPages().length - 1].route);
2025-05-21 22:52:33 +08:00
uni.navigateTo({ url: '/pages/login/login' })
2025-05-15 22:24:39 +08:00
}
2025-05-21 22:52:33 +08:00
},
fail: function (res) {
2025-05-22 22:05:55 +08:00
uni.setStorageSync("lastPage", getCurrentPages()[getCurrentPages().length - 1].route);
uni.navigateTo({ url: '/pages/login/login' })
2025-05-21 22:52:33 +08:00
reject(res);
2025-05-15 22:24:39 +08:00
}
2025-05-21 22:52:33 +08:00
});
2025-05-19 18:34:04 +08:00
});
} else {
return new Promise((resolve, reject) => {
2025-08-05 22:05:56 +08:00
uni.getStorage({
key: "token",
success: (res) => {
console.log("token", res.data);
uni.request({
url: baseUrl,
data: data,
method: method,
header: {
token: res.data,
...header
},
success: function (res) {
console.log("请求成功", res);
resolve(addPrefixToHeaderIcon(res.data));
},
fail: function (res) {
reject(res);
}
});
2025-05-15 22:24:39 +08:00
},
2025-08-05 22:05:56 +08:00
fail: (res) => {
uni.request({
url: baseUrl,
data: data,
method: method,
header: {
...header
},
success: function (res) {
console.log("请求成功", res);
resolve(addPrefixToHeaderIcon(res.data));
},
fail: function (res) {
reject(res);
}
});
2025-05-15 22:24:39 +08:00
}
});
});
}
}