From 2d43a1df3f5ba42710e6d21c27d1e13bdb8dfd56 Mon Sep 17 00:00:00 2001
From: zhangwei <1504152376@qq.com>
Date: 星期一, 24 三月 2025 09:23:08 +0800
Subject: [PATCH] -

---
 src/common/request/request.js |  248 +++++++++++++++++++++++++++----------------------
 1 files changed, 137 insertions(+), 111 deletions(-)

diff --git a/src/common/request/request.js b/src/common/request/request.js
index acebc6c..f2dfe5d 100644
--- a/src/common/request/request.js
+++ b/src/common/request/request.js
@@ -1,117 +1,143 @@
-锘縤mport { apiBaseUrl } from '@/common/setting/constVarsHelper.js';
+// 寮曞叆luch-request
+import {
+	http
+} from '@/uni_modules/uview-plus'
+import {
+	apiBaseUrl
+} from '@/common/setting/constVarsHelper.js';
 import * as db from '@/common/utils/dbHelper.js' //寮曞叆common
-
-// 姝m鍙傛暟涓洪〉闈㈢殑瀹炰緥锛屽彲浠ラ�氳繃瀹冨紩鐢╲uex涓殑鍙橀噺
-module.exports = (vm) => {
-    // 鍒濆鍖栬姹傞厤缃�
-    uni.$u.http.setConfig((defaultConfig) => {
-        /* defaultConfig 涓洪粯璁ゅ叏灞�閰嶇疆 */
-        defaultConfig.baseURL = apiBaseUrl; /* 鏍瑰煙鍚� */
-        //defaultConfig.header = {
-        //    'Content-type': 'application/json1'
-        //};
-        defaultConfig.method = 'POST';
-        defaultConfig.dataType = 'json';
-        // #ifndef MP-ALIPAY
-        defaultConfig.responseType = 'text';
-        // #endif
-        // 娉細濡傛灉灞�閮╟ustom涓庡叏灞�custom鏈夊悓鍚嶅睘鎬э紝鍒欏悗闈㈢殑灞炴�т細瑕嗙洊鍓嶉潰鐨勫睘鎬э紝鐩稿綋浜嶰bject.assign(鍏ㄥ眬锛屽眬閮�)
-        //defaultConfig.custom = {}; // 鍏ㄥ眬鑷畾涔夊弬鏁伴粯璁ゅ��
-        // #ifdef APP-PLUS || MP-ALIPAY || MP-WEIXIN
-        defaultConfig.timeout = 60000;
-        // #endif
-        // #ifdef APP-PLUS
-        defaultConfig.sslVerify = true;
-        // #endif
-        // #ifdef APP-PLUS
-        defaultConfig.firstIpv4 = false; // DNS瑙f瀽鏃朵紭鍏堜娇鐢╥pv4 浠� App-Android 鏀寔 (HBuilderX 2.8.0+)
-        // #endif
-        // 灞�閮ㄤ紭鍏堢骇楂樹簬鍏ㄥ眬锛岃繑鍥炲綋鍓嶈姹傜殑task,options銆傝鍕垮湪姝ゅ淇敼options銆傞潪蹇呭~
-        // getTask: (task, options) => {
-        // 鐩稿綋浜庤缃簡璇锋眰瓒呮椂鏃堕棿500ms
-        //   setTimeout(() => {
-        //     task.abort()
-        //   }, 500)
-        // },
-        // 鍏ㄥ眬鑷畾涔夐獙璇佸櫒銆傚弬鏁颁负statusCode 涓斿繀瀛樺湪锛屼笉鐢ㄥ垽鏂┖鎯呭喌銆�
-        defaultConfig.validateStatus = (statusCode) => { // statusCode 蹇呭瓨鍦ㄣ�傛澶勭ず渚嬩负鍏ㄥ眬榛樿閰嶇疆
-            return statusCode >= 200 && statusCode < 501
-        }
-
-        return defaultConfig
-    })
-
-    // 璇锋眰鎷︽埅
-    uni.$u.http.interceptors.request.use((config) => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣�
-        // 鍒濆鍖栬姹傛嫤鎴櫒鏃讹紝浼氭墽琛屾鏂规硶锛屾鏃禿ata涓簎ndefined锛岃祴浜堥粯璁}
-        config.data = config.data || {}
-        if (config?.custom?.needToken) {
-            // 鑾峰彇鐢ㄦ埛token
-		const userToken = db.get("userToken");		
-		if (!userToken) {	
-		    //console.log("寮�鍚脊绐�");
-	vm.$store.commit('showLoginTip', true);
-		    //console.log("寮圭獥宸茬粡寮�鍚�");
-		    // return false;
-		} else {
-		    config.header.Authorization = 'Bearer ' + userToken;
+//  鍒濆鍖栬姹傞厤缃�
+const initRequest = (vm) => {
+	http.setConfig((defaultConfig) => {
+		/* defaultConfig 涓洪粯璁ゅ叏灞�閰嶇疆 */
+		defaultConfig.baseURL = apiBaseUrl; /* 鏍瑰煙鍚� */
+		//defaultConfig.header = {
+		//    'Content-type': 'application/json1'
+		//};
+		defaultConfig.method = 'POST';
+		defaultConfig.dataType = 'json';
+		// #ifndef MP-ALIPAY
+		defaultConfig.responseType = 'text';
+		// #endif
+		// 娉細濡傛灉灞�閮╟ustom涓庡叏灞�custom鏈夊悓鍚嶅睘鎬э紝鍒欏悗闈㈢殑灞炴�т細瑕嗙洊鍓嶉潰鐨勫睘鎬э紝鐩稿綋浜嶰bject.assign(鍏ㄥ眬锛屽眬閮�)
+		//defaultConfig.custom = {}; // 鍏ㄥ眬鑷畾涔夊弬鏁伴粯璁ゅ��
+		// #ifdef APP-PLUS || MP-ALIPAY || MP-WEIXIN
+		defaultConfig.timeout = 60000;
+		// #endif
+		// #ifdef APP-PLUS
+		defaultConfig.sslVerify = true;
+		// #endif
+		// #ifdef APP-PLUS
+		defaultConfig.firstIpv4 = false; // DNS瑙f瀽鏃朵紭鍏堜娇鐢╥pv4 浠� App-Android 鏀寔 (HBuilderX 2.8.0+)
+		// #endif
+		// 灞�閮ㄤ紭鍏堢骇楂樹簬鍏ㄥ眬锛岃繑鍥炲綋鍓嶈姹傜殑task,options銆傝鍕垮湪姝ゅ淇敼options銆傞潪蹇呭~
+		// getTask: (task, options) => {
+		// 鐩稿綋浜庤缃簡璇锋眰瓒呮椂鏃堕棿500ms
+		//   setTimeout(() => {
+		//     task.abort()
+		//   }, 500)
+		// },
+		// 鍏ㄥ眬鑷畾涔夐獙璇佸櫒銆傚弬鏁颁负statusCode 涓斿繀瀛樺湪锛屼笉鐢ㄥ垽鏂┖鎯呭喌銆�
+		defaultConfig.validateStatus = (statusCode) => { // statusCode 蹇呭瓨鍦ㄣ�傛澶勭ず渚嬩负鍏ㄥ眬榛樿閰嶇疆
+			return statusCode >= 200 && statusCode < 501
 		}
-          
-        } 
-		
-        //棰濆闇�姹�
-        if (config.custom.methodName == 'user.share'||config.custom.methodName =="pages.getpageconfig" || config.custom.methodName == "goods.goodsList") {
-            const userToken = db.get("userToken");
-            config.header.Authorization = 'Bearer ' + userToken;
-        }
 
-        return config
-    }, config => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣�
-        return Promise.reject(config)
-    })
+		return defaultConfig
+	})
 
-    // 鍝嶅簲鎷︽埅
-    uni.$u.http.interceptors.response.use((response) => { /* 瀵瑰搷搴旀垚鍔熷仛鐐逛粈涔� 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣�*/
-        //console.log(response);
-        const data = response.data
-        if (response.statusCode == 200) {
-            let pages = getCurrentPages();
-            var page = pages[pages.length - 1];
+	// 璇锋眰鎷︽埅
+	http.interceptors.request.use((config) => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣�
+		// 鍒濆鍖栬姹傛嫤鎴櫒鏃讹紝浼氭墽琛屾鏂规硶锛屾鏃禿ata涓簎ndefined锛岃祴浜堥粯璁}
+		config.data = config.data || {}
+		if (config?.custom?.needToken) {
+			// 鑾峰彇鐢ㄦ埛token
+			const userToken = db.get("userToken");
+			if (!userToken) {
+				console.log("寮�鍚脊绐�",vm.$store);
+				// vm.$store.commit('showLoginTip', true);
+				//console.log("寮圭獥宸茬粡寮�鍚�");
+				// return false;
+			} else {
+				config.header.Authorization = 'Bearer ' + userToken;
+			}
+			console.log(config, config?.custom?.needToken, 'config?.custom?.needToken');
+		}
+		//棰濆闇�姹�
+		if (config.custom.methodName == 'user.share' || config.custom.methodName == "pages.getpageconfig" ||
+			config.custom.methodName == "goods.goodsList") {
+			const userToken = db.get("userToken");
+			config.header.Authorization = 'Bearer ' + userToken;
+		}
 
-            if (!data.status && page) {
-                // 鐧诲綍淇℃伅杩囨湡鎴栬�呮湭鐧诲綍
-                if (data.data === 14007 || data.data === 14006) {
-                    // #ifdef APP-PLUS || APP-PLUS-NVUE
-                    if (page.route.indexOf('pages/login/index') < 0) {
-                        db.del("userToken");
-                        uni.showToast({
-                            title: result.msg,
-                            icon: 'none',
-                            duration: 1000,
-                            complete: function () {
-                                setTimeout(function () {
-                                    uni.hideToast();
-                                    uni.navigateTo({
-                                        url: '/pages/login/index'
-                                    });
-                                },
-                                    1000);
-                            }
-                        });
-                    }
-                    // #endif
-                    // #ifdef MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO
-                    db.del("userToken");
-                    vm.$store.commit('userInfo', null);
-                    //console.log("寮�鍚櫥褰曞脊绐�");
-                    vm.$store.commit('hasLogin', false);
-                    // #endif
-                }
-            }
-        }
-        return data === undefined ? {} : data
-    }, (response) => {
-        // 瀵瑰搷搴旈敊璇仛鐐逛粈涔� 锛坰tatusCode !== 200锛�
-        return Promise.reject(response)
-    })
+		return config
+	}, config => { // 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣�
+		return Promise.reject(config)
+	})
+
+	// 鍝嶅簲鎷︽埅
+	http.interceptors.response.use((response) => {
+		/* 瀵瑰搷搴旀垚鍔熷仛鐐逛粈涔� 鍙娇鐢╝sync await 鍋氬紓姝ユ搷浣�*/
+		console.log(response,'response,');
+		const data = response.data
+		if (response.statusCode == 200) {
+			let pages = getCurrentPages();
+			var page = pages[pages.length - 1];
+
+			if (!data.status && page) {
+				// 鐧诲綍淇℃伅杩囨湡鎴栬�呮湭鐧诲綍
+				if (data.data === 401 || data.data === 14006) {
+					// #ifdef APP-PLUS || APP-PLUS-NVUE
+					if (page.route.indexOf('pages/login/index') < 0) {
+						db.del("userToken");
+						uni.showToast({
+							title: result.msg,
+							icon: 'none',
+							duration: 1000,
+							complete: function() {
+								setTimeout(function() {
+										uni.hideToast();
+										uni.navigateTo({
+											url: '/pages/login/index'
+										});
+									},
+									1000);
+							}
+						});
+					}
+					// #endif
+					// #ifdef MP-WEIXIN || MP-ALIPAY || MP-TOUTIAO
+					db.del("userToken");
+					// vm.$store.commit('userInfo', null);
+					//console.log("寮�鍚櫥褰曞脊绐�");
+					// vm.$store.commit('hasLogin', false);
+					// #endif
+				}
+			}
+		}else if(response.statusCode==401){
+			db.del("userToken");
+			uni.showToast({
+				title: '鐧诲綍澶辨晥锛岃閲嶆柊鐧诲綍锛�',
+				icon: 'none',
+				duration: 1000,
+				complete: function() {
+					setTimeout(function() {
+							uni.hideToast();
+							uni.redirectTo({
+								url: '/pages/login/index'
+							});
+						},
+						1000);
+				}
+			});
+		}
+		return data === undefined ? {} : data
+	}, (response) => {
+		// 瀵瑰搷搴旈敊璇仛鐐逛粈涔� 锛坰tatusCode !== 200锛�
+		return Promise.reject(response)
+	})
+	// requestInterceptors()
+	// responseInterceptors()
+}
+export {
+	initRequest
 }
\ No newline at end of file

--
Gitblit v1.9.1