283 lines
No EOL
7.5 KiB
JavaScript
283 lines
No EOL
7.5 KiB
JavaScript
// 启动页逻辑 - 性能优化版本
|
||
const app = getApp();
|
||
const config = require('../../config/config.js');
|
||
const systemInfoUtil = require('../../utils/system-info.js');
|
||
const wsManager = require('../../utils/websocket-manager-v2.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) {
|
||
console.log('启动页加载开始');
|
||
const startTime = Date.now();
|
||
|
||
// 立即开始启动流程,系统信息异步初始化
|
||
this.startLaunchSequence().then(() => {
|
||
const endTime = Date.now();
|
||
console.log(`启动页加载完成,耗时: ${endTime - startTime}ms`);
|
||
});
|
||
|
||
// 异步初始化系统信息,不阻塞启动流程
|
||
this.initSystemInfoAsync();
|
||
},
|
||
|
||
// 页面显示
|
||
onShow: function () {
|
||
console.log('启动页显示');
|
||
},
|
||
|
||
// 页面隐藏
|
||
onHide: function () {
|
||
console.log('启动页隐藏');
|
||
},
|
||
|
||
// 异步初始化系统信息
|
||
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
|
||
});
|
||
|
||
console.log('系统信息异步初始化完成');
|
||
} catch (error) {
|
||
console.error('系统信息初始化失败,使用默认值:', error);
|
||
}
|
||
},
|
||
|
||
// 开始启动流程 - 优化版本
|
||
async startLaunchSequence() {
|
||
try {
|
||
console.log('开始应用启动流程');
|
||
|
||
// 第1步:显示启动动画 (0-20%)
|
||
this.updateProgress(20, '初始化应用...');
|
||
await this.delay(200); // 减少延迟时间
|
||
|
||
// 第2步:检查基础服务 (20-50%)
|
||
await this.updateProgress(50, '检查服务状态...');
|
||
await this.checkBasicServices();
|
||
|
||
// 第3步:初始化用户数据 (50-80%)
|
||
await this.updateProgress(80, '加载用户数据...');
|
||
await this.initUserData();
|
||
|
||
// 第4步:准备界面 (80-100%)
|
||
await this.updateProgress(100, '启动完成');
|
||
await this.delay(300); // 减少完成状态显示时间
|
||
|
||
// 跳转到主页面
|
||
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);
|
||
console.log('基础服务检查完成');
|
||
} catch (error) {
|
||
console.error('基础服务检查失败:', error);
|
||
throw new Error('服务检查失败');
|
||
}
|
||
},
|
||
|
||
// 检查应用权限 - 快速版本
|
||
async checkAppPermissions() {
|
||
return new Promise((resolve) => {
|
||
// 简化权限检查,避免耗时操作
|
||
wx.getSetting({
|
||
success: () => {
|
||
console.log('权限检查完成');
|
||
resolve();
|
||
},
|
||
fail: () => {
|
||
console.log('权限检查失败,继续启动');
|
||
resolve(); // 不阻塞启动流程
|
||
}
|
||
});
|
||
});
|
||
},
|
||
|
||
// 检查网络状态 - 快速版本
|
||
async checkNetworkStatus() {
|
||
return new Promise((resolve) => {
|
||
wx.getNetworkType({
|
||
success: (res) => {
|
||
console.log('网络状态:', res.networkType);
|
||
resolve();
|
||
},
|
||
fail: () => {
|
||
console.log('网络检查失败,继续启动');
|
||
resolve(); // 不阻塞启动流程
|
||
}
|
||
});
|
||
});
|
||
},
|
||
|
||
// 初始化用户数据 - 优化版本
|
||
async initUserData() {
|
||
try {
|
||
// 检查本地存储的用户信息
|
||
const userInfo = wx.getStorageSync('userInfo');
|
||
if (userInfo) {
|
||
console.log('发现本地用户信息');
|
||
app.globalData.userInfo = userInfo;
|
||
app.globalData.isLoggedIn = true;
|
||
|
||
// 如果用户已登录,测试WebSocket连接
|
||
this.testWebSocketConnection();
|
||
}
|
||
|
||
// 异步更新用户数据,不阻塞启动
|
||
this.updateUserDataAsync();
|
||
|
||
} catch (error) {
|
||
console.error('用户数据初始化失败:', error);
|
||
// 不抛出错误,允许继续启动
|
||
}
|
||
},
|
||
|
||
// 测试WebSocket连接 - 异步执行,不阻塞启动
|
||
async testWebSocketConnection() {
|
||
try {
|
||
console.log('🔌 启动页面:开始测试WebSocket连接...');
|
||
|
||
// 设置token到WebSocket管理器
|
||
const userInfo = app.globalData.userInfo;
|
||
if (userInfo && userInfo.token) {
|
||
wsManager.setToken(userInfo.token);
|
||
|
||
// 异步尝试连接,不等待结果
|
||
wsManager.connect().then(() => {
|
||
console.log('✅ 启动页面:WebSocket连接成功');
|
||
}).catch((error) => {
|
||
console.log('⚠️ 启动页面:WebSocket连接失败,稍后重试:', error.message);
|
||
});
|
||
}
|
||
} catch (error) {
|
||
console.log('⚠️ 启动页面:WebSocket连接测试失败:', error);
|
||
}
|
||
},
|
||
|
||
// 异步更新用户数据
|
||
async updateUserDataAsync() {
|
||
try {
|
||
// 这里可以添加后台数据同步逻辑
|
||
console.log('异步更新用户数据...');
|
||
} 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 userInfo = app.globalData.userInfo;
|
||
const targetPage = userInfo && userInfo.token ? '/pages/map/map' : '/pages/login/login';
|
||
|
||
console.log('跳转到:', targetPage);
|
||
|
||
wx.reLaunch({
|
||
url: targetPage,
|
||
success: () => {
|
||
console.log('页面跳转成功');
|
||
},
|
||
fail: (error) => {
|
||
console.error('页面跳转失败:', error);
|
||
// 兜底跳转到地图页面
|
||
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() {
|
||
console.log('启动页卸载');
|
||
}
|
||
});
|