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

This commit is contained in:
sexygoat
2026-04-17 12:20:02 +08:00
parent 5f4a26e693
commit 40b8f76eef
7 changed files with 146 additions and 87 deletions

View File

@@ -69,7 +69,7 @@
import { authApi } from '@/api/index.js';
import { useUserStore } from '@/store/index.js';
import { APP_ID } from '@/utils/env.js';
import { initWxSdk } from '@/utils/wxsdk.js';
import weChatScanService from '@/utils/wxsdk.js';
const userStore = useUserStore();
@@ -126,26 +126,38 @@
const handleScanLogin = async () => {
// #ifdef H5
const result = await initWxSdk();
console.log('微信SDK初始化结果:', result);
if (!result.success) {
uni.showToast({ title: result.error || '扫一扫功能暂不可用', icon: 'none' });
return;
}
wx.scanQRCode({
needResult: 1,
scanType: ['qrCode', 'barCode'],
success: (res) => {
const result = res.resultStr;
try {
// 检查是否在微信环境
if (!weChatScanService.isInWechat()) {
uni.showToast({ title: '请在微信中打开', icon: 'none' });
return;
}
// 初始化微信 SDK
const initResult = await weChatScanService.initWxSDK();
console.log('微信SDK初始化结果:', initResult);
if (!initResult.success) {
uni.showToast({ title: initResult.error || '扫一扫功能暂不可用', icon: 'none' });
return;
}
// 调用扫一扫
const scanResult = await weChatScanService.scanQRCode();
console.log('扫码结果:', scanResult);
if (scanResult.success) {
const resultStr = scanResult.result;
let value = '';
if (result.includes('?')) {
const params = new URLSearchParams(result.split('?')[1]);
// 解析二维码内容
if (resultStr.includes('?')) {
const params = new URLSearchParams(resultStr.split('?')[1]);
value = params.get('cardNum') || '';
}
if (!value) {
const match = result.match(/=([^=]+)$/);
const match = resultStr.match(/=([^=]+)$/);
value = match ? match[1] : '';
}
@@ -155,11 +167,11 @@ const handleScanLogin = async () => {
} else {
uni.showToast({ title: '无效的二维码', icon: 'none' });
}
},
fail: () => {
uni.showToast({ title: '扫码失败', icon: 'none' });
}
});
} catch (err) {
console.error('扫码错误:', err);
uni.showToast({ title: err.message || '扫码失败', icon: 'none' });
}
// #endif
// #ifndef H5
uni.showToast({ title: '请在微信中打开', icon: 'none' });