refund-processing.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import { getAfterSaleDetailMethod, cancelAfterSaleMethod, applyAfterSaleMethod, updateAfterSaleMethod } from "../../request";
  2. import { getFullImageUrl } from "../../../../utils/util";
  3. // progress 数字转页面状态
  4. const progressToState: Record<string, string> = {
  5. '0': 'processing',
  6. '1': 'revoked',
  7. '2': 'rejected',
  8. '3': 'approved',
  9. '4': 'completed',
  10. };
  11. Page({
  12. data: {
  13. id: '',
  14. recordId: '',
  15. aftersaleId: '',
  16. pageTitle: '商家处理',
  17. refundState: '',
  18. days: '00',
  19. hours: '00',
  20. minutes: '00',
  21. seconds: '00',
  22. goods: {
  23. name: "",
  24. meta1: "",
  25. price: "",
  26. image: "",
  27. },
  28. refundDetail: {
  29. reason: "",
  30. amount: "",
  31. refundAmount: "",
  32. finishTime: "",
  33. applyTime: "",
  34. refundNo: "",
  35. updateTime: ""
  36. },
  37. // 协商历史标题和内容
  38. negotiateTitle: "",
  39. negotiateContent: "",
  40. // 拒绝状态处理截止时间
  41. handleEndTime: "",
  42. refundDestination: {
  43. type: '退回微信',
  44. account: '',
  45. },
  46. // 弹窗状态
  47. refundReasonPopupVisible: false,
  48. refundConfirmPopupVisible: false,
  49. refundProofPopupVisible: false,
  50. refundStatus: '',
  51. refundAction: '', // 'modify' 修改申请 | 'reApply' 再次申请
  52. refundMaxAmount: '0',
  53. refundProofImages: [] as string[],
  54. refundDesc: '',
  55. // 接口提交所需原始字段
  56. rawDetail: {} as any,
  57. },
  58. timer: null as any,
  59. onLoad(options: any) {
  60. if (options.id) {
  61. this.setData({ id: options.id, recordId: options.recordId || '' });
  62. this.loadDetail(options.id);
  63. }
  64. },
  65. onShow() {
  66. if (this.data.id && this.data.refundState) {
  67. this.loadDetail(this.data.id);
  68. }
  69. },
  70. async loadDetail(id: string) {
  71. wx.showLoading({ title: '加载中' });
  72. try {
  73. const res = await getAfterSaleDetailMethod(id);
  74. const detail = (res as any)?.data;
  75. if (!detail) return;
  76. const refundState = progressToState[String(detail.progress)] || 'processing';
  77. console.log(detail, "888")
  78. this.setData({
  79. rawDetail: detail,
  80. aftersaleId: detail.id || '',
  81. refundState,
  82. goods: {
  83. name: detail.conditioningProgramName || '',
  84. meta1: detail.convertDose ? `${detail.convertDose}${detail.convertUnit || '次'}` : '',
  85. price: String(detail.unitPrice || 0),
  86. image: getFullImageUrl(detail.conditioningProgramPhoto) || '',
  87. },
  88. refundDetail: {
  89. reason: detail.reason || '',
  90. amount: String(detail.totalPrice || 0),
  91. refundAmount: String(detail.refundAmount || 0),
  92. finishTime: detail.finishTime || '',
  93. applyTime: detail.applyTime || '',
  94. refundNo: detail.ref || '',
  95. updateTime: detail.updateTime || ''
  96. },
  97. negotiateTitle: detail.title || '',
  98. negotiateContent: (() => {
  99. const c = detail.content || '';
  100. try { if (c && JSON.parse(c)) return ''; } catch (e) { /* 不是JSON,保留原文 */ }
  101. return c;
  102. })(),
  103. handleEndTime: detail.handleEndTime || '',
  104. refundMaxAmount: String(detail.totalPrice || 0),
  105. refundProofImages: detail.voucherImgs || [],
  106. refundDesc: detail.remark || '',
  107. });
  108. this.updatePageTitle();
  109. if (refundState === 'processing' || refundState === 'rejected') {
  110. this.startCountdown();
  111. }
  112. } catch (error: any) {
  113. wx.showToast({ title: error.errMsg || '获取详情失败', icon: 'none' });
  114. }
  115. wx.hideLoading();
  116. },
  117. updatePageTitle() {
  118. let title = '商家处理';
  119. if (this.data.refundState === 'revoked') {
  120. title = '撤销申请';
  121. } else if (this.data.refundState === 'completed') {
  122. title = '退款完成';
  123. }
  124. this.setData({ pageTitle: title });
  125. },
  126. onUnload() {
  127. if (this.timer) {
  128. clearInterval(this.timer);
  129. }
  130. },
  131. startCountdown() {
  132. if (this.timer) clearInterval(this.timer);
  133. const setTime = (diff: number) => {
  134. if (diff <= 0) {
  135. this.setData({ days: '00', hours: '00', minutes: '00', seconds: '00' });
  136. if (this.timer) {
  137. clearInterval(this.timer);
  138. this.timer = null;
  139. }
  140. // 拒绝状态倒计时结束,清除截止时间,触发按钮切换
  141. if (this.data.refundState === 'rejected' && this.data.handleEndTime) {
  142. this.setData({ handleEndTime: '' });
  143. }
  144. return;
  145. }
  146. const d = Math.floor(diff / (1000 * 60 * 60 * 24));
  147. const h = Math.floor((diff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  148. const m = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
  149. const s = Math.floor((diff % (1000 * 60)) / 1000);
  150. this.setData({
  151. days: d < 10 ? '0' + d : String(d),
  152. hours: h < 10 ? '0' + h : String(h),
  153. minutes: m < 10 ? '0' + m : String(m),
  154. seconds: s < 10 ? '0' + s : String(s),
  155. });
  156. };
  157. if (this.data.refundState === 'rejected' && this.data.handleEndTime) {
  158. // rejected:倒计时到处理截止时间
  159. const target = new Date(this.data.handleEndTime.replace(/-/g, '/')).getTime();
  160. const update = () => setTime(target - new Date().getTime());
  161. update();
  162. this.timer = setInterval(update, 1000);
  163. } else {
  164. // processing:已过去的时间
  165. const applyTime = new Date(this.data.refundDetail.applyTime.replace(/-/g, '/')).getTime();
  166. const update = () => setTime(new Date().getTime() - applyTime);
  167. update();
  168. this.timer = setInterval(update, 1000);
  169. }
  170. },
  171. // 修改申请:打开确认弹窗
  172. onModify() {
  173. this.setData({ refundAction: 'modify', refundConfirmPopupVisible: true });
  174. },
  175. onRefundConfirmClose() {
  176. this.setData({ refundConfirmPopupVisible: false });
  177. },
  178. onChangeRefundStatus(e: any) {
  179. this.setData({ refundStatus: e.detail.status });
  180. },
  181. onOpenRefundReasonAgain() {
  182. this.setData({
  183. refundConfirmPopupVisible: false,
  184. refundReasonPopupVisible: true
  185. });
  186. },
  187. onOpenRefundProofEditor() {
  188. this.setData({
  189. refundConfirmPopupVisible: false,
  190. refundProofPopupVisible: true
  191. });
  192. },
  193. onRefundAmountConfirm(e: any) {
  194. this.setData({
  195. ['refundDetail.amount']: e.detail.amount
  196. });
  197. },
  198. async onSubmitRefundApply() {
  199. if (!this.data.refundProofImages.length && !this.data.refundDesc.trim()) {
  200. wx.showToast({ title: '请上传凭证或填写描述', icon: 'none' });
  201. return;
  202. }
  203. wx.showLoading({ title: '正在提交' });
  204. const { rawDetail, refundStatus, refundDetail, refundProofImages, refundDesc, recordId, id, aftersaleId, refundAction } = this.data;
  205. const params = {
  206. patientConditioningRecordId: recordId,
  207. patientConditioningProgramId: id,
  208. type: rawDetail.type,
  209. ...(rawDetail.sellType === '1' ? { receiptStatus: refundStatus || rawDetail.receiptStatus } : {}),
  210. reason: refundDetail.reason,
  211. applyAmount: Number(refundDetail.amount) || 0,
  212. voucherImgs: refundProofImages,
  213. remark: refundDesc,
  214. };
  215. try {
  216. if (refundAction === 'modify') {
  217. await updateAfterSaleMethod(Number(aftersaleId), params);
  218. } else {
  219. await applyAfterSaleMethod(params);
  220. }
  221. this.setData({ refundConfirmPopupVisible: false });
  222. wx.navigateTo({
  223. url: '/module/order/pages/refund-success/refund-success'
  224. });
  225. } catch (error: any) {
  226. wx.showToast({ title: error.errMsg || '提交失败', icon: 'none' });
  227. }
  228. wx.hideLoading();
  229. },
  230. onRefundReasonPopupClose() {
  231. this.setData({ refundReasonPopupVisible: false });
  232. },
  233. onRefundReasonNext(e: any) {
  234. this.setData({
  235. ['refundDetail.reason']: e.detail.reason,
  236. refundReasonPopupVisible: false,
  237. refundConfirmPopupVisible: true
  238. });
  239. },
  240. onRefundProofPopupClose() {
  241. this.setData({ refundProofPopupVisible: false, refundConfirmPopupVisible: true });
  242. },
  243. onRefundProofSubmit(e: any) {
  244. this.setData({
  245. refundProofImages: e.detail.images,
  246. refundDesc: e.detail.desc || '',
  247. refundProofPopupVisible: false,
  248. refundConfirmPopupVisible: true
  249. });
  250. },
  251. onViewHistory() {
  252. wx.navigateTo({
  253. url: `/module/order/pages/negotiation-history/negotiation-history?id=${this.data.aftersaleId}`
  254. });
  255. },
  256. onCopyRefundNo() {
  257. wx.setClipboardData({
  258. data: this.data.refundDetail.refundNo,
  259. success: () => {
  260. wx.showToast({ title: '已复制', icon: 'none' });
  261. }
  262. });
  263. },
  264. onCallService() {
  265. wx.makePhoneCall({
  266. phoneNumber: '400-123-4567',
  267. fail: () => { }
  268. });
  269. },
  270. onReApply() {
  271. const originalPrice = String(this.data.rawDetail.totalPrice || 0);
  272. this.setData({
  273. refundAction: 'reApply',
  274. refundDetail: { ...this.data.refundDetail, amount: originalPrice },
  275. refundMaxAmount: originalPrice,
  276. refundConfirmPopupVisible: true,
  277. });
  278. },
  279. onRevoke() {
  280. wx.showModal({
  281. title: '撤销退款申请',
  282. content: '撤销退款申请后,可以再次发起退款申请(总共可发起2次)',
  283. confirmText: '确定撤销',
  284. cancelText: '暂不撤销',
  285. success: async (res) => {
  286. if (res.confirm) {
  287. try {
  288. await cancelAfterSaleMethod(Number(this.data.aftersaleId));
  289. wx.showToast({ title: '已撤销', icon: 'success' });
  290. setTimeout(() => {
  291. wx.redirectTo({
  292. url: '/module/article/pages/success-page/success-page?title=撤销退款申请成功'
  293. });
  294. }, 1500);
  295. } catch (error: any) {
  296. wx.showToast({ title: error.errMsg || '撤销失败', icon: 'none' });
  297. }
  298. }
  299. }
  300. });
  301. }
  302. });