fix: 扫一扫
Some checks failed
构建并部署前端到生产环境 / build-and-deploy (push) Failing after 20s

This commit is contained in:
sexygoat
2026-04-17 15:35:03 +08:00
parent fc7305dffe
commit 5dc948efec
2 changed files with 92 additions and 133 deletions

View File

@@ -1,108 +1,67 @@
import * as wx from 'jweixin-module';
import { wechatApi } from '@/api/index.js';
// utils/wxsdk.js —— 封装微信扫一扫
import jweixin from 'jweixin-module'
import { wechatApi } from '@/api/index.js'
/**
* 微信 JS-SDK 初始化和扫码功能封装
* 初始化微信 JS-SDK
*/
class WeChatScanService {
constructor() {
this.isWxReady = false;
this.config = {
debug: true, // 开发环境设置为 true 便于调试
jsApiList: ['scanQRCode']
};
}
export async function initWxConfig() {
// iOS 微信 SPA 需用首次进入的 URL 签名
const url = window.location.href.split('#')[0]
/**
* 初始化微信 JS-SDK
*/
async initWxSDK() {
try {
console.log('[WxSDK] 开始初始化...');
try {
// 调用后端接口获取签名
const data = await wechatApi.getJssdkConfig(url)
const { app_id, timestamp, nonce_str, signature } = data
// 获取当前页面 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);
jweixin.config({
debug: true,
appId: app_id,
timestamp: timestamp,
nonceStr: nonce_str,
signature: signature,
jsApiList: ['scanQRCode']
})
jweixin.ready(() => {
console.log('微信 SDK 就绪')
resolve()
})
jweixin.error((err) => {
console.error('wx.config 失败', err)
reject(err)
})
})
} catch (e) {
console.error('获取微信签名失败', e)
throw e
}
}
// 导出单例
export default new WeChatScanService();
/**
* 调起微信扫一扫
* @returns {Promise<string>} 扫码结果字符串
*/
export function wxScan() {
return new Promise((resolve, reject) => {
jweixin.scanQRCode({
needResult: 1, // 1 = 返回结果给开发者
scanType: ['qrCode', 'barCode'], // 同时支持二维码和条形码
success(res) {
console.log('扫码成功:', res)
resolve(res.resultStr)
},
fail(err) {
console.error('扫码失败:', err)
reject(err)
}
})
})
}
/**
* 检查是否在微信环境中
*/
export function isInWechat() {
return /micromessenger/i.test(navigator.userAgent)
}