This commit is contained in:
@@ -1,67 +1,16 @@
|
||||
import { wechatApi } from '@/api/index.js';
|
||||
|
||||
// 动态加载微信 JSSDK
|
||||
const loadWxJSSDK = () => {
|
||||
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') {
|
||||
console.log('[JSSDK] 已存在,直接使用');
|
||||
resolve();
|
||||
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] 脚本加载完成,等待wx对象初始化...');
|
||||
|
||||
// 脚本加载完成后,需要等待一小段时间让wx对象被赋值
|
||||
// 使用轮询检查wx对象是否可用
|
||||
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 = (err) => {
|
||||
console.error('[JSSDK] 脚本加载失败', err);
|
||||
reject(new Error('微信JSSDK脚本加载失败,请检查网络'));
|
||||
};
|
||||
|
||||
console.log('[JSSDK] 插入 script 标签到 head');
|
||||
document.head.appendChild(script);
|
||||
});
|
||||
};
|
||||
|
||||
export const initWxSdk = async () => {
|
||||
// #ifdef H5
|
||||
try {
|
||||
console.log('[initWxSdk] 开始初始化...');
|
||||
console.log('[initWxSdk] typeof wx:', typeof wx);
|
||||
|
||||
// 动态加载微信 JSSDK
|
||||
await loadWxJSSDK();
|
||||
// 检查 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);
|
||||
@@ -69,7 +18,7 @@ export const initWxSdk = async () => {
|
||||
const data = await wechatApi.getJssdkConfig(currentUrl);
|
||||
console.log('[initWxSdk] 获取到配置:', data);
|
||||
|
||||
window.wx.config({
|
||||
wx.config({
|
||||
debug: true,
|
||||
appId: data.app_id,
|
||||
timestamp: data.timestamp,
|
||||
@@ -79,11 +28,11 @@ export const initWxSdk = async () => {
|
||||
});
|
||||
|
||||
return new Promise((resolve) => {
|
||||
window.wx.ready(() => {
|
||||
wx.ready(() => {
|
||||
console.log('[initWxSdk] SDK配置成功');
|
||||
resolve({ success: true });
|
||||
});
|
||||
window.wx.error((err) => {
|
||||
wx.error((err) => {
|
||||
console.error('[initWxSdk] SDK配置失败:', err);
|
||||
resolve({ success: false, error: err.errMsg || '微信配置失败' });
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user