From f26f29d84e0a68831a6af14dab3eec5500496d2e Mon Sep 17 00:00:00 2001
From: spring <2396852758@qq.com>
Date: 星期三, 28 五月 2025 16:48:52 +0800
Subject: [PATCH] 初始化项目

---
 uview-ui/libs/request/index.js |  172 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 172 insertions(+), 0 deletions(-)

diff --git a/uview-ui/libs/request/index.js b/uview-ui/libs/request/index.js
new file mode 100644
index 0000000..ca31670
--- /dev/null
+++ b/uview-ui/libs/request/index.js
@@ -0,0 +1,172 @@
+import deepMerge from "../function/deepMerge";
+import validate from "../function/test";
+class Request {
+	// 璁剧疆鍏ㄥ眬榛樿閰嶇疆
+	setConfig(customConfig) {
+		// 娣卞害鍚堝苟瀵硅薄锛屽惁鍒欎細閫犳垚瀵硅薄娣卞眰灞炴�т涪澶�
+		this.config = deepMerge(this.config, customConfig);
+	}
+
+	// 涓昏璇锋眰閮ㄥ垎
+	request(options = {}) {
+		// 妫�鏌ヨ姹傛嫤鎴�
+		if (this.interceptor.request && typeof this.interceptor.request === 'function') {
+			let tmpConfig = {};
+			let interceptorRequest = this.interceptor.request(options);
+			if (interceptorRequest === false) {
+				// 杩斿洖涓�涓浜巔ending鐘舵�佷腑鐨凱romise锛屾潵鍙栨秷鍘焢romise锛岄伩鍏嶈繘鍏hen()鍥炶皟
+				return new Promise(()=>{});
+			}
+			this.options = interceptorRequest;
+		}
+		options.sslVerify=false
+		options.dataType = options.dataType || this.config.dataType;
+		options.responseType = options.responseType || this.config.responseType;
+		options.url = options.url || '';
+		options.params = options.params || {};
+		options.header = Object.assign({}, this.config.header, options.header);
+		options.method = options.method || this.config.method;
+
+		return new Promise((resolve, reject) => {
+			options.complete = (response) => {
+				// 璇锋眰杩斿洖鍚庯紝闅愯棌loading(濡傛灉璇锋眰杩斿洖蹇殑璇濓紝鍙兘浼氭病鏈塴oading)
+				uni.hideLoading();
+				// 娓呴櫎瀹氭椂鍣紝濡傛灉璇锋眰鍥炴潵浜嗭紝灏辨棤闇�loading
+				clearTimeout(this.config.timer);
+				this.config.timer = null;
+				// 鍒ゆ柇鐢ㄦ埛瀵规嫤鎴繑鍥炴暟鎹殑瑕佹眰锛屽鏋渙riginalData涓簍rue锛岃繑鍥炴墍鏈夌殑鏁版嵁(response)鍒版嫤鎴櫒锛屽惁鍒欏彧杩斿洖response.data
+				if(this.config.originalData) {
+					// 鍒ゆ柇鏄惁瀛樺湪鎷︽埅鍣�
+					if (this.interceptor.response && typeof this.interceptor.response === 'function') {
+						let resInterceptors = this.interceptor.response(response, options); // 澧炲姞璇锋眰閫夐」锛屾柟渚垮悗缁鐞� ThinkGem
+						// 濡傛灉鎷︽埅鍣ㄤ笉杩斿洖false锛屽氨灏嗘嫤鎴櫒杩斿洖鐨勫唴瀹圭粰this.$u.post鐨則hen鍥炶皟
+						if (resInterceptors !== false) {
+							resolve(resInterceptors);
+						} else {
+							// 濡傛灉鎷︽埅鍣ㄨ繑鍥瀎alse锛屾剰鍛崇潃鎷︽埅鍣ㄥ畾涔夎�呰涓鸿繑鍥炴湁闂锛岀洿鎺ユ帴鍏atch鍥炶皟
+							reject(response);
+						}
+					} else {
+						// 濡傛灉瑕佹眰杩斿洖鍘熷鏁版嵁锛屽氨绠楁病鏈夋嫤鎴櫒锛屼篃杩斿洖鏈�鍘熷鐨勬暟鎹�
+						resolve(response);
+					}
+				} else {
+					if (response.statusCode == 200) {
+						if (this.interceptor.response && typeof this.interceptor.response === 'function') {
+							let resInterceptors = this.interceptor.response(response.data, options); // 澧炲姞璇锋眰閫夐」锛屾柟渚垮悗缁鐞� ThinkGem
+							if (resInterceptors !== false) {
+								resolve(resInterceptors);
+							} else {
+								reject(response.data);
+							}
+						} else {
+							// 濡傛灉涓嶆槸杩斿洖鍘熷鏁版嵁(originalData=false)锛屼笖娌℃湁鎷︽埅鍣ㄧ殑鎯呭喌涓嬶紝杩斿洖绾暟鎹粰then鍥炶皟
+							resolve(response.data);
+						}
+					} else {
+						// 涓嶈繑鍥炲師濮嬫暟鎹殑鎯呭喌涓嬶紝鏈嶅姟鍣ㄧ姸鎬佺爜涓嶄负200锛宮odal寮规鎻愮ず
+						// if(response.errMsg) {
+						// 	uni.showModal({
+						// 		title: response.errMsg
+						// 	});
+						// }
+						reject(response)
+					}
+				}
+			}
+
+			// 鍒ゆ柇鐢ㄦ埛浼犻�掔殑URL鏄惁/寮�澶�,濡傛灉涓嶆槸,鍔犱笂/锛岃繖閲屼娇鐢ㄤ簡uView鐨則est.js楠岃瘉搴撶殑url()鏂规硶
+			options.url = validate.url(options.url) ? options.url : (this.config.baseUrl + (options.url.indexOf('/') == 0 ?
+				options.url : '/' + options.url));
+
+			// 鏄惁鏄剧ずloading
+			// 鍔犱竴涓槸鍚﹀凡鏈塼imer瀹氭椂鍣ㄧ殑鍒ゆ柇锛屽惁鍒欐湁涓や釜鍚屾椂璇锋眰鐨勬椂鍊欙紝鍚庤�呬細娓呴櫎鍓嶈�呯殑瀹氭椂鍣╥d
+			// 鑰屾病鏈夋竻闄ゅ墠鑰呯殑瀹氭椂鍣紝瀵艰嚧鍓嶈�呰秴鏃讹紝涓�鐩存樉绀簂oading
+			if(this.config.showLoading && !this.config.timer) {
+				this.config.timer = setTimeout(() => {
+					uni.showLoading({
+						title: this.config.loadingText,
+						mask: this.config.loadingMask
+					})
+					this.config.timer = null;
+				}, this.config.loadingTime);
+			}
+			uni.request(options);
+		})
+		// .catch(res => {
+		// 	// 濡傛灉杩斿洖reject()锛屼笉璁╁叾杩涘叆this.$u.post().then().catch()鍚庨潰鐨刢atct()
+		// 	// 鍥犱负寰堝浜洪兘浼氬繕浜嗗啓鍚庨潰鐨刢atch()锛屽鑷存姤閿欐崟鑾蜂笉鍒癱atch
+		// 	return new Promise(()=>{});
+		// })
+	}
+
+	constructor() {
+		this.config = {
+			baseUrl: '', // 璇锋眰鐨勬牴鍩熷悕
+			// 榛樿鐨勮姹傚ご
+			header: {
+				//'content-type': 'application/x-www-form-urlencoded'
+			},
+			method: 'POST',
+			// 璁剧疆涓簀son锛岃繑鍥炲悗uni.request浼氬鏁版嵁杩涜涓�娆SON.parse
+			dataType: 'json',
+			// 姝ゅ弬鏁版棤闇�澶勭悊锛屽洜涓�5+鍜屾敮浠樺疂灏忕▼搴忎笉鏀寔锛岄粯璁や负text鍗冲彲
+			responseType: 'text',
+			showLoading: true, // 鏄惁鏄剧ず璇锋眰涓殑loading
+			loadingText: '璇锋眰涓�...',
+			loadingTime: 800, // 鍦ㄦ鏃堕棿鍐咃紝璇锋眰杩樻病鍥炴潵鐨勮瘽锛屽氨鏄剧ず鍔犺浇涓姩鐢伙紝鍗曚綅ms
+			timer: null, // 瀹氭椂鍣�
+			originalData: false, // 鏄惁鍦ㄦ嫤鎴櫒涓繑鍥炴湇鍔$鐨勫師濮嬫暟鎹紝瑙佹枃妗h鏄�
+			loadingMask: true, // 灞曠ずloading鐨勬椂鍊欙紝鏄惁缁欎竴涓�忔槑鐨勮挋灞傦紝闃叉瑙︽懜绌块��
+		}
+
+		// 鎷︽埅鍣�
+		this.interceptor = {
+			// 璇锋眰鍓嶇殑鎷︽埅
+			request: null,
+			// 璇锋眰鍚庣殑鎷︽埅
+			response: null
+		}
+
+		// get璇锋眰
+		this.get = (url, data = {}, header = {}) => {
+			return this.request({
+				method: 'GET',
+				url,
+				header,
+				data
+			})
+		}
+
+		// post璇锋眰
+		this.post = (url, data = {}, header = {}) => {
+			return this.request({
+				url,
+				method: 'POST',
+				header,
+				data
+			})
+		}
+
+		// put璇锋眰锛屼笉鏀寔鏀粯瀹濆皬绋嬪簭(HX2.6.15)
+		this.put = (url, data = {}, header = {}) => {
+			return this.request({
+				url,
+				method: 'PUT',
+				header:{'Content-type': 'application/json;charset=UTF-8'},
+				data
+			})
+		}
+
+		// delete璇锋眰锛屼笉鏀寔鏀粯瀹濆拰澶存潯灏忕▼搴�(HX2.6.15)
+		this.delete = (url, data = {}, header = {}) => {
+			return this.request({
+				url,
+				method: 'DELETE',
+				header,
+				data
+			})
+		}
+	}
+}
+export default new Request

--
Gitblit v1.9.3