Browse Source

feat(desk-category): 调整分类层级逻辑与树结构构建方式

- 修改分类级别判断条件,将原 level 2、3 的操作调整为 level 1、2
- 更新新增和编辑分类时的 level 值及标题显示逻辑
- 移除 buildTree 方法,不再在前端进行树结构构建
- 调整提交表单逻辑中对 level 判断的条件
- 删除无用的 console.log 输出
- 优化关联商品和删除按钮的显示层级条件
zhangtao 22 hours ago
parent
commit
bb3c9b3ae6
1 changed files with 7 additions and 50 deletions
  1. 7 50
      src/views/goods/desk-category/index.vue

+ 7 - 50
src/views/goods/desk-category/index.vue

@@ -187,12 +187,12 @@ const tableColumns: NaiveUI.TableColumn<Api.goods.ShopCategory>[] = [
     width: 230,
     render: row => (
       <div class="flex-center gap-8px">
-        {row.level == 2 && (
+        {row.level == 1 && (
           <NButton type="primary" size="small" quaternary onClick={() => add(row)}>
             添加三级分类
           </NButton>
         )}
-        {row.level == 3 && (
+        {row.level == 2 && (
           <NButton type="primary" size="small" quaternary onClick={() => handleOpenRelatedGoods(row)}>
             关联商品
           </NButton>
@@ -203,7 +203,7 @@ const tableColumns: NaiveUI.TableColumn<Api.goods.ShopCategory>[] = [
           </NButton>
         }
 
-        {row.level == 3 && (
+        {row.level == 2 && (
           <NButton type="primary" size="small" quaternary onClick={() => del(row)}>
             删除
           </NButton>
@@ -293,7 +293,7 @@ async function handleSubmit(file: File) {
 }
 function edit(row: Api.goods.ShopCategory) {
   openModalForm(row);
-  setModalProps({ title: `修改${row.level}级分类` });
+  setModalProps({ title: `修改${Number(row.level) + 1}级分类` });
   setModalFormValue({ ...row, label: row.label });
 }
 function add(row: Api.goods.ShopCategory) {
@@ -302,7 +302,7 @@ function add(row: Api.goods.ShopCategory) {
   const level2Name = current.name;
   openModalForm();
   setModalProps({ title: `新增三级分类` });
-  setModalFormValue({ level: 3, pid: row.id, parentCode: row.code, BreadName: `${level1Name} > ${level2Name}` });
+  setModalFormValue({ level: 2, pid: row.id, parentCode: row.code, BreadName: `${level1Name} > ${level2Name}` });
 }
 function del(row: Api.goods.ShopCategory) {
   window.$dialog?.info({
@@ -322,8 +322,7 @@ async function getData() {
   const { data, error } = await fetchGetDeskCategoryList(getSearchForm());
 
   if (!error) {
-    deskData.value = buildTree(data).sort((a, b) => b.num - a.num);
-    console.log(deskData.value);
+    deskData.value = data;
   }
 }
 function handleOpenRelatedGoods(row: Api.goods.ShopCategory) {
@@ -337,7 +336,7 @@ async function handleSubmitForm() {
   const form = await getModalFormValue();
   const level = form.level;
   const { error } =
-    level != 3 ? await fetchUpdateCategory(form) : await fetchAddCategory({ ...form, shopId: getSearchForm().shopId });
+    level != 2 ? await fetchUpdateCategory(form) : await fetchAddCategory({ ...form, shopId: getSearchForm().shopId });
   if (!error) {
     closeModalForm();
     getData();
@@ -363,49 +362,7 @@ function hanleExportFailure(taskCode: string) {
     '_blank'
   );
 }
-interface BuildTreeOptions {
-  idKey?: string;
-  pidKey?: string;
-  childrenKey?: string;
-}
-
-function buildTree<T>(items: T[], options: BuildTreeOptions = {}): T[] {
-  const { idKey = 'id', pidKey = 'pid', childrenKey = 'children' } = options;
-
-  const itemMap = new Map<number, T>();
-
-  items.forEach(item => {
-    const id = item[idKey as keyof T];
-    if (typeof id === 'number') {
-      itemMap.set(id, { ...item, [childrenKey]: [] });
-    }
-  });
-
-  const tree: T[] = [];
 
-  items.forEach(item => {
-    const id = item[idKey as keyof T];
-    const pid = item[pidKey as keyof T];
-
-    if (typeof id !== 'number' || typeof pid !== 'number') {
-      return;
-    }
-
-    const currentNode = itemMap.get(id);
-    if (!currentNode) return;
-
-    if (pid === 0) {
-      tree.push(currentNode);
-    } else {
-      const parentNode = itemMap.get(pid);
-      if (parentNode && parentNode[childrenKey as keyof T]) {
-        (parentNode[childrenKey as keyof T] as T[]).push(currentNode);
-      }
-    }
-  });
-
-  return tree;
-}
 function getRowHierarchyInfo(
   row: Api.goods.ShopCategory,
   allData: Api.goods.ShopCategory[]