You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
229 lines
5.2 KiB
229 lines
5.2 KiB
// pages/order/index.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
|
|
},
|
|
|
|
onLoad() {
|
|
// 页面加载时的初始化逻辑
|
|
this.loadUserInfo(() => {
|
|
this.loadOrders();
|
|
});
|
|
},
|
|
|
|
onShow() {
|
|
// 页面显示时的逻辑
|
|
this.loadUserInfo(() => {
|
|
this.loadOrders();
|
|
// 更新自定义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);
|
|
|
|
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 totalOrders = isLoadMore ? [...this.data.orders, ...newOrders] : newOrders;
|
|
const hasMore = newOrders.length === pageSize;
|
|
|
|
this.setData({
|
|
orders: totalOrders,
|
|
page: page,
|
|
hasMore: hasMore,
|
|
loading: false,
|
|
loadingMore: false
|
|
});
|
|
} else {
|
|
this.setData({
|
|
error: res.data.message || '获取订单失败',
|
|
loading: false,
|
|
loadingMore: false
|
|
});
|
|
}
|
|
},
|
|
fail: (err) => {
|
|
console.error('获取订单列表失败:', err);
|
|
this.setData({
|
|
error: '网络请求失败,请稍后重试',
|
|
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);
|
|
});
|
|
},
|
|
|
|
// 滚动到底部的预加载逻辑
|
|
onReachBottom() {
|
|
if (!this.data.loadingMore && this.data.hasMore) {
|
|
this.loadOrders(true, this.data.activeTab);
|
|
}
|
|
}
|
|
})
|
|
|