From 2cc85c64f1c64a2dbaeae276a3e2ca8420de76b7 Mon Sep 17 00:00:00 2001
From: WXL <wl_5969728@163.com>
Date: 星期三, 22 四月 2026 18:09:58 +0800
Subject: [PATCH] 上报转运调试
---
utils/request.js | 154 ++++++++++++++++++++++++++-------------------------
1 files changed, 78 insertions(+), 76 deletions(-)
diff --git a/utils/request.js b/utils/request.js
index 90a071b..8249b77 100644
--- a/utils/request.js
+++ b/utils/request.js
@@ -6,35 +6,34 @@
*/
const showToast = (message) => {
if (uni.$u?.toast) {
- uni.$u.toast(message)
+ uni.$u.toast(message);
} else {
uni.showToast({
title: message,
- icon: 'none'
- })
+ icon: "none",
+ });
}
-}
+};
// 鍩虹閰嶇疆 - 浠庣幆澧冨彉閲忎腑鑾峰彇baseURL
const config = {
- baseURL: '/api', // 浣跨敤鐜鍙橀噺涓殑閰嶇疆
+ baseURL: "/api", // 浣跨敤鐜鍙橀噺涓殑閰嶇疆
timeout: 60000,
header: {
- 'Content-Type': 'application/json',
- 'X-Business-System': 'medical-system',
-
+ "Content-Type": "application/json",
+ "X-Business-System": "medical-system",
},
// 鏂板閰嶇疆椤�
- loginPage: '/pages/login/Login', // 鐧诲綍椤佃矾寰�
+ loginPage: "/pages/login/Login", // 鐧诲綍椤佃矾寰�
tokenExpiredCode: 401, // 鍚庣杩斿洖鐨凾oken杩囨湡鐘舵�佺爜
noPermissionCode: 403, // 鏃犳潈闄愮姸鎬佺爜
// 娣诲姞鐧藉悕鍗曢厤缃紙涓嶉渶瑕佹牎楠宼oken鐨勬帴鍙o級
whiteList: [
- '/login', // 鐧诲綍鎺ュ彛
+ "/login", // 鐧诲綍鎺ュ彛
// '/dingtalk/auth/login', // 閽夐拤鎺堟潈鐧诲綍鎺ュ彛
// '/dingtalk/auth/bind' // 閽夐拤鎺堟潈鐧诲綍缁戝畾鎺ュ彛
- ]
-}
+ ],
+};
/**
* 妫�鏌ヨ姹傛槸鍚﹀湪鐧藉悕鍗曚腑
@@ -42,33 +41,36 @@
*/
const isInWhiteList = (url) => {
// 鎻愬彇鐩稿璺緞锛堢Щ闄aseURL锛夊苟蹇界暐鏌ヨ鍙傛暟
- const relativeUrl = url.replace(config.baseURL, '').split('?')[0];
- return config.whiteList.some(path => {
+ const relativeUrl = url.replace(config.baseURL, "").split("?")[0];
+ return config.whiteList.some((path) => {
// 澶勭悊閫氶厤绗︽儏鍐�
- if (path.endsWith('/')) {
- return relativeUrl.startsWith(path)
+ if (path.endsWith("/")) {
+ return relativeUrl.startsWith(path);
}
// 绮剧‘鍖归厤璺緞
- return relativeUrl === path
- })
-}
+ return relativeUrl === path;
+ });
+};
/**
* 璺宠浆鍒扮櫥褰曢〉
*/
const navigateToLogin = () => {
uni.redirectTo({
- url: config.loginPage + '?redirect=' + encodeURIComponent(getCurrentPagePath())
- })
-}
+ url:
+ config.loginPage +
+ "?redirect=" +
+ encodeURIComponent(getCurrentPagePath()),
+ });
+};
/**
* 鑾峰彇褰撳墠椤甸潰璺緞锛堢敤浜庣櫥褰曞悗璺冲洖锛�
*/
const getCurrentPagePath = () => {
- const pages = getCurrentPages()
- return pages[pages.length - 1]?.route || ''
-}
+ const pages = getCurrentPages();
+ return pages[pages.length - 1]?.route || "";
+};
/**
* 妫�鏌oken鏄惁杩囨湡
@@ -76,72 +78,72 @@
*/
const checkTokenExpired = (statusCode) => {
if (statusCode === config.tokenExpiredCode) {
- showToast('鐧诲綍宸茶繃鏈燂紝璇烽噸鏂扮櫥褰�')
- navigateToLogin()
- return true
+ showToast("鐧诲綍宸茶繃鏈燂紝璇烽噸鏂扮櫥褰�");
+ navigateToLogin();
+ return true;
}
- return false
-}
+ return false;
+};
/**
* 璇锋眰鎷︽埅鍣�
*/
const requestInterceptor = (options) => {
- const token = uni.getStorageSync('token')
-
+ const token = uni.getStorageSync("token");
+
// 濡傛灉璇锋眰鍦ㄧ櫧鍚嶅崟涓紝鐩存帴鏀捐
if (isInWhiteList(options.url)) {
- return options
+ return options;
}
// 濡傛灉鏈櫥褰曚笖涓嶆槸鐧藉悕鍗曟帴鍙o紝璺宠浆鐧诲綍
// console.log(token,'token');
-
+
if (!token) {
- navigateToLogin()
- throw new Error('鏈櫥褰�')
+ navigateToLogin();
+ throw new Error("鏈櫥褰�");
}
// 娣诲姞Token鍒癏eader
options.header = {
...options.header,
- 'Authorization': `Bearer ${token}`
- }
-
- return options
-}
+ Authorization: `Bearer ${token}`,
+ };
+
+ return options;
+};
/**
* 鍝嶅簲鎷︽埅鍣�
*/
const responseInterceptor = (response) => {
- const { statusCode, data } = response
-
+ const { statusCode, data } = response;
+
// Token杩囨湡澶勭悊
if (checkTokenExpired(statusCode)) {
- return Promise.reject(data)
+ return Promise.reject(data);
}
// 鏃犳潈闄愬鐞�
if (statusCode === config.noPermissionCode) {
- showToast('鏃犳潈闄愯闂�')
- return Promise.reject(data)
+ showToast("鏃犳潈闄愯闂�");
+ return Promise.reject(data);
}
// 鍏朵粬閿欒鐘舵�佺爜
if (statusCode !== 200) {
- showToast(`璇锋眰澶辫触: ${statusCode}`)
- return Promise.reject(data)
+ showToast(`璇锋眰澶辫触: ${statusCode}`);
+ return Promise.reject(data);
}
// 鎺ュ彛鑷畾涔夐敊璇爜澶勭悊锛堝亣璁綿ata.code涓�0琛ㄧず鎴愬姛锛�
if (data?.code !== 200) {
- showToast(data?.msg || '鎿嶄綔澶辫触')
- return Promise.reject(data)
+ showToast(data?.msg || "鎿嶄綔澶辫触");
+ return Promise.reject(data);
}
- return data.data || data // 杩斿洖瀹為檯涓氬姟鏁版嵁
-}
+ return data; // 杩斿洖瀹為檯涓氬姟鏁版嵁
+};
/**
* 鏍稿績璇锋眰鏂规硶
@@ -153,58 +155,58 @@
...config,
...options,
url: config.baseURL + options.url,
- header: { ...config.header, ...options.header }
- }
+ header: { ...config.header, ...options.header },
+ };
// 璇锋眰鎷︽埅
- options = await requestInterceptor(options)
-
+ options = await requestInterceptor(options);
+
// 鍙戣捣璇锋眰
return new Promise((resolve, reject) => {
uni.request({
...options,
success: (res) => {
- resolve(responseInterceptor(res))
+ resolve(responseInterceptor(res));
},
fail: (err) => {
- showToast('缃戠粶閿欒锛岃閲嶈瘯')
- reject(err)
- }
- })
- })
+ showToast("缃戠粶閿欒锛岃閲嶈瘯");
+ reject(err);
+ },
+ });
+ });
} catch (err) {
- return Promise.reject(err)
+ return Promise.reject(err);
}
-}
+};
// 蹇嵎鏂规硶灏佽
const http = {
get(url, data = {}, options = {}) {
- return request({ url, data, method: 'GET', ...options })
+ return request({ url, data, method: "GET", ...options });
},
post(url, data = {}, options = {}) {
- return request({ url, data, method: 'POST', ...options })
+ return request({ url, data, method: "POST", ...options });
},
put(url, data = {}, options = {}) {
- return request({ url, data, method: 'PUT', ...options })
+ return request({ url, data, method: "PUT", ...options });
},
delete(url, data = {}, options = {}) {
- return request({ url, data, method: 'DELETE', ...options })
+ return request({ url, data, method: "DELETE", ...options });
},
// 鏂板锛氫笂浼犳枃浠舵柟娉�
- upload(url, filePath, name = 'file', formData = {}) {
+ upload(url, filePath, name = "file", formData = {}) {
return request({
url,
- method: 'POST',
+ method: "POST",
filePath,
name,
formData,
- header: { 'Content-Type': 'multipart/form-data' }
- })
- }
-}
+ header: { "Content-Type": "multipart/form-data" },
+ });
+ },
+};
// 鎸傝浇鍒板叏灞�
-uni.$uapi = http
+uni.$uapi = http;
-export default http
\ No newline at end of file
+export default http;
--
Gitblit v1.9.3