zhuli 3 هفته پیش
والد
کامیت
9ca196e522
1فایلهای تغییر یافته به همراه71 افزوده شده و 23 حذف شده
  1. 71 23
      HBuilderProjects/shareIndex.html

+ 71 - 23
HBuilderProjects/shareIndex.html

@@ -1051,7 +1051,8 @@
 			'/wx/jsapi/signature'
 			'/wx/jsapi/signature'
 		];
 		];
 		var H5_PAGE_BASE_FALLBACK = 'https://test.ailien.shop/h5/HBuilderProjects/';
 		var H5_PAGE_BASE_FALLBACK = 'https://test.ailien.shop/h5/HBuilderProjects/';
-		var weChatOpenTagReady = false;
+		/** wx.config 已成功(开放标签可展示);与「用户已成功唤起 App」不是同一回事 */
+		var weChatJssdkConfigured = false;
 
 
 		/**
 		/**
 		 * 关店(businessStatus=99)更多推荐:POST …/ai/multimodal-services/api/v1/search/global/store-recommend
 		 * 关店(businessStatus=99)更多推荐:POST …/ai/multimodal-services/api/v1/search/global/store-recommend
@@ -1083,6 +1084,17 @@
 			}
 			}
 		}
 		}
 
 
+		function showAppOpenFailTip(msg) {
+			var tip =
+				msg ||
+				'未能打开 App,请确认已安装最新版「U店在哪」;微信内请使用页面底部按钮打开。';
+			if (typeof uni !== 'undefined' && typeof uni.showToast === 'function') {
+				uni.showToast({ title: tip, icon: 'none', duration: 2800 });
+			} else {
+				console.warn('[openApp]', tip);
+			}
+		}
+
 		/** 详情页 onLoad 使用 storeId;分享链常为 id,补全 storeId 避免 App 内参数为空 */
 		/** 详情页 onLoad 使用 storeId;分享链常为 id,补全 storeId 避免 App 内参数为空 */
 		function buildAppOpenQueryString() {
 		function buildAppOpenQueryString() {
 			var raw = (location.search && location.search.charAt(0) === '?') ? location.search.slice(1) : (location.search || '');
 			var raw = (location.search && location.search.charAt(0) === '?') ? location.search.slice(1) : (location.search || '');
@@ -1142,11 +1154,26 @@
 			return u;
 			return u;
 		}
 		}
 
 
+		/**
+		 * 微信开放标签 extinfo:优先传当前 H5 分享链(与 App 解析分享 URL 一致);
+		 * 超长时再传 path+query JSON,最后才用 shopro://。
+		 */
 		function buildWeChatLaunchExtinfo() {
 		function buildWeChatLaunchExtinfo() {
+			var pageUrl = getPageUrlForWxSign();
+			if (pageUrl && pageUrl.length <= 1024) return pageUrl;
+			var path = getAppUniPathForBusinessSection().replace(/^\//, '');
+			var qs = buildAppOpenQueryString().replace(/^\?/, '');
+			try {
+				var pack = JSON.stringify({
+					path: path,
+					query: qs,
+					h5: pageUrl
+				});
+				if (pack.length <= 1024) return pack;
+			} catch (eJson) {}
 			var deep = buildAppDeepLink();
 			var deep = buildAppDeepLink();
 			if (deep && deep.length <= 1024) return deep;
 			if (deep && deep.length <= 1024) return deep;
-			var page = getPageUrlForWxSign();
-			return page.length <= 1024 ? page : page.slice(0, 1024);
+			return pageUrl ? pageUrl.slice(0, 1024) : deep.slice(0, 1024);
 		}
 		}
 
 
 		function absH5AssetUrl(rel) {
 		function absH5AssetUrl(rel) {
@@ -1163,7 +1190,8 @@
 			if (!res || typeof res !== 'object') return null;
 			if (!res || typeof res !== 'object') return null;
 			var d = res.data != null && typeof res.data === 'object' ? res.data : res;
 			var d = res.data != null && typeof res.data === 'object' ? res.data : res;
 			if (!d || typeof d !== 'object') return null;
 			if (!d || typeof d !== 'object') return null;
-			var appId = d.appId || d.appid || WECHAT_OPEN_APP_ID;
+			/** wx.config 必须用公众号 appId;不可用移动应用 WECHAT_OPEN_APP_ID 顶替 */
+			var appId = d.appId || d.appid || d.wxAppId;
 			var timestamp = d.timestamp != null ? d.timestamp : d.timeStamp;
 			var timestamp = d.timestamp != null ? d.timestamp : d.timeStamp;
 			var nonceStr = d.nonceStr || d.noncestr || d.nonce;
 			var nonceStr = d.nonceStr || d.noncestr || d.nonce;
 			var signature = d.signature || d.sign;
 			var signature = d.signature || d.sign;
@@ -1255,12 +1283,26 @@
 				tag.setAttribute('extinfo', buildWeChatLaunchExtinfo());
 				tag.setAttribute('extinfo', buildWeChatLaunchExtinfo());
 			} catch (eExt) {}
 			} catch (eExt) {}
 			tag.addEventListener('launch', function () {
 			tag.addEventListener('launch', function () {
-				weChatOpenTagReady = true;
+				console.log('[wx-open-launch-app] launch ok');
 			});
 			});
 			tag.addEventListener('error', function (e) {
 			tag.addEventListener('error', function (e) {
-				console.warn('[wx-open-launch-app]', e && e.detail);
-				weChatOpenTagReady = false;
-				setFabLaunchMode('scheme');
+				var detail = e && e.detail;
+				console.warn('[wx-open-launch-app]', detail);
+				var errMsg =
+					detail && detail.errMsg
+						? String(detail.errMsg)
+						: detail && detail.errmsg
+							? String(detail.errmsg)
+							: '';
+				if (/launch:fail/i.test(errMsg)) {
+					showAppOpenFailTip(
+						'微信未能唤起 App(' +
+							errMsg +
+							')。请确认已安装 App,且移动应用已与公众号关联。'
+					);
+				} else {
+					showAppOpenFailTip();
+				}
 			});
 			});
 		}
 		}
 
 
@@ -1285,6 +1327,9 @@
 				.then(function (sign) {
 				.then(function (sign) {
 					if (!sign) {
 					if (!sign) {
 						console.warn('[wx] JSSDK sign unavailable — check WECHAT_JSSDK_SIGN_PATHS or ?wxSignPath=');
 						console.warn('[wx] JSSDK sign unavailable — check WECHAT_JSSDK_SIGN_PATHS or ?wxSignPath=');
+						showAppOpenFailTip(
+							'微信打开 App 需后端 JSSDK 签名接口,当前未配置成功,无法使用「APP内打开」。'
+						);
 						setFabLaunchMode('scheme');
 						setFabLaunchMode('scheme');
 						return false;
 						return false;
 					}
 					}
@@ -1299,7 +1344,7 @@
 							openTagList: ['wx-open-launch-app']
 							openTagList: ['wx-open-launch-app']
 						});
 						});
 						wx.ready(function () {
 						wx.ready(function () {
-							weChatOpenTagReady = true;
+							weChatJssdkConfigured = true;
 							var tag = document.getElementById('launch-btn');
 							var tag = document.getElementById('launch-btn');
 							if (tag) {
 							if (tag) {
 								try {
 								try {
@@ -1311,7 +1356,8 @@
 						});
 						});
 						wx.error(function (err) {
 						wx.error(function (err) {
 							console.warn('[wx.config]', err);
 							console.warn('[wx.config]', err);
-							weChatOpenTagReady = false;
+							weChatJssdkConfigured = false;
+							showAppOpenFailTip('微信 JSSDK 配置失败,开放标签不可用。');
 							setFabLaunchMode('scheme');
 							setFabLaunchMode('scheme');
 							resolve(false);
 							resolve(false);
 						});
 						});
@@ -1345,11 +1391,14 @@
 		 * 系统浏览器:scheme 唤起;微信内优先 wx-open-launch-app,失败再 scheme(不再弹「在浏览器中打开」)。
 		 * 系统浏览器:scheme 唤起;微信内优先 wx-open-launch-app,失败再 scheme(不再弹「在浏览器中打开」)。
 		 */
 		 */
 		function tryOpenHBuilderApp() {
 		function tryOpenHBuilderApp() {
-			if (isWeChatInAppBrowser() && weChatOpenTagReady) {
+			/** 微信内且开放标签已配置:仅开放标签可唤起,scheme 会被拦截,勿误报「去应用商店」 */
+			if (isWeChatInAppBrowser() && weChatJssdkConfigured) {
+				showAppOpenFailTip('请使用页面底部「APP内打开」按钮(微信开放标签)。');
 				return;
 				return;
 			}
 			}
 
 
 			var deepLink = buildAppDeepLink();
 			var deepLink = buildAppDeepLink();
+			var inWx = isWeChatInAppBrowser();
 
 
 			if (typeof plus !== 'undefined' && plus.runtime) {
 			if (typeof plus !== 'undefined' && plus.runtime) {
 				var installed = null;
 				var installed = null;
@@ -1363,20 +1412,19 @@
 				} catch (e) {
 				} catch (e) {
 					console.warn(e);
 					console.warn(e);
 				}
 				}
-				if (installed === false) {
-					showDownloadTip();
-					return;
-				}
 				try {
 				try {
 					plus.runtime.openURL(deepLink);
 					plus.runtime.openURL(deepLink);
 				} catch (e2) {
 				} catch (e2) {
 					console.warn(e2);
 					console.warn(e2);
-					showDownloadTip();
+					if (installed === false) {
+						showDownloadTip();
+					} else if (!inWx) {
+						showAppOpenFailTip();
+					}
 				}
 				}
 				return;
 				return;
 			}
 			}
 
 
-			var t0 = Date.now();
 			var done = false;
 			var done = false;
 			function finish() {
 			function finish() {
 				if (done) return;
 				if (done) return;
@@ -1397,17 +1445,17 @@
 				launchAppDeepLink(deepLink);
 				launchAppDeepLink(deepLink);
 			} catch (e3) {
 			} catch (e3) {
 				finish();
 				finish();
-				showDownloadTip();
+				if (!inWx) showAppOpenFailTip();
 				return;
 				return;
 			}
 			}
 
 
+			/**
+			 * 微信内 scheme 常被拦截且页面仍 visible,原 2.6s 后 showDownloadTip 会误报「请到应用商店下载」。
+			 * 与 shareDynamic 一致:超时仅收尾,不弹下载提示。
+			 */
 			window.setTimeout(function () {
 			window.setTimeout(function () {
-				if (done) return;
-				if (document.visibilityState === 'visible' && Date.now() - t0 < 3500) {
-					showDownloadTip();
-				}
 				finish();
 				finish();
-			}, 2600);
+			}, 3200);
 		}
 		}
 
 
 		function qs() {
 		function qs() {