// index.ts // 引入服务和工具函数 import { UserInfo, Marker } from '../../types'; import { showToast } from '../../utils/helpers'; import userService from '../../services/userService'; import locationTrackingService from '../../services/locationTrackingService'; // 引入模块 import { MainPageModule } from './modules/mainPageModule'; // 主页面组件 Component({ data: { // 地图中心点坐标 longitude: 102.833722, latitude: 24.880095, scale: 13, // 地图缩放级别 markers: [] as Marker[], // 地图标记点数组 userInfo: null as UserInfo | null, // 用户信息 // 用户认证状态(分离外部登录状态和用户二级状态) authStatus: { hasWxCode: false, // 是否已获取微信code(外部登录状态) userStatus: 'unknown' as 'unknown' | 'registered' | 'unregistered' | 'signed_in' | 'signed_out', // 用户二级状态 }, showUserPanel: false, // 是否显示用户信息面板 showOrderPanel: false, // 是否显示订单详情面板 currentOrder: null as any, // 当前选中的订单 currentDeliveryPerson: null as any, // 当前选中的货运人员 currentWarehouse: null as any, // 当前选中的仓库 currentPanelPosition: { x: 0, y: 0 }, // 当前信息面板位置 polyline: null as any, // 路线规划结果 pendingOrders: [] as any[] ,// 待分配订单 currentRoute: null as any, // 当前路线信息 showRoute: false, // 是否显示路线 routeDistance: 0, // 路线距离 routeDuration: 0, // 路线预计时间 // 底部弹窗相关状态 showWarehouseModal: false, // 仓库底部弹窗 showDeliveryPersonModal: false, // 货运人员底部弹窗 // 底部弹窗状态(bottom或full) warehouseModalState: 'bottom', deliveryPersonModalState: 'bottom', // 操作按钮显示状态(基于用户状态动态计算) showSignOutButton: false // 是否显示签退按钮 }, lifetimes: { async attached() { // 组件挂载时初始化 await this.initPage(); }, detached() { // 组件卸载时清理 (this as any).mainPageModule = null; } }, methods: { // 处理货运人员底部弹窗关闭 onDeliveryPersonModalClose() { this.setData({ showDeliveryPersonModal: false }); }, // 处理仓库底部弹窗关闭 onWarehouseModalClose() { this.setData({ showWarehouseModal: false }); }, // 初始化页面 async initPage() { const app = getApp(); // 初始化主页面模块 (this as any).mainPageModule = new MainPageModule(this); const loginModule = (this as any).mainPageModule.getLoginModule(); // 设置globalData中的loginModule引用,用于废弃方法的重定向 app.globalData.loginModule = loginModule; // 异步检查登录状态 await this.checkAndUpdateLoginStatus(); }, // 异步检查并更新登录状态 async checkAndUpdateLoginStatus() { const app = getApp(); const loginModule = (this as any).mainPageModule.getLoginModule(); try { // 显示加载状态 wx.showLoading({ title: '检查登录状态...', mask: true }); // 异步检查登录状态 const isLoggedIn = await this.asyncCheckLoginStatus(loginModule); if (isLoggedIn) { // 已登录状态 const userStatus = await loginModule.determineUserStatus(app.globalData.userInfo); this.setData({ userInfo: app.globalData.userInfo, 'authStatus.hasWxCode': true, 'authStatus.userStatus': userStatus, // 初始化按钮显示状态 showSignInButton: loginModule.shouldShowSignInButton(), showRegisterButton: loginModule.shouldShowRegisterButton(), showAuthButton: false }); // === 全局登录流程完成,登录成功 === // 统一在此处执行一次完整的页面刷新 this.refreshPageAfterLogin(); } else { // 未登录状态 this.setData({ userInfo: null, 'authStatus.hasWxCode': false, 'authStatus.userStatus': 'unknown', // 初始化按钮显示状态 showSignInButton: false, showRegisterButton: false, showAuthButton: true }); } } catch (error) { console.error('检查登录状态失败:', error); // 出错时设置为未登录状态 this.setData({ userInfo: null, 'authStatus.hasWxCode': false, 'authStatus.userStatus': 'unknown', showSignInButton: false, showRegisterButton: false, showAuthButton:true }); console.log('❌ 登录检查失败,重置为未登录状态'); console.log(' - authStatus.hasWxCode 设置为 false'); console.log(' - showRegisterButton 设置为 false'); } finally { wx.hideLoading(); } }, // 异步检查登录状态 async asyncCheckLoginStatus(loginModule: any): Promise { const app = getApp(); // 首先检查本地是否有登录状态 if (app.globalData.isLoggedIn && app.globalData.userInfo) { // 本地已有登录状态,此时可以安全获取用户状态(因为有token) const userStatus = await loginModule.determineUserStatus(app.globalData.userInfo); // 更新页面状态 this.setData({ userInfo: app.globalData.userInfo, 'authStatus.hasWxCode': true, 'authStatus.userStatus': userStatus }); console.log('✅ 使用本地登录状态,用户状态已更新:', userStatus); return true; } // 如果本地没有登录状态,尝试执行静默登录 try { const loginResult = await userService.wxLogin(); if (loginResult.success && loginResult.userInfo) { // 登录成功,更新全局用户信息 app.globalData.userInfo = loginResult.userInfo; app.globalData.isLoggedIn = true; // 登录成功后,此时已经有token,可以安全获取用户状态 const userStatus = await loginModule.determineUserStatus(loginResult.userInfo); // 更新页面状态 this.setData({ userInfo: loginResult.userInfo, 'authStatus.hasWxCode': true, 'authStatus.userStatus': userStatus }); console.log('✅ 静默登录成功,用户状态已更新:', userStatus); // === 全局登录流程完成,登录成功 === // 统一在此处执行一次完整的页面刷新 this.refreshPageAfterLogin(); return true; } } catch (error) { console.warn('静默登录失败:', error); } return false; }, // 更新按钮显示状态 updateButtonDisplayStatus() { const loginModule = (this as any).mainPageModule.getLoginModule(); const showSignInButton = loginModule.shouldShowSignInButton(); const showRegisterButton = loginModule.shouldShowRegisterButton(); // 签退按钮显示逻辑:已签到用户显示签退按钮 const showSignOutButton = this.data.authStatus.userStatus === 'signed_in'; console.log('🔄 更新按钮显示状态:'); console.log(' - showSignInButton:', showSignInButton); console.log(' - showRegisterButton:', showRegisterButton); console.log(' - showSignOutButton:', showSignOutButton); console.log(' - 当前用户状态:', this.data.authStatus.userStatus); console.log(' - 当前hasWxCode:', this.data.authStatus.hasWxCode); this.setData({ showSignInButton, showRegisterButton, showSignOutButton }); console.log('✅ 按钮状态已更新到页面'); }, // 登录成功后统一刷新页面 async refreshPageAfterLogin() { const loginModule = (this as any).mainPageModule.getLoginModule(); const app = getApp(); console.log('🔄 === 全局登录流程完成,执行统一页面刷新 ==='); // 1. 更新页面登录状态 loginModule.updatePageAfterLogin(app.globalData.userInfo); // 2. 更新按钮显示状态 this.updateButtonDisplayStatus(); // 3. 初始化主页面模块 await (this as any).mainPageModule.onLoad(); // 4. 登录成功后主动加载业务数据(仓库、订单等) if (app.globalData.isLoggedIn) { console.log('🔍 登录成功,开始加载业务数据...'); await (this as any).mainPageModule.refreshAllData(); console.log('✅ 业务数据加载完成'); } console.log('✅ 统一页面刷新完成'); }, // 处理仓库底部弹窗状态变化 onWarehouseModalStateChange(e: any) { const state = e.detail.state; this.setData({ warehouseModalState: state }); }, // 处理货运人员底部弹窗状态变化 onDeliveryPersonModalStateChange(e: any) { const state = e.detail.state; this.setData({ deliveryPersonModalState: state }); }, // 处理签到 - 已迁移到LoginModule async handleSignIn() { const loginModule = (this as any).mainPageModule.getLoginModule(); const success = await loginModule.handleSignIn(); if (success) { // 更新按钮显示状态 this.updateButtonDisplayStatus(); // 刷新页面数据 await (this as any).mainPageModule.refreshAllData(); } }, // 处理授权登录 - 已迁移到LoginModule async handleAuthLogin() { const loginModule = (this as any).mainPageModule.getLoginModule(); const success = await loginModule.handleAuthLogin(); if (success) { const app = getApp(); const loginModule = (this as any).mainPageModule.getLoginModule(); const userStatus = loginModule.determineUserStatus(app.globalData.userInfo); this.setData({ userInfo: app.globalData.userInfo, 'authStatus.hasWxCode': true, 'authStatus.userStatus': userStatus }); console.log('✅ 手动登录成功,authStatus.hasWxCode 设置为 true'); console.log('✅ 用户状态已更新:', userStatus); // === 全局登录流程完成,登录成功 === // 统一在此处执行一次完整的页面刷新 this.refreshPageAfterLogin(); } }, // 处理退出登录 async handleLogout() { try { wx.showLoading({ title: '正在退出...', mask: true }); // 调用loginModule的logout方法 const loginModule = (this as any).mainPageModule.getLoginModule(); await loginModule.logout(); // 更新页面状态 this.setData({ userInfo: null, 'authStatus.hasWxCode': false, 'authStatus.userStatus': 'unknown', showUserPanel: false }); // 更新按钮显示状态 this.updateButtonDisplayStatus(); wx.hideLoading(); showToast('已退出登录'); console.log('用户已退出登录'); } catch (error) { wx.hideLoading(); console.error('退出登录失败:', error); showToast('退出登录失败'); } }, // 处理签退 async handleSignOut() { try { console.log('调试信息 - 开始执行签退流程'); wx.showLoading({ title: '正在签退...', mask: true }); // 停止位置追踪服务 try { await locationTrackingService.stopTracking(); console.log('调试信息 - 位置追踪服务已停止'); } catch (trackingError) { console.warn('调试信息 - 停止位置追踪失败:', trackingError); } // 清除登录信息,防止自动重新登录 console.log('调试信息 - 开始清除登录信息'); const app = getApp(); app.doGlobalLogout(); // 保存签退状态到本地存储,防止自动重新登录 console.log('调试信息 - 保存签退状态到本地存储'); wx.setStorageSync('userStatus', 'signed_out'); // 验证签退状态是否保存成功 const savedStatus = wx.getStorageSync('userStatus'); console.log('调试信息 - 验证保存的签退状态:', savedStatus); // 更新用户状态为已签退 this.setData({ 'authStatus.userStatus': 'signed_out', showSignOutButton: false, showSignInButton: true }); wx.hideLoading(); showToast('签退成功'); console.log('调试信息 - 用户签退完成'); } catch (error) { wx.hideLoading(); console.error('调试信息 - 签退失败:', error); showToast('签退失败'); } }, // 显示申请加入货运人员表单 - 跳转到独立页面 showApplyForm() { wx.navigateTo({ url: '/pages/apply/apply' }); }, // 阻止事件冒泡 stopPropagation(e: any) { e.stopPropagation(); }, // 获取性别文本表示 getGenderText(gender: number): string { if (gender === 1) return '男'; if (gender === 2) return '女'; return '未知'; }, // 格式化坐标对 formatCoordinatePair(longitude: number, latitude: number): string { return `${longitude.toFixed(6)}, ${latitude.toFixed(6)}`; }, // 用户登出 userLogout() { if ((this as any).mainPageModule) { const loginModule = (this as any).mainPageModule.getLoginModule(); loginModule.logout(); // 更新页面状态 this.setData({ isLoggedIn: false, userInfo: null }); // 更新按钮显示状态 this.updateButtonDisplayStatus(); } }, // 显示用户面板 showUserPanel() { this.setData({ showUserPanel: true }); }, // 隐藏用户面板 hideUserPanel() { this.setData({ showUserPanel: false }); }, // 隐藏所有面板 hideAllPanels() { if ((this as any).mainPageModule) { (this as any).mainPageModule.hideAllPanels(); } }, // 重置标记点状态 resetMarkers() { if ((this as any).mainPageModule) { (this as any).mainPageModule.resetMarkers(); } }, // 地图点击事件 onMapTap(e: any) { if ((this as any).mainPageModule) { (this as any).mainPageModule.onMapTap(e); } }, // 标记点点击事件 onMarkerTap(e: any) { if ((this as any).mainPageModule) { (this as any).mainPageModule.onMarkerTap(e); } }, // 分配订单 async assignOrder(orderId: number, deliveryPersonId: number) { if ((this as any).mainPageModule) { const orderModule = (this as any).mainPageModule.getOrderModule(); await orderModule.assignOrder(orderId, deliveryPersonId); } }, // 更新订单状态 async updateOrderStatus(orderId: number, status: 'pending' | 'assigned' | 'in_transit' | 'delivered') { if ((this as any).mainPageModule) { const orderModule = (this as any).mainPageModule.getOrderModule(); await orderModule.updateOrderStatus(orderId, status); } }, // 展开仓库面板 expandWarehousePanel() { if ((this as any).mainPageModule) { const warehouseModule = (this as any).mainPageModule.getWarehouseModule(); warehouseModule.expandWarehousePanel(); } }, // 收起仓库面板 collapseWarehousePanel() { if ((this as any).mainPageModule) { const warehouseModule = (this as any).mainPageModule.getWarehouseModule(); warehouseModule.collapseWarehousePanel(); } }, // 展开货运人员面板 expandDeliveryPersonPanel() { if ((this as any).mainPageModule) { const deliveryPersonModule = (this as any).mainPageModule.getDeliveryPersonModule(); deliveryPersonModule.expandDeliveryPersonPanel(); } }, // 收起货运人员面板 collapseDeliveryPersonPanel() { if ((this as any).mainPageModule) { const deliveryPersonModule = (this as any).mainPageModule.getDeliveryPersonModule(); deliveryPersonModule.collapseDeliveryPersonPanel(); } }, // 刷新所有数据 async refreshAllData() { if ((this as any).mainPageModule) { await (this as any).mainPageModule.refreshAllData(); } }, // 开始定位(处理地图控制按钮点击) async startLocation() { if ((this as any).mainPageModule) { const mapModule = (this as any).mainPageModule.getMapModule(); await mapModule.startLocation(); } }, } });