From 39773341bd4722bc95cafe6d0dc571ab57e020d6 Mon Sep 17 00:00:00 2001 From: sexygoat <1538832180@qq.com> Date: Fri, 17 Apr 2026 10:56:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=89=AB=E4=B8=80?= =?UTF-8?q?=E6=89=AB=E7=99=BB=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- manifest.json | 4 ++- pages/login/login.vue | 2 +- utils/wxsdk.js | 78 ++++++++++++++++++++++++++++++++++++++----- 3 files changed, 74 insertions(+), 10 deletions(-) diff --git a/manifest.json b/manifest.json index 3e2ef94..306b67b 100644 --- a/manifest.json +++ b/manifest.json @@ -100,6 +100,8 @@ } } }, - "sdkConfigs" : {} + "sdkConfigs" : { + "maps" : {} + } } } diff --git a/pages/login/login.vue b/pages/login/login.vue index 7cd341a..cab2d88 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -132,7 +132,7 @@ const handleScanLogin = async () => { uni.showToast({ title: result.error || '扫一扫功能暂不可用', icon: 'none' }); return; } - wx.scanQRCode({ + window.wx.scanQRCode({ needResult: 1, scanType: ['qrCode', 'barCode'], success: (res) => { diff --git a/utils/wxsdk.js b/utils/wxsdk.js index 56cb674..5c22fd9 100644 --- a/utils/wxsdk.js +++ b/utils/wxsdk.js @@ -1,17 +1,72 @@ import { wechatApi } from '@/api/index.js'; -export const initWxSdk = async () => { - if (typeof wx === 'undefined') { - return { success: false, error: '请在微信中打开' }; - } +// 动态加载微信 JSSDK +const loadWxJSSDK = () => { + return new Promise((resolve, reject) => { + // 如果已经加载,直接返回 + if (window.wx && typeof window.wx.config === 'function') { + console.log('微信JSSDK已存在'); + resolve(); + return; + } + // 检查是否已经有 script 标签在加载 + const existingScript = document.querySelector('script[src*="jweixin"]'); + if (existingScript) { + console.log('检测到JSSDK脚本标签,等待加载完成...'); + // 等待已有的脚本加载完成 + let count = 0; + const maxCount = 50; + const timer = setInterval(() => { + count++; + if (window.wx && typeof window.wx.config === 'function') { + clearInterval(timer); + console.log('微信JSSDK加载成功'); + resolve(); + } else if (count >= maxCount) { + clearInterval(timer); + reject(new Error('微信JSSDK加载超时')); + } + }, 100); + return; + } + + console.log('开始动态加载微信JSSDK...'); + // 动态创建 script 标签 + const script = document.createElement('script'); + script.type = 'text/javascript'; + script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'; + + script.onload = () => { + console.log('微信JSSDK脚本加载完成'); + if (window.wx && typeof window.wx.config === 'function') { + resolve(); + } else { + reject(new Error('微信JSSDK脚本加载失败')); + } + }; + + script.onerror = () => { + console.error('微信JSSDK脚本加载失败'); + reject(new Error('微信JSSDK脚本加载失败,请检查网络')); + }; + + document.head.appendChild(script); + }); +}; + +export const initWxSdk = async () => { + // #ifdef H5 try { + // 动态加载微信 JSSDK + await loadWxJSSDK(); + const currentUrl = window.location.href.split('#')[0]; console.log('JSSDK URL:', currentUrl); const data = await wechatApi.getJssdkConfig(currentUrl); console.log('JSSDK Config:', data); - wx.config({ + window.wx.config({ debug: true, appId: data.app_id, timestamp: data.timestamp, @@ -21,14 +76,21 @@ export const initWxSdk = async () => { }); return new Promise((resolve) => { - wx.ready(() => resolve({ success: true })); - wx.error((err) => { + window.wx.ready(() => { + console.log('微信SDK配置成功'); + resolve({ success: true }); + }); + window.wx.error((err) => { console.error('wx.config error:', err); resolve({ success: false, error: err.errMsg || '微信配置失败' }); }); }); } catch (e) { console.error('WX SDK init failed', e); - return { success: false, error: e.message || '获取微信配置失败' }; + return { success: false, error: e.message || '微信JSSDK未加载,请在微信中打开' }; } + // #endif + // #ifndef H5 + return { success: false, error: '请在H5环境中使用' }; + // #endif }; \ No newline at end of file