import dayjs from "dayjs"; import editForm from "../form.vue"; import { message } from "@/utils/message"; import { getRoleList } from "@/api/system"; import { ElMessageBox } from "element-plus"; import { usePublicHooks } from "../../hooks"; import { addDialog } from "@/components/ReDialog"; import { type FormItemProps } from "../utils/types"; import { type PaginationProps } from "@pureadmin/table"; import { reactive, ref, onMounted, h, toRaw } from "vue"; export function useRole() { const form = reactive({ name: "", code: "", status: "" }); const formRef = ref(); const dataList = ref([]); const loading = ref(true); const switchLoadMap = ref({}); const { switchStyle } = usePublicHooks(); const pagination = reactive({ total: 0, pageSize: 10, currentPage: 1, background: true }); const columns: TableColumnList = [ { label: "角色编号", prop: "id", minWidth: 100 }, { label: "角色名称", prop: "name", minWidth: 120 }, { label: "角色标识", prop: "code", minWidth: 150 }, { label: "状态", minWidth: 130, cellRenderer: scope => ( onChange(scope as any)} /> ) }, { label: "备注", prop: "remark", minWidth: 150 }, { label: "创建时间", minWidth: 180, prop: "createTime", formatter: ({ createTime }) => dayjs(createTime).format("YYYY-MM-DD HH:mm:ss") }, { label: "操作", fixed: "right", width: 240, slot: "operation" } ]; // const buttonClass = computed(() => { // return [ // "!h-[20px]", // "reset-margin", // "!text-gray-500", // "dark:!text-white", // "dark:hover:!text-primary" // ]; // }); function onChange({ row, index }) { ElMessageBox.confirm( `确认要${ row.status === 0 ? "停用" : "启用" }${ row.name }吗?`, "系统提示", { confirmButtonText: "确定", cancelButtonText: "取消", type: "warning", dangerouslyUseHTMLString: true, draggable: true } ) .then(() => { switchLoadMap.value[index] = Object.assign( {}, switchLoadMap.value[index], { loading: true } ); setTimeout(() => { switchLoadMap.value[index] = Object.assign( {}, switchLoadMap.value[index], { loading: false } ); message(`已${row.status === 0 ? "停用" : "启用"}${row.name}`, { type: "success" }); }, 300); }) .catch(() => { row.status === 0 ? (row.status = 1) : (row.status = 0); }); } function handleDelete(row) { message(`您删除了角色名称为${row.name}的这条数据`, { type: "success" }); onSearch(); } function handleSizeChange(val: number) { console.log(`${val} items per page`); } function handleCurrentChange(val: number) { console.log(`current page: ${val}`); } function handleSelectionChange(val) { console.log("handleSelectionChange", val); } async function onSearch() { loading.value = true; const { data } = await getRoleList(toRaw(form)); dataList.value = data.list; pagination.total = data.total; pagination.pageSize = data.pageSize; pagination.currentPage = data.currentPage; setTimeout(() => { loading.value = false; }, 500); } const resetForm = formEl => { if (!formEl) return; formEl.resetFields(); onSearch(); }; function openDialog(title = "新增", row?: FormItemProps) { addDialog({ title: `${title}角色`, props: { formInline: { name: row?.name ?? "", code: row?.code ?? "", remark: row?.remark ?? "" } }, width: "40%", draggable: true, fullscreenIcon: true, closeOnClickModal: false, contentRenderer: () => h(editForm, { ref: formRef }), beforeSure: (done, { options }) => { const FormRef = formRef.value.getRef(); const curData = options.props.formInline as FormItemProps; function chores() { message(`您${title}了角色名称为${curData.name}的这条数据`, { type: "success" }); done(); // 关闭弹框 onSearch(); // 刷新表格数据 } FormRef.validate(valid => { if (valid) { console.log("curData", curData); // 表单规则校验通过 if (title === "新增") { // 实际开发先调用新增接口,再进行下面操作 chores(); } else { // 实际开发先调用编辑接口,再进行下面操作 chores(); } } }); } }); } /** 菜单权限 */ function handleMenu() { message("等菜单管理页面开发后完善"); } /** 数据权限 可自行开发 */ // function handleDatabase() {} onMounted(() => { onSearch(); }); return { form, loading, columns, dataList, pagination, // buttonClass, onSearch, resetForm, openDialog, handleMenu, handleDelete, // handleDatabase, handleSizeChange, handleCurrentChange, handleSelectionChange }; }