import { wechatApi } from '@/api/index.js'; export const initWxSdk = async () => { // #ifdef H5 try { console.log('[initWxSdk] 开始初始化...'); console.log('[initWxSdk] typeof wx:', typeof wx); // 检查 wx 是否存在 if (typeof wx === 'undefined') { console.error('[initWxSdk] wx对象不存在,请确保在微信中打开'); return { success: false, error: '请在微信中打开' }; } const currentUrl = window.location.href.split('#')[0]; console.log('[initWxSdk] 当前URL:', currentUrl); const data = await wechatApi.getJssdkConfig(currentUrl); console.log('[initWxSdk] 获取到配置:', data); wx.config({ debug: true, appId: data.app_id, timestamp: data.timestamp, nonceStr: data.nonce_str, signature: data.signature, jsApiList: ['scanQRCode'] }); return new Promise((resolve) => { wx.ready(() => { console.log('[initWxSdk] SDK配置成功'); resolve({ success: true }); }); wx.error((err) => { console.error('[initWxSdk] SDK配置失败:', err); resolve({ success: false, error: err.errMsg || '微信配置失败' }); }); }); } catch (e) { console.error('[initWxSdk] 初始化失败:', e); return { success: false, error: e.message || '初始化失败' }; } // #endif // #ifndef H5 return { success: false, error: '请在H5环境中使用' }; // #endif };