Browse Source

fix: 订单统计功能优化,添加总斤数统计

pull/19/head
Trae AI 1 month ago
parent
commit
ecd7c43327
  1. 2
      pages/order/detail/index.js
  2. 127
      pages/order/index.js
  3. 33
      pages/order/index.wxml
  4. 69
      pages/order/index.wxss
  5. 101
      server-example/server-mysql.js

2
pages/order/detail/index.js

@ -43,7 +43,7 @@ Page({
this.setData({ loading: true, error: '' });
wx.request({
url: `http://localhost:3003/api/orders/detail/${orderId}`,
url: `http://192.168.1.30:3003/api/orders/detail/${orderId}`,
method: 'GET',
header: {
'Content-Type': 'application/json'

127
pages/order/index.js

@ -1,4 +1,6 @@
// pages/order/index.js
const API = require('../../utils/api.js');
Page({
// 分享给朋友/群聊
onShareAppMessage() {
@ -34,7 +36,16 @@ Page({
page: 1,
pageSize: 10,
hasMore: true,
loadingMore: false
loadingMore: false,
// 统计信息
statistics: {
totalOrders: 0,
totalAmount: 0,
totalPieces: 0,
totalWeight: 0,
unpaidAmount: 0,
paidAmount: 0
}
},
onLoad() {
@ -47,7 +58,6 @@ Page({
onShow() {
// 页面显示时的逻辑
this.loadUserInfo(() => {
this.loadOrders();
// 更新自定义tabBar状态
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
this.getTabBar().setData({
@ -109,50 +119,71 @@ Page({
console.log('loadOrders - activeTab:', activeTab, 'paymentStatus:', paymentStatus, 'page:', page);
wx.request({
url: 'http://localhost:3003/api/orders/list',
method: 'POST',
header: {
'Content-Type': 'application/json'
},
data: {
// 并行请求订单列表和统计数据
const orderRequest = API.request('/api/orders/list', 'POST', {
phoneNumber: phoneNumber,
startDate: this.data.dateRange.start,
endDate: this.data.dateRange.end,
paymentStatus: paymentStatus,
page: page,
pageSize: pageSize
},
success: (res) => {
console.log('获取订单列表成功:', res.data);
if (res.data.success) {
const newOrders = res.data.data.orders || [];
}).then(res => {
return { data: res };
}).catch(err => {
throw err;
});
// 只有在非加载更多时才请求统计数据(加载更多时统计数据不变)
const statisticsRequest = isLoadMore ? Promise.resolve(null) : API.request('/api/orders/statistics', 'POST', {
phoneNumber: phoneNumber,
startDate: this.data.dateRange.start,
endDate: this.data.dateRange.end,
paymentStatus: paymentStatus
}).then(res => {
return { data: res };
}).catch(err => {
throw err;
});
// 处理所有请求
Promise.all([orderRequest, statisticsRequest])
.then(([orderRes, statisticsRes]) => {
console.log('获取订单列表成功:', orderRes.data);
if (orderRes.data.success) {
const newOrders = orderRes.data.data.orders || [];
const totalOrders = isLoadMore ? [...this.data.orders, ...newOrders] : newOrders;
const hasMore = newOrders.length === pageSize;
// 处理统计数据
let statistics = this.data.statistics;
if (statisticsRes && statisticsRes.data && statisticsRes.data.success) {
statistics = statisticsRes.data.data;
console.log('获取统计数据成功:', statistics);
}
this.setData({
orders: totalOrders,
page: page,
hasMore: hasMore,
loading: false,
loadingMore: false
loadingMore: false,
statistics: statistics
});
} else {
this.setData({
error: res.data.message || '获取订单失败',
error: orderRes.data.message || '获取订单失败',
loading: false,
loadingMore: false
});
}
},
fail: (err) => {
console.error('获取订单列表失败:', err);
})
.catch((err) => {
console.error('请求失败:', err);
this.setData({
error: '网络请求失败,请稍后重试',
error: err.message || '网络请求失败,请稍后重试',
loading: false,
loadingMore: false
});
}
});
},
@ -220,6 +251,60 @@ Page({
});
},
// 计算统计信息
calculateStatistics() {
const orders = this.data.orders;
if (!orders || orders.length === 0) {
this.setData({
statistics: {
totalOrders: 0,
totalAmount: 0,
totalPieces: 0,
unpaidAmount: 0,
paidAmount: 0
}
});
return;
}
let totalOrders = orders.length;
let totalAmount = 0;
let totalPieces = 0;
let unpaidAmount = 0;
let paidAmount = 0;
orders.forEach(order => {
// 累加总金额和总件数
if (order.total_amount) {
totalAmount += parseFloat(order.total_amount) || 0;
}
if (order.total_pieces) {
totalPieces += parseInt(order.total_pieces) || 0;
}
// 区分未付款和已付款
if (order.payment_status === '未收款') {
if (order.total_amount) {
unpaidAmount += parseFloat(order.total_amount) || 0;
}
} else if (order.payment_status === '全款') {
if (order.total_amount) {
paidAmount += parseFloat(order.total_amount) || 0;
}
}
});
this.setData({
statistics: {
totalOrders,
totalAmount: Math.round(totalAmount * 100) / 100,
totalPieces,
unpaidAmount: Math.round(unpaidAmount * 100) / 100,
paidAmount: Math.round(paidAmount * 100) / 100
}
});
},
// 滚动到底部的预加载逻辑
onReachBottom() {
if (!this.data.loadingMore && this.data.hasMore) {

33
pages/order/index.wxml

@ -32,6 +32,39 @@
</view>
</view>
<!-- 统计信息 -->
<view class="statistics-card">
<view class="statistics-header">
<text class="statistics-title">订单统计</text>
</view>
<view class="statistics-body">
<view class="stat-item">
<view class="stat-label">总订单数</view>
<view class="stat-value">{{statistics.totalOrders}}</view>
</view>
<view class="stat-item">
<view class="stat-label">总金额</view>
<view class="stat-value">¥{{statistics.totalAmount}}</view>
</view>
<view class="stat-item">
<view class="stat-label">总件数</view>
<view class="stat-value">{{statistics.totalPieces}}</view>
</view>
<view class="stat-item">
<view class="stat-label">总斤数</view>
<view class="stat-value">{{statistics.totalWeight}}</view>
</view>
<view class="stat-item">
<view class="stat-label">未付款</view>
<view class="stat-value unpaid">¥{{statistics.unpaidAmount}}</view>
</view>
<view class="stat-item">
<view class="stat-label">已消费</view>
<view class="stat-value paid">¥{{statistics.paidAmount}}</view>
</view>
</view>
</view>
<!-- 订单列表 -->
<view class="order-list">
<!-- 加载中 -->

69
pages/order/index.wxss

@ -127,6 +127,59 @@
color: #fff;
}
/* 统计信息卡片 */
.statistics-card {
background-color: #fff;
margin: 10rpx;
border-radius: 12rpx;
box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.05);
overflow: hidden;
}
.statistics-header {
background-color: #1677ff;
color: #fff;
padding: 20rpx;
font-size: 28rpx;
font-weight: bold;
}
.statistics-body {
padding: 20rpx;
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 20rpx;
}
.stat-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 15rpx;
background-color: #f9f9f9;
border-radius: 8rpx;
}
.stat-label {
font-size: 24rpx;
color: #666;
font-weight: 500;
}
.stat-value {
font-size: 28rpx;
font-weight: bold;
color: #333;
}
.stat-value.unpaid {
color: #ff4d4f;
}
.stat-value.paid {
color: #52c41a;
}
/* 响应式设计 */
@media (max-width: 375px) {
.date-filter-row {
@ -147,6 +200,22 @@
.picker {
flex: 1;
}
.statistics-body {
grid-template-columns: 1fr;
}
.stat-item {
padding: 12rpx;
}
.stat-label {
font-size: 22rpx;
}
.stat-value {
font-size: 26rpx;
}
}
/* 订单列表 */

101
server-example/server-mysql.js

@ -10348,6 +10348,107 @@ app.get('/api/orders/detail/:dataid', async (req, res) => {
}
});
// 订单统计API端点 - 获取完整的订单统计数据(不分页)
app.post('/api/orders/statistics', async (req, res) => {
try {
const { phoneNumber, startDate, endDate, paymentStatus } = req.body;
console.log('获取订单统计请求:', { phoneNumber, startDate, endDate, paymentStatus });
if (!phoneNumber) {
return res.status(400).json({
success: false,
code: 400,
message: '用户电话号码不能为空'
});
}
// 构建查询条件
const whereCondition = {
phone: phoneNumber
};
// 添加时间范围过滤
if (startDate) {
whereCondition.order_date = {
...whereCondition.order_date,
[Op.gte]: startDate
};
}
if (endDate) {
whereCondition.order_date = {
...whereCondition.order_date,
[Op.lte]: endDate
};
}
// 添加付款状态过滤
if (paymentStatus) {
whereCondition.payment_status = paymentStatus;
}
console.log('统计查询条件:', whereCondition);
// 查询所有符合条件的订单(不分页)
const allOrders = await JdSalesMain.findAll({
where: whereCondition,
order: [['order_date', 'DESC']]
});
console.log('统计查询到的订单数量:', allOrders.length);
// 计算统计信息
let totalOrders = allOrders.length;
let totalAmount = 0;
let totalPieces = 0;
let totalWeight = 0;
let unpaidAmount = 0;
let paidAmount = 0;
allOrders.forEach(order => {
const orderData = order.toJSON();
const amount = parseFloat(orderData.total_amount) || 0;
const pieces = parseInt(orderData.total_pieces) || 0;
const weight = parseFloat(orderData.total_weight) || 0;
totalAmount += amount;
totalPieces += pieces;
totalWeight += weight;
if (orderData.payment_status === '未收款') {
unpaidAmount += amount;
} else if (orderData.payment_status === '全款') {
paidAmount += amount;
}
});
const statistics = {
totalOrders,
totalAmount: Math.round(totalAmount * 100) / 100,
totalPieces,
totalWeight: Math.round(totalWeight * 1000) / 1000,
unpaidAmount: Math.round(unpaidAmount * 100) / 100,
paidAmount: Math.round(paidAmount * 100) / 100
};
console.log('计算的完整统计信息:', statistics);
res.status(200).json({
success: true,
code: 200,
message: '获取订单统计成功',
data: statistics
});
} catch (error) {
console.error('获取订单统计失败:', error);
res.status(500).json({
success: false,
code: 500,
message: '获取订单统计失败: ' + error.message
});
}
});
// 导出模型和Express应用供其他模块使用
module.exports = {
User,

Loading…
Cancel
Save