|
|
|
|
// pages/order/index.js
|
|
|
|
|
const API = require('../../utils/api.js');
|
|
|
|
|
|
|
|
|
|
Page({
|
|
|
|
|
// 分享给朋友/群聊
|
|
|
|
|
onShareAppMessage() {
|
|
|
|
|
return {
|
|
|
|
|
title: '鸡蛋贸易平台 - 我的订单',
|
|
|
|
|
path: '/pages/order/index',
|
|
|
|
|
imageUrl: '/images/你有好蛋.png'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 分享到朋友圈
|
|
|
|
|
onShareTimeline() {
|
|
|
|
|
return {
|
|
|
|
|
title: '鸡蛋贸易平台 - 我的订单',
|
|
|
|
|
query: '',
|
|
|
|
|
imageUrl: '/images/你有好蛋.png'
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
data: {
|
|
|
|
|
userInfo: {},
|
|
|
|
|
orders: [],
|
|
|
|
|
loading: false,
|
|
|
|
|
error: '',
|
|
|
|
|
// 订单状态标签
|
|
|
|
|
activeTab: 'all',
|
|
|
|
|
// 时间筛选相关
|
|
|
|
|
dateRange: {
|
|
|
|
|
start: '',
|
|
|
|
|
end: ''
|
|
|
|
|
},
|
|
|
|
|
// 分页相关
|
|
|
|
|
page: 1,
|
|
|
|
|
pageSize: 10,
|
|
|
|
|
hasMore: true,
|
|
|
|
|
loadingMore: false,
|
|
|
|
|
// 统计信息
|
|
|
|
|
statistics: {
|
|
|
|
|
totalOrders: 0,
|
|
|
|
|
totalAmount: 0,
|
|
|
|
|
totalPieces: 0,
|
|
|
|
|
totalWeight: 0,
|
|
|
|
|
unpaidAmount: 0,
|
|
|
|
|
paidAmount: 0
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onLoad() {
|
|
|
|
|
// 页面加载时的初始化逻辑
|
|
|
|
|
this.loadUserInfo(() => {
|
|
|
|
|
this.loadOrders();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
onShow() {
|
|
|
|
|
// 页面显示时的逻辑
|
|
|
|
|
this.loadUserInfo(() => {
|
|
|
|
|
// 更新自定义tabBar状态
|
|
|
|
|
if (typeof this.getTabBar === 'function' && this.getTabBar()) {
|
|
|
|
|
this.getTabBar().setData({
|
|
|
|
|
selected: 4 // 保持与个人中心相同的选中状态
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 加载用户信息
|
|
|
|
|
loadUserInfo(callback) {
|
|
|
|
|
const app = getApp();
|
|
|
|
|
let userInfo = {};
|
|
|
|
|
|
|
|
|
|
if (app.globalData.userInfo) {
|
|
|
|
|
userInfo = app.globalData.userInfo;
|
|
|
|
|
} else {
|
|
|
|
|
userInfo = wx.getStorageSync('userInfo') || {};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 同步更新用户信息,确保loadOrders能获取到最新数据
|
|
|
|
|
this.data.userInfo = userInfo;
|
|
|
|
|
|
|
|
|
|
// 同时异步更新UI
|
|
|
|
|
this.setData({ userInfo: userInfo }, () => {
|
|
|
|
|
if (callback) callback();
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 加载订单数据
|
|
|
|
|
loadOrders(isLoadMore = false, activeTab = this.data.activeTab) {
|
|
|
|
|
const userInfo = this.data.userInfo;
|
|
|
|
|
const phoneNumber = userInfo.phoneNumber;
|
|
|
|
|
|
|
|
|
|
if (!phoneNumber) {
|
|
|
|
|
this.setData({
|
|
|
|
|
error: '请先登录并绑定电话号码',
|
|
|
|
|
orders: []
|
|
|
|
|
});
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isLoadMore) {
|
|
|
|
|
this.setData({ loadingMore: true });
|
|
|
|
|
} else {
|
|
|
|
|
this.setData({ loading: true, error: '' });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const page = isLoadMore ? this.data.page + 1 : 1;
|
|
|
|
|
const pageSize = this.data.pageSize;
|
|
|
|
|
|
|
|
|
|
// 根据标签确定付款状态
|
|
|
|
|
let paymentStatus = '';
|
|
|
|
|
if (activeTab === 'unpaid') {
|
|
|
|
|
paymentStatus = '未收款';
|
|
|
|
|
} else if (activeTab === 'completed') {
|
|
|
|
|
paymentStatus = '全款';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('loadOrders - activeTab:', activeTab, 'paymentStatus:', paymentStatus, 'page:', page);
|
|
|
|
|
|
|
|
|
|
// 并行请求订单列表和统计数据
|
|
|
|
|
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,
|
|
|
|
|
statistics: statistics
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
this.setData({
|
|
|
|
|
error: orderRes.data.message || '获取订单失败',
|
|
|
|
|
loading: false,
|
|
|
|
|
loadingMore: false
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
console.error('请求失败:', err);
|
|
|
|
|
this.setData({
|
|
|
|
|
error: err.message || '网络请求失败,请稍后重试',
|
|
|
|
|
loading: false,
|
|
|
|
|
loadingMore: false
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 返回上一页
|
|
|
|
|
goBack() {
|
|
|
|
|
wx.navigateBack();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 查看订单详情
|
|
|
|
|
viewOrderDetail(e) {
|
|
|
|
|
const orderId = e.currentTarget.dataset.orderId;
|
|
|
|
|
wx.navigateTo({
|
|
|
|
|
url: `/pages/order/detail/index?orderId=${orderId}`
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 去首页购物
|
|
|
|
|
goToHome() {
|
|
|
|
|
wx.switchTab({
|
|
|
|
|
url: '/pages/index/index'
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 选择开始日期
|
|
|
|
|
bindStartDateChange(e) {
|
|
|
|
|
const startDate = e.detail.value;
|
|
|
|
|
this.setData({
|
|
|
|
|
'dateRange.start': startDate
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 选择结束日期
|
|
|
|
|
bindEndDateChange(e) {
|
|
|
|
|
const endDate = e.detail.value;
|
|
|
|
|
this.setData({
|
|
|
|
|
'dateRange.end': endDate
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 重置日期筛选
|
|
|
|
|
resetDateFilter() {
|
|
|
|
|
this.setData({
|
|
|
|
|
'dateRange.start': '',
|
|
|
|
|
'dateRange.end': ''
|
|
|
|
|
});
|
|
|
|
|
this.loadOrders();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 应用日期筛选
|
|
|
|
|
applyDateFilter() {
|
|
|
|
|
this.loadOrders();
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 切换订单状态标签
|
|
|
|
|
switchTab(e) {
|
|
|
|
|
const tab = e.currentTarget.dataset.tab;
|
|
|
|
|
console.log('switchTab - tab:', tab);
|
|
|
|
|
this.setData({
|
|
|
|
|
activeTab: tab,
|
|
|
|
|
page: 1,
|
|
|
|
|
orders: []
|
|
|
|
|
}, () => {
|
|
|
|
|
console.log('switchTab - after setData, activeTab:', this.data.activeTab);
|
|
|
|
|
this.loadOrders(false, tab);
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
|
|
|
|
|
// 计算统计信息
|
|
|
|
|
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) {
|
|
|
|
|
this.loadOrders(true, this.data.activeTab);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|