import { ref, h } from "vue"; import editForm from "./form.vue"; import { message } from "@/utils/message"; import { queryTopicList, updateTopicData, deleteTopicData } from "@/api/modules/dynamics"; import { addDialog } from "@/components/ReDialog"; export function useData() { const formRef = ref(); const loading = ref(true); const tableList = ref([]); const isShow = ref(false); const searchForm = ref({ search_name: "" }); const searchLabel = ref([ { label: "话题标题", prop: "search_name", type: "input" } ]); const pagination = ref({ total: 0, pageSize: 10, pageSizes: [10, 20, 50, 100], currentPage: 1, background: true }); const tableLabel = ref([ { label: "ID", prop: "id" }, { label: "话题标题", prop: "title" }, { label: "话题内容", prop: "content" }, { label: "引入数量", prop: "num" }, { label: "图片", prop: "avatar", cellRenderer: ({ row }) => ( ), }, { label: "状态", prop: "is_delete", cellRenderer: ({ row }) => ( {row.is_delete === 1 ? '正常' : '已删除'} ), }, { label: "创建时间", prop: "createtime" }, { label: "操作", fixed: "right", slot: "operation" } ]); const onSearch = async (formData) => { loading.value = true; searchForm.value = { ...formData } const { data, code } = await queryTopicList({ ...formData, page: pagination.value.currentPage, page_limit: pagination.value.pageSize }); if (code) { tableList.value = data.lists; pagination.value.total = data.count; pagination.value.currentPage = data.page; } loading.value = false; }; const handleSizeChange = (val: number) => { pagination.value.pageSize = val; onSearch(searchForm.value); }; const handleCurrentChange = (val: number) => { pagination.value.currentPage = val; onSearch(searchForm.value); }; const handleDelete = async rowData => { const { code } = await deleteTopicData({ id: rowData.id }); if (code === "0") { message(`您删除了话题为【${rowData.title}】的这条数据`, { type: "success" }); onSearch(searchForm.value); } }; // 新增 const openDialog = (title = "新增", rowData: any) => { addDialog({ title: `${title}话题`, props: { formInline: { title: rowData?.title ?? "", content: rowData?.content ?? "", pic: rowData?.pic ?? "" } }, 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 updateTopicData(form); if (code) { message("新增成功", { type: "success" }); onSearch(searchForm.value); done(); } else { message("新增失败", { type: "error" }); } }; const updateData = async form => { const { code } = await updateTopicData({ ...form, id: rowData.id }); if (code) { message("修改成功", { type: "success" }); onSearch(searchForm.value); done(); } else { message("修改失败", { type: "error" }); } }; FormRef.validate(valid => { if (valid) { console.log("curData", curData); delete curData.higherMenuOptions; // 表单规则校验通过 if (title === "新增") { // 实际开发先调用新增接口,再进行下面操作 saveData(curData); } else { // 实际开发先调用修改接口,再进行下面操作 updateData(curData); } } }); } }); }; return { searchForm, searchLabel, onSearch, isShow, tableList, tableLabel, pagination, handleSizeChange, handleCurrentChange, loading, handleDelete, openDialog }; }