Files
wxapp_companion/pages/order/index.js
T
lik 8d38c90274 feat(admin): 订单列表按取消状态展示与操作
- 新增 displayStatus,展示状态优先取 cancelStatus(待取消/已取消)
- 首页最近订单、订单列表与筛选统一按展示状态统计
- 拉取订单包含已取消记录,筛选改在本页完成
- 取消操作只写 cancelStatus,不再覆盖服务状态
2026-11-05 18:55:28 +08:00

416 lines
10 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// pages/order/index.js
const API = require('../../utils/api.js')
const { calcAge } = require('../../utils/format.js')
// 状态映射配置
const STATUS_MAP = {
pending: { label: '待确认', text: '待确认', color: '#f59f00' },
confirmed: { label: '已确认', text: '已确认', color: '#1abc9c' },
cancel_requested: { label: '待取消', text: '待取消', color: '#f59f00' },
in_progress: { label: '进行中', text: '进行中', color: '#20c997' },
completed: { label: '已完成', text: '已完成', color: '#51cf66' },
cancelled: { label: '已取消', text: '已取消', color: '#ff6b6b' }
};
// 状态筛选选项
const STATUS_FILTERS = [
{ label: '全部', value: '', count: 0 },
{ label: '待确认', value: 'pending', count: 0 },
{ label: '待取消', value: 'cancel_requested', count: 0 },
{ label: '已确认', value: 'confirmed', count: 0 },
{ label: '进行中', value: 'in_progress', count: 0 },
{ label: '已完成', value: 'completed', count: 0 },
{ label: '已取消', value: 'cancelled', count: 0 }
];
// 取消与 status 解耦:展示状态优先取 cancelStatus
function resolveDisplayStatus(order) {
const cancelStatus = order?.cancelStatus || 'none'
if (cancelStatus === 'cancelled') return 'cancelled'
if (cancelStatus === 'requested') return 'cancel_requested'
return order?.status || 'pending'
}
Page({
/**
* 页面的初始数据
*/
data: {
// 统计
stats: {
total: 0,
pending: 0,
cancelRequested: 0,
confirmed: 0,
inProgress: 0,
completed: 0,
cancelled: 0
},
// 状态筛选
statusFilters: STATUS_FILTERS,
currentStatus: 'pending',
// 订单列表
orderList: [],
// 分页
page: 1,
pageSize: 10,
hasMore: true,
// 加载状态
isLoading: false,
isLoadingMore: false,
isRefreshing: false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
this.loadOrderList();
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
if (this.data.orderList.length > 0) {
this.refreshData();
}
},
/**
* 计算统计数据
*/
calculateStats(orders) {
const stats = {
total: orders.length,
pending: 0,
cancelRequested: 0,
confirmed: 0,
inProgress: 0,
completed: 0,
cancelled: 0
};
orders.forEach(order => {
switch (resolveDisplayStatus(order)) {
case 'pending':
stats.pending++;
break;
case 'cancel_requested':
stats.cancelRequested++;
break;
case 'confirmed':
stats.confirmed++;
break;
case 'in_progress':
stats.inProgress++;
break;
case 'completed':
stats.completed++;
break;
case 'cancelled':
stats.cancelled++;
break;
}
});
return stats;
},
/**
* 更新筛选标签计数
*/
updateFilterCounts(stats) {
const statusFilters = this.data.statusFilters.map(filter => {
let count = 0;
switch (filter.value) {
case '':
count = stats.total;
break;
case 'pending':
count = stats.pending;
break;
case 'cancel_requested':
count = stats.cancelRequested;
break;
case 'confirmed':
count = stats.confirmed;
break;
case 'in_progress':
count = stats.inProgress;
break;
case 'completed':
count = stats.completed;
break;
case 'cancelled':
count = stats.cancelled;
break;
}
return { ...filter, count };
});
this.setData({ statusFilters });
},
/**
* 格式化日期
*/
formatDate(dateStr) {
if (!dateStr) return '';
const date = new Date(dateStr);
const month = (date.getMonth() + 1).toString().padStart(2, '0');
const day = date.getDate().toString().padStart(2, '0');
return `${month}月${day}日`;
},
/**
* 处理订单数据
*/
processOrders(orders) {
return orders.map(order => {
const age = calcAge(order.patient?.birth)
const displayStatus = resolveDisplayStatus(order)
return {
...order,
displayStatus,
statusText: STATUS_MAP[displayStatus]?.text || displayStatus,
patientFirstChar: (order.patient?.name || '?')[0],
patientAgeText: age ? `${age}岁` : '',
schedule: {
...order.schedule,
dateText: this.formatDate(order.schedule?.date)
}
};
});
},
/**
* 加载订单列表
*/
loadOrderList(isRefresh = false) {
if (this.data.isLoading || this.data.isLoadingMore) return;
const { currentStatus, page, pageSize } = this.data;
this.setData({ isLoading: !isRefresh, isLoadingMore: isRefresh && page > 1 });
const params = {
page,
pageSize,
// 筛选在本页按展示状态完成,因此一次取回含已取消在内的全部订单
status: 'pending,confirmed,in_progress,completed',
includeCancelled: 1
};
API.escort.getAllRecords(params)
.then(res => {
if (res.code !== 0) {
wx.showToast({ title: res.msg || '获取订单失败', icon: 'none' });
this.setData({ isLoading: false, isLoadingMore: false, isRefreshing: false });
return;
}
const data = res.data || {};
const list = data.records || [];
let total = list.length || 0;
const processedOrders = this.processOrders(list);
const allOrders = isRefresh && page > 1
? [...this.data.orderList, ...processedOrders]
: processedOrders;
const stats = this.calculateStats(allOrders);
this.updateFilterCounts(stats);
const { currentStatus } = this.data;
const orderList = currentStatus
? allOrders.filter(order => order.displayStatus === currentStatus)
: allOrders;
total = orderList.length;
this.setData({
orderList,
stats,
isLoading: false,
isLoadingMore: false,
isRefreshing: false,
hasMore: orderList.length < total
});
})
.catch(err => {
console.error('获取订单列表失败', err);
wx.showToast({ title: '网络错误,请重试', icon: 'none' });
this.setData({ isLoading: false, isLoadingMore: false, isRefreshing: false });
});
},
/**
* 刷新数据
*/
refreshData() {
this.setData({ page: 1, hasMore: true }, () => {
this.loadOrderList();
});
},
/**
* 跳转新增预约(页面内选择健康档案)
*/
onAddOrder() {
wx.navigateTo({
url: '/pages/order/orderEdit'
});
},
/**
* 空状态刷新
*/
onReload() {
this.setData({ page: 1, hasMore: true }, () => {
this.loadOrderList();
});
},
/**
* 状态筛选切换
*/
onStatusChange(e) {
const status = e.currentTarget.dataset.status;
if (status === this.data.currentStatus) return;
this.setData({
currentStatus: status,
page: 1,
hasMore: true,
orderList: []
}, () => {
this.loadOrderList();
});
},
/**
* 订单详情
*/
onOrderDetail(e) {
const id = e.currentTarget.dataset.id;
wx.navigateTo({
url: `/pages/order/orderDetail?id=${id}`
});
},
/**
* 订单操作
*/
onOrderAction(e) {
const { id, action } = e.currentTarget.dataset;
const order = this.data.orderList.find(item => item._id === id);
if (!order) return;
const actionMap = {
confirm: { title: '确认订单', content: '确认接受此订单?', nextStatus: 'confirmed' },
cancel: { title: '取消订单', content: '确定要取消此订单?', nextStatus: 'cancelled' },
start: { title: '开始服务', content: '确认开始陪诊服务?', nextStatus: 'in_progress' },
complete: { title: '完成服务', content: '确认陪诊服务已完成?', nextStatus: 'completed' },
detail: { title: '', content: '', nextStatus: '' }
};
const actionConfig = actionMap[action];
if (!actionConfig) return;
if (action === 'detail') {
wx.navigateTo({
url: `/pages/order/orderDetail?id=${id}`
});
return;
}
wx.showModal({
title: actionConfig.title,
content: actionConfig.content,
confirmColor: '#1abc9c',
success: (res) => {
if (res.confirm) {
this.updateOrderStatus(id, actionConfig.nextStatus);
}
}
});
},
/**
* 更新订单状态
*/
updateOrderStatus(id, newStatus) {
wx.showLoading({ title: '处理中...' });
API.escort.updateStatus(id, { status: newStatus })
.then(res => {
if (res.code !== 0) {
wx.showToast({ title: res.msg || '操作失败', icon: 'none' });
wx.hideLoading();
return;
}
const orderList = this.data.orderList.map(order => {
if (order._id === id) {
// 取消只写 cancelStatus,status 保留取消前的服务状态
const next = newStatus === 'cancelled'
? { ...order, cancelStatus: 'cancelled', cancelledAt: new Date().toISOString() }
: { ...order, status: newStatus }
const displayStatus = resolveDisplayStatus(next)
return {
...next,
displayStatus,
statusText: STATUS_MAP[displayStatus]?.text || displayStatus,
'meta.updatetime': new Date().toISOString()
};
}
return order;
});
const stats = this.calculateStats(orderList);
this.updateFilterCounts(stats);
this.setData({ orderList, stats });
wx.hideLoading();
wx.showToast({ title: '操作成功', icon: 'success' });
})
.catch(err => {
console.error('更新订单状态失败', err);
wx.showToast({ title: '网络错误,请重试', icon: 'none' });
wx.hideLoading();
});
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
this.setData({ isRefreshing: true, page: 1, hasMore: true }, () => {
this.loadOrderList();
});
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
if (!this.data.hasMore || this.data.isLoadingMore) return;
this.setData({ page: this.data.page + 1 }, () => {
this.loadOrderList(true);
});
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
return {
title: '订单管理',
path: '/pages/order/index'
};
}
});