Files
certd/packages/ui/certd-client/src/views/sys/settings/header-menus/crud.tsx
T

204 lines
5.1 KiB
TypeScript
Raw Normal View History

2024-10-25 23:56:24 +08:00
import { useI18n } from "vue-i18n";
import { Ref, ref } from "vue";
import { useRouter } from "vue-router";
2024-10-27 02:51:56 +08:00
import { AddReq, compute, CreateCrudOptionsProps, CreateCrudOptionsRet, DelReq, EditReq, UserPageQuery, UserPageRes } from "@fast-crud/fast-crud";
2024-10-25 23:56:24 +08:00
import { useSettingStore } from "/@/store/modules/settings";
2024-10-27 02:51:56 +08:00
import { cloneDeep, find, merge, remove } from "lodash-es";
2024-10-25 23:56:24 +08:00
import { nanoid } from "nanoid";
2024-10-27 02:51:56 +08:00
import { SettingsSave } from "../api";
2024-10-25 23:56:24 +08:00
export default function ({ crudExpose, context }: CreateCrudOptionsProps): CreateCrudOptionsRet {
const { crudBinding } = crudExpose;
const router = useRouter();
const { t } = useI18n();
const settingStore = useSettingStore();
2024-10-27 02:51:56 +08:00
async function saveMenus() {
const menus = settingStore.headerMenus;
await SettingsSave("sys.header.menus", menus);
}
2024-10-25 23:56:24 +08:00
2024-10-27 02:51:56 +08:00
function eachTree(tree: any[], callback: (item: any) => void) {
tree.forEach((item) => {
callback(item);
if (item.children) {
eachTree(item.children, callback);
}
});
}
const expandedRowKeys = ref<string[]>([]);
const pageRequest = async (query: UserPageQuery): Promise<UserPageRes> => {
const records = cloneDeep(settingStore.headerMenus?.menus || []);
expandedRowKeys.value = [];
eachTree(records, (item) => {
if (item.children && item.children.length > 0) {
expandedRowKeys.value.push(item.id);
}
});
return {
records: records,
total: records.length,
limit: 9999999,
offset: 0
};
};
const editRequest = async ({ form, row }: EditReq) => {
form.id = row.id;
let found: any = undefined;
eachTree(settingStore.headerMenus?.menus || [], (item) => {
if (item.id === row.id) {
merge(item, form);
found = item;
}
});
await saveMenus();
return found;
};
const delRequest = async ({ row }: DelReq) => {
eachTree([{ children: settingStore.headerMenus?.menus }], (item) => {
if (item.children) {
remove(item.children, (child) => child.id === row.id);
}
});
await saveMenus();
};
const addRequest = async ({ form }: AddReq) => {
form.id = nanoid();
if (form.parentId) {
eachTree(settingStore.headerMenus?.menus || [], (item) => {
if (item.id === form.parentId) {
if (!item.children) {
item.children = [];
2024-10-25 23:56:24 +08:00
}
2024-10-27 02:51:56 +08:00
item.children.push(form);
2024-10-25 23:56:24 +08:00
}
2024-10-27 02:51:56 +08:00
});
} else {
settingStore.headerMenus?.menus.push(form);
}
parent.value = null;
await saveMenus();
return form;
};
return {
crudOptions: {
request: {
pageRequest,
addRequest,
editRequest,
delRequest
2024-10-25 23:56:24 +08:00
},
search: {
show: false
},
table: {
expandRowByClick: true,
2024-10-27 02:51:56 +08:00
defaultExpandAllRows: true,
expandedRowKeys: expandedRowKeys,
"onUpdate:expandedRowKeys": (val: string[]) => {
expandedRowKeys.value = val;
2024-10-25 23:56:24 +08:00
}
},
pagination: { show: false, pageSize: 9999999 },
rowHandle: {
width: 300,
fixed: "right",
2024-10-27 02:51:56 +08:00
buttons: {
addChild: {
title: "添加子菜单",
text: null,
type: "link",
icon: "ion:add-circle-outline",
click: ({ row }) => {
crudExpose.openAdd({
row: {
parentId: row.id
2024-10-25 23:56:24 +08:00
}
2024-10-27 02:51:56 +08:00
});
2024-10-25 23:56:24 +08:00
}
}
}
},
columns: {
id: {
title: "ID",
key: "id",
type: "text",
column: {
2024-10-27 02:51:56 +08:00
width: 200,
show: false
2024-10-25 23:56:24 +08:00
},
form: {
show: false
}
},
title: {
title: "菜单标题",
type: "text",
column: {
width: 300
2024-10-27 02:51:56 +08:00
},
form: {
rules: [
{
required: true,
message: "请输入标题"
}
]
2024-10-25 23:56:24 +08:00
}
},
icon: {
title: "图标",
type: "text",
column: {
2024-10-27 02:51:56 +08:00
width: 300,
cellRender: ({ row }) => {
return <fs-icon class={"fs-16"} icon={row.icon}></fs-icon>;
}
},
form: {
component: {
placeholder: "ion:add-circle"
},
helper: {
render: () => {
return (
<span>
<a href="https://icon-sets.iconify.design/" target="_blank">
</a>
<span>--------</span>
</span>
);
}
}
2024-10-25 23:56:24 +08:00
}
},
2024-10-27 02:51:56 +08:00
path: {
2024-10-25 23:56:24 +08:00
title: "链接",
2024-10-27 02:51:56 +08:00
type: "link",
2024-10-25 23:56:24 +08:00
column: {
width: 300
2024-10-27 02:51:56 +08:00
},
form: {
rules: [
{
required: true,
message: "请输入链接"
},
{
type: "url",
message: "请输入正确的链接"
}
]
2024-10-25 23:56:24 +08:00
}
}
}
}
};
}