fix: 修复扫一扫登录
All checks were successful
构建并部署前端到生产环境 / build-and-deploy (push) Successful in 48s

This commit is contained in:
sexygoat
2026-04-17 11:03:04 +08:00
parent 39773341bd
commit f72b77fe0d

View File

@@ -3,54 +3,54 @@ import { wechatApi } from '@/api/index.js';
// 动态加载微信 JSSDK // 动态加载微信 JSSDK
const loadWxJSSDK = () => { const loadWxJSSDK = () => {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
console.log('[JSSDK] 开始检查加载状态...');
console.log('[JSSDK] window.wx =', window.wx);
console.log('[JSSDK] typeof window.wx =', typeof window.wx);
// 如果已经加载,直接返回 // 如果已经加载,直接返回
if (window.wx && typeof window.wx.config === 'function') { if (window.wx && typeof window.wx.config === 'function') {
console.log('微信JSSDK已存在'); console.log('[JSSDK] 已存在,直接使用');
resolve(); resolve();
return; return;
} }
// 检查是否已经有 script 标签在加载 console.log('[JSSDK] 未加载,开始动态加载...');
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 标签 // 动态创建 script 标签
const script = document.createElement('script'); const script = document.createElement('script');
script.type = 'text/javascript'; script.type = 'text/javascript';
script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js'; script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js';
script.onload = () => { script.onload = () => {
console.log('微信JSSDK脚本加载完成'); console.log('[JSSDK] 脚本加载完成,等待wx对象初始化...');
if (window.wx && typeof window.wx.config === 'function') {
resolve(); // 脚本加载完成后,需要等待一小段时间让wx对象被赋值
} else { // 使用轮询检查wx对象是否可用
reject(new Error('微信JSSDK脚本加载失败')); let checkCount = 0;
} const maxCheckCount = 20; // 最多检查2秒 (20 * 100ms)
const checkWx = setInterval(() => {
checkCount++;
console.log(`[JSSDK] 检查wx对象 (${checkCount}/${maxCheckCount})`, window.wx);
if (window.wx && typeof window.wx.config === 'function') {
clearInterval(checkWx);
console.log('[JSSDK] wx对象已就绪');
resolve();
} else if (checkCount >= maxCheckCount) {
clearInterval(checkWx);
console.error('[JSSDK] wx对象初始化超时');
console.error('[JSSDK] window.wx =', window.wx);
reject(new Error('微信JSSDK初始化超时,请在微信中打开'));
}
}, 100);
}; };
script.onerror = () => { script.onerror = (err) => {
console.error('微信JSSDK脚本加载失败'); console.error('[JSSDK] 脚本加载失败', err);
reject(new Error('微信JSSDK脚本加载失败,请检查网络')); reject(new Error('微信JSSDK脚本加载失败,请检查网络'));
}; };
console.log('[JSSDK] 插入 script 标签到 head');
document.head.appendChild(script); document.head.appendChild(script);
}); });
}; };
@@ -58,13 +58,16 @@ const loadWxJSSDK = () => {
export const initWxSdk = async () => { export const initWxSdk = async () => {
// #ifdef H5 // #ifdef H5
try { try {
console.log('[initWxSdk] 开始初始化...');
// 动态加载微信 JSSDK // 动态加载微信 JSSDK
await loadWxJSSDK(); await loadWxJSSDK();
const currentUrl = window.location.href.split('#')[0]; const currentUrl = window.location.href.split('#')[0];
console.log('JSSDK URL:', currentUrl); console.log('[initWxSdk] 当前URL:', currentUrl);
const data = await wechatApi.getJssdkConfig(currentUrl); const data = await wechatApi.getJssdkConfig(currentUrl);
console.log('JSSDK Config:', data); console.log('[initWxSdk] 获取到配置:', data);
window.wx.config({ window.wx.config({
debug: true, debug: true,
@@ -77,17 +80,17 @@ export const initWxSdk = async () => {
return new Promise((resolve) => { return new Promise((resolve) => {
window.wx.ready(() => { window.wx.ready(() => {
console.log('微信SDK配置成功'); console.log('[initWxSdk] SDK配置成功');
resolve({ success: true }); resolve({ success: true });
}); });
window.wx.error((err) => { window.wx.error((err) => {
console.error('wx.config error:', err); console.error('[initWxSdk] SDK配置失败:', err);
resolve({ success: false, error: err.errMsg || '微信配置失败' }); resolve({ success: false, error: err.errMsg || '微信配置失败' });
}); });
}); });
} catch (e) { } catch (e) {
console.error('WX SDK init failed', e); console.error('[initWxSdk] 初始化失败:', e);
return { success: false, error: e.message || '微信JSSDK未加载,请在微信中打开' }; return { success: false, error: e.message || '初始化失败' };
} }
// #endif // #endif
// #ifndef H5 // #ifndef H5