php请求增加未登录跳转

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

View File

@@ -1,86 +1,100 @@
//服务器接口地址 //服务器接口地址
// 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,
uni.getNetworkType({ method: "GET" | "POST" | undefined,
success: (res) => { data: object | any,
networkType = res.networkType toast: boolean
} ) {
}); let networkType = "";
if (networkType == 'none') { uni.getNetworkType({
uni.showToast({ success: (res) => {
title: '网络异常,请检查网络', networkType = res.networkType;
icon: 'none' },
}) });
return false; if (networkType == "none") {
} uni.showToast({
if (toast) { title: "网络异常,请检查网络",
uni.showLoading({ icon: "none",
title: '加载中', });
mask: true return false;
}) }
} if (toast) {
return new Promise(async (resolve, reject) => { uni.showLoading({
let header : any title: "加载中",
header = { mask: true,
'content-type': 'application/json', });
'clinttype':uni.getStorageSync('clint_type'), }
'bausertoken': uni.getStorageSync('phpuserinfo').token||'', return new Promise(async (resolve, reject) => {
'token': uni.getStorageSync('iToken').tokenValue||'', let header: any;
}; header = {
uni.request({ "content-type": "application/json",
url: baseURL + url, clinttype: uni.getStorageSync("clint_type"),
method: method, bausertoken: uni.getStorageSync("phpuserinfo").token || "",
data: data, token: uni.getStorageSync("iToken").tokenValue || "",
header: header, };
success(res : any) { uni.request({
if (res.data.code != 1) { url: baseURL + url,
//是否提示错误 method: method,
if (toast) { data: data,
uni.showToast({ header: header,
title: res.data.msg || res.data.message, success(res: any) {
icon: 'none' if (res.data.code != 1) {
}) if (res.data.code === 3000) {
setTimeout(() => { uni.hideLoading();
uni.hideLoading() go.to("PAGES_LOGIN", {}, "redirect");
}, 1000) reject();
} return;
if (res.data.code == 401) { }
uni.showToast({ //是否提示错误
title: res.message || res.msg, if (toast) {
icon: "none", uni.showToast({
success: () => { title: res.data.msg || res.data.message,
// uni.removeStorageSync('logintoken'); icon: "none",
// uni.removeStorageSync('token'); });
uni.reLaunch({ setTimeout(() => {
url: '/pages/index/index' uni.hideLoading();
}) }, 1000);
} }
}) if (res.data.code == 401) {
} uni.showToast({
uni.hideLoading() title: res.message || res.msg,
reject(res.message | res.msg); icon: "none",
} else { success: () => {
uni.hideLoading() // uni.removeStorageSync('logintoken');
resolve(res.data.data); // uni.removeStorageSync('token');
} uni.reLaunch({
}, url: "/pages/index/index",
fail(err) { });
console.log(err) },
uni.hideLoading() });
//请求失败 }
uni.showToast({ uni.hideLoading();
title: '无法连接到服务器', reject(res.message | res.msg);
icon: 'none' } else {
}) uni.hideLoading();
reject(err) console.log(res);
} resolve(res.data.data);
}) }
}) },
fail(err) {
uni.hideLoading();
//请求失败
uni.showToast({
title: "无法连接到服务器",
icon: "none",
});
reject(err);
},
});
});
} }
export { request, baseURL } export { request, baseURL };