findme-miniprogram-frontend/pages/splash/splash.js
2025-12-27 17:16:03 +08:00

330 lines
8.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 启动页逻辑 - 性能优化版本
const app = getApp();
const config = require('../../config/config.js');
const systemInfoUtil = require('../../utils/system-info.js');
const wsManager = require('../../utils/websocket-manager-v2.js');
const versionManager = require('../../utils/simple-version-manager.js');
Page({
data: {
// 应用信息
appName: config.appName || 'FindMe',
appVersion: config.appVersion || '1.0.0',
// 状态控制
isLoading: true,
showProgress: false,
showError: false,
// 显示文本
loadingText: '正在启动...',
errorMessage: '',
progress: 0,
// 系统适配信息 - 先设置默认值
statusBarHeight: 44,
menuButtonHeight: 32,
menuButtonTop: 6,
navBarHeight: 88,
windowHeight: 667,
safeAreaBottom: 0
},
// 页面加载 - 性能优化
onLoad: function (options) {
// 🔥 优先检查版本更新(必须在启动流程之前)
const hasVersionUpdate = versionManager.checkVersionUpdate();
if (hasVersionUpdate) {
console.log('⚠️ 启动页检测到版本更新,停止启动流程');
return; // 如果有版本更新,停止启动流程,等待用户处理
}
const startTime = Date.now();
// 立即开始启动流程,系统信息异步初始化
this.startLaunchSequence().then(() => {
const endTime = Date.now();
});
// 异步初始化系统信息,不阻塞启动流程
this.initSystemInfoAsync();
},
// 页面显示
onShow: function () {
},
// 页面隐藏
onHide: function () {
},
// 异步初始化系统信息
async initSystemInfoAsync() {
try {
await systemInfoUtil.init();
const adaptInfo = systemInfoUtil.getSystemAdaptInfo();
this.setData({
statusBarHeight: adaptInfo.statusBarHeight,
menuButtonHeight: adaptInfo.menuButtonHeight,
menuButtonTop: adaptInfo.menuButtonTop,
navBarHeight: adaptInfo.navBarHeight,
windowHeight: adaptInfo.windowHeight,
safeAreaBottom: adaptInfo.safeAreaBottom
});
} catch (error) {
console.error('系统信息初始化失败,使用默认值:', error);
}
},
// 开始启动流程 - 优化版本
async startLaunchSequence() {
try {
// 🔥 检查版本更新(在启动流程开始时)
const hasVersionUpdate = versionManager.checkVersionUpdate();
if (hasVersionUpdate) {
console.log('⚠️ 启动流程中检测到版本更新,停止启动');
return; // 停止启动流程
}
// 第1步显示启动动画 (0-20%)
this.updateProgress(20, '初始化应用...');
await this.delay(200); // 减少延迟时间
// 🔥 再次检查版本更新
const hasVersionUpdate1 = versionManager.checkVersionUpdate();
if (hasVersionUpdate1) {
console.log('⚠️ 初始化过程中检测到版本更新,停止启动');
return;
}
// 第2步检查基础服务 (20-50%)
await this.updateProgress(50, '检查服务状态...');
await this.checkBasicServices();
// 🔥 再次检查版本更新
const hasVersionUpdate2 = versionManager.checkVersionUpdate();
if (hasVersionUpdate2) {
console.log('⚠️ 服务检查后检测到版本更新,停止启动');
return;
}
// 第3步初始化用户数据 (50-80%)
await this.updateProgress(80, '加载用户数据...');
await this.initUserData();
// 🔥 再次检查版本更新
const hasVersionUpdate3 = versionManager.checkVersionUpdate();
if (hasVersionUpdate3) {
console.log('⚠️ 用户数据加载后检测到版本更新,停止启动');
return;
}
// 第4步准备界面 (80-100%)
await this.updateProgress(100, '启动完成');
await this.delay(300); // 减少完成状态显示时间
// 🔥 跳转前最后一次检查版本更新
const hasVersionUpdateBeforeJump = versionManager.checkVersionUpdate();
if (hasVersionUpdateBeforeJump) {
console.log('⚠️ 跳转前检测到版本更新,取消跳转');
return;
}
// 跳转到主页面
this.navigateToMainPage();
} catch (error) {
console.error('启动失败:', error);
this.showError(error.message || '启动失败,请重试');
}
},
// 检查基础服务 - 优化版本
async checkBasicServices() {
try {
// 并行检查多个服务,提高效率
const checks = [
this.checkAppPermissions(),
this.checkNetworkStatus(),
this.delay(100) // 最小延迟,确保用户能看到进度
];
await Promise.all(checks);
} catch (error) {
console.error('基础服务检查失败:', error);
throw new Error('服务检查失败');
}
},
// 检查应用权限 - 快速版本
async checkAppPermissions() {
return new Promise((resolve) => {
// 简化权限检查,避免耗时操作
wx.getSetting({
success: () => {
resolve();
},
fail: () => {
resolve(); // 不阻塞启动流程
}
});
});
},
// 检查网络状态 - 快速版本
async checkNetworkStatus() {
return new Promise((resolve) => {
wx.getNetworkType({
success: (res) => {
resolve();
},
fail: () => {
resolve(); // 不阻塞启动流程
}
});
});
},
// 初始化用户数据 - 优化版本
async initUserData() {
try {
// 检查本地存储的用户信息
const userInfo = wx.getStorageSync('userInfo');
if (userInfo) {
app.globalData.userInfo = userInfo;
app.globalData.isLoggedIn = true;
// 如果用户已登录测试WebSocket连接
this.testWebSocketConnection();
}
// 异步更新用户数据,不阻塞启动
this.updateUserDataAsync();
} catch (error) {
console.error('用户数据初始化失败:', error);
// 不抛出错误,允许继续启动
}
},
// 测试WebSocket连接 - 异步执行,不阻塞启动
async testWebSocketConnection() {
try {
// 设置token到WebSocket管理器
const userInfo = app.globalData.userInfo;
if (userInfo && userInfo.token) {
wsManager.setToken(userInfo.token);
// 异步尝试连接,不等待结果
wsManager.connect().then(() => {
}).catch((error) => {
});
}
} catch (error) {
}
},
// 异步更新用户数据
async updateUserDataAsync() {
try {
// 这里可以添加后台数据同步逻辑
} catch (error) {
console.error('异步更新用户数据失败:', error);
}
},
// 更新进度 - 优化版本
async updateProgress(progress, text) {
this.setData({
progress: progress,
loadingText: text,
showProgress: true
});
// 使用requestAnimationFrame优化动画性能
if (wx.nextTick) {
await new Promise(resolve => wx.nextTick(resolve));
}
},
// 延迟函数 - 性能优化
delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
},
// 跳转到主页面 - 优化版本
navigateToMainPage() {
// 🔥 跳转前再次确认版本更新状态
const hasVersionUpdate = versionManager.checkVersionUpdate();
if (hasVersionUpdate) {
console.log('⚠️ 跳转时检测到版本更新,取消跳转');
return; // 如果有版本更新,不执行跳转
}
// 始终跳转到地图页面,无论登录状态如何
// 登录判断应该在功能执行时进行,而不是在应用启动时就进行判断
const targetPage = '/pages/map/map';
wx.reLaunch({
url: targetPage,
success: () => {
},
fail: (error) => {
console.error('页面跳转失败:', error);
// 🔥 即使失败也要检查版本更新
const hasVersionUpdateOnFail = versionManager.checkVersionUpdate();
if (!hasVersionUpdateOnFail) {
// 兜底跳转到地图页面
wx.reLaunch({
url: '/pages/map/map'
});
}
}
});
},
// 显示错误
showError(message) {
this.setData({
showError: true,
errorMessage: message,
isLoading: false
});
},
// 重试启动
onRetry() {
this.setData({
showError: false,
isLoading: true,
progress: 0
});
// 延迟重试,避免立即重试
setTimeout(() => {
this.startLaunchSequence();
}, 300);
},
// 页面卸载
onUnload() {
}
});