mock清除登录状态机制/mock的user_info字段更新
Uni-app H5 Deploy (Prod + Staging) / deploy (release) Successful in 48s

This commit is contained in:
kk
2026-07-10 14:02:26 +08:00
parent a7e0366fb7
commit 71606ebfe5
2 changed files with 43 additions and 20 deletions
+40 -19
View File
@@ -6,31 +6,43 @@
onLaunch: function(options) { onLaunch: function(options) {
console.log('App Launch') console.log('App Launch')
// 0. Mock 模式:清除登录状态,强制走登录/注册流程 // 0. Mock 模式:仅首次进入时清除登录状态(刷新时保留)
if (config.mockWechatLogin) { if (config.mockWechatLogin) {
uni.removeStorageSync('token'); const mockInitialized = uni.getStorageSync('__mock_initialized');
uni.removeStorageSync('user_info'); if (!mockInitialized) {
uni.removeStorageSync('temp_token'); uni.removeStorageSync('token');
uni.removeStorageSync('auth_info'); 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 // 1. 环境检测 + 缓存 app_no/platform
initEnv(); initEnv();
// #ifdef H5 // #ifdef H5
// 2. 解析 URL 路径中的设备编号(如 /A1036) // 2. 解析设备编号:优先从 query 参数获取,其次从路径解析
let deviceId = ''; let deviceId = '';
const path = window.location.pathname; const urlParams = new URLSearchParams(window.location.search);
const segments = path.split('/').filter(Boolean); deviceId = urlParams.get('device_id') || '';
if (segments.length > 0) {
const last = segments[segments.length - 1]; // 兜底:从路径解析(如 /pages/index/index/N1154
if (/^[A-Z][A-Z0-9]+$/.test(last)) { if (!deviceId) {
deviceId = last; 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 // 3. 检查 URL 中是否有授权回调的 code
const urlParams = new URLSearchParams(window.location.search);
const code = urlParams.get('code'); const code = urlParams.get('code');
if (code) { if (code) {
@@ -43,15 +55,24 @@
return; 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'); const token = uni.getStorageSync('token');
if (token) { if (token) {
// 有 JWT → 直接进入首页 if (isRootOrDevice) {
const homeUrl = deviceId // 根路径/设备编号路径 → 跳转首页
? `/pages/index/index?device_id=${deviceId}` const homeUrl = deviceId
: '/pages/index/scan'; ? `/pages/index/index?device_id=${deviceId}`
uni.redirectTo({ url: homeUrl }); : '/pages/index/scan';
uni.redirectTo({ url: homeUrl });
} else {
// 其他业务页面 → 留在当前页面
console.log('[App] 已登录,保留在当前页面');
}
} else { } else {
// 无 JWT → 进入授权 Loading 页 // 无 JWT → 进入授权 Loading 页
const loadingUrl = deviceId const loadingUrl = deviceId
+3 -1
View File
@@ -27,8 +27,10 @@ const dev = {
mockUser: { mockUser: {
token: 'mock_token_dev_2024', token: 'mock_token_dev_2024',
user_info: { user_info: {
_id: 'mock_user_001', user_id: 'mock_user_001',
phone: '13264706088', phone: '13264706088',
open_id: 'test_open_id',
union_id: 'test_union_id',
nickname: '测试用户', nickname: '测试用户',
avatar: 'https://img.wewemivending.com/vms/us/img/uploads/2025/12/18/6943d37c7d82b3439.jpeg', avatar: 'https://img.wewemivending.com/vms/us/img/uploads/2025/12/18/6943d37c7d82b3439.jpeg',
}, },