Files
WXProgram/dist/pages/staff/order-management.js

386 lines
12 KiB
JavaScript
Raw Permalink Normal View History

2025-10-26 13:15:04 +08:00
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const orderService_1 = __importDefault(require("../../services/orderService"));
const deliveryPersonService_1 = __importDefault(require("../../services/deliveryPersonService"));
const warehouseService_1 = __importDefault(require("../../services/warehouseService"));
Page({
data: {
userInfo: null,
// 订单相关数据
orders: [],
filteredOrders: [],
activeTab: 'all',
// 员工相关数据
deliveryPersons: [],
availableStaff: [],
// 仓库数据
warehouses: [],
// 弹窗状态
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_1.default.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_1.default.getPendingOrders();
// 获取员工数据
const deliveryPersons = await deliveryPersonService_1.default.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) {
const tab = e.currentTarget.dataset.tab;
this.setData({
activeTab: tab
});
this.filterOrders();
},
// 处理订单点击
handleOrderTap(e) {
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) {
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) {
this.setData({
warehouseIndex: e.detail.value
});
},
// 终点名称输入
onEndNameInput(e) {
this.setData({
endName: e.detail.value
});
},
// 货物类型输入
onGoodsTypeInput(e) {
this.setData({
goodsType: e.detail.value
});
},
// 货物重量输入
onGoodsWeightInput(e) {
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 = {
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_1.default.createOrder(newOrder).then(() => {
this.showSuccess('订单创建成功');
this.refreshData();
}).catch(error => {
console.error('创建订单失败:', error);
this.showError('创建订单失败');
});
this.hideAddModal();
},
// 分配订单
assignOrder(e) {
const staffId = e.currentTarget.dataset.id;
const { selectedOrderId } = this.data;
if (!selectedOrderId || !staffId) {
this.showError('请选择订单和货运人员');
return;
}
// 分配订单
orderService_1.default.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) {
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) {
const orderId = e.currentTarget.dataset.id;
const status = e.currentTarget.dataset.status;
// 调用更新订单状态接口
orderService_1.default.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) {
const statusMap = {
'pending': '未分配',
'assigned': '已分配',
'in_transit': '配送中',
'delivered': '已完成',
'cancelled': '已取消'
};
return statusMap[status] || status;
},
// 格式化时间
formatTime(time) {
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) {
this.setData({
errorMessage: message,
successMessage: ''
});
// 3秒后自动清除
setTimeout(() => {
this.setData({
errorMessage: ''
});
}, 3000);
},
// 显示成功消息
showSuccess(message) {
this.setData({
successMessage: message,
errorMessage: ''
});
// 3秒后自动清除
setTimeout(() => {
this.setData({
successMessage: ''
});
}, 3000);
},
// 清除消息
clearMessage() {
this.setData({
errorMessage: '',
successMessage: ''
});
},
// 返回上一页
goBack() {
wx.navigateBack();
}
});