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/function/route.js |  122 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 122 insertions(+), 0 deletions(-)

diff --git a/uview-ui/libs/function/route.js b/uview-ui/libs/function/route.js
new file mode 100644
index 0000000..28a81b7
--- /dev/null
+++ b/uview-ui/libs/function/route.js
@@ -0,0 +1,122 @@
+/**
+ * 璺敱璺宠浆鏂规硶锛岃鏂规硶鐩稿浜庣洿鎺ヤ娇鐢╱ni.xxx鐨勫ソ澶勬槸浣跨敤鏇村姞绠�鍗曞揩鎹�
+ * 骞朵笖甯︽湁璺敱鎷︽埅鍔熻兘
+ */
+
+class Router {
+	constructor() {
+		// 鍘熷灞炴�у畾涔�
+		this.config = {
+			type: 'navigateTo',
+			url: '',
+			delta: 1, // navigateBack椤甸潰鍚庨��鏃�,鍥為��鐨勫眰鏁�
+			params: {}, // 浼犻�掔殑鍙傛暟
+			animationType: 'pop-in', // 绐楀彛鍔ㄧ敾,鍙湪APP鏈夋晥
+			animationDuration: 300, // 绐楀彛鍔ㄧ敾鎸佺画鏃堕棿,鍗曚綅姣,鍙湪APP鏈夋晥
+			intercept: false, // 鏄惁闇�瑕佹嫤鎴�
+		}
+		// 鍥犱负route鏂规硶鏄渶瑕佸澶栬祴鍊肩粰鍙﹀鐨勫璞′娇鐢紝鍚屾椂route鍐呴儴鏈変娇鐢╰his锛屼細瀵艰嚧route澶卞幓涓婁笅鏂�
+		// 杩欓噷鍦ㄦ瀯閫犲嚱鏁颁腑杩涜this缁戝畾
+		this.route = this.route.bind(this)
+	}
+
+	// 鍒ゆ柇url鍓嶉潰鏄惁鏈�"/"锛屽鏋滄病鏈夊垯鍔犱笂锛屽惁鍒欐棤娉曡烦杞�
+	addRootPath(url) {
+		return url[0] === '/' ? url : `/${url}`
+	}
+
+	// 鏁村悎璺敱鍙傛暟
+	mixinParam(url, params) {
+		url = url && this.addRootPath(url)
+		
+		// 浣跨敤姝e垯鍖归厤锛屼富瑕佷緷鎹槸鍒ゆ柇鏄惁鏈�"/","?","="绛夛紝濡傗��/page/index/index?name=mary"
+		// 濡傛灉鏈塽rl涓湁get鍙傛暟锛岃浆鎹㈠悗鏃犻渶甯︿笂"?"
+		let query = ''
+		if (/.*\/.*\?.*=.*/.test(url)) {
+			// object瀵硅薄杞负get绫诲瀷鐨勫弬鏁�
+			query = uni.$u.queryParams(params, false);
+			// 鍥犱负宸叉湁get鍙傛暟,鎵�浠ュ悗闈㈡嫾鎺ョ殑鍙傛暟闇�瑕佸甫涓�"&"闅斿紑
+			return url += "&" + query
+		} else {
+			// 鐩存帴鎷兼帴鍙傛暟锛屽洜涓烘澶剈rl涓病鏈夊悗闈㈢殑query鍙傛暟锛屼篃灏辨病鏈�"?/&"涔嬬被鐨勭鍙�
+			query = uni.$u.queryParams(params);
+			return url += query
+		}
+	}
+
+	// 瀵瑰鐨勬柟娉曞悕绉�
+	async route(options = {}, params = {}) {
+		// 鍚堝苟鐢ㄦ埛鐨勯厤缃拰鍐呴儴鐨勯粯璁ら厤缃�
+		let mergeConfig = {}
+
+		if (typeof options === 'string') {
+			// 濡傛灉options涓哄瓧绗︿覆锛屽垯涓簉oute(url, params)鐨勫舰寮�
+			mergeConfig.url = this.mixinParam(options, params)
+			mergeConfig.type = 'navigateTo'
+		} else {
+			mergeConfig = uni.$u.deepClone(options, this.config)
+			// 鍚﹀垯姝e父浣跨敤mergeConfig涓殑url鍜宲arams杩涜鎷兼帴
+			mergeConfig.url = this.mixinParam(options.url, options.params)
+		}
+		
+		if(params.intercept) {
+			this.config.intercept = params.intercept
+		}
+		// params鍙傛暟涔熷甫缁欐嫤鎴櫒
+		mergeConfig.params = params
+		// 鍚堝苟鍐呭閮ㄥ弬鏁�
+		mergeConfig = uni.$u.deepMerge(this.config, mergeConfig)
+		// 鍒ゆ柇鐢ㄦ埛鏄惁瀹氫箟浜嗘嫤鎴櫒
+		if (typeof uni.$u.routeIntercept === 'function') {
+			// 瀹氫竴涓猵romise锛屾牴鎹敤鎴锋墽琛宺esolve(true)鎴栬�卹esolve(false)鏉ュ喅瀹氭槸鍚﹁繘琛岃矾鐢辫烦杞�
+			const isNext = await new Promise((resolve, reject) => {
+				uni.$u.routeIntercept(mergeConfig, resolve)
+			})
+			// 濡傛灉isNext涓簍rue锛屽垯鎵ц璺敱璺宠浆
+			isNext && this.openPage(mergeConfig)
+		} else {
+			this.openPage(mergeConfig)
+		}
+	}
+
+	// 鎵ц璺敱璺宠浆
+	openPage(config) {
+		// 瑙f瀯鍙傛暟
+		const {
+			url,
+			type,
+			delta,
+			animationType,
+			animationDuration
+		} = config
+		if (config.type == 'navigateTo' || config.type == 'to') {
+			uni.navigateTo({
+				url,
+				animationType,
+				animationDuration
+			});
+		}
+		if (config.type == 'redirectTo' || config.type == 'redirect') {
+			uni.redirectTo({
+				url
+			});
+		}
+		if (config.type == 'switchTab' || config.type == 'tab') {
+			uni.switchTab({
+				url
+			});
+		}
+		if (config.type == 'reLaunch' || config.type == 'launch') {
+			uni.reLaunch({
+				url
+			});
+		}
+		if (config.type == 'navigateBack' || config.type == 'back') {
+			uni.navigateBack({
+				delta
+			});
+		}
+	}
+}
+
+export default (new Router()).route
\ No newline at end of file

--
Gitblit v1.9.3