sunshibo 1 semana atrás
pai
commit
2901bf933b
1 arquivos alterados com 69 adições e 30 exclusões
  1. 69 30
      HBuilderProjects/shareIndex.html

+ 69 - 30
HBuilderProjects/shareIndex.html

@@ -1107,6 +1107,25 @@
 			return /MicroMessenger/i.test(navigator.userAgent || '');
 		}
 
+		function isMobileDevice() {
+			return /Android|iPhone|iPad|iPod|Mobile/i.test(navigator.userAgent || '');
+		}
+
+		/** 微信内、PC 等均无法可靠用 shopro:// 唤起,避免控制台 scheme 报错 */
+		function canLaunchAppByScheme() {
+			if (isWeChatInAppBrowser()) return false;
+			return isMobileDevice();
+		}
+
+		function showWxLaunchTip(msg) {
+			var text = msg || '暂时无法打开 App,请稍后重试';
+			if (typeof uni !== 'undefined' && typeof uni.showToast === 'function') {
+				uni.showToast({ title: text, icon: 'none', duration: 2800 });
+			} else {
+				window.alert(text);
+			}
+		}
+
 		var wxJssdkLoadPromise = null;
 		var wxConfigReady = false;
 		var wxLaunchAppIdCache = '';
@@ -1162,6 +1181,9 @@
 		function normalizeWxConfigPayload(res) {
 			var d = res;
 			if (!res || typeof res !== 'object') return null;
+			if (res.code != null && Number(res.code) !== 200 && res.code !== '200' && res.code !== 0) {
+				return null;
+			}
 			if (res.data != null && typeof res.data === 'object') d = res.data;
 			else if (res.result != null && typeof res.result === 'object') d = res.result;
 			var appId = d.appId || d.appid || d.publicId || '';
@@ -1194,7 +1216,7 @@
 					return;
 				}
 				wx.config({
-					debug: true,
+					debug: false,
 					appId: cfg.appId,
 					timestamp: cfg.timestamp,
 					nonceStr: cfg.nonceStr,
@@ -1255,7 +1277,7 @@
 				});
 				tag.addEventListener('error', function (e) {
 					console.warn('[wx-open-launch-app]', e && e.detail);
-					tryLaunchAppBySchemeOnly();
+					showWxLaunchTip('无法跳转 App,请确认已安装应用且公众号已关联该应用');
 				});
 			}
 
@@ -1268,20 +1290,13 @@
 			return true;
 		}
 
-		function clickWxOpenLaunchInner() {
-			var host = document.getElementById('openAppWxLaunchHost');
-			if (!host) return false;
-			var inner = host.querySelector('.wx-fab-inner');
-			if (inner && typeof inner.click === 'function') {
-				inner.click();
-				return true;
-			}
-			return false;
-		}
-
 		/** 公众号内:先 GET getWxConfig,再 wx.config + 开放标签唤起 App */
 		function prepareWeChatAppLaunch(forceRefresh) {
 			if (!forceRefresh && wxPreparePromise) return wxPreparePromise;
+			if (forceRefresh) {
+				wxPreparePromise = null;
+				wxConfigReady = false;
+			}
 			wxPreparePromise = loadWxJssdk()
 				.then(function () {
 					return fetchWxConfig();
@@ -1305,21 +1320,47 @@
 		}
 
 		function openAppViaWeChat() {
-			prepareWeChatAppLaunch(false)
+			if (wxLaunchMounted && wxConfigReady) {
+				return;
+			}
+			prepareWeChatAppLaunch(true)
 				.then(function () {
-					if (!clickWxOpenLaunchInner()) {
-						tryLaunchAppBySchemeOnly();
+					if (wxLaunchMounted) {
+						showWxLaunchTip('请再次点击「APP内打开」');
+					} else {
+						showWxLaunchTip('微信唤起组件未就绪,请稍后重试');
 					}
 				})
 				.catch(function (e) {
 					console.warn('[微信唤起App]', e);
-					tryLaunchAppBySchemeOnly();
+					var msg = '暂时无法打开 App';
+					if (e && e.message) {
+						if (/HTTP 404/i.test(e.message)) {
+							msg = '微信配置接口不可用,请联系管理员';
+						} else if (/缺少移动应用/i.test(e.message)) {
+							msg = '缺少移动应用 AppId,请在 getWxConfig 返回 launchAppId';
+						} else if (/getWxConfig|签名|wx\.config/i.test(e.message)) {
+							msg = '微信签名失败,请确认 JS 安全域名与页面 URL 一致';
+						}
+					}
+					showWxLaunchTip(msg);
 				});
 		}
 
 		/** 部分 WebView 对 location.href 拦截,用 a 标签点击略稳 */
 		function launchAppDeepLink(deepLink) {
-			
+			if (!canLaunchAppByScheme()) return;
+			try {
+				var iframe = document.createElement('iframe');
+				iframe.style.display = 'none';
+				iframe.src = deepLink;
+				document.body.appendChild(iframe);
+				window.setTimeout(function () {
+					try {
+						document.body.removeChild(iframe);
+					} catch (ignore) {}
+				}, 2000);
+			} catch (e0) {}
 			try {
 				var a = document.createElement('a');
 				a.href = deepLink;
@@ -1328,13 +1369,14 @@
 				a.click();
 				document.body.removeChild(a);
 			} catch (e1) {}
-			try {
-				window.location.href = deepLink;
-			} catch (e2) {}
 		}
 
-		/** 仅 scheme 唤起(无微信提示),供开放标签失败时回退 */
+		/** 移动端系统浏览器 scheme 唤起;微信内不使用 */
 		function tryLaunchAppBySchemeOnly() {
+			if (!canLaunchAppByScheme()) {
+				showDownloadTip();
+				return;
+			}
 			var deepLink = buildAppDeepLink();
 			var t0 = Date.now();
 			var done = false;
@@ -1422,6 +1464,11 @@
 			document.addEventListener('visibilitychange', onVis);
 			window.addEventListener('pagehide', onHide);
 
+			if (!canLaunchAppByScheme()) {
+				showDownloadTip();
+				return;
+			}
+
 			try {
 				launchAppDeepLink(deepLink);
 			} catch (e3) {
@@ -2596,14 +2643,6 @@
 					tryOpenHBuilderApp();
 				});
 			}
-			var wxHost = document.getElementById('openAppWxLaunchHost');
-			if (wxHost) {
-				wxHost.addEventListener('click', function () {
-					if (!wxConfigReady) {
-						openAppViaWeChat();
-					}
-				});
-			}
 		}
 
 		if (document.readyState === 'complete') {