From 71606ebfe5dc349a99e4c02323d65c89aeb8b67b Mon Sep 17 00:00:00 2001 From: kk <875203880@qq.com> Date: Fri, 10 Jul 2026 14:02:26 +0800 Subject: [PATCH] =?UTF-8?q?mock=E6=B8=85=E9=99=A4=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E7=8A=B6=E6=80=81=E6=9C=BA=E5=88=B6/mock=E7=9A=84user=5Finfo?= =?UTF-8?q?=E5=AD=97=E6=AE=B5=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/App.vue | 59 ++++++++++++++++++++++++++++++++--------------- src/config/dev.js | 4 +++- 2 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/App.vue b/src/App.vue index 7fb0329..7b9e9f3 100644 --- a/src/App.vue +++ b/src/App.vue @@ -6,31 +6,43 @@ onLaunch: function(options) { console.log('App Launch') - // 0. Mock 模式:清除登录状态,强制走登录/注册流程 + // 0. Mock 模式:仅首次进入时清除登录状态(刷新时保留) if (config.mockWechatLogin) { - uni.removeStorageSync('token'); - uni.removeStorageSync('user_info'); - uni.removeStorageSync('temp_token'); - uni.removeStorageSync('auth_info'); + const mockInitialized = uni.getStorageSync('__mock_initialized'); + if (!mockInitialized) { + uni.removeStorageSync('token'); + uni.removeStorageSync('user_info'); + uni.removeStorageSync('temp_token'); + uni.removeStorageSync('auth_info'); + uni.setStorageSync('__mock_initialized', 'true'); + console.log('[Mock] 首次进入,清除登录状态'); + } else { + console.log('[Mock] 刷新页面,保留登录状态'); + } } // 1. 环境检测 + 缓存 app_no/platform initEnv(); // #ifdef H5 - // 2. 解析 URL 路径中的设备编号(如 /A1036) + // 2. 解析设备编号:优先从 query 参数获取,其次从路径解析 let deviceId = ''; - const path = window.location.pathname; - const segments = path.split('/').filter(Boolean); - if (segments.length > 0) { - const last = segments[segments.length - 1]; - if (/^[A-Z][A-Z0-9]+$/.test(last)) { - deviceId = last; + const urlParams = new URLSearchParams(window.location.search); + deviceId = urlParams.get('device_id') || ''; + + // 兜底:从路径解析(如 /pages/index/index/N1154) + if (!deviceId) { + const path = window.location.pathname; + const segments = path.split('/').filter(Boolean); + if (segments.length > 0) { + const last = segments[segments.length - 1]; + if (/^[A-Z][A-Z0-9]+$/.test(last)) { + deviceId = last; + } } } // 3. 检查 URL 中是否有授权回调的 code - const urlParams = new URLSearchParams(window.location.search); const code = urlParams.get('code'); if (code) { @@ -43,15 +55,24 @@ return; } - // 4. 检查 JWT + // 4. 判断是否为根路径或设备编号路径(如 / 或 /N1154) + const currentPath = window.location.pathname; + const isRootOrDevice = currentPath === '/' || /^\/[A-Z][A-Z0-9]+$/.test(currentPath); + + // 5. 检查 JWT const token = uni.getStorageSync('token'); if (token) { - // 有 JWT → 直接进入首页 - const homeUrl = deviceId - ? `/pages/index/index?device_id=${deviceId}` - : '/pages/index/scan'; - uni.redirectTo({ url: homeUrl }); + if (isRootOrDevice) { + // 根路径/设备编号路径 → 跳转首页 + const homeUrl = deviceId + ? `/pages/index/index?device_id=${deviceId}` + : '/pages/index/scan'; + uni.redirectTo({ url: homeUrl }); + } else { + // 其他业务页面 → 留在当前页面 + console.log('[App] 已登录,保留在当前页面'); + } } else { // 无 JWT → 进入授权 Loading 页 const loadingUrl = deviceId diff --git a/src/config/dev.js b/src/config/dev.js index f0f5f35..a508572 100644 --- a/src/config/dev.js +++ b/src/config/dev.js @@ -27,8 +27,10 @@ const dev = { mockUser: { token: 'mock_token_dev_2024', user_info: { - _id: 'mock_user_001', + user_id: 'mock_user_001', phone: '13264706088', + open_id: 'test_open_id', + union_id: 'test_union_id', nickname: '测试用户', avatar: 'https://img.wewemivending.com/vms/us/img/uploads/2025/12/18/6943d37c7d82b3439.jpeg', },