// pages/refund/refund-detail/index.js import Toast from 'tdesign-miniprogram/toast/index'; import { config } from '../../../config/index'; Page({ data: { refundDetail: null, loading: true, error: null }, onLoad(options) { console.log('[退款详情] 页面加载', options); this.refundId = options.id; if (!this.refundId) { this.setData({ error: '退款记录ID不能为空', loading: false }); return; } this.loadRefundDetail(); }, onShow() { // 页面显示时刷新数据 if (this.refundId) { this.loadRefundDetail(); } }, onPullDownRefresh() { this.loadRefundDetail(); }, // 加载退款详情 loadRefundDetail() { this.setData({ loading: true, error: null }); let token = wx.getStorageSync('token'); if (!token) { // 自动设置测试token const testToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyX2lkIjozNywiZXhwIjoxNzYxNjE5NjAyLCJpYXQiOjE3NjE1MzMyMDJ9.xyZLQbwhYyUDiF9_UOCX39nVwYOHvvd6d4TqwFnT_yg'; wx.setStorageSync('token', testToken); token = testToken; console.log('[退款详情] 自动设置测试token'); } console.log('[退款详情] 请求退款详情', { refundId: this.refundId }); wx.request({ url: `${config.apiBase}/refunds/${this.refundId}`, 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 refundDetail = this.processRefundDetail(res.data.data); this.setData({ refundDetail: refundDetail, loading: false }); wx.stopPullDownRefresh(); } else { console.error('[退款详情] API错误', res.data); this.setData({ error: res.data.message || '获取退款详情失败', loading: false }); } }, fail: (error) => { console.error('[退款详情] 网络错误', error); this.setData({ error: '网络错误', loading: false }); } }); }, // 处理退款详情数据 processRefundDetail(data) { // 状态映射 - 后端返回的是数字状态 const statusMap = { 1: { text: '待审核', desc: '您的退款申请已提交,请耐心等待审核', icon: '⏳' }, 2: { text: '审核通过', desc: '退款申请已通过,正在处理退款', icon: '✓' }, 3: { text: '审核拒绝', desc: '退款申请被拒绝,如有疑问请联系客服', icon: '✗' }, 4: { text: '退款中', desc: '退款正在处理中,请耐心等待', icon: '⏳' }, 5: { text: '退款成功', desc: '退款已完成,请查看您的账户余额', icon: '✓' }, 6: { text: '退款失败', desc: '退款处理失败,如有疑问请联系客服', icon: '✗' } }; const statusInfo = statusMap[data.status] || { text: '未知状态', desc: '', icon: '?' }; // 格式化金额 - 添加安全检查,将分转换为元 const refundAmount = ((data.refund_amount || 0) / 100).toFixed(2); // 订单金额从关联的订单对象中获取,将分转换为元 const orderAmount = ((data.order && data.order.total_amount ? data.order.total_amount : 0) / 100).toFixed(2); // 格式化时间 const createdAt = this.formatTime(data.created_at); const processedAt = data.processed_at ? this.formatTime(data.processed_at) : null; const completedAt = data.completed_at ? this.formatTime(data.completed_at) : null; // 退款方式 const refundMethodMap = { 'wechat': '微信支付', 'alipay': '支付宝', 'bank': '银行卡', 'balance': '账户余额' }; // 订单状态 const orderStatusMap = { 1: '待付款', 2: '待发货', 3: '待发货', 4: '已发货', 5: '待收货', 6: '已完成', 7: '已取消', 8: '退货中', 9: '已退款' }; // 处理退款日志 const logs = (data.logs || []).map(log => ({ id: log.id, action: log.action, remark: log.remark, createdAt: this.formatTime(log.created_at) })).reverse(); // 倒序显示,最新的在前面 // 生成操作按钮 const actions = this.generateActions(data.status); return { id: data.id, orderNo: data.order_no, refundAmount: refundAmount, orderAmount: orderAmount, reason: data.reason, adminRemark: data.admin_remark, status: data.status, statusText: statusInfo.text, statusDesc: statusInfo.desc, statusIcon: statusInfo.icon, createdAt: createdAt, processedAt: processedAt, completedAt: completedAt, refundMethod: refundMethodMap[data.refund_method] || '未知方式', orderStatusText: orderStatusMap[data.order_status] || '未知状态', logs: logs, actions: actions }; }, // 生成操作按钮 generateActions(status) { const actions = []; switch (status) { case 'pending': actions.push({ type: 'cancel', text: '取消申请' }); break; case 'rejected': actions.push({ type: 'reapply', 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'); const second = String(date.getSeconds()).padStart(2, '0'); return `${year}-${month}-${day} ${hour}:${minute}:${second}`; }, // 订单点击 onOrderTap() { const { refundDetail } = this.data; if (refundDetail && refundDetail.orderNo) { wx.navigateTo({ url: `/pages/order/order-detail/index?orderNo=${refundDetail.orderNo}` }); } }, // 操作按钮点击 onActionTap(e) { const action = e.currentTarget.dataset.action; console.log('[退款详情] 操作按钮点击', action); switch (action) { case 'cancel': this.cancelRefund(); break; case 'reapply': this.reapplyRefund(); break; } }, // 取消退款申请 cancelRefund() { wx.showModal({ title: '取消申请', content: '确定要取消这个退款申请吗?', success: (res) => { if (res.confirm) { this.performCancelRefund(); } } }); }, // 执行取消退款 performCancelRefund() { const token = wx.getStorageSync('token'); wx.showLoading({ title: '处理中...' }); wx.request({ url: `${config.apiBase}/refunds/${this.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', }); // 刷新详情 setTimeout(() => { this.loadRefundDetail(); }, 1000); } 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() { const { refundDetail } = this.data; if (refundDetail) { wx.navigateTo({ url: `/pages/refund/refund-apply/index?reapply=1&orderNo=${refundDetail.orderNo}&refundId=${this.refundId}` }); } }, // 重试 onRetry() { this.loadRefundDetail(); } });