php请求增加未登录跳转

This commit is contained in:
2025-12-04 10:15:00 +08:00
parent 1b58014462
commit be7ae6ff52

View File

@@ -1,54 +1,68 @@
//服务器接口地址 //服务器接口地址
// const baseURL : string = 'https://newblockwlx.sxczgkj.cn/index.php/api/' // const baseURL : string = 'https://newblockwlx.sxczgkj.cn/index.php/api/'
let baseURL : string = 'http://192.168.1.42:8787/api/' let baseURL: string = "http://192.168.1.42:8787/api/";
// #ifdef H5 // #ifdef H5
baseURL='/phpapi/api/' baseURL = "/phpapi/api/";
// #endif // #endif
import go from "@/commons/utils/go.js";
// 封装公共请求方法 // 封装公共请求方法
function request(url : string, method : "GET" | "POST" | undefined, data : object | any, toast : boolean) { function request(
let networkType = '' url: string,
method: "GET" | "POST" | undefined,
data: object | any,
toast: boolean
) {
let networkType = "";
uni.getNetworkType({ uni.getNetworkType({
success: (res) => { success: (res) => {
networkType = res.networkType networkType = res.networkType;
} },
}); });
if (networkType == 'none') { if (networkType == "none") {
uni.showToast({ uni.showToast({
title: '网络异常,请检查网络', title: "网络异常,请检查网络",
icon: 'none' icon: "none",
}) });
return false; return false;
} }
if (toast) { if (toast) {
uni.showLoading({ uni.showLoading({
title: '加载中', title: "加载中",
mask: true mask: true,
}) });
} }
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
let header : any let header: any;
header = { header = {
'content-type': 'application/json', "content-type": "application/json",
'clinttype':uni.getStorageSync('clint_type'), clinttype: uni.getStorageSync("clint_type"),
'bausertoken': uni.getStorageSync('phpuserinfo').token||'', bausertoken: uni.getStorageSync("phpuserinfo").token || "",
'token': uni.getStorageSync('iToken').tokenValue||'', token: uni.getStorageSync("iToken").tokenValue || "",
}; };
uni.request({ uni.request({
url: baseURL + url, url: baseURL + url,
method: method, method: method,
data: data, data: data,
header: header, header: header,
success(res : any) { success(res: any) {
if (res.data.code != 1) { if (res.data.code != 1) {
if (res.data.code === 3000) {
uni.hideLoading();
go.to("PAGES_LOGIN", {}, "redirect");
reject();
return;
}
//是否提示错误 //是否提示错误
if (toast) { if (toast) {
uni.showToast({ uni.showToast({
title: res.data.msg || res.data.message, title: res.data.msg || res.data.message,
icon: 'none' icon: "none",
}) });
setTimeout(() => { setTimeout(() => {
uni.hideLoading() uni.hideLoading();
}, 1000) }, 1000);
} }
if (res.data.code == 401) { if (res.data.code == 401) {
uni.showToast({ uni.showToast({
@@ -58,29 +72,29 @@ function request(url : string, method : "GET" | "POST" | undefined, data : objec
// uni.removeStorageSync('logintoken'); // uni.removeStorageSync('logintoken');
// uni.removeStorageSync('token'); // uni.removeStorageSync('token');
uni.reLaunch({ uni.reLaunch({
url: '/pages/index/index' url: "/pages/index/index",
}) });
},
});
} }
}) uni.hideLoading();
}
uni.hideLoading()
reject(res.message | res.msg); reject(res.message | res.msg);
} else { } else {
uni.hideLoading() uni.hideLoading();
console.log(res);
resolve(res.data.data); resolve(res.data.data);
} }
}, },
fail(err) { fail(err) {
console.log(err) uni.hideLoading();
uni.hideLoading()
//请求失败 //请求失败
uni.showToast({ uni.showToast({
title: '无法连接到服务器', title: "无法连接到服务器",
icon: 'none' icon: "none",
}) });
reject(err) reject(err);
} },
}) });
}) });
} }
export { request, baseURL } export { request, baseURL };