diff --git a/pages/order/detail/index.js b/pages/order/detail/index.js index a5a2978..6f65d47 100644 --- a/pages/order/detail/index.js +++ b/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' diff --git a/pages/order/index.js b/pages/order/index.js index c2897d7..a8ee3db 100644 --- a/pages/order/index.js +++ b/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,51 +119,72 @@ 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: { - 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 || []; + // 并行请求订单列表和统计数据 + 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 + }).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) { diff --git a/pages/order/index.wxml b/pages/order/index.wxml index f8d81e2..4da5420 100644 --- a/pages/order/index.wxml +++ b/pages/order/index.wxml @@ -32,6 +32,39 @@ + + + + 订单统计 + + + + 总订单数 + {{statistics.totalOrders}} + + + 总金额 + ¥{{statistics.totalAmount}} + + + 总件数 + {{statistics.totalPieces}} + + + 总斤数 + {{statistics.totalWeight}} + + + 未付款 + ¥{{statistics.unpaidAmount}} + + + 已消费 + + + + + diff --git a/pages/order/index.wxss b/pages/order/index.wxss index cd49fe6..539381a 100644 --- a/pages/order/index.wxss +++ b/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; + } } /* 订单列表 */ diff --git a/server-example/server-mysql.js b/server-example/server-mysql.js index c224f1b..5cba933 100644 --- a/server-example/server-mysql.js +++ b/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,