This commit is contained in:
148
utils/wxsdk.js
148
utils/wxsdk.js
@@ -1,48 +1,108 @@
|
||||
import wx from 'jweixin-module';
|
||||
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,
|
||||
/**
|
||||
* 微信 JS-SDK 初始化和扫码功能封装
|
||||
*/
|
||||
class WeChatScanService {
|
||||
constructor() {
|
||||
this.isWxReady = false;
|
||||
this.config = {
|
||||
debug: true, // 开发环境设置为 true 便于调试
|
||||
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
|
||||
};
|
||||
|
||||
/**
|
||||
* 初始化微信 JS-SDK
|
||||
*/
|
||||
async initWxSDK() {
|
||||
try {
|
||||
console.log('[WxSDK] 开始初始化...');
|
||||
|
||||
// 获取当前页面 URL (去掉 hash 部分)
|
||||
const currentUrl = window.location.href.split('#')[0];
|
||||
console.log('[WxSDK] 当前URL:', currentUrl);
|
||||
|
||||
// 调用后端接口获取签名
|
||||
const signatureData = await wechatApi.getJssdkConfig(currentUrl);
|
||||
console.log('[WxSDK] 获取到签名配置:', signatureData);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
wx.config({
|
||||
debug: this.config.debug,
|
||||
appId: signatureData.app_id,
|
||||
timestamp: signatureData.timestamp,
|
||||
nonceStr: signatureData.nonce_str,
|
||||
signature: signatureData.signature,
|
||||
jsApiList: this.config.jsApiList
|
||||
});
|
||||
|
||||
wx.ready(() => {
|
||||
console.log('[WxSDK] 初始化成功');
|
||||
this.isWxReady = true;
|
||||
resolve({ success: true });
|
||||
});
|
||||
|
||||
wx.error((res) => {
|
||||
console.error('[WxSDK] 初始化失败:', res);
|
||||
this.isWxReady = false;
|
||||
reject({ success: false, error: res.errMsg || '微信配置失败' });
|
||||
});
|
||||
});
|
||||
} catch (e) {
|
||||
console.error('[WxSDK] 初始化异常:', e);
|
||||
return { success: false, error: e.message || '初始化失败' };
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用微信扫一扫功能
|
||||
*/
|
||||
async scanQRCode(options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (!this.isWxReady) {
|
||||
reject({ success: false, error: '微信 JS-SDK 未就绪,请先初始化' });
|
||||
return;
|
||||
}
|
||||
|
||||
const defaultOptions = {
|
||||
needResult: 1,
|
||||
scanType: ['qrCode', 'barCode']
|
||||
};
|
||||
|
||||
const scanOptions = { ...defaultOptions, ...options };
|
||||
|
||||
wx.scanQRCode({
|
||||
needResult: scanOptions.needResult,
|
||||
scanType: scanOptions.scanType,
|
||||
success: (res) => {
|
||||
console.log('[WxSDK] 扫码成功:', res);
|
||||
resolve({
|
||||
success: true,
|
||||
result: res.resultStr || res.result,
|
||||
data: res
|
||||
});
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('[WxSDK] 扫码失败:', err);
|
||||
reject({
|
||||
success: false,
|
||||
error: err,
|
||||
message: '扫码失败,请重试'
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查是否在微信环境中
|
||||
*/
|
||||
isInWechat() {
|
||||
return /micromessenger/i.test(navigator.userAgent);
|
||||
}
|
||||
}
|
||||
|
||||
// 导出单例
|
||||
export default new WeChatScanService();
|
||||
Reference in New Issue
Block a user