zhuli 1 week ago
parent
commit
ec026c7f49
1 changed files with 20 additions and 14 deletions
  1. 20 14
      HBuilderProjects/shareIndex.html

+ 20 - 14
HBuilderProjects/shareIndex.html

@@ -1090,11 +1090,11 @@
 
 		/**
 		 * 微信 JSSDK(服务号「麦丽恩严U店」)— 仅用于 wx.config 签名,Secret 只放后端
-		 * 后端 GET {API_BASE}/wx/getWxConfig 须用:AppID wx412792c77f47babd + 服务号 AppSecret
+		 * 后端 POST {API_BASE}/wx/getWxConfig,body 传 url(当前页完整地址)须用:AppID wx412792c77f47babd + 服务号 AppSecret
 		 *
 		 * 签名 url 官方要求(invalid signature 排查):
 		 * 1. 前端用 location.href.split('#')[0] 动态取当前页完整 URL(含 ? 参数,不含 #)
-		 * 2. encodeURIComponent 后作为 url 参数传给后端签名
+		 * 2. 作为 body.url 传给后端签名
 		 * 3. wx.config 的 appId/timestamp/nonceStr/signature 须与后端生成签名时完全一致(nonceStr 驼峰)
 		 * 4. appId 须与后端取 jsapi_ticket 的公众号一致
 		 * 5. 分享进入时微信会附加 from、isappinstalled 等参数,必须参与签名,不可用静态写死 URL
@@ -1319,13 +1319,14 @@
 			return getWxSignPageUrlForApi();
 		}
 
-		function buildWxGetConfigRequestUrl(htmlUrl) {
-			return (
-				API_BASE.replace(/\/$/, '') +
-				WECHAT_GET_WX_CONFIG_PATH +
-				'?url=' +
-				encodeURIComponent(String(htmlUrl || '').split('#')[0].trim())
-			);
+		function getWxGetConfigApiUrl() {
+			return API_BASE.replace(/\/$/, '') + WECHAT_GET_WX_CONFIG_PATH;
+		}
+
+		function buildWxGetConfigRequestBody(htmlUrl) {
+			return {
+				url: String(htmlUrl || '').split('#')[0].trim()
+			};
 		}
 
 		function resolveWxConfigAppIdFromSignData(d) {
@@ -1433,7 +1434,7 @@
 		/**
 		 * 教程标准流程:
 		 * htmlUrl = location.href.split('#')[0]
-		 * GET getWxConfig?url=encodeURIComponent(htmlUrl)
+		 * POST getWxConfig,body: { url: htmlUrl }
 		 * success → wx.config({ appId, timestamp, nonceStr, signature, ... })
 		 * 不修改地址栏,保证「浏览器 url」与「签名 url」一致。
 		 */
@@ -1453,15 +1454,20 @@
 				} catch (eDbg) {}
 			}
 
-			var requestUrl = buildWxGetConfigRequestUrl(htmlUrl);
+			var requestUrl = getWxGetConfigApiUrl();
+			var requestBody = buildWxGetConfigRequestBody(htmlUrl);
 			console.log('[wx] htmlUrl=', htmlUrl);
-			console.log('[wx] GET getWxConfig →', requestUrl);
+			console.log('[wx] POST getWxConfig →', requestUrl, requestBody);
 
 			return fetch(requestUrl, {
-				method: 'GET',
+				method: 'POST',
 				mode: 'cors',
 				credentials: 'omit',
-				headers: { Accept: 'application/json' }
+				headers: {
+					Accept: 'application/json',
+					'Content-Type': 'application/json;charset=UTF-8'
+				},
+				body: JSON.stringify(requestBody)
 			})
 				.then(function (r) {
 					if (r.ok) return r.json();