Przeglądaj źródła

微信内打开app

zhuli 2 tygodni temu
rodzic
commit
e8a715329c
1 zmienionych plików z 87 dodań i 3 usunięć
  1. 87 3
      HBuilderProjects/shareIndex.html

+ 87 - 3
HBuilderProjects/shareIndex.html

@@ -544,6 +544,28 @@
 			opacity: 0.2;
 		}
 
+		#openAppToast {
+			display: none;
+			position: fixed;
+			left: 16px;
+			right: 16px;
+			bottom: calc(72px + var(--safe-bottom));
+			z-index: 10001;
+			padding: 10px 14px;
+			font-size: 13px;
+			line-height: 1.45;
+			color: #fff;
+			text-align: center;
+			background: rgba(0, 0, 0, 0.78);
+			border-radius: 8px;
+			pointer-events: none;
+			word-break: break-all;
+		}
+
+		#openApp {
+			touch-action: manipulation;
+		}
+
 		/* 营销活动(getStoreCouponList.marketingList) */
 		.marketing-section {
 			padding: 4px 15px 20px;
@@ -1007,6 +1029,7 @@
 			<div id="staffGroupsMount"></div>
 		</section>
 	</div>
+	<div id="openAppToast" role="status" aria-live="polite"></div>
 	<div id="fabDock" class="fab-wrap">
 		<div class="fab-dock__slot">
 			<button type="button" class="fab" id="openApp">
@@ -1524,11 +1547,56 @@
 			tick();
 		}
 
+		function showFabToast(msg, ms) {
+			var tip = String(msg || '').trim();
+			if (!tip) return;
+			var el = document.getElementById('openAppToast');
+			if (el) {
+				el.textContent = tip;
+				el.style.display = 'block';
+				if (showFabToast._t) clearTimeout(showFabToast._t);
+				showFabToast._t = setTimeout(function () {
+					el.style.display = 'none';
+				}, ms || 2800);
+			}
+			console.log('[openApp]', tip);
+		}
+
+		function tryTriggerWxOpenLaunchApp() {
+			var tag = document.getElementById('launch-btn');
+			if (!tag) return false;
+			try {
+				if (tag.shadowRoot) {
+					var inner = tag.shadowRoot.querySelector('button');
+					if (inner && typeof inner.click === 'function') {
+						inner.click();
+						return true;
+					}
+				}
+				if (typeof tag.click === 'function') {
+					tag.click();
+					return true;
+				}
+			} catch (eWx) {
+				console.warn('[wx-open-launch-app] trigger', eWx);
+			}
+			return false;
+		}
+
 		function ensureWeChatJssdkReadyOnClick() {
+			showFabToast('正在获取微信签名…');
+			var timedOut = false;
+			var timer = window.setTimeout(function () {
+				timedOut = true;
+				showAppOpenFailTip('getWxConfig 请求超时,请检查网络或后端接口');
+			}, 15000);
 			wxJssdkInitPromise = null;
 			return initWeChatOpenLaunchApp().then(function (ok) {
+				window.clearTimeout(timer);
+				if (timedOut) return false;
 				if (ok) {
 					updateFabOpenLayer();
+					showFabToast('请再点一次底部按钮打开 App');
 					return true;
 				}
 				showAppOpenFailTip(
@@ -1573,7 +1641,11 @@
 		 */
 		function tryOpenHBuilderApp() {
 			var inWx = isWeChatInAppBrowser();
+			showFabToast('正在处理…');
+
 			if (inWx && weChatJssdkConfigured) {
+				showFabToast('正在打开 U店在哪…');
+				tryTriggerWxOpenLaunchApp();
 				return;
 			}
 			if (inWx && !weChatJssdkConfigured) {
@@ -1581,6 +1653,7 @@
 				return;
 			}
 
+			showFabToast('正在打开 U店在哪…');
 			var deepLink = buildAppDeepLink();
 
 			if (typeof plus !== 'undefined' && plus.runtime) {
@@ -2783,11 +2856,22 @@
 		function boot() {
 			var launchTag = document.getElementById('launch-btn');
 			if (launchTag) launchTag.setAttribute('appid', WECHAT_OPEN_APP_ID);
+			var openAppClickLock = 0;
+			function onOpenAppActivate(e) {
+				if (e && e.cancelable) e.preventDefault();
+				var now = Date.now();
+				if (now - openAppClickLock < 400) return;
+				openAppClickLock = now;
+				tryOpenHBuilderApp();
+			}
 			var openBtn = document.getElementById('openApp');
 			if (openBtn) {
-				openBtn.addEventListener('click', function () {
-					tryOpenHBuilderApp();
-				});
+				openBtn.addEventListener('click', onOpenAppActivate);
+				openBtn.addEventListener('touchend', onOpenAppActivate);
+			}
+			var fabDock = document.getElementById('fabDock');
+			if (fabDock) {
+				fabDock.addEventListener('touchend', onOpenAppActivate);
 			}
 			/* 先于 run(),避免误以为没调 getWxConfig */
 			scheduleWeChatJssdkBootstrap();