zhuli 3 недель назад
Родитель
Сommit
336740b436
1 измененных файлов с 45 добавлено и 11 удалено
  1. 45 11
      HBuilderProjects/shareIndex.html

+ 45 - 11
HBuilderProjects/shareIndex.html

@@ -1090,11 +1090,27 @@
 				'未能打开 App,请确认已安装最新版「U店在哪」;微信内请使用页面底部按钮打开。';
 			if (typeof uni !== 'undefined' && typeof uni.showToast === 'function') {
 				uni.showToast({ title: tip, icon: 'none', duration: 2800 });
+			} else if (isWeChatInAppBrowser()) {
+				window.alert(tip);
 			} else {
 				console.warn('[openApp]', tip);
 			}
 		}
 
+		/** 与公众平台「JS接口安全域名」一致;分享页实际打开的 host 必须在此列表中 */
+		var WECHAT_JS_SAFE_HOSTS = ['uat.ailien.shop', 'test.ailien.shop'];
+
+		function checkWxJsSafeDomainHint() {
+			var host = String(location.hostname || '').toLowerCase();
+			if (!host || host === 'localhost' || host === '127.0.0.1') return '';
+			if (WECHAT_JS_SAFE_HOSTS.indexOf(host) >= 0) return '';
+			return (
+				'当前页面域名为「' +
+				host +
+				'」,未在 WECHAT_JS_SAFE_HOSTS 中;请在公众号后台「JS接口安全域名」添加该域名(不含 https:// 与路径)。'
+			);
+		}
+
 		/** 详情页 onLoad 使用 storeId;分享链常为 id,补全 storeId 避免 App 内参数为空 */
 		function buildAppOpenQueryString() {
 			var raw = (location.search && location.search.charAt(0) === '?') ? location.search.slice(1) : (location.search || '');
@@ -1311,6 +1327,11 @@
 				setFabLaunchMode('scheme');
 				return Promise.resolve(false);
 			}
+			var domainHint = checkWxJsSafeDomainHint();
+			if (domainHint) {
+				console.warn('[wx]', domainHint);
+				showAppOpenFailTip(domainHint);
+			}
 			/** 签名完成前不展示 scheme 按钮,避免误点触发无效唤起 */
 			var fbWrap0 = document.getElementById('fabWrapFallback');
 			if (fbWrap0) {
@@ -1370,20 +1391,33 @@
 				});
 		}
 
-		/** 部分 WebView 对 location.href 拦截,用 a 标签点击略稳 */
+		function isCustomAppSchemeUrl(url) {
+			var s = String(url || '').trim();
+			if (!s) return false;
+			if (/^https?:/i.test(s)) return false;
+			return /^[a-z][a-z0-9+.-]*:/i.test(s);
+		}
+
 		function launchAppDeepLink(deepLink) {
-			
-			try {
-				var a = document.createElement('a');
-				a.href = deepLink;
-				a.setAttribute('target', '_self');
-				document.body.appendChild(a);
-				a.click();
-				document.body.removeChild(a);
-			} catch (e1) {}
+			deepLink = String(deepLink || '').trim();
+			if (!deepLink) return;
+			if (isCustomAppSchemeUrl(deepLink)) {
+				var anchor = document.getElementById('appDeepLinkAnchor');
+				if (!anchor) {
+					anchor = document.createElement('a');
+					anchor.id = 'appDeepLinkAnchor';
+					anchor.setAttribute('aria-hidden', 'true');
+					anchor.style.cssText =
+						'position:fixed;left:-9999px;top:0;width:1px;height:1px;opacity:0;pointer-events:none;';
+					document.body.appendChild(anchor);
+				}
+				anchor.setAttribute('href', deepLink);
+				anchor.click();
+				return;
+			}
 			try {
 				window.location.href = deepLink;
-			} catch (e2) {}
+			} catch (eHref) {}
 		}
 
 		/**