34 lines
942 B
JavaScript
34 lines
942 B
JavaScript
import { wechatApi } from '@/api/index.js';
|
|
|
|
export const initWxSdk = async () => {
|
|
if (typeof wx === 'undefined') {
|
|
return { success: false, error: '请在微信中打开' };
|
|
}
|
|
|
|
try {
|
|
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({
|
|
debug: true,
|
|
appId: data.app_id,
|
|
timestamp: data.timestamp,
|
|
nonceStr: data.nonce_str,
|
|
signature: data.signature,
|
|
jsApiList: ['scanQRCode']
|
|
});
|
|
|
|
return new Promise((resolve) => {
|
|
wx.ready(() => resolve({ success: true }));
|
|
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 || '获取微信配置失败' };
|
|
}
|
|
}; |