import dayjs from "dayjs"; import { message } from "@/utils/message"; import { addDialog } from "@/components/ReDialog"; import type { PaginationProps } from "@pureadmin/table"; import { ElForm, ElFormItem, ElInput, ElDescriptions, ElDescriptionsItem, ElImage, ElTable, ElTableColumn } from "element-plus"; import { getRefundApplicationList, getRefundApplicationDetail, reviewRefundApplication } from "@/api/order"; import type { RefundApplicationItem, RefundApplicationSearchForm } from "../../utils/types"; import { type Ref, ref, reactive, onMounted } from "vue"; import { initPagination, handlePageSizeChange, handleCurrentPageChange, resetPagination } from "@/utils/paginationHelper"; export function useRefund(tableRef: Ref) { const form = reactive({ orderNo: "", status: "", startDate: "", endDate: "" }); const formRef = ref(); const dataList = ref([]); const loading = ref(true); const pagination = reactive(initPagination()); const columns: TableColumnList = [ { label: "申请编号", prop: "applicationNo", minWidth: 160 }, { label: "订单编号", prop: "orderNo", minWidth: 160 }, { label: "用户ID", prop: "userId", minWidth: 80 }, { label: "退款金额", prop: "refundAmount", minWidth: 100, cellRenderer: ({ row }: any) => ( ¥{row.refundAmount || "0.00"} ) }, { label: "申请原因", prop: "reason", minWidth: 180, formatter: ({ reason }: any) => reason || "-" }, { label: "退款状态", prop: "status", minWidth: 90, cellRenderer: ({ row }: any) => { const statusMap: Record = { 0: { text: "待审核", type: "warning" }, 1: { text: "已通过", type: "success" }, 2: { text: "已拒绝", type: "danger" } }; const s = statusMap[row.status] || { text: "未知", type: "info" }; return {s.text}; } }, { label: "申请时间", prop: "createTime", minWidth: 160, formatter: ({ createTime }: any) => createTime ? dayjs(createTime).format("YYYY-MM-DD HH:mm:ss") : "-" }, { label: "操作", fixed: "right", width: 200, slot: "operation" } ]; async function onSearch() { loading.value = true; try { const searchParams: any = { page: pagination.currentPage, pageSize: pagination.pageSize }; if (form.orderNo) searchParams.orderNo = form.orderNo; if (form.status !== "") searchParams.status = parseInt(form.status as string, 10); if (form.startDate) searchParams.startDate = form.startDate; if (form.endDate) searchParams.endDate = form.endDate; const { data } = await getRefundApplicationList(searchParams); dataList.value = data.list || []; pagination.total = Number(data.total) || 0; } catch (error) { console.error("获取退款申请列表失败:", error); dataList.value = []; pagination.total = 0; } finally { setTimeout(() => { loading.value = false; }, 300); } } function resetForm(formEl: any) { if (!formEl) return; formEl.resetFields(); resetPagination(pagination, onSearch); } function handleSizeChange(val: number) { handlePageSizeChange(val, pagination, onSearch); } function handleCurrentChange(val: number) { handleCurrentPageChange(val, pagination, onSearch); } async function handleDetail(row: RefundApplicationItem) { try { const { data } = await getRefundApplicationDetail(row.id); const app = data; const statusMap: Record = { 0: { text: "待审核", type: "warning" }, 1: { text: "已通过", type: "success" }, 2: { text: "已拒绝", type: "danger" } }; const s = statusMap[app.status] || { text: "未知", type: "info" }; addDialog({ title: "退款申请详情 - " + app.applicationNo, width: "600px", draggable: true, closeOnClickModal: false, contentRenderer: () => (
基本信息
{app.applicationNo} {app.orderNo} {app.userId} ¥{(app.refundAmount || 0).toFixed(2)} {s.text} {app.createTime ? dayjs(app.createTime).format("YYYY-MM-DD HH:mm:ss") : "-"} {app.reason || "-"} {app.reviewTime && ( {dayjs(app.reviewTime).format("YYYY-MM-DD HH:mm:ss")} )} {app.reviewRemark && ( {app.reviewRemark} )} {app.refundProducts && app.refundProducts.length > 0 && ( <>
退款商品
{{ default: ({ row: r }: any) => (
{r.pic ? ( {{ error: () => (
暂无
) }}
) : (
暂无
)} {r.productName}
) }}
¥{(r.price || 0).toFixed(2)} }} /> ×{r.quantity || 1} }} /> ( ¥{((r.price || 0) * (r.quantity || 1)).toFixed(2)} ) }} />
)}
), hideFooter: true }); } catch (error) { message("获取退款详情失败", { type: "error" }); } } async function handleApprove(row: RefundApplicationItem) { try { const res = await reviewRefundApplication(row.id, { approved: true }); if (res.code === 200) { message("退款申请已通过", { type: "success" }); onSearch(); } else { message(res.message || "操作失败", { type: "error" }); } } catch (error) { message("操作失败", { type: "error" }); } } function handleReject(row: RefundApplicationItem) { const remarkRef = ref(""); const formRef2 = ref(); addDialog({ title: "拒绝退款 - " + row.applicationNo, width: "35%", draggable: true, closeOnClickModal: false, contentRenderer: () => ( ), beforeSure: async (done) => { const valid = await formRef2.value?.validate().catch(() => false); if (!valid) return; try { const res = await reviewRefundApplication(row.id, { approved: false, remark: remarkRef.value }); if (res.code === 200) { message("退款申请已拒绝", { type: "success" }); done(); onSearch(); } else { message(res.message || "操作失败", { type: "error" }); } } catch (error) { message("操作失败", { type: "error" }); } } }); } onMounted(() => { onSearch(); }); return { form, loading, columns, dataList, pagination, onSearch, resetForm, handleDetail, handleApprove, handleReject, handleSizeChange, handleCurrentChange }; }