添加管理员逻辑

This commit is contained in:
2025-10-19 23:38:54 +08:00
parent 118ec38550
commit 5ee4e077fb
46 changed files with 5263 additions and 883 deletions

View File

@@ -1,8 +1,7 @@
// 位置模块 - 处理位置追踪、位置更新和地图标记点更新
import { DataModule } from './dataModule';
import { showToast } from '../../../utils/helpers';
import locationTrackingService from '../../../services/locationTrackingService';
import { OnlineUserInfo } from '../../../services/locationTrackingService';
import { DataModule } from './dataModule';
import locationTrackingService from '../../../services/locationTrackingService';
// 位置模块接口定义
export interface LocationModule {
@@ -10,21 +9,15 @@ export interface LocationModule {
cleanup(): void;
startRealTimeTracking(): Promise<void>;
stopRealTimeTracking(): Promise<void>;
updateEmployeeLocation(employeeId: number, location: { longitude: number, latitude: number }): Promise<void>;
}
export class LocationModule {
private pageContext: any;
private dataModule: DataModule;
private isTracking: boolean;
constructor(pageContext: any, dataModule: DataModule) {
this.pageContext = pageContext;
constructor(_pageContext: any, dataModule: DataModule) {
this.dataModule = dataModule;
this.isTracking = false;
// 绑定回调方法
this.handleLocationUpdates = this.handleLocationUpdates.bind(this);
}
/**
@@ -33,10 +26,28 @@ export class LocationModule {
async initialize(): Promise<void> {
console.log('位置模块初始化');
// 订阅位置更新
this.subscribeToLocationUpdates();
// 注册位置更新回调,当位置追踪服务有更新时自动刷新地图标记
this.setupLocationUpdateCallback();
console.log('✅ 位置模块初始化完成');
}
/**
* 设置位置更新回调
*/
private setupLocationUpdateCallback(): void {
console.log('📝 [LocationModule] 设置位置更新回调');
console.log('位置模块初始化完成');
// 注册位置更新回调
locationTrackingService.subscribeToLocationUpdates((onlineUsers) => {
console.log('🔔 [LocationModule] 收到位置更新通知');
console.log('👥 [LocationModule] 在线用户数量:', onlineUsers.length);
// 调用updateEmployeeMarkers方法更新地图标记
this.updateEmployeeMarkers(onlineUsers);
});
console.log('✅ [LocationModule] 位置更新回调设置完成');
}
/**
@@ -44,16 +55,15 @@ export class LocationModule {
*/
cleanup(): void {
console.log('清理位置模块');
// 取消位置更新订阅
this.unsubscribeFromLocationUpdates();
// 停止实时跟踪
if (this.isTracking) {
this.stopRealTimeTracking().catch(error => {
console.error('停止实时跟踪失败:', error);
});
}
console.log('✅ 位置模块清理完成');
}
/**
@@ -67,8 +77,8 @@ export class LocationModule {
throw new Error('用户信息获取失败');
}
// 启动位置跟踪订阅
await locationTrackingService.startTrackingAfterSignIn();
// 启动位置跟踪订阅WebSocket连接
await locationTrackingService.startTracking();
this.isTracking = true;
showToast('已开始实时跟踪');
@@ -104,74 +114,13 @@ export class LocationModule {
}
}
/**
* 更新员工位置
*/
async updateEmployeeLocation(employeeId: number, location: { longitude: number, latitude: number }): Promise<void> {
try {
console.log(`员工 ${employeeId} 位置更新:`, location);
// 通过locationTrackingService更新位置
await locationTrackingService.updateUserLocation(location);
console.log(`员工 ${employeeId} 位置更新完成`);
} catch (error) {
console.error('更新员工位置失败:', error);
showToast('更新位置失败');
throw error;
}
}
/**
* 订阅位置更新
*/
private subscribeToLocationUpdates(): void {
console.log('订阅位置更新');
locationTrackingService.subscribeToLocationUpdates(this.handleLocationUpdates);
}
/**
* 取消订阅位置更新
*/
private unsubscribeFromLocationUpdates(): void {
console.log('取消订阅位置更新');
locationTrackingService.unsubscribeFromLocationUpdates(this.handleLocationUpdates);
}
/**
* 处理位置更新回调
*/
private handleLocationUpdates(locationData: any): void {
console.log('🔔 [LocationModule] 收到位置更新回调');
console.log('📊 [LocationModule] 回调数据类型:', locationData.type || '未知类型');
console.log('📋 [LocationModule] 回调数据内容:', JSON.stringify(locationData, null, 2));
if (locationData.type === 'onlineUserList' && locationData.users) {
// 处理在线用户列表更新
console.log('👥 [LocationModule] 处理在线用户列表,用户数量:', locationData.users.length);
this.updateEmployeeMarkers(locationData.users);
} else if (Array.isArray(locationData)) {
// 处理位置更新数组(旧格式)
console.log('📋 [LocationModule] 处理位置更新数组,用户数量:', locationData.length);
this.updateEmployeeMarkers(locationData);
} else if (locationData.userId && locationData.latitude && locationData.longitude) {
// 处理单个用户位置更新
console.log('👤 [LocationModule] 处理单个用户位置更新:', locationData.userId);
this.updateSingleEmployeeMarker(locationData);
} else {
console.warn('❌ [LocationModule] 未知的位置数据格式:', locationData);
}
console.log('🏁 [LocationModule] 位置更新回调处理完成');
}
/**
* 更新员工标记点
*/
private updateEmployeeMarkers(onlineUsers: any[]): void {
public updateEmployeeMarkers(onlineUsers: any[]): void {
console.log('📍 [LocationModule] 开始更新员工标记点');
console.log('👥 [LocationModule] 传入用户数量:', onlineUsers.length);
console.log('📋 [LocationModule] 用户数据示例:', JSON.stringify(onlineUsers[0], null, 2));
// 获取地图模块来更新标记点
const mapModule = this.dataModule.getMapModule();
@@ -180,8 +129,13 @@ export class LocationModule {
return;
}
// 获取当前地图上的所有标记点
const { markers } = this.dataModule.getData();
const currentEmployeeMarkers = markers.filter((marker: any) => marker.type === 'employee');
console.log('🗺️ [LocationModule] 当前地图上员工标记点数量:', currentEmployeeMarkers.length);
// 为每个在线用户创建标记点
const employeeMarkers = onlineUsers.map((user, index) => {
const newEmployeeMarkers = onlineUsers.map((user, index) => {
console.log(`🔍 [LocationModule] 处理第 ${index + 1} 个用户:`, user.userId || '未知ID');
// 获取用户角色信息
@@ -215,55 +169,94 @@ export class LocationModule {
return employeeMarker;
});
console.log('📍 [LocationModule] 生成标记点数量:', employeeMarkers.length);
console.log('📋 [LocationModule] 标记点数据示例:', JSON.stringify(employeeMarkers[0], null, 2));
console.log('📍 [LocationModule] 生成标记点数量:', newEmployeeMarkers.length);
// 调用地图模块更新员工标记点
mapModule.updateEmployeeMarkers(employeeMarkers);
// 对比新旧标记点,精确更新
this.updateMarkersPrecisely(mapModule, currentEmployeeMarkers, newEmployeeMarkers);
console.log('✅ [LocationModule] 员工标记点更新完成');
}
/**
* 更新单个员工标记点
* 精确更新标记点 - 通过对比新旧标记点来添加、更新和删除
*/
private updateSingleEmployeeMarker(locationUpdate: any): void {
console.log('更新单个员工标记点:', locationUpdate.userId);
private updateMarkersPrecisely(mapModule: any, currentMarkers: any[], newMarkers: any[]): void {
console.log('🎯 [LocationModule] 开始精确更新标记点');
// 获取地图模块
const mapModule = this.dataModule.getMapModule();
if (!mapModule) {
console.error('地图模块未找到,无法更新员工标记点');
return;
}
// 获取用户角色信息
const userRole = this.getUserRole(locationUpdate.userId);
// 创建单个员工标记点
const employeeMarker = {
id: 10000 + locationUpdate.userId,
type: 'employee',
title: `员工${locationUpdate.userId}`,
longitude: locationUpdate.longitude,
latitude: locationUpdate.latitude,
iconPath: this.getEmployeeIcon(locationUpdate.userId, userRole),
width: 32,
height: 32,
zIndex: 30,
data: {
userId: locationUpdate.userId,
role: userRole,
lastUpdateTime: locationUpdate.timestamp || Date.now()
// 创建用户ID到标记点的映射
const currentMarkerMap = new Map();
currentMarkers.forEach(marker => {
if (marker.data && marker.data.userId) {
currentMarkerMap.set(marker.data.userId, marker);
}
};
});
// 调用地图模块更新单个标记点
mapModule.updateSingleEmployeeMarker(employeeMarker);
const newMarkerMap = new Map();
newMarkers.forEach(marker => {
if (marker.data && marker.data.userId) {
newMarkerMap.set(marker.data.userId, marker);
}
});
console.log('单个员工标记点更新完成');
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] 精确更新标记点完成');
}
/**
* 获取用户角色
*/
@@ -281,12 +274,14 @@ export class LocationModule {
/**
* 获取员工图标
*/
private getEmployeeIcon(userId: number, userRole: string): string {
private getEmployeeIcon(_userId: number, userRole: string): string {
// 根据用户角色返回不同的图标
if (userRole === 'ADMIN') {
return '/images/crown.png'; // 管理员图标
} else if (userRole === 'DRIVER') {
return '/images/truck.png'; // 司机图标
} else {
return '/images/trucks.png'; // 货运人员图标
return '👤'; // 普通员工图标(表情符号)
}
}
}