zhuli 1 maand geleden
bovenliggende
commit
360b753c3b
1 gewijzigde bestanden met toevoegingen van 88 en 67 verwijderingen
  1. 88 67
      HBuilderProjects/shareDynamic.html

+ 88 - 67
HBuilderProjects/shareDynamic.html

@@ -912,71 +912,81 @@
 		}
 
 		function tryOpenHBuilderApp() {
-			prefetchShareVideoBeforeAppOpen();
-			var deepLink = buildAppDeepLink();
+			var runOpen = function () {
+				var deepLink = buildAppDeepLink();
 
-			if (typeof plus !== 'undefined' && plus.runtime) {
-				var installed = null;
-				try {
-					if (typeof plus.runtime.isApplicationExist === 'function') {
-						installed = plus.runtime.isApplicationExist({
-							pname: APP_ANDROID_PACKAGE,
-							action: APP_IOS_URL_SCHEME
-						});
+				if (typeof plus !== 'undefined' && plus.runtime) {
+					var installed = null;
+					try {
+						if (typeof plus.runtime.isApplicationExist === 'function') {
+							installed = plus.runtime.isApplicationExist({
+								pname: APP_ANDROID_PACKAGE,
+								action: APP_IOS_URL_SCHEME
+							});
+						}
+					} catch (e) {
+						console.warn(e);
 					}
-				} catch (e) {
-					console.warn(e);
-				}
-				/**
-				 * 不在「未安装」时直接弹下载:部分 ROM 对 isApplicationExist 误判为 false,
-				 * 仍应尝试 openURL,避免一点按钮就「请到应用商店下载」。
-				 */
-				try {
-					plus.runtime.openURL(deepLink);
-				} catch (e2) {
-					console.warn(e2);
-					if (installed === false) {
-						showDownloadTip();
+					/**
+					 * 不在「未安装」时直接弹下载:部分 ROM 对 isApplicationExist 误判为 false,
+					 * 仍应尝试 openURL,避免一点按钮就「请到应用商店下载」。
+					 */
+					try {
+						plus.runtime.openURL(deepLink);
+					} catch (e2) {
+						console.warn(e2);
+						if (installed === false) {
+							showDownloadTip();
+						}
 					}
+					return;
 				}
-				return;
-			}
 
-			var done = false;
-			function finish() {
-				if (done) return;
-				done = true;
-				document.removeEventListener('visibilitychange', onVis);
-				window.removeEventListener('pagehide', onHide);
-			}
-			function onVis() {
-				if (document.visibilityState === 'hidden') finish();
-			}
-			function onHide() {
-				finish();
-			}
-			document.addEventListener('visibilitychange', onVis);
-			window.addEventListener('pagehide', onHide);
+				var done = false;
+				function finish() {
+					if (done) return;
+					done = true;
+					document.removeEventListener('visibilitychange', onVis);
+					window.removeEventListener('pagehide', onHide);
+				}
+				function onVis() {
+					if (document.visibilityState === 'hidden') finish();
+				}
+				function onHide() {
+					finish();
+				}
+				document.addEventListener('visibilitychange', onVis);
+				window.addEventListener('pagehide', onHide);
 
-			if (isWeChatInAppBrowser()) {
-				window.alert('若点击后无法打开 App:请先点右上角「···」,选择「在浏览器中打开」,再点「APP内打开」。');
-			}
+				if (isWeChatInAppBrowser()) {
+					window.alert('若点击后无法打开 App:请先点右上角「···」,选择「在浏览器中打开」,再点「APP内打开」。');
+				}
 
-			try {
-				launchAppDeepLink(deepLink);
-			} catch (e3) {
-				finish();
-				showDownloadTip();
-				return;
-			}
+				try {
+					launchAppDeepLink(deepLink);
+				} catch (e3) {
+					finish();
+					showDownloadTip();
+					return;
+				}
 
-			/**
-			 * 不在超时后弹「去应用商店」:App 已打开时页面常仍 visible,易误报;
-			 * 若未安装,用户无反应可自行去商店,避免打断操作。
-			 */
-			window.setTimeout(function () {
-				finish();
-			}, 3200);
+				/**
+				 * 不在超时后弹「去应用商店」:App 已打开时页面常仍 visible,易误报;
+				 * 若未安装,用户无反应可自行去商店,避免打断操作。
+				 */
+				window.setTimeout(function () {
+					finish();
+				}, 3200);
+			};
+
+			var pf = prefetchShareVideoBeforeAppOpen();
+			if (pf && typeof pf.then === 'function') {
+				pf.then(runOpen).catch(function () {
+					runOpen();
+				});
+			} else {
+				runOpen();
+			}
 		}
 
 		function qs() {
@@ -2024,18 +2034,24 @@
 			return '';
 		}
 
-		/** 唤醒 App 前尽力拉取视频(Cache API / prefetch / fetch);与 App 沙箱缓存独立,仅减轻部分环境下的首包 */
+		/**
+		 * 唤醒 App 前尽力拉取视频(Cache API / prefetch / fetch);与 App 沙箱缓存独立。
+		 * 返回 Promise,便于「先缓存再跳转」;失败仍 resolve,避免阻断打开 App。
+		 */
 		function prefetchShareVideoBeforeAppOpen() {
 			try {
 				var u = pickFirstHttpsMp4ForShareVideoPrecache();
-				if (!u) return;
+				if (!u) return Promise.resolve();
+				var tasks = [];
 				if ('caches' in window && window.caches && window.caches.open) {
-					window.caches
-						.open('shopro-share-dynamic-video-v1')
-						.then(function (cache) {
-							return cache.add(u);
-						})
-						.catch(function () {});
+					tasks.push(
+						window.caches
+							.open('shopro-share-dynamic-video-v1')
+							.then(function (cache) {
+								return cache.add(u);
+							})
+							.catch(function () {})
+					);
 				}
 				try {
 					var lnk = document.createElement('link');
@@ -2045,9 +2061,14 @@
 					document.head.appendChild(lnk);
 				} catch (eL) {}
 				if (typeof fetch === 'function') {
-					fetch(u, { method: 'GET', mode: 'cors', cache: 'force-cache' }).catch(function () {});
+					tasks.push(
+						fetch(u, { method: 'GET', mode: 'cors', cache: 'force-cache' }).catch(function () {})
+					);
 				}
-			} catch (eP) {}
+				return tasks.length ? Promise.all(tasks) : Promise.resolve();
+			} catch (eP) {
+				return Promise.resolve();
+			}
 		}
 
 		var heroI = 0;