import { ref, h, nextTick } from "vue"; import editForm from "./form.vue"; import { message } from "@/utils/message"; import { queryGiftPack, addGiftPackData, editGiftPackData, deteleGiftPackData } from "@/api/modules/activities"; import { addDialog } from "@/components/ReDialog"; export function useData() { const formRef = ref(); const propRowData = ref(null) const loading = ref(true); const tableList = ref([]); const isShow = ref(false); const pagination = ref({ total: 0, pageSize: 10, currentPage: 1, pageSizes: [10, 20, 50, 100], background: true }); const tableLabel = ref([ { label: "ID", prop: "id" }, { label: "礼物名称", prop: "name" }, { label: "封面图", prop: "icon", cellRenderer: ({ row }) => ( ) }, { label: "数量", prop: "num" }, { label: "价格", prop: "gold" }, { label: "类型", prop: "type_str" }, { label: "添加时间", prop: "createtime" }, { label: "操作", fixed: "right", width: 210, slot: "operation" } ]); const onSearch = async (rowData) => { propRowData.value = { ...rowData } loading.value = true; const { data, code } = await queryGiftPack({ gift_bag_id: propRowData.value.id, page: pagination.value.currentPage, page_limit: pagination.value.pageSize }); if (code) { tableList.value = data.lists.map(ele => { return { ...ele } }); pagination.value.total = data.count; pagination.value.currentPage = data.page; } loading.value = false; }; const handleSizeChange = (val: number) => { pagination.value.pageSize = val; onSearch(propRowData.value); }; const handleCurrentChange = (val: number) => { pagination.value.currentPage = val; onSearch(propRowData.value); }; // 新增 const openDialog = (title = "新增", rowData: any) => { addDialog({ title: `${title}礼物`, props: { formInline: { type: rowData?.type ?? "", num: rowData?.num ?? "", gift_id: rowData?.gift_id ?? "", gift_bag_id: propRowData.value.id, } }, width: "40%", closeOnClickModal: false, contentRenderer: () => h(editForm, { ref: formRef, formInline: null }), beforeSure: (done, { options }) => { const FormRef = formRef.value.getRef(); const curData = options.props.formInline; const saveData = async form => { const { code } = await addGiftPackData(form); if (code) { message("新增成功", { type: "success" }); onSearch(propRowData.value); done(); } else { message("新增失败", { type: "error" }); } }; const updateData = async form => { const { code } = await editGiftPackData({ ...form, id: rowData.id }); if (code) { message("修改成功", { type: "success" }); onSearch(propRowData.value); done(); } else { message("修改失败", { type: "error" }); } }; FormRef.validate(valid => { if (valid) { console.log("curData", curData); // 表单规则校验通过 if (title === "新增") { // 实际开发先调用新增接口,再进行下面操作 saveData(curData); } else { // 实际开发先调用修改接口,再进行下面操作 updateData(curData); } } }); } }); }; const handleDelete = async rowData => { const { code } = await deteleGiftPackData({ id: rowData.id }); if (code) { message(`您删除了名称为${rowData.name}的这条数据`, { type: "success" }); onSearch(propRowData.value); } }; return { onSearch, isShow, tableList, tableLabel, pagination, handleSizeChange, handleCurrentChange, handleDelete, loading, openDialog }; }