修改位置交互,修改代码逻辑

This commit is contained in:
2025-10-18 22:21:04 +08:00
parent c446df73b5
commit 39fa0b2d04
36 changed files with 2743 additions and 1995 deletions

View File

@@ -1,10 +1,12 @@
// 货运人员模块 - 处理货运人员管理、位置跟踪、交互
import deliveryPersonService from '../../../services/deliveryPersonService';
// 员模块 - 处理所有员工(管理员和货运人员管理、位置跟踪、交互
import employeeService from '../../../services/employeeService';
import { Role } from '../../../utils/roleUtils';
// getApp是微信小程序全局函数无需导入
import { showToast } from '../../../utils/helpers';
import { DataModule } from './dataModule';
export class DeliveryPersonModule {
export class EmployeeModule {
private pageContext: any;
private dataModule: DataModule;
@@ -14,210 +16,44 @@ export class DeliveryPersonModule {
}
/**
* 加载货运人员数据
* 加载所有员工数据(包括管理员和货运人员
*/
async loadDeliveryPersons(): Promise<void> {
async loadAllEmployees(): Promise<void> {
try {
const deliveryPersons = await deliveryPersonService.getDeliveryPersons();
// 获取所有员工数据
const allEmployees = await employeeService.getEmployees();
// 更新地图标记点
this.updateDeliveryPersonMarkers(deliveryPersons);
console.log('货运人员数据加载完成,数量:', deliveryPersons.length);
console.log('所有员工数据加载完成,数量:', allEmployees.length);
} catch (error) {
console.error('加载货运人员数据失败:', error);
showToast('加载货运人员数据失败');
}
}
/**
* 更新货运人员位置
*/
async updateDeliveryPersonLocation(personId: number, location: { longitude: number, latitude: number }): Promise<void> {
try {
await deliveryPersonService.updateDeliveryPersonLocation(personId, location);
console.log(`货运人员 ${personId} 位置更新:`, location);
// 重新加载货运人员数据
await this.loadDeliveryPersons();
} catch (error) {
console.error('更新货运人员位置失败:', error);
showToast('更新位置失败');
}
}
/**
* 开始实时位置跟踪
*/
async startRealTimeTracking(): Promise<void> {
try {
// 获取当前用户信息
const userInfo = this.dataModule.getData().userInfo;
if (!userInfo || !userInfo.id) {
throw new Error('用户信息获取失败');
}
// 使用新的WebSocket接口订阅位置更新
await deliveryPersonService.subscribeToRealTimeLocations(userInfo.id);
// 设置位置更新回调
deliveryPersonService.subscribeToRealTimeLocations(this.handleRealTimeLocationUpdate.bind(this));
showToast('开始实时跟踪');
console.log('开始实时跟踪货运人员位置');
} catch (error) {
console.error('开始实时跟踪失败:', error);
showToast('开始实时跟踪失败');
console.error('加载员数据失败:', error);
showToast('加载员数据失败');
}
}
handleRealTimeLocationUpdate(location: any): void {
console.log('收到实时位置更新:', location);
// 这里可以添加更新地图标记点的逻辑
// 根据位置更新信息更新对应的货运人员标记点
if (location && location.deliveryPersonId) {
this.updateSingleDeliveryPersonMarker(location);
}
}
/**
* 更新单个货运人员标记点
* 获取员工角色对应的图标
*/
private updateSingleDeliveryPersonMarker(location: any): void {
const { markers } = this.pageContext.data;
private getEmployeeIcon(role: string): string {
console.log(`获取员工图标,角色: ${role}`);
// 查找并更新对应的货运人员标记点
const updatedMarkers = markers.map((marker: any) => {
if (marker.type === 'delivery_person' && marker.data && marker.data.id === location.deliveryPersonId) {
// 更新标记点位置
return {
...marker,
longitude: location.longitude,
latitude: location.latitude,
data: {
...marker.data,
currentLocation: {
longitude: location.longitude,
latitude: location.latitude
}
}
};
}
return marker;
});
// 更新数据模块中的标记点
this.dataModule.updateMarkers(updatedMarkers);
}
/**
* 停止实时跟踪货运人员位置
*/
async stopRealTimeTracking(): Promise<void> {
try {
// 获取当前用户信息
const userInfo = this.dataModule.getData().userInfo;
if (!userInfo || !userInfo.id) {
throw new Error('用户信息获取失败');
}
// 使用新的WebSocket接口取消订阅
await deliveryPersonService.unsubscribeFromRealTimeLocations();
showToast('已停止实时跟踪');
console.log('停止实时跟踪货运人员位置');
} catch (error) {
console.error('停止实时跟踪失败:', error);
showToast('停止实时跟踪失败');
}
}
/**
* 更新货运人员标记点
*/
private updateDeliveryPersonMarkers(deliveryPersons: any[]): void {
console.log(`[DELIVERY PERSON MODULE] 开始更新货运人员标记点,货运人员总数: ${deliveryPersons.length}`);
const { markers } = this.pageContext.data;
// 移除现有的货运人员标记点
const filteredMarkers = markers.filter((marker: any) => marker.type !== 'delivery_person');
console.log(`[DELIVERY PERSON MODULE] 移除现有货运人员标记点后,剩余标记点数量: ${filteredMarkers.length}`);
// 添加新的货运人员标记点
const deliveryPersonMarkers = deliveryPersons.map((person, index) => {
// 验证货运人员坐标是否有效
// 注意坐标信息嵌套在currentLocation对象中
let validLongitude = person.currentLocation?.longitude;
let validLatitude = person.currentLocation?.latitude;
if (isNaN(validLongitude) || isNaN(validLatitude)) {
console.error(`[DELIVERY PERSON MODULE] 货运人员${index} (ID: ${person.id}) 坐标无效: (${validLongitude}, ${validLatitude}),使用默认坐标`);
validLongitude = 102.833722; // 默认经度
validLatitude = 24.880095; // 默认纬度
} else {
console.log(`[DELIVERY PERSON MODULE] 货运人员${index} (ID: ${person.id}) 坐标有效: (${validLongitude}, ${validLatitude})`);
}
const iconPath = this.getDeliveryPersonIcon(person.status);
return {
id: 2000 + index, // 货运人员标记点ID从2000开始
longitude: validLongitude,
latitude: validLatitude,
iconPath: iconPath,
width: 26,
height: 26,
zIndex: 20,
type: 'delivery_person',
data: person
};
});
console.log(`[DELIVERY PERSON MODULE] 生成新货运人员标记点数量: ${deliveryPersonMarkers.length}`);
// 更新数据模块中的标记点
const allMarkers = [...filteredMarkers, ...deliveryPersonMarkers];
console.log(`[DELIVERY PERSON MODULE] 准备更新所有标记点,总数: ${allMarkers.length}`);
this.dataModule.updateMarkers(allMarkers);
}
/**
* 获取货运人员状态对应的图标
*/
private getDeliveryPersonIcon(status: string): string {
console.log(`获取货运人员图标,状态: ${status}`);
// 根据报错信息,直接使用已知存在的备用图片路径
// 实际项目中,应确保相关图片资源正确放置在指定路径
const fallbackIconPath = '/images/trucks.png';
// 根据不同状态使用不同的图片路径
// 根据角色使用不同的图标
let iconPath = '';
switch (status) {
case 'idle':
// 为避免图片加载失败,暂时使用备用图片
iconPath = fallbackIconPath;
console.log('使用备用图标idle状态');
switch (role) {
case Role.ADMIN:
// 管理员使用皇冠图标
iconPath = '/images/crown.png';
console.log('使用管理员图标(👑');
break;
case 'busy':
// 为避免图片加载失败,暂时使用备用图片
iconPath = fallbackIconPath;
console.log('使用备用图标busy状态');
break;
case 'offline':
// 为避免图片加载失败,暂时使用备用图片
iconPath = fallbackIconPath;
console.log('使用备用图标offline状态');
case Role.DELIVERY_PERSON:
// 货运人员使用货车图标
iconPath = '/images/truck.png';
console.log('使用货运人员图标(🚚');
break;
default:
iconPath = fallbackIconPath;
console.log('使用备用图标(默认状态)');
// 默认使用货车图标
iconPath = '/images/truck.png';
console.log('使用默认图标(🚚)');
break;
}