diff --git a/main.js b/main.js index 663627e..49d00b6 100644 --- a/main.js +++ b/main.js @@ -1,7 +1,8 @@ - + // #ifndef VUE3 import Vue from 'vue' import App from './App' +import { initVConsole } from '@/utils/vconsole' Vue.config.productionTip = false @@ -11,15 +12,19 @@ const app = new Vue({ ...App }) app.$mount() -// #endif - +// #endif + // #ifdef VUE3 -import { createSSRApp } from 'vue' -import App from './App.vue' -export function createApp() { - const app = createSSRApp(App) - return { - app - } +import { createSSRApp } from 'vue' +import App from './App.vue' +import { initVConsole } from '@/utils/vconsole' +export function createApp() { + const app = createSSRApp(App) + return { + app + } } -// #endif \ No newline at end of file +// #endif + +// 初始化 vConsole(开发环境) +initVConsole() \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..36a8472 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,61 @@ +{ + "name": "consumer-front", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "vconsole": "^3.15.1" + } + }, + "node_modules/@babel/runtime": { + "version": "7.29.7", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.29.7.tgz", + "integrity": "sha512-Nq8OhGWiZIZGV6hLHoyAKLLcJihP/xFeBMGJoUrxTX2psI8dCifzLhZISFb+VWS3wFMRDmCGw5R+dOySCqPLhw==", + "license": "MIT", + "engines": { + "node": ">=6.9.0" + } + }, + "node_modules/copy-text-to-clipboard": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/copy-text-to-clipboard/-/copy-text-to-clipboard-3.2.2.tgz", + "integrity": "sha512-T6SqyLd1iLuqPA90J5N4cTalrtovCySh58iiZDGJ6FGznbclKh4UI+FGacQSgFzwKG77W7XT5gwbVEbd9cIH1A==", + "license": "MIT", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/core-js": { + "version": "3.49.0", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-3.49.0.tgz", + "integrity": "sha512-es1U2+YTtzpwkxVLwAFdSpaIMyQaq0PBgm3YD1W3Qpsn1NAmO3KSgZfu+oGSWVu6NvLHoHCV/aYcsE5wiB7ALg==", + "hasInstallScript": true, + "license": "MIT", + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/core-js" + } + }, + "node_modules/mutation-observer": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/mutation-observer/-/mutation-observer-1.0.3.tgz", + "integrity": "sha512-M/O/4rF2h776hV7qGMZUH3utZLO/jK7p8rnNgGkjKUw8zCGjRQPxB8z6+5l8+VjRUQ3dNYu4vjqXYLr+U8ZVNA==" + }, + "node_modules/vconsole": { + "version": "3.15.1", + "resolved": "https://registry.npmjs.org/vconsole/-/vconsole-3.15.1.tgz", + "integrity": "sha512-KH8XLdrq9T5YHJO/ixrjivHfmF2PC2CdVoK6RWZB4yftMykYIaXY1mxZYAic70vADM54kpMQF+dYmvl5NRNy1g==", + "license": "MIT", + "dependencies": { + "@babel/runtime": "^7.17.2", + "copy-text-to-clipboard": "^3.0.1", + "core-js": "^3.11.0", + "mutation-observer": "^1.0.3" + } + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..647136a --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "dependencies": { + "vconsole": "^3.15.1" + } +} diff --git a/pages/index/index.vue b/pages/index/index.vue index b969468..1ac7ee9 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -113,6 +113,15 @@ > + + + + 开发工具 + + vConsole 调试 + + + @@ -139,6 +148,7 @@ @@ -403,6 +431,12 @@ page { height: 100%; background-color: #fff; } .menu-text { font-size: 30rpx; color: #333; } .menu-arrow { font-size: 28rpx; color: #ccc; } +/* 开发工具区域 */ +.dev-tools { margin-top: 20rpx; background-color: #fff; border-radius: 16rpx; overflow: hidden; } +.dev-tools-title { padding: 20rpx 28rpx 10rpx; font-size: 24rpx; color: #999; } +.dev-tools .menu-item { align-items: center; } +.dev-tools switch { transform: scale(0.8); } + /* ====== 公众号二维码弹窗 ====== */ .qrcode-popup-mask { position: fixed; diff --git a/utils/vconsole.js b/utils/vconsole.js new file mode 100644 index 0000000..98a1608 --- /dev/null +++ b/utils/vconsole.js @@ -0,0 +1,118 @@ +/** + * vConsole 调试工具管理 + * + * 使用方式: + * 1. URL 参数:?vconsole=1 开启,?vconsole=0 关闭 + * 2. localStorage:设置 'vconsole' 为 '1' 开启,'0' 关闭 + * 3. 代码调用:import { showVConsole, hideVConsole, toggleVConsole } from '@/utils/vconsole' + */ + +let vConsoleInstance = null; + +// 检测是否为开发环境(兼容多种方式) +const isDev = process.env.NODE_ENV !== 'production'; + +/** + * 获取 vConsole 是否开启 + */ +export function isVConsoleEnabled() { + if (!isDev) return false; + + // 优先从 URL 参数获取 + try { + const pages = getCurrentPages(); + if (pages.length > 0) { + const currentPage = pages[pages.length - 1]; + const options = currentPage.$page?.options || currentPage.options || {}; + if (options.vconsole !== undefined) { + return options.vconsole === '1' || options.vconsole === 'true'; + } + } + } catch (e) { + // ignore + } + + // 其次从 localStorage 获取 + try { + return uni.getStorageSync('vconsole') === '1'; + } catch (e) { + return false; + } +} + +/** + * 开启 vConsole + */ +export function showVConsole() { + if (vConsoleInstance) return; + + // 动态加载 vConsole + import('vconsole').then((module) => { + const VConsole = module.default || module; + vConsoleInstance = new VConsole({ + theme: 'dark', + disableLogScrolling: false, + buttonSize: 48, + }); + + // 增强 vConsole 按钮样式,确保不被遮挡 + setTimeout(() => { + const btn = document.querySelector('#__vconsole'); + if (btn) { + btn.style.zIndex = '999999'; + btn.style.position = 'fixed'; + btn.style.bottom = '80px'; + btn.style.right = '10px'; + } + }, 500); + + uni.setStorageSync('vconsole', '1'); + }).catch((err) => { + console.error('[vConsole] 加载失败:', err); + }); +} + +/** + * 关闭 vConsole + */ +export function hideVConsole() { + if (vConsoleInstance) { + vConsoleInstance.destroy(); + vConsoleInstance = null; + } + try { + uni.setStorageSync('vconsole', '0'); + } catch (e) { + // ignore + } +} + +/** + * 切换 vConsole 状态 + */ +export function toggleVConsole() { + if (vConsoleInstance) { + hideVConsole(); + } else { + showVConsole(); + } +} + +/** + * 根据配置初始化 vConsole + */ +export function initVConsole() { + if (!isDev) return; + + if (isVConsoleEnabled()) { + showVConsole(); + } +} + +export default { + isVConsoleEnabled, + showVConsole, + hideVConsole, + toggleVConsole, + initVConsole, +};