From 68c5ef7d9fea3f911e250fb5f8b300bc76099e49 Mon Sep 17 00:00:00 2001
From: jiangping <jp@doumee.com>
Date: 星期四, 26 十月 2023 13:55:49 +0800
Subject: [PATCH] 小程序
---
minipro_standard/uni_modules/uview-ui/libs/util/route.js | 124 +++++++++++++++++++++++++++++++++++++++++
1 files changed, 124 insertions(+), 0 deletions(-)
diff --git a/minipro_standard/uni_modules/uview-ui/libs/util/route.js b/minipro_standard/uni_modules/uview-ui/libs/util/route.js
new file mode 100644
index 0000000..2afeea5
--- /dev/null
+++ b/minipro_standard/uni_modules/uview-ui/libs/util/route.js
@@ -0,0 +1,124 @@
+/**
+ * 璺敱璺宠浆鏂规硶锛岃鏂规硶鐩稿浜庣洿鎺ヤ娇鐢╱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}`
+ }
+ // 鐩存帴鎷兼帴鍙傛暟锛屽洜涓烘澶剈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.deepMerge(this.config, options)
+ // 鍚﹀垯姝e父浣跨敤mergeConfig涓殑url鍜宲arams杩涜鎷兼帴
+ mergeConfig.url = this.mixinParam(options.url, options.params)
+ }
+
+ // 濡傛灉鏈璺宠浆鐨勮矾寰勫拰鏈〉闈㈣矾寰勪竴鑷达紝涓嶆墽琛岃烦杞紝闃叉鐢ㄦ埛蹇�熺偣鍑昏烦杞寜閽紝閫犳垚澶氭璺宠浆鍚屼竴涓〉闈㈢殑闂
+ if (mergeConfig.url === uni.$u.page()) return
+
+ 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
--
Gitblit v1.9.3