2026-06-26 17:55:48 +08:00
|
|
|
|
<script>
|
|
|
|
|
|
import { initEnv } from '@/utils/env.js';
|
2026-07-09 18:49:56 +08:00
|
|
|
|
import config from '@/config/env.js';
|
2026-06-26 17:55:48 +08:00
|
|
|
|
|
|
|
|
|
|
export default {
|
|
|
|
|
|
onLaunch: function(options) {
|
|
|
|
|
|
console.log('App Launch')
|
|
|
|
|
|
|
2026-07-10 14:02:26 +08:00
|
|
|
|
// 0. Mock 模式:仅首次进入时清除登录状态(刷新时保留)
|
2026-07-09 18:49:56 +08:00
|
|
|
|
if (config.mockWechatLogin) {
|
2026-07-10 14:02:26 +08:00
|
|
|
|
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] 刷新页面,保留登录状态');
|
|
|
|
|
|
}
|
2026-07-09 18:49:56 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-06-26 17:55:48 +08:00
|
|
|
|
// 1. 环境检测 + 缓存 app_no/platform
|
|
|
|
|
|
initEnv();
|
|
|
|
|
|
|
|
|
|
|
|
// #ifdef H5
|
2026-07-10 14:02:26 +08:00
|
|
|
|
// 2. 解析设备编号:优先从 query 参数获取,其次从路径解析
|
2026-07-02 18:25:23 +08:00
|
|
|
|
let deviceId = '';
|
2026-07-10 14:02:26 +08:00
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
|
|
deviceId = urlParams.get('device_id') || '';
|
|
|
|
|
|
|
2026-07-11 11:01:22 +08:00
|
|
|
|
// 兜底:从路径解析(如 /N1154)
|
2026-07-10 14:02:26 +08:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2026-06-26 17:55:48 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-07-02 18:25:23 +08:00
|
|
|
|
|
|
|
|
|
|
// 3. 检查 URL 中是否有授权回调的 code
|
|
|
|
|
|
const code = urlParams.get('code');
|
|
|
|
|
|
|
|
|
|
|
|
if (code) {
|
2026-07-11 11:01:22 +08:00
|
|
|
|
// 有 code → 进入授权 Loading 页处理
|
2026-07-11 11:37:53 +08:00
|
|
|
|
// 从 state 中解析 device_id 和 action(OAuth 标准保证 state 原样回传)
|
|
|
|
|
|
let action = '';
|
|
|
|
|
|
let callbackDeviceId = deviceId || '';
|
|
|
|
|
|
try {
|
|
|
|
|
|
const stateStr = urlParams.get('state');
|
|
|
|
|
|
if (stateStr) {
|
|
|
|
|
|
const stateObj = JSON.parse(decodeURIComponent(stateStr));
|
|
|
|
|
|
action = stateObj.action || action;
|
|
|
|
|
|
callbackDeviceId = stateObj.device_id || callbackDeviceId;
|
|
|
|
|
|
}
|
|
|
|
|
|
} catch (_) {
|
|
|
|
|
|
// state 解析失败,忽略
|
|
|
|
|
|
}
|
2026-07-11 12:12:53 +08:00
|
|
|
|
// 存入 localStorage,确保 loading 页面复用时也能获取(redirectTo 可能不触发 onLoad)
|
|
|
|
|
|
if (action) {
|
|
|
|
|
|
localStorage.setItem('__auth_action', action);
|
|
|
|
|
|
}
|
|
|
|
|
|
if (callbackDeviceId) {
|
|
|
|
|
|
localStorage.setItem('__auth_device_id', callbackDeviceId);
|
|
|
|
|
|
}
|
2026-07-02 19:32:07 +08:00
|
|
|
|
let loadingUrl = '/pages/auth/loading?code=' + encodeURIComponent(code);
|
2026-07-11 10:28:43 +08:00
|
|
|
|
if (action) {
|
|
|
|
|
|
loadingUrl += '&action=' + action;
|
|
|
|
|
|
}
|
2026-07-11 11:01:22 +08:00
|
|
|
|
if (callbackDeviceId) {
|
|
|
|
|
|
loadingUrl += '&device_id=' + callbackDeviceId;
|
2026-07-02 19:32:07 +08:00
|
|
|
|
}
|
2026-07-02 18:25:23 +08:00
|
|
|
|
uni.redirectTo({ url: loadingUrl });
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-07-10 14:02:26 +08:00
|
|
|
|
// 4. 判断是否为根路径或设备编号路径(如 / 或 /N1154)
|
|
|
|
|
|
const currentPath = window.location.pathname;
|
|
|
|
|
|
const isRootOrDevice = currentPath === '/' || /^\/[A-Z][A-Z0-9]+$/.test(currentPath);
|
|
|
|
|
|
|
|
|
|
|
|
// 5. 检查 JWT
|
2026-07-02 18:25:23 +08:00
|
|
|
|
const token = uni.getStorageSync('token');
|
|
|
|
|
|
|
|
|
|
|
|
if (token) {
|
2026-07-10 14:02:26 +08:00
|
|
|
|
if (isRootOrDevice) {
|
|
|
|
|
|
// 根路径/设备编号路径 → 跳转首页
|
|
|
|
|
|
const homeUrl = deviceId
|
|
|
|
|
|
? `/pages/index/index?device_id=${deviceId}`
|
|
|
|
|
|
: '/pages/index/scan';
|
|
|
|
|
|
uni.redirectTo({ url: homeUrl });
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// 其他业务页面 → 留在当前页面
|
|
|
|
|
|
console.log('[App] 已登录,保留在当前页面');
|
|
|
|
|
|
}
|
2026-07-02 18:25:23 +08:00
|
|
|
|
} else {
|
|
|
|
|
|
// 无 JWT → 进入授权 Loading 页
|
|
|
|
|
|
const loadingUrl = deviceId
|
|
|
|
|
|
? `/pages/auth/loading?device_id=${deviceId}`
|
|
|
|
|
|
: '/pages/auth/loading';
|
|
|
|
|
|
uni.redirectTo({ url: loadingUrl });
|
|
|
|
|
|
}
|
|
|
|
|
|
// #endif
|
|
|
|
|
|
|
|
|
|
|
|
// #ifndef H5
|
|
|
|
|
|
// 小程序环境:直接进入扫码页
|
|
|
|
|
|
uni.redirectTo({ url: '/pages/index/scan' });
|
2026-06-26 17:55:48 +08:00
|
|
|
|
// #endif
|
|
|
|
|
|
},
|
|
|
|
|
|
onShow: function() {
|
|
|
|
|
|
console.log('App Show')
|
|
|
|
|
|
},
|
|
|
|
|
|
onHide: function() {
|
|
|
|
|
|
console.log('App Hide')
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
|
|
<style lang="scss">
|
|
|
|
|
|
/*每个页面公共css */
|
|
|
|
|
|
@import '@/uni_modules/uni-scss/index.scss';
|
|
|
|
|
|
/* #ifndef APP-NVUE */
|
|
|
|
|
|
@import '@/static/customicons.css';
|
|
|
|
|
|
// 设置整个项目的背景色
|
|
|
|
|
|
page {
|
|
|
|
|
|
background-color: #f5f5f5;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* #endif */
|
|
|
|
|
|
.example-info {
|
|
|
|
|
|
font-size: 14px;
|
|
|
|
|
|
color: #333;
|
|
|
|
|
|
padding: 10px;
|
|
|
|
|
|
}
|
|
|
|
|
|
</style>
|