"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.LocationModule = void 0; // 位置模块 - 处理位置追踪、位置更新和地图标记点更新 const helpers_1 = require("../../../utils/helpers"); const locationTrackingService_1 = __importDefault(require("../../../services/locationTrackingService")); class LocationModule { constructor(_pageContext, dataModule) { this.dataModule = dataModule; this.isTracking = false; } /** * 初始化位置模块 */ async initialize() { console.log('位置模块初始化'); // 注册位置更新回调,当位置追踪服务有更新时自动刷新地图标记 this.setupLocationUpdateCallback(); console.log('✅ 位置模块初始化完成'); } /** * 设置位置更新回调 */ setupLocationUpdateCallback() { console.log('📝 [LocationModule] 设置位置更新回调'); // 注册位置更新回调 locationTrackingService_1.default.subscribeToLocationUpdates(async (onlineUsers) => { console.log('🔔 [LocationModule] 收到位置更新通知'); console.log('👥 [LocationModule] 在线用户数量:', onlineUsers.length); // 调用updateEmployeeMarkers方法更新地图标记 try { await this.updateEmployeeMarkers(onlineUsers); } catch (error) { console.error('更新员工标记点失败:', error); } }); console.log('✅ [LocationModule] 位置更新回调设置完成'); } /** * 清理位置模块 */ cleanup() { console.log('清理位置模块'); // 停止实时跟踪 if (this.isTracking) { this.stopRealTimeTracking().catch(error => { console.error('停止实时跟踪失败:', error); }); } console.log('✅ 位置模块清理完成'); } /** * 开始实时跟踪位置 */ async startRealTimeTracking() { try { // 获取当前用户信息 const userInfo = this.dataModule.getData().userInfo; if (!userInfo || !userInfo.id) { throw new Error('用户信息获取失败'); } // 启动位置跟踪订阅(WebSocket连接) await locationTrackingService_1.default.startTracking(); this.isTracking = true; (0, helpers_1.showToast)('已开始实时跟踪'); console.log('开始实时跟踪位置'); } catch (error) { console.error('开始实时跟踪失败:', error); (0, helpers_1.showToast)('开始实时跟踪失败'); throw error; } } /** * 停止实时跟踪位置 */ async stopRealTimeTracking() { try { // 获取当前用户信息 const userInfo = this.dataModule.getData().userInfo; if (!userInfo || !userInfo.id) { throw new Error('用户信息获取失败'); } // 停止位置跟踪 await locationTrackingService_1.default.stopTracking(); this.isTracking = false; (0, helpers_1.showToast)('已停止实时跟踪'); console.log('停止实时跟踪位置'); } catch (error) { console.error('停止实时跟踪失败:', error); (0, helpers_1.showToast)('停止实时跟踪失败'); throw error; } } /** * 更新员工标记点 */ async updateEmployeeMarkers(onlineUsers) { console.log('📍 [LocationModule] 开始更新员工标记点'); console.log('👥 [LocationModule] 传入用户数量:', onlineUsers.length); // 获取地图模块来更新标记点 const mapModule = this.dataModule.getMapModule(); if (!mapModule) { console.error('地图模块未找到,无法更新员工标记点'); return; } // 获取当前地图上的所有标记点 const { markers } = this.dataModule.getData(); const currentEmployeeMarkers = markers.filter((marker) => marker.type === 'employee'); console.log('🗺️ [LocationModule] 当前地图上员工标记点数量:', currentEmployeeMarkers.length); // 为每个在线用户创建标记点 const newEmployeeMarkers = await Promise.all(onlineUsers.map(async (user, index) => { console.log(`🔍 [LocationModule] 处理第 ${index + 1} 个用户:`, user.userId || '未知ID'); // 获取用户角色信息 const userRole = user.role || this.getUserRole(user.userId); // 解析位置信息(支持多种格式) const longitude = user.longitude || (user.lastLocation && user.lastLocation.longitude) || 0; const latitude = user.latitude || (user.lastLocation && user.lastLocation.latitude) || 0; const lastUpdateTime = user.lastUpdateTime || Date.now(); console.log(`📡 [LocationModule] 用户 ${index + 1} 位置: ${latitude}, ${longitude}`); console.log(`👤 [LocationModule] 用户 ${index + 1} 信息: ${user.userName || '未知用户'} (${userRole})`); // 获取员工图标 const iconPath = await this.getEmployeeIcon(user.userId, userRole); const employeeMarker = { id: 10000 + user.userId, // 避免ID冲突 type: 'employee', title: user.userName || `员工${user.userId}`, longitude: longitude, latitude: latitude, iconPath: iconPath, width: 32, height: 32, zIndex: 30, data: { userId: user.userId, role: userRole, lastUpdateTime: lastUpdateTime } }; return employeeMarker; })); console.log('📍 [LocationModule] 新生成标记点数量:', newEmployeeMarkers.length); // 对比新旧标记点,精确更新 this.updateMarkersPrecisely(mapModule, currentEmployeeMarkers, newEmployeeMarkers); console.log('✅ [LocationModule] 员工标记点更新完成'); } /** * 精确更新标记点 - 通过对比新旧标记点来添加、更新和删除 */ updateMarkersPrecisely(mapModule, currentMarkers, newMarkers) { console.log('🎯 [LocationModule] 开始精确更新标记点'); // 创建用户ID到标记点的映射 const currentMarkerMap = new Map(); currentMarkers.forEach(marker => { if (marker.data && marker.data.userId) { currentMarkerMap.set(marker.data.userId, marker); } }); const newMarkerMap = new Map(); newMarkers.forEach(marker => { if (marker.data && marker.data.userId) { newMarkerMap.set(marker.data.userId, marker); } }); console.log('📊 [LocationModule] 当前标记点用户ID:', Array.from(currentMarkerMap.keys())); console.log('📊 [LocationModule] 新标记点用户ID:', Array.from(newMarkerMap.keys())); // 找出需要删除的标记点(在当前但不在新的) const markersToRemove = currentMarkers.filter(marker => { if (marker.data && marker.data.userId) { return !newMarkerMap.has(marker.data.userId); } return false; }); // 找出需要添加的标记点(在新的但不在当前的) const markersToAdd = newMarkers.filter(marker => { if (marker.data && marker.data.userId) { return !currentMarkerMap.has(marker.data.userId); } return false; }); // 找出需要更新的标记点(位置或信息有变化) const markersToUpdate = newMarkers.filter(marker => { if (marker.data && marker.data.userId) { const currentMarker = currentMarkerMap.get(marker.data.userId); if (currentMarker) { // 检查位置是否变化 return currentMarker.longitude !== marker.longitude || currentMarker.latitude !== marker.latitude || currentMarker.title !== marker.title || currentMarker.iconPath !== marker.iconPath; } } return false; }); console.log(`🗑️ [LocationModule] 需要删除的标记点数量: ${markersToRemove.length}`); console.log(`➕ [LocationModule] 需要添加的标记点数量: ${markersToAdd.length}`); console.log(`🔄 [LocationModule] 需要更新的标记点数量: ${markersToUpdate.length}`); // 执行删除操作 markersToRemove.forEach(marker => { console.log(`🗑️ [LocationModule] 删除标记点: 用户 ${marker.data.userId}`); mapModule.removeEmployeeMarker(marker.id); }); // 执行添加操作 markersToAdd.forEach(marker => { console.log(`➕ [LocationModule] 添加标记点: 用户 ${marker.data.userId}`); mapModule.addEmployeeMarker(marker); }); // 执行更新操作 markersToUpdate.forEach(marker => { console.log(`🔄 [LocationModule] 更新标记点: 用户 ${marker.data.userId}`); mapModule.updateSingleEmployeeMarker(marker); }); console.log('🎯 [LocationModule] 精确更新标记点完成'); } /** * 获取用户角色 */ getUserRole(userId) { // 从数据模块获取用户信息 const userInfo = this.dataModule.getData().userInfo; if (userInfo && userInfo.id === userId) { return userInfo.role || 'employee'; } // 如果是其他用户,默认为货运人员 return 'employee'; } /** * 获取员工图标 */ async getEmployeeIcon(userId, userRole) { // 首先尝试获取用户头像URL const userInfo = this.dataModule.getData().userInfo; // 如果是当前用户,使用当前用户的头像 if (userInfo && userInfo.id === userId && userInfo.avatarUrl) { return userInfo.avatarUrl; } // 对于其他用户,尝试从员工服务获取头像URL try { const employeeService = require('../../services/employeeService').default; const avatarUrl = await employeeService.getAvatarUrl(userId); if (avatarUrl) { return avatarUrl; } } catch (error) { console.warn(`获取用户 ${userId} 的头像失败:`, error); } // 如果获取头像失败,使用默认图标 if (userRole === 'ADMIN') { return '/images/crown.png'; // 管理员图标 } else if (userRole === 'DRIVER') { return '/images/truck.png'; // 司机图标 } else { return '/images/truck.png'; // 普通员工也使用货运图标 } } } exports.LocationModule = LocationModule;