From 5f4a26e693f90db22988792cf447c62e64b9511a Mon Sep 17 00:00:00 2001
From: sexygoat <1538832180@qq.com>
Date: Fri, 17 Apr 2026 11:18:46 +0800
Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=89=AB=E4=B8=80?=
=?UTF-8?q?=E6=89=AB=E7=99=BB=E5=BD=95?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
index.html | 2 +-
pages/login/login.vue | 2 +-
utils/wxsdk.js | 69 ++++++-------------------------------------
vite.config.js | 23 ++++++++++++++-
4 files changed, 33 insertions(+), 63 deletions(-)
diff --git a/index.html b/index.html
index 95303c1..dadd21a 100644
--- a/index.html
+++ b/index.html
@@ -5,9 +5,9 @@
-
+
diff --git a/pages/login/login.vue b/pages/login/login.vue
index cab2d88..7cd341a 100644
--- a/pages/login/login.vue
+++ b/pages/login/login.vue
@@ -132,7 +132,7 @@ const handleScanLogin = async () => {
uni.showToast({ title: result.error || '扫一扫功能暂不可用', icon: 'none' });
return;
}
- window.wx.scanQRCode({
+ wx.scanQRCode({
needResult: 1,
scanType: ['qrCode', 'barCode'],
success: (res) => {
diff --git a/utils/wxsdk.js b/utils/wxsdk.js
index 1b858dd..537091d 100644
--- a/utils/wxsdk.js
+++ b/utils/wxsdk.js
@@ -1,67 +1,16 @@
import { wechatApi } from '@/api/index.js';
-// 动态加载微信 JSSDK
-const loadWxJSSDK = () => {
- return new Promise((resolve, reject) => {
- console.log('[JSSDK] 开始检查加载状态...');
- console.log('[JSSDK] window.wx =', window.wx);
- console.log('[JSSDK] typeof window.wx =', typeof window.wx);
-
- // 如果已经加载,直接返回
- if (window.wx && typeof window.wx.config === 'function') {
- console.log('[JSSDK] 已存在,直接使用');
- resolve();
- return;
- }
-
- console.log('[JSSDK] 未加载,开始动态加载...');
- // 动态创建 script 标签
- const script = document.createElement('script');
- script.type = 'text/javascript';
- script.src = 'https://res.wx.qq.com/open/js/jweixin-1.6.0.js';
-
- script.onload = () => {
- console.log('[JSSDK] 脚本加载完成,等待wx对象初始化...');
-
- // 脚本加载完成后,需要等待一小段时间让wx对象被赋值
- // 使用轮询检查wx对象是否可用
- let checkCount = 0;
- const maxCheckCount = 20; // 最多检查2秒 (20 * 100ms)
-
- const checkWx = setInterval(() => {
- checkCount++;
- console.log(`[JSSDK] 检查wx对象 (${checkCount}/${maxCheckCount})`, window.wx);
-
- if (window.wx && typeof window.wx.config === 'function') {
- clearInterval(checkWx);
- console.log('[JSSDK] wx对象已就绪');
- resolve();
- } else if (checkCount >= maxCheckCount) {
- clearInterval(checkWx);
- console.error('[JSSDK] wx对象初始化超时');
- console.error('[JSSDK] window.wx =', window.wx);
- reject(new Error('微信JSSDK初始化超时,请在微信中打开'));
- }
- }, 100);
- };
-
- script.onerror = (err) => {
- console.error('[JSSDK] 脚本加载失败', err);
- reject(new Error('微信JSSDK脚本加载失败,请检查网络'));
- };
-
- console.log('[JSSDK] 插入 script 标签到 head');
- document.head.appendChild(script);
- });
-};
-
export const initWxSdk = async () => {
// #ifdef H5
try {
console.log('[initWxSdk] 开始初始化...');
+ console.log('[initWxSdk] typeof wx:', typeof wx);
- // 动态加载微信 JSSDK
- await loadWxJSSDK();
+ // 检查 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);
@@ -69,7 +18,7 @@ export const initWxSdk = async () => {
const data = await wechatApi.getJssdkConfig(currentUrl);
console.log('[initWxSdk] 获取到配置:', data);
- window.wx.config({
+ wx.config({
debug: true,
appId: data.app_id,
timestamp: data.timestamp,
@@ -79,11 +28,11 @@ export const initWxSdk = async () => {
});
return new Promise((resolve) => {
- window.wx.ready(() => {
+ wx.ready(() => {
console.log('[initWxSdk] SDK配置成功');
resolve({ success: true });
});
- window.wx.error((err) => {
+ wx.error((err) => {
console.error('[initWxSdk] SDK配置失败:', err);
resolve({ success: false, error: err.errMsg || '微信配置失败' });
});
diff --git a/vite.config.js b/vite.config.js
index c0b38e0..13b88e9 100644
--- a/vite.config.js
+++ b/vite.config.js
@@ -2,5 +2,26 @@ import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
export default defineConfig({
- plugins: [uni()],
+ plugins: [
+ uni({
+ vueOptions: {
+ template: {
+ compilerOptions: {
+ // 保留微信 JSSDK 相关的全局变量
+ isCustomElement: (tag) => tag === 'wx'
+ }
+ }
+ }
+ })
+ ],
+ build: {
+ rollupOptions: {
+ external: ['wx'],
+ output: {
+ globals: {
+ wx: 'wx'
+ }
+ }
+ }
+ }
})