This commit is contained in:
@@ -5,9 +5,9 @@
|
|||||||
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
|
<meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
|
||||||
<title></title>
|
<title></title>
|
||||||
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/misans@4.1.0/lib/Normal/MiSans-Regular.min.css"/>
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/misans@4.1.0/lib/Normal/MiSans-Regular.min.css"/>
|
||||||
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
|
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
|
<script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script>
|
||||||
<div id="app"><!--app-html--></div>
|
<div id="app"><!--app-html--></div>
|
||||||
<script type="module" src="/main.js"></script>
|
<script type="module" src="/main.js"></script>
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ const handleScanLogin = async () => {
|
|||||||
uni.showToast({ title: result.error || '扫一扫功能暂不可用', icon: 'none' });
|
uni.showToast({ title: result.error || '扫一扫功能暂不可用', icon: 'none' });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
window.wx.scanQRCode({
|
wx.scanQRCode({
|
||||||
needResult: 1,
|
needResult: 1,
|
||||||
scanType: ['qrCode', 'barCode'],
|
scanType: ['qrCode', 'barCode'],
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
|
|||||||
@@ -1,67 +1,16 @@
|
|||||||
import { wechatApi } from '@/api/index.js';
|
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 () => {
|
export const initWxSdk = async () => {
|
||||||
// #ifdef H5
|
// #ifdef H5
|
||||||
try {
|
try {
|
||||||
console.log('[initWxSdk] 开始初始化...');
|
console.log('[initWxSdk] 开始初始化...');
|
||||||
|
console.log('[initWxSdk] typeof wx:', typeof wx);
|
||||||
|
|
||||||
// 动态加载微信 JSSDK
|
// 检查 wx 是否存在
|
||||||
await loadWxJSSDK();
|
if (typeof wx === 'undefined') {
|
||||||
|
console.error('[initWxSdk] wx对象不存在,请确保在微信中打开');
|
||||||
|
return { success: false, error: '请在微信中打开' };
|
||||||
|
}
|
||||||
|
|
||||||
const currentUrl = window.location.href.split('#')[0];
|
const currentUrl = window.location.href.split('#')[0];
|
||||||
console.log('[initWxSdk] 当前URL:', currentUrl);
|
console.log('[initWxSdk] 当前URL:', currentUrl);
|
||||||
@@ -69,7 +18,7 @@ export const initWxSdk = async () => {
|
|||||||
const data = await wechatApi.getJssdkConfig(currentUrl);
|
const data = await wechatApi.getJssdkConfig(currentUrl);
|
||||||
console.log('[initWxSdk] 获取到配置:', data);
|
console.log('[initWxSdk] 获取到配置:', data);
|
||||||
|
|
||||||
window.wx.config({
|
wx.config({
|
||||||
debug: true,
|
debug: true,
|
||||||
appId: data.app_id,
|
appId: data.app_id,
|
||||||
timestamp: data.timestamp,
|
timestamp: data.timestamp,
|
||||||
@@ -79,11 +28,11 @@ export const initWxSdk = async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return new Promise((resolve) => {
|
return new Promise((resolve) => {
|
||||||
window.wx.ready(() => {
|
wx.ready(() => {
|
||||||
console.log('[initWxSdk] SDK配置成功');
|
console.log('[initWxSdk] SDK配置成功');
|
||||||
resolve({ success: true });
|
resolve({ success: true });
|
||||||
});
|
});
|
||||||
window.wx.error((err) => {
|
wx.error((err) => {
|
||||||
console.error('[initWxSdk] SDK配置失败:', err);
|
console.error('[initWxSdk] SDK配置失败:', err);
|
||||||
resolve({ success: false, error: err.errMsg || '微信配置失败' });
|
resolve({ success: false, error: err.errMsg || '微信配置失败' });
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -2,5 +2,26 @@ import { defineConfig } from 'vite'
|
|||||||
import uni from '@dcloudio/vite-plugin-uni'
|
import uni from '@dcloudio/vite-plugin-uni'
|
||||||
|
|
||||||
export default defineConfig({
|
export default defineConfig({
|
||||||
plugins: [uni()],
|
plugins: [
|
||||||
|
uni({
|
||||||
|
vueOptions: {
|
||||||
|
template: {
|
||||||
|
compilerOptions: {
|
||||||
|
// 保留微信 JSSDK 相关的全局变量
|
||||||
|
isCustomElement: (tag) => tag === 'wx'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
],
|
||||||
|
build: {
|
||||||
|
rollupOptions: {
|
||||||
|
external: ['wx'],
|
||||||
|
output: {
|
||||||
|
globals: {
|
||||||
|
wx: 'wx'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user