Files
WXProgram/miniprogram/pages/index/modules/dataModule.ts
2025-10-21 21:51:51 +08:00

285 lines
6.6 KiB
TypeScript

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);
}
}
this.pageContext.setData({
// 地图相关数据
longitude: initialLongitude,
latitude: initialLatitude,
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
});
}
/**
* 更新地图标记点
*/
public updateMarkers(markers: Marker[]): void {
// 验证每个标记点的坐标
const validatedMarkers = markers.map((marker) => {
// 检查经纬度是否为有效数字
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;
}
}