328 lines
8.4 KiB
JavaScript
328 lines
8.4 KiB
JavaScript
// pages/refund/refund-list/index.js
|
||
import Toast from 'tdesign-miniprogram/toast/index';
|
||
import { config } from '../../../config/index';
|
||
|
||
Page({
|
||
data: {
|
||
refundList: [],
|
||
currentStatus: '', // 当前筛选状态
|
||
loading: false,
|
||
page: 1,
|
||
pageSize: 10,
|
||
hasMore: true
|
||
},
|
||
|
||
onLoad(options) {
|
||
console.log('[退款记录] 页面加载', options);
|
||
this.loadRefundList();
|
||
},
|
||
|
||
onShow() {
|
||
// 页面显示时刷新数据
|
||
this.refreshData();
|
||
},
|
||
|
||
onPullDownRefresh() {
|
||
this.refreshData();
|
||
},
|
||
|
||
onReachBottom() {
|
||
if (this.data.hasMore && !this.data.loading) {
|
||
this.loadMoreData();
|
||
}
|
||
},
|
||
|
||
// 刷新数据
|
||
refreshData() {
|
||
this.setData({
|
||
page: 1,
|
||
hasMore: true,
|
||
refundList: []
|
||
});
|
||
this.loadRefundList();
|
||
},
|
||
|
||
// 加载更多数据
|
||
loadMoreData() {
|
||
this.setData({
|
||
page: this.data.page + 1
|
||
});
|
||
this.loadRefundList(false);
|
||
},
|
||
|
||
// 加载退款记录列表
|
||
loadRefundList(showLoading = true) {
|
||
if (showLoading) {
|
||
this.setData({ loading: true });
|
||
}
|
||
|
||
let token = wx.getStorageSync('token');
|
||
if (!token) {
|
||
// 自动设置测试token
|
||
const testToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjozNywiZXhwIjoxNzYxNjE5NjAyLCJpYXQiOjE3NjE1MzMyMDJ9.xyZLQbwhYyUDiF9_UOCX39nVwYOHvvd6d4TqwFnT_yg';
|
||
wx.setStorageSync('token', testToken);
|
||
token = testToken;
|
||
console.log('[退款记录] 自动设置测试token');
|
||
}
|
||
|
||
const params = {
|
||
page: this.data.page,
|
||
page_size: this.data.pageSize
|
||
};
|
||
|
||
if (this.data.currentStatus) {
|
||
params.status = this.data.currentStatus;
|
||
}
|
||
|
||
const queryString = Object.keys(params)
|
||
.map(key => `${key}=${encodeURIComponent(params[key])}`)
|
||
.join('&');
|
||
|
||
console.log('[退款记录] 请求参数', params);
|
||
|
||
wx.request({
|
||
url: `${config.apiBase}/refunds/user?${queryString}`,
|
||
method: 'GET',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`,
|
||
'Content-Type': 'application/json'
|
||
},
|
||
success: (res) => {
|
||
console.log('[退款记录] API响应', res);
|
||
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
const newRefunds = res.data.data.list || [];
|
||
console.log('API返回的原始数据:', newRefunds);
|
||
const processedRefunds = newRefunds.map(item => this.processRefundItem(item));
|
||
console.log('处理后的数据:', processedRefunds);
|
||
|
||
this.setData({
|
||
refundList: this.data.page === 1 ? processedRefunds : [...this.data.refundList, ...processedRefunds],
|
||
hasMore: newRefunds.length === this.data.pageSize,
|
||
loading: false
|
||
});
|
||
|
||
if (showLoading) {
|
||
wx.stopPullDownRefresh();
|
||
}
|
||
} else {
|
||
console.error('[退款记录] API错误', res.data);
|
||
Toast({
|
||
context: this,
|
||
selector: '#t-toast',
|
||
message: res.data.message || '获取退款记录失败',
|
||
theme: 'error',
|
||
});
|
||
this.setData({ loading: false });
|
||
}
|
||
},
|
||
fail: (error) => {
|
||
console.error('[退款记录] 网络错误', error);
|
||
Toast({
|
||
context: this,
|
||
selector: '#t-toast',
|
||
message: '网络错误',
|
||
theme: 'error',
|
||
});
|
||
this.setData({ loading: false });
|
||
}
|
||
});
|
||
},
|
||
|
||
// 处理退款记录数据
|
||
processRefundItem(item) {
|
||
// 状态文本映射 - 后端返回的是数字状态
|
||
const statusMap = {
|
||
1: '待审核',
|
||
2: '审核通过',
|
||
3: '审核拒绝',
|
||
4: '退款中',
|
||
5: '退款成功',
|
||
6: '退款失败'
|
||
};
|
||
|
||
// 状态字符串映射(用于CSS类名)
|
||
const statusClassMap = {
|
||
1: 'pending',
|
||
2: 'approved',
|
||
3: 'rejected',
|
||
4: 'processing',
|
||
5: 'completed',
|
||
6: 'failed'
|
||
};
|
||
|
||
// 格式化金额 - 添加安全检查,将分转换为元
|
||
const refundAmount = ((item.refund_amount || 0) / 100).toFixed(2);
|
||
|
||
// 格式化时间
|
||
const createdAt = this.formatTime(item.created_at);
|
||
|
||
// 生成操作按钮
|
||
const actions = this.generateActions(statusClassMap[item.status]);
|
||
|
||
return {
|
||
id: item.id,
|
||
orderNo: item.order ? item.order.order_no : '',
|
||
refundAmount: refundAmount,
|
||
reason: item.refund_reason,
|
||
status: statusClassMap[item.status] || 'pending',
|
||
statusText: statusMap[item.status] || '未知状态',
|
||
createdAt: createdAt,
|
||
actions: actions
|
||
};
|
||
},
|
||
|
||
// 生成操作按钮
|
||
generateActions(status) {
|
||
const actions = [];
|
||
|
||
switch (status) {
|
||
case 'pending':
|
||
actions.push({ type: 'cancel', text: '取消申请' });
|
||
break;
|
||
case 'approved':
|
||
actions.push({ type: 'view', text: '查看详情' });
|
||
break;
|
||
case 'rejected':
|
||
actions.push({ type: 'view', text: '查看详情' });
|
||
actions.push({ type: 'reapply', text: '重新申请' });
|
||
break;
|
||
case 'completed':
|
||
actions.push({ type: 'view', text: '查看详情' });
|
||
break;
|
||
}
|
||
|
||
return actions;
|
||
},
|
||
|
||
// 格式化时间
|
||
formatTime(timeStr) {
|
||
if (!timeStr) return '';
|
||
|
||
const date = new Date(timeStr);
|
||
const year = date.getFullYear();
|
||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||
const day = String(date.getDate()).padStart(2, '0');
|
||
const hour = String(date.getHours()).padStart(2, '0');
|
||
const minute = String(date.getMinutes()).padStart(2, '0');
|
||
|
||
return `${year}-${month}-${day} ${hour}:${minute}`;
|
||
},
|
||
|
||
// 状态筛选
|
||
onStatusFilter(e) {
|
||
const status = e.currentTarget.dataset.status;
|
||
console.log('[退款记录] 状态筛选', status);
|
||
|
||
this.setData({
|
||
currentStatus: status,
|
||
page: 1,
|
||
hasMore: true,
|
||
refundList: []
|
||
});
|
||
|
||
this.loadRefundList();
|
||
},
|
||
|
||
// 退款记录项点击
|
||
onRefundItemTap(e) {
|
||
const refund = e.currentTarget.dataset.refund;
|
||
console.log('[退款记录] 点击退款记录', refund);
|
||
|
||
// 跳转到退款详情页面
|
||
wx.navigateTo({
|
||
url: `/pages/refund/refund-detail/index?id=${refund.id}`
|
||
});
|
||
},
|
||
|
||
// 操作按钮点击
|
||
onActionTap(e) {
|
||
e.stopPropagation(); // 阻止事件冒泡
|
||
|
||
const { refundId, action } = e.currentTarget.dataset;
|
||
console.log('[退款记录] 操作按钮点击', { refundId, action });
|
||
|
||
switch (action) {
|
||
case 'cancel':
|
||
this.cancelRefund(refundId);
|
||
break;
|
||
case 'view':
|
||
wx.navigateTo({
|
||
url: `/pages/refund/refund-detail/index?id=${refundId}`
|
||
});
|
||
break;
|
||
case 'reapply':
|
||
this.reapplyRefund(refundId);
|
||
break;
|
||
}
|
||
},
|
||
|
||
// 取消退款申请
|
||
cancelRefund(refundId) {
|
||
wx.showModal({
|
||
title: '取消申请',
|
||
content: '确定要取消这个退款申请吗?',
|
||
success: (res) => {
|
||
if (res.confirm) {
|
||
this.performCancelRefund(refundId);
|
||
}
|
||
}
|
||
});
|
||
},
|
||
|
||
// 执行取消退款
|
||
performCancelRefund(refundId) {
|
||
const token = wx.getStorageSync('token');
|
||
|
||
wx.showLoading({ title: '处理中...' });
|
||
|
||
wx.request({
|
||
url: `${config.apiBase}/refunds/${refundId}/cancel`,
|
||
method: 'PUT',
|
||
header: {
|
||
'Authorization': `Bearer ${token}`,
|
||
'Content-Type': 'application/json'
|
||
},
|
||
success: (res) => {
|
||
wx.hideLoading();
|
||
|
||
if (res.statusCode === 200 && res.data.code === 200) {
|
||
Toast({
|
||
context: this,
|
||
selector: '#t-toast',
|
||
message: '取消成功',
|
||
theme: 'success',
|
||
});
|
||
|
||
// 刷新列表
|
||
this.refreshData();
|
||
} else {
|
||
Toast({
|
||
context: this,
|
||
selector: '#t-toast',
|
||
message: res.data.message || '取消失败',
|
||
theme: 'error',
|
||
});
|
||
}
|
||
},
|
||
fail: (error) => {
|
||
wx.hideLoading();
|
||
console.error('[退款记录] 取消退款失败', error);
|
||
Toast({
|
||
context: this,
|
||
selector: '#t-toast',
|
||
message: '网络错误',
|
||
theme: 'error',
|
||
});
|
||
}
|
||
});
|
||
},
|
||
|
||
// 重新申请退款
|
||
reapplyRefund(refundId) {
|
||
// 获取原退款记录信息,跳转到退款申请页面
|
||
wx.navigateTo({
|
||
url: `/pages/refund/refund-apply/index?reapply=1&refundId=${refundId}`
|
||
});
|
||
}
|
||
}); |