Files
WXProgram/miniprogram/pages/staff/order-management.ts
2025-10-19 23:38:54 +08:00

433 lines
10 KiB
TypeScript

// 订单管理页面 - 处理订单的增删改查和状态管理
import { Order, UserInfo, DeliveryPerson, WarehouseInfo } from '../../types';
import orderService from '../../services/orderService';
import deliveryService from '../../services/deliveryPersonService';
import warehouseService from '../../services/warehouseService';
Page({
data: {
userInfo: null as UserInfo | null,
// 订单相关数据
orders: [] as Order[],
filteredOrders: [] as Order[],
activeTab: 'all',
// 员工相关数据
deliveryPersons: [] as DeliveryPerson[],
availableStaff: [] as DeliveryPerson[],
// 仓库数据
warehouses: [] as WarehouseInfo[],
// 弹窗状态
showAssignModal: false,
showAddModal: false,
selectedOrderId: 0,
// 新增订单表单数据
warehouseIndex: 0,
endName: '',
goodsType: '',
goodsWeight: 0,
// 页面状态
loading: false,
errorMessage: '',
successMessage: ''
},
onLoad() {
// 获取用户信息
this.getUserInfo();
// 初始化数据
this.initData();
},
onShow() {
// 页面显示时刷新数据
this.refreshData();
},
onPullDownRefresh() {
// 下拉刷新
this.refreshData().then(() => {
wx.stopPullDownRefresh();
});
},
// 获取用户信息
getUserInfo() {
try {
const userInfo = wx.getStorageSync('userInfo');
if (userInfo) {
this.setData({
userInfo
});
}
} catch (error) {
console.error('获取用户信息失败:', error);
}
},
// 初始化数据
initData() {
// 获取仓库数据
warehouseService.getWarehouses().then(warehouses => {
this.setData({
warehouses
});
}).catch(error => {
console.error('获取仓库数据失败:', error);
this.showError('获取仓库数据失败');
});
},
// 刷新数据
async refreshData() {
this.setData({
loading: true,
errorMessage: '',
successMessage: ''
});
try {
// 获取订单数据
const orders = await orderService.getPendingOrders();
// 获取员工数据
const deliveryPersons = await deliveryService.getDeliveryPersons();
this.setData({
orders,
deliveryPersons,
loading: false
});
// 过滤订单
this.filterOrders();
} catch (error) {
console.error('刷新数据失败:', error);
this.setData({
loading: false
});
this.showError('刷新数据失败,请重试');
}
},
// 过滤订单
filterOrders() {
const { orders, activeTab } = this.data;
let filteredOrders = orders;
if (activeTab !== 'all') {
filteredOrders = orders.filter(order => order.status === activeTab);
}
this.setData({
filteredOrders
});
},
// 切换订单标签
switchTab(e: any) {
const tab = e.currentTarget.dataset.tab;
this.setData({
activeTab: tab
});
this.filterOrders();
},
// 处理订单点击
handleOrderTap(e: any) {
const orderId = e.currentTarget.dataset.id;
const order = this.data.orders.find(o => o.id === orderId);
if (!order) return;
if (order.status === 'pending') {
// 未分配的订单,显示分配弹窗
const availableStaff = this.data.deliveryPersons.filter(person => person.status === 'idle');
this.setData({
showAssignModal: true,
selectedOrderId: orderId,
availableStaff
});
} else {
// 显示订单详情
this.showOrderDetail(order);
}
},
// 显示订单详情
showOrderDetail(order: Order) {
wx.showModal({
title: `订单 #${order.id}`,
content: `起点: ${order.startPoint.name}\n终点: ${order.endPoint.name}\n货物: ${order.goodsType} ${order.goodsWeight}kg\n状态: ${this.getStatusText(order.status)}`,
showCancel: false
});
},
// 显示新增订单弹窗
showAddOrderModal() {
this.setData({
showAddModal: true,
endName: '',
goodsType: '',
goodsWeight: 0,
warehouseIndex: 0
});
},
// 隐藏新增订单弹窗
hideAddModal() {
this.setData({
showAddModal: false
});
},
// 隐藏分配弹窗
hideAssignModal() {
this.setData({
showAssignModal: false
});
},
// 仓库选择变化
onWarehouseChange(e: any) {
this.setData({
warehouseIndex: e.detail.value
});
},
// 终点名称输入
onEndNameInput(e: any) {
this.setData({
endName: e.detail.value
});
},
// 货物类型输入
onGoodsTypeInput(e: any) {
this.setData({
goodsType: e.detail.value
});
},
// 货物重量输入
onGoodsWeightInput(e: any) {
this.setData({
goodsWeight: parseFloat(e.detail.value) || 0
});
},
// 创建订单
createOrder() {
const { warehouses, warehouseIndex, endName, goodsType, goodsWeight } = this.data;
// 表单验证
if (!warehouses[warehouseIndex]) {
this.showError('请选择仓库');
return;
}
if (!endName) {
this.showError('请输入终点名称');
return;
}
if (!goodsType) {
this.showError('请输入货物类型');
return;
}
if (goodsWeight <= 0) {
this.showError('请输入有效的货物重量');
return;
}
// 获取仓库信息
const warehouse = warehouses[warehouseIndex];
// 创建订单数据
const newOrder: Omit<Order, 'id' | 'createTime'> = {
startPoint: {
id: warehouse.id,
name: warehouse.name,
longitude: warehouse.longitude || 102.715,
latitude: warehouse.latitude || 25.045
},
endPoint: {
name: endName,
longitude: 102.715 + (Math.random() - 0.5) * 0.1,
latitude: 25.045 + (Math.random() - 0.5) * 0.1
},
status: 'pending',
goodsType,
goodsWeight
};
// 调用服务创建订单
orderService.createOrder(newOrder).then(() => {
this.showSuccess('订单创建成功');
this.refreshData();
}).catch(error => {
console.error('创建订单失败:', error);
this.showError('创建订单失败');
});
this.hideAddModal();
},
// 分配订单
assignOrder(e: any) {
const staffId = e.currentTarget.dataset.id;
const { selectedOrderId } = this.data;
if (!selectedOrderId || !staffId) {
this.showError('请选择订单和货运人员');
return;
}
// 分配订单
orderService.assignOrder(selectedOrderId, staffId).then(result => {
if (result.success) {
this.showSuccess('订单指派成功');
this.refreshData();
} else {
this.showError(result.message || '指派失败');
}
}).catch(error => {
console.error('指派订单失败:', error);
this.showError('指派失败,请重试');
});
this.hideAssignModal();
},
// 删除订单
deleteOrder(e: any) {
const orderId = e.currentTarget.dataset.id;
wx.showModal({
title: '确认删除',
content: '确定要删除这个订单吗?',
success: (res) => {
if (res.confirm) {
// 调用删除订单接口
// orderService.deleteOrder(orderId).then(() => {
// this.showSuccess('订单删除成功');
// this.refreshData();
// }).catch(error => {
// console.error('删除订单失败:', error);
// this.showError('删除订单失败');
// });
// 暂时使用模拟删除
const orders = this.data.orders.filter(order => order.id !== orderId);
this.setData({
orders
});
this.filterOrders();
this.showSuccess('订单删除成功');
}
}
});
},
// 更新订单状态
updateOrderStatus(e: any) {
const orderId = e.currentTarget.dataset.id;
const status = e.currentTarget.dataset.status;
// 调用更新订单状态接口
orderService.updateOrderStatus(orderId, status).then(result => {
if (result.success) {
this.showSuccess('订单状态更新成功');
this.refreshData();
} else {
this.showError(result.message || '更新失败');
}
}).catch(error => {
console.error('更新订单状态失败:', error);
this.showError('更新失败,请重试');
});
},
// 获取状态文本
getStatusText(status: string): string {
const statusMap: Record<string, string> = {
'pending': '未分配',
'assigned': '已分配',
'in_transit': '配送中',
'delivered': '已完成',
'cancelled': '已取消'
};
return statusMap[status] || status;
},
// 格式化时间
formatTime(time: number): string {
const date = new Date(time);
const now = new Date();
const diff = now.getTime() - date.getTime();
if (diff < 60000) {
return '刚刚';
} else if (diff < 3600000) {
return `${Math.floor(diff / 60000)}分钟前`;
} else if (diff < 86400000) {
return `${Math.floor(diff / 3600000)}小时前`;
} else {
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();
return `${month}${day}${hour}:${minute < 10 ? '0' + minute : minute}`;
}
},
// 显示错误消息
showError(message: string) {
this.setData({
errorMessage: message,
successMessage: ''
});
// 3秒后自动清除
setTimeout(() => {
this.setData({
errorMessage: ''
});
}, 3000);
},
// 显示成功消息
showSuccess(message: string) {
this.setData({
successMessage: message,
errorMessage: ''
});
// 3秒后自动清除
setTimeout(() => {
this.setData({
successMessage: ''
});
}, 3000);
},
// 清除消息
clearMessage() {
this.setData({
errorMessage: '',
successMessage: ''
});
},
// 返回上一页
goBack() {
wx.navigateBack();
}
});