From 1e03191e820d3f1cbc79721634edab5bec3f04d3 Mon Sep 17 00:00:00 2001 From: sexygoat <1538832180@qq.com> Date: Fri, 17 Apr 2026 15:57:13 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=89=AB=E4=B8=80=E6=89=AB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pages/login/login.vue | 16 ++++++++++++++-- utils/wxsdk.js | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 4 deletions(-) diff --git a/pages/login/login.vue b/pages/login/login.vue index 797e880..7d2d113 100644 --- a/pages/login/login.vue +++ b/pages/login/login.vue @@ -69,7 +69,7 @@ import { authApi } from '@/api/index.js'; import { useUserStore } from '@/store/index.js'; import { APP_ID } from '@/utils/env.js'; - import { wxScan, isInWechat } from '@/utils/wxsdk.js'; + import { initWxConfig, wxScan, isInWechat } from '@/utils/wxsdk.js'; const userStore = useUserStore(); @@ -80,6 +80,18 @@ } handleWechatCallback(); getPathDeviceId(); + + // 初始化微信 SDK (仅在微信浏览器内执行) + // #ifdef H5 + if (isInWechat()) { + try { + await initWxConfig(); + console.log('微信 SDK 初始化成功'); + } catch (e) { + console.error('微信 SDK 初始化失败', e); + } + } + // #endif }); const identifier = ref(''); @@ -158,7 +170,7 @@ const handleScanLogin = async () => { } } catch (err) { console.error('扫码错误:', err); - uni.showToast({ title: '扫码取消或失败', icon: 'none' }); + uni.showToast({ title: JSON.stringify(err), icon: 'none' }); } // #endif // #ifndef H5 diff --git a/utils/wxsdk.js b/utils/wxsdk.js index 9bd1f3e..490eae3 100644 --- a/utils/wxsdk.js +++ b/utils/wxsdk.js @@ -1,4 +1,40 @@ // utils/wxsdk.js —— 封装微信扫一扫 +import wx from '@/node_modules/jweixin-module/lib/index' +import { wechatApi } from '@/api/index.js' + +/** + * 初始化微信 JS-SDK + */ +export async function initWxConfig() { + const url = window.location.href.split('#')[0] + + try { + const data = await wechatApi.getJssdkConfig(url) + const { app_id, timestamp, nonce_str, signature } = data + + return new Promise((resolve, reject) => { + wx.config({ + debug: true, + appId: app_id, + timestamp: timestamp, + nonceStr: nonce_str, + signature: signature, + jsApiList: ['scanQRCode'] + }) + wx.ready(() => { + console.log('微信 SDK 就绪') + resolve() + }) + wx.error((err) => { + console.error('wx.config 失败', err) + reject(err) + }) + }) + } catch (e) { + console.error('获取微信签名失败', e) + throw e + } +} /** * 调起微信扫一扫 @@ -6,11 +42,12 @@ */ export function wxScan() { return new Promise((resolve, reject) => { - uni.scanCode({ + wx.scanQRCode({ + needResult: 1, scanType: ['qrCode', 'barCode'], success(res) { console.log('扫码成功:', res) - resolve(res.result) + resolve(res.resultStr) }, fail(err) { console.error('扫码失败:', err)