This commit is contained in:
@@ -69,17 +69,29 @@
|
||||
import { authApi } from '@/api/index.js';
|
||||
import { useUserStore } from '@/store/index.js';
|
||||
import { APP_ID } from '@/utils/env.js';
|
||||
import weChatScanService from '@/utils/wxsdk.js';
|
||||
import { initWxConfig, wxScan, isInWechat } from '@/utils/wxsdk.js';
|
||||
|
||||
const userStore = useUserStore();
|
||||
|
||||
onMounted(() => {
|
||||
onMounted(async () => {
|
||||
const token = uni.getStorageSync('token');
|
||||
if (token) {
|
||||
uni.reLaunch({ url: '/pages/index/index' });
|
||||
}
|
||||
handleWechatCallback();
|
||||
getPathDeviceId();
|
||||
|
||||
// 初始化微信 SDK (仅在微信浏览器内执行)
|
||||
// #ifdef H5
|
||||
if (isInWechat()) {
|
||||
try {
|
||||
await initWxConfig();
|
||||
console.log('微信 SDK 初始化成功');
|
||||
} catch (e) {
|
||||
console.error('微信 SDK 初始化失败', e);
|
||||
}
|
||||
}
|
||||
// #endif
|
||||
});
|
||||
|
||||
const identifier = ref('');
|
||||
@@ -128,49 +140,37 @@ const handleScanLogin = async () => {
|
||||
// #ifdef H5
|
||||
try {
|
||||
// 检查是否在微信环境
|
||||
if (!weChatScanService.isInWechat()) {
|
||||
if (!isInWechat()) {
|
||||
uni.showToast({ title: '请在微信中打开', icon: 'none' });
|
||||
return;
|
||||
}
|
||||
|
||||
// 初始化微信 SDK
|
||||
const initResult = await weChatScanService.initWxSDK();
|
||||
console.log('微信SDK初始化结果:', initResult);
|
||||
// 调用扫一扫
|
||||
const resultStr = await wxScan();
|
||||
console.log('扫码结果:', resultStr);
|
||||
|
||||
if (!initResult.success) {
|
||||
uni.showToast({ title: initResult.error || '扫一扫功能暂不可用', icon: 'none' });
|
||||
return;
|
||||
let value = '';
|
||||
|
||||
// 解析二维码内容
|
||||
if (resultStr.includes('?')) {
|
||||
const params = new URLSearchParams(resultStr.split('?')[1]);
|
||||
value = params.get('cardNum') || '';
|
||||
}
|
||||
|
||||
// 调用扫一扫
|
||||
const scanResult = await weChatScanService.scanQRCode();
|
||||
console.log('扫码结果:', scanResult);
|
||||
if (!value) {
|
||||
const match = resultStr.match(/=([^=]+)$/);
|
||||
value = match ? match[1] : '';
|
||||
}
|
||||
|
||||
if (scanResult.success) {
|
||||
const resultStr = scanResult.result;
|
||||
let value = '';
|
||||
|
||||
// 解析二维码内容
|
||||
if (resultStr.includes('?')) {
|
||||
const params = new URLSearchParams(resultStr.split('?')[1]);
|
||||
value = params.get('cardNum') || '';
|
||||
}
|
||||
|
||||
if (!value) {
|
||||
const match = resultStr.match(/=([^=]+)$/);
|
||||
value = match ? match[1] : '';
|
||||
}
|
||||
|
||||
if (value) {
|
||||
identifier.value = value;
|
||||
handleLogin();
|
||||
} else {
|
||||
uni.showToast({ title: '无效的二维码', icon: 'none' });
|
||||
}
|
||||
if (value) {
|
||||
identifier.value = value;
|
||||
handleLogin();
|
||||
} else {
|
||||
uni.showToast({ title: '无效的二维码', icon: 'none' });
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('扫码错误:', err);
|
||||
uni.showToast({ title: err.message || '扫码失败', icon: 'none' });
|
||||
uni.showToast({ title: '扫码取消或失败', icon: 'none' });
|
||||
}
|
||||
// #endif
|
||||
// #ifndef H5
|
||||
|
||||
Reference in New Issue
Block a user