From be7ae6ff5252d9ac586d682bbdc5f47323c27fa9 Mon Sep 17 00:00:00 2001 From: YeMingfei666 <1619116647@qq.com> Date: Thu, 4 Dec 2025 10:15:00 +0800 Subject: [PATCH] =?UTF-8?q?php=E8=AF=B7=E6=B1=82=E5=A2=9E=E5=8A=A0?= =?UTF-8?q?=E6=9C=AA=E7=99=BB=E5=BD=95=E8=B7=B3=E8=BD=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http/php/request.ts | 174 ++++++++++++++++++++++++-------------------- 1 file changed, 94 insertions(+), 80 deletions(-) diff --git a/http/php/request.ts b/http/php/request.ts index d2199f5..5147f91 100644 --- a/http/php/request.ts +++ b/http/php/request.ts @@ -1,86 +1,100 @@ //服务器接口地址 // 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 -baseURL='/phpapi/api/' +baseURL = "/phpapi/api/"; // #endif + +import go from "@/commons/utils/go.js"; + // 封装公共请求方法 -function request(url : string, method : "GET" | "POST" | undefined, data : object | any, toast : boolean) { - let networkType = '' - uni.getNetworkType({ - success: (res) => { - networkType = res.networkType - } - }); - if (networkType == 'none') { - uni.showToast({ - title: '网络异常,请检查网络', - icon: 'none' - }) - return false; - } - if (toast) { - uni.showLoading({ - title: '加载中', - mask: true - }) - } - return new Promise(async (resolve, reject) => { - let header : any - header = { - 'content-type': 'application/json', - 'clinttype':uni.getStorageSync('clint_type'), - 'bausertoken': uni.getStorageSync('phpuserinfo').token||'', - 'token': uni.getStorageSync('iToken').tokenValue||'', - }; - uni.request({ - url: baseURL + url, - method: method, - data: data, - header: header, - success(res : any) { - if (res.data.code != 1) { - //是否提示错误 - if (toast) { - uni.showToast({ - title: res.data.msg || res.data.message, - icon: 'none' - }) - setTimeout(() => { - uni.hideLoading() - }, 1000) - } - if (res.data.code == 401) { - uni.showToast({ - title: res.message || res.msg, - icon: "none", - success: () => { - // uni.removeStorageSync('logintoken'); - // uni.removeStorageSync('token'); - uni.reLaunch({ - url: '/pages/index/index' - }) - } - }) - } - uni.hideLoading() - reject(res.message | res.msg); - } else { - uni.hideLoading() - resolve(res.data.data); - } - }, - fail(err) { - console.log(err) - uni.hideLoading() - //请求失败 - uni.showToast({ - title: '无法连接到服务器', - icon: 'none' - }) - reject(err) - } - }) - }) +function request( + url: string, + method: "GET" | "POST" | undefined, + data: object | any, + toast: boolean +) { + let networkType = ""; + uni.getNetworkType({ + success: (res) => { + networkType = res.networkType; + }, + }); + if (networkType == "none") { + uni.showToast({ + title: "网络异常,请检查网络", + icon: "none", + }); + return false; + } + if (toast) { + uni.showLoading({ + title: "加载中", + mask: true, + }); + } + return new Promise(async (resolve, reject) => { + let header: any; + header = { + "content-type": "application/json", + clinttype: uni.getStorageSync("clint_type"), + bausertoken: uni.getStorageSync("phpuserinfo").token || "", + token: uni.getStorageSync("iToken").tokenValue || "", + }; + uni.request({ + url: baseURL + url, + method: method, + data: data, + header: header, + success(res: any) { + if (res.data.code != 1) { + if (res.data.code === 3000) { + uni.hideLoading(); + go.to("PAGES_LOGIN", {}, "redirect"); + reject(); + return; + } + //是否提示错误 + if (toast) { + uni.showToast({ + title: res.data.msg || res.data.message, + icon: "none", + }); + setTimeout(() => { + uni.hideLoading(); + }, 1000); + } + if (res.data.code == 401) { + uni.showToast({ + title: res.message || res.msg, + icon: "none", + success: () => { + // uni.removeStorageSync('logintoken'); + // uni.removeStorageSync('token'); + uni.reLaunch({ + url: "/pages/index/index", + }); + }, + }); + } + uni.hideLoading(); + reject(res.message | res.msg); + } else { + uni.hideLoading(); + console.log(res); + resolve(res.data.data); + } + }, + fail(err) { + uni.hideLoading(); + //请求失败 + uni.showToast({ + title: "无法连接到服务器", + icon: "none", + }); + reject(err); + }, + }); + }); } -export { request, baseURL } \ No newline at end of file +export { request, baseURL };