import { getAfterSaleDetailMethod, cancelAfterSaleMethod, applyAfterSaleMethod, updateAfterSaleMethod } from "../../request"; // progress 数字转页面状态 const progressToState: Record = { '0': 'processing', '1': 'revoked', '2': 'rejected', '3': 'approved', '4': 'completed', }; Page({ data: { id: '', recordId: '', aftersaleId: '', pageTitle: '商家处理', refundState: '', days: '00', hours: '00', minutes: '00', seconds: '00', goods: { name: "", meta1: "", price: "", image: "", }, refundDetail: { reason: "", amount: "", finishTime: "", applyTime: "", refundNo: "", updateTime:"" }, // 协商历史标题和内容 negotiateTitle: "", negotiateContent: "", // 拒绝状态处理截止时间 handleEndTime: "", refundDestination: { type: '退回微信', account: '', }, // 弹窗状态 refundReasonPopupVisible: false, refundConfirmPopupVisible: false, refundProofPopupVisible: false, refundStatus: '', refundAction: '', // 'modify' 修改申请 | 'reApply' 再次申请 refundMaxAmount: '0', refundProofImages: [] as string[], refundDesc: '', // 接口提交所需原始字段 rawDetail: {} as any, }, timer: null as any, onLoad(options: any) { if (options.id) { this.setData({ id: options.id, recordId: options.recordId || '' }); this.loadDetail(options.id); } }, onShow() { if (this.data.id && this.data.refundState) { this.loadDetail(this.data.id); } }, async loadDetail(id: string) { wx.showLoading({ title: '加载中' }); try { const res = await getAfterSaleDetailMethod(id); const detail = (res as any)?.data; if (!detail) return; const refundState = progressToState[String(detail.progress)] || 'processing'; this.setData({ rawDetail: detail, aftersaleId: detail.id || '', refundState, goods: { name: detail.conditioningProgramName || '', meta1: detail.convertDose ? `${detail.convertDose}${detail.convertUnit || '次'}` : '', price: String(detail.applyAmount || 0), image: detail.conditioningProgramPhoto || '', }, refundDetail: { reason: detail.reason || '', amount: String(detail.applyAmount || 0), finishTime: detail.finishTime || '', applyTime: detail.applyTime || '', refundNo: detail.ref || '', updateTime:detail.updateTime || '' }, negotiateTitle: detail.title || '', negotiateContent: (() => { const c = detail.content || ''; try { if (c && JSON.parse(c)) return ''; } catch (e) { /* 不是JSON,保留原文 */ } return c; })(), handleEndTime: detail.handleEndTime || '', refundMaxAmount: String(detail.applyAmount || 0), refundProofImages: detail.voucherImgs || [], refundDesc: detail.remark || '', }); this.updatePageTitle(); if (refundState === 'processing' || refundState === 'rejected') { this.startCountdown(); } } catch (error: any) { wx.showToast({ title: error.errMsg || '获取详情失败', icon: 'none' }); } wx.hideLoading(); }, updatePageTitle() { let title = '商家处理'; if (this.data.refundState === 'revoked') { title = '撤销申请'; } else if (this.data.refundState === 'completed') { title = '退款完成'; } this.setData({ pageTitle: title }); }, onUnload() { if (this.timer) { clearInterval(this.timer); } }, startCountdown() { if (this.timer) clearInterval(this.timer); const setTime = (diff: number) => { if (diff <= 0) { this.setData({ days: '00', hours: '00', minutes: '00', seconds: '00' }); if (this.timer) { clearInterval(this.timer); this.timer = null; } // 拒绝状态倒计时结束,清除截止时间,触发按钮切换 if (this.data.refundState === 'rejected' && this.data.handleEndTime) { this.setData({ handleEndTime: '' }); } return; } const d = Math.floor(diff / (1000 * 60 * 60 * 24)); const h = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); const m = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60)); const s = Math.floor((diff % (1000 * 60)) / 1000); this.setData({ days: d < 10 ? '0' + d : String(d), hours: h < 10 ? '0' + h : String(h), minutes: m < 10 ? '0' + m : String(m), seconds: s < 10 ? '0' + s : String(s), }); }; if (this.data.refundState === 'rejected' && this.data.handleEndTime) { // rejected:倒计时到处理截止时间 const target = new Date(this.data.handleEndTime.replace(/-/g, '/')).getTime(); const update = () => setTime(target - new Date().getTime()); update(); this.timer = setInterval(update, 1000); } else { // processing:已过去的时间 const applyTime = new Date(this.data.refundDetail.applyTime.replace(/-/g, '/')).getTime(); const update = () => setTime(new Date().getTime() - applyTime); update(); this.timer = setInterval(update, 1000); } }, // 修改申请:打开确认弹窗 onModify() { this.setData({ refundAction: 'modify', refundConfirmPopupVisible: true }); }, onRefundConfirmClose() { this.setData({ refundConfirmPopupVisible: false }); }, onChangeRefundStatus(e: any) { this.setData({ refundStatus: e.detail.status }); }, onOpenRefundReasonAgain() { this.setData({ refundConfirmPopupVisible: false, refundReasonPopupVisible: true }); }, onOpenRefundProofEditor() { this.setData({ refundConfirmPopupVisible: false, refundProofPopupVisible: true }); }, onRefundAmountConfirm(e: any) { this.setData({ ['refundDetail.amount']: e.detail.amount }); }, async onSubmitRefundApply() { if (!this.data.refundProofImages.length) { wx.showToast({ title: '请上传凭证图片', icon: 'none' }); return; } wx.showLoading({ title: '正在提交' }); const { rawDetail, refundStatus, refundDetail, refundProofImages, refundDesc, recordId, id, aftersaleId, refundAction } = this.data; const params = { patientConditioningRecordId: recordId, patientConditioningProgramId: id, type: rawDetail.type, ...(rawDetail.sellType === '1' ? { receiptStatus: refundStatus || rawDetail.receiptStatus } : {}), reason: refundDetail.reason, applyAmount: Number(refundDetail.amount) || 0, voucherImgs: refundProofImages, remark: refundDesc, }; try { if (refundAction === 'modify') { await updateAfterSaleMethod(Number(aftersaleId), params); } else { await applyAfterSaleMethod(params); } this.setData({ refundConfirmPopupVisible: false }); wx.navigateTo({ url: '/module/order/pages/refund-success/refund-success' }); } catch (error: any) { wx.showToast({ title: error.errMsg || '提交失败', icon: 'none' }); } wx.hideLoading(); }, onRefundReasonPopupClose() { this.setData({ refundReasonPopupVisible: false }); }, onRefundReasonNext(e: any) { this.setData({ ['refundDetail.reason']: e.detail.reason, refundReasonPopupVisible: false, refundConfirmPopupVisible: true }); }, onRefundProofPopupClose() { this.setData({ refundProofPopupVisible: false, refundConfirmPopupVisible: true }); }, onRefundProofSubmit(e: any) { this.setData({ refundProofImages: e.detail.images, refundDesc: e.detail.desc || '', refundProofPopupVisible: false, refundConfirmPopupVisible: true }); }, onViewHistory() { wx.navigateTo({ url: `/module/order/pages/negotiation-history/negotiation-history?id=${this.data.aftersaleId}` }); }, onCopyRefundNo() { wx.setClipboardData({ data: this.data.refundDetail.refundNo, success: () => { wx.showToast({ title: '已复制', icon: 'none' }); } }); }, onCallService() { wx.makePhoneCall({ phoneNumber: '400-123-4567', fail: () => { } }); }, onReApply() { this.setData({ refundAction: 'reApply', refundConfirmPopupVisible: true }); }, onRevoke() { wx.showModal({ title: '撤销退款申请', content: '撤销退款申请后,可以再次发起退款申请(总共可发起2次)', confirmText: '确定撤销', cancelText: '暂不撤销', success: async (res) => { if (res.confirm) { try { await cancelAfterSaleMethod(Number(this.data.aftersaleId)); wx.showToast({ title: '已撤销', icon: 'success' }); setTimeout(() => { wx.redirectTo({ url: '/module/article/pages/success-page/success-page?title=撤销退款申请成功' }); }, 1500); } catch (error: any) { wx.showToast({ title: error.errMsg || '撤销失败', icon: 'none' }); } } } }); } });