Files
WXProgram/miniprogram/pages/index/modules/dataModule.ts

329 lines
8.0 KiB
TypeScript
Raw Normal View History

2025-10-16 21:32:16 +08:00
import { UserInfo, Marker } from '../../../types';
/**
*
* index页面的所有数据状态
*/
export class DataModule {
private pageContext: any;
constructor(pageContext: any) {
this.pageContext = pageContext;
}
/**
*
*/
public async initializeData(): Promise<void> {
// 检查是否已静默登录,如果是则尝试获取真实位置
let initialLongitude = 102.833722;
let initialLatitude = 24.880095;
const app = getApp<any>();
if (app.globalData.isLoggedIn) {
try {
// 导入地图服务
const mapService = require('../../../services/mapService').default;
const location = await mapService.getCurrentLocation();
if (location && !isNaN(location.latitude) && !isNaN(location.longitude)) {
initialLongitude = location.longitude;
initialLatitude = location.latitude;
console.log('[DATA MODULE] 静默登录后使用真实位置:', location);
}
} catch (error) {
console.warn('[DATA MODULE] 获取真实位置失败,使用默认位置:', error);
}
}
2025-10-16 21:32:16 +08:00
this.pageContext.setData({
// 地图相关数据
longitude: initialLongitude,
latitude: initialLatitude,
2025-10-16 21:32:16 +08:00
scale: 13,
markers: [] as Marker[],
polyline: null,
showRoute: false,
routeDistance: 0,
routeDuration: 0,
currentRoute: null,
// 用户相关数据
userInfo: null as UserInfo | null,
isLoggedIn: false,
showUserPanel: false,
// 订单相关数据
pendingOrders: [] as any[],
showOrderPanel: false,
currentOrder: null,
// 货运人员相关数据
showDeliveryPersonModal: false,
deliveryPersonModalState: 'bottom',
currentDeliveryPerson: null,
// 仓库相关数据
showWarehouseModal: false,
warehouseModalState: 'bottom',
currentWarehouse: null,
// 面板位置数据
currentPanelPosition: { x: 0, y: 0 },
// 对话框相关数据
showNickNameDialog: false,
tempNickName: ''
});
}
/**
*
*/
public updateUserInfo(userInfo: UserInfo | null): void {
this.pageContext.setData({
userInfo,
isLoggedIn: !!userInfo
});
// 更新按钮显示状态
if (this.pageContext.updateButtonDisplayStatus) {
this.pageContext.updateButtonDisplayStatus();
}
}
/**
*
*/
public setLoginStatus(isLoggedIn: boolean): void {
this.pageContext.setData({
isLoggedIn
});
}
/**
*
*/
public setMapScale(scale: number): void {
this.pageContext.setData({
scale
});
}
/**
*
*/
public updateUserLocation(latitude: number, longitude: number): void {
this.pageContext.setData({
latitude,
longitude
});
// 同时更新地图上的用户标记点
this.updateUserMarkerPosition(longitude, latitude);
}
/**
*
*/
public updateUserMarkerPosition(longitude: number, latitude: number, userId?: number): void {
const { markers } = this.pageContext.data;
// 如果没有指定用户ID默认使用-1当前用户
const targetUserId = userId !== undefined ? userId : -1;
const markerTitle = targetUserId === -1 ? '用户位置' : `用户${targetUserId}`;
// 查找用户标记点
const userMarkerIndex = markers.findIndex((marker: any) => marker.id === targetUserId);
if (userMarkerIndex !== -1) {
// 更新用户标记点位置
const updatedMarkers = [...markers];
updatedMarkers[userMarkerIndex] = {
...updatedMarkers[userMarkerIndex],
latitude,
longitude
};
this.updateMarkers(updatedMarkers);
} else {
// 创建新的用户标记点
const newUserMarker = {
id: targetUserId,
title: markerTitle,
longitude: longitude,
latitude: latitude,
iconPath: '/images/trucks.png',
width: 40,
height: 40,
zIndex: targetUserId === -1 ? 99 : 98 // 当前用户层级更高
};
const updatedMarkers = [...markers, newUserMarker];
this.updateMarkers(updatedMarkers);
}
}
/**
*
*/
public updateMarkers(markers: Marker[]): void {
// 验证每个标记点的坐标
const validatedMarkers = markers.map((marker) => {
2025-10-16 21:32:16 +08:00
// 检查经纬度是否为有效数字
if (isNaN(marker.longitude) || isNaN(marker.latitude)) {
// 为无效坐标设置默认值
return {
...marker,
longitude: 102.833722, // 默认经度
latitude: 24.880095 // 默认纬度
};
} else {
return marker;
}
});
// 执行更新
this.pageContext.setData({ markers: validatedMarkers });
}
/**
*
*/
public updatePendingOrders(orders: any[]): void {
this.pageContext.setData({ pendingOrders: orders });
}
/**
*
*/
public setCurrentOrder(order: any): void {
this.pageContext.setData({ currentOrder: order });
}
/**
*
*/
public setCurrentDeliveryPerson(person: any): void {
this.pageContext.setData({ currentDeliveryPerson: person });
}
/**
*
*/
public setCurrentWarehouse(warehouse: any): void {
this.pageContext.setData({ currentWarehouse: warehouse });
}
/**
*
*/
public setPanelPosition(position: { x: number; y: number }): void {
this.pageContext.setData({ currentPanelPosition: position });
}
/**
* /
*/
public toggleUserPanel(show: boolean): void {
this.pageContext.setData({ showUserPanel: show });
}
/**
* /
*/
public toggleOrderPanel(show: boolean): void {
this.pageContext.setData({ showOrderPanel: show });
}
/**
* /
*/
public toggleWarehouseModal(show: boolean, state: 'bottom' | 'full' = 'bottom'): void {
this.pageContext.setData({
showWarehouseModal: show,
warehouseModalState: state
});
}
/**
* /
*/
public toggleDeliveryPersonModal(show: boolean, state: 'bottom' | 'full' = 'bottom'): void {
this.pageContext.setData({
showDeliveryPersonModal: show,
deliveryPersonModalState: state
});
}
/**
* /
*/
public toggleNickNameDialog(show: boolean, tempNickName: string = ''): void {
this.pageContext.setData({
showNickNameDialog: show,
tempNickName
});
}
/**
* 线
*/
public updateRouteInfo(polyline: any, distance: number, duration: number): void {
this.pageContext.setData({
polyline,
routeDistance: distance,
routeDuration: duration,
showRoute: true
});
}
/**
* 线
*/
public clearRouteInfo(): void {
this.pageContext.setData({
polyline: null,
showRoute: false,
routeDistance: 0,
routeDuration: 0,
currentRoute: null
});
}
/**
*
*/
public getData(): any {
return this.pageContext.data;
}
/**
*
*/
public resetAllData(): void {
this.initializeData();
}
/**
* 访
*/
public getLocationModule(): any {
// 通过页面上下文获取位置模块
if (this.pageContext.data.mainPageModule) {
return this.pageContext.data.mainPageModule.getLocationModule();
}
return null;
}
/**
* 访
*/
public getMapModule(): any {
// 通过页面上下文获取地图模块
if (this.pageContext.data.mainPageModule) {
return this.pageContext.data.mainPageModule.getMapModule();
}
return null;
}
2025-10-16 21:32:16 +08:00
}