Prechádzať zdrojové kódy

Merge branch 'master' into hurx

hurx 3 dní pred
rodič
commit
debd0640e0
1 zmenil súbory, kde vykonal 85 pridanie a 18 odobranie
  1. 85 18
      src/views/system/bottomNav/index.vue

+ 85 - 18
src/views/system/bottomNav/index.vue

@@ -5,39 +5,44 @@
         <div class="card-header">
           <span class="title">底部导航信息列表</span>
           <div>
-            <el-button type="primary" @click="handleAdd" v-hasPermi="['system:navigation:add']">+ 新增</el-button>
+            <el-button type="primary" @click="handleAdd()" v-hasPermi="['system:navigation:add']">+ 新增</el-button>
             <el-button @click="getList">刷新</el-button>
           </div>
         </div>
       </template>
 
-      <el-table v-loading="loading" :data="dataList" border>
-        <el-table-column label="导航标题" prop="navigationName" align="center" />
-        <el-table-column label="链接地址" prop="url" align="center" show-overflow-tooltip />
+      <el-table v-loading="loading" :data="treeData" border row-key="id" :tree-props="{ children: 'children' }" default-expand-all>
+        <el-table-column label="导航标题" prop="navigationName" />
+        <el-table-column label="链接地址" prop="url" show-overflow-tooltip />
         <el-table-column label="排序" prop="sort" width="100" align="center" />
         <el-table-column label="是否显示" prop="isEnable" width="100" align="center">
           <template #default="scope">
             {{ scope.row.isEnable === '0' ? '显示' : '不显示' }}
           </template>
         </el-table-column>
-        <el-table-column label="操作" width="150" align="center">
+        <el-table-column label="操作" width="200" align="center">
           <template #default="scope">
+            <el-button link type="primary" @click="handleAdd(scope.row)" v-if="!scope.row.parentId" v-hasPermi="['system:navigation:add']">新增子级</el-button>
             <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['system:navigation:edit']">编辑</el-button>
             <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['system:navigation:remove']">删除</el-button>
           </template>
         </el-table-column>
       </el-table>
-
-      <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
     </el-card>
 
     <!-- 添加或修改对话框 -->
     <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
       <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
+        <el-form-item label="上级导航" prop="parentId">
+          <el-select v-model="form.parentId" placeholder="无(顶级导航)" clearable style="width: 100%">
+            <el-option label="无(顶级导航)" :value="0" />
+            <el-option v-for="item in parentOptions" :key="item.id" :label="item.navigationName" :value="item.id" />
+          </el-select>
+        </el-form-item>
         <el-form-item label="导航名称" prop="navigationName">
           <el-input v-model="form.navigationName" placeholder="请输入导航名称" />
         </el-form-item>
-        <el-form-item label="链接地址" prop="url">
+        <el-form-item label="链接地址" prop="url" required>
           <el-input v-model="form.url" placeholder="请输入链接地址" />
         </el-form-item>
         <el-form-item label="排序" prop="sort">
@@ -62,14 +67,13 @@
 
 <script setup name="BottomNav" lang="ts">
 import { listNavigation, getNavigation, addNavigation, updateNavigation, delNavigation } from '@/api/system/navigation';
-import { ComponentInternalInstance, getCurrentInstance, ref, reactive, toRefs } from 'vue';
+import { ComponentInternalInstance, getCurrentInstance, ref, reactive, toRefs, computed } from 'vue';
 import type { FormInstance } from 'element-plus';
 
 const { proxy } = getCurrentInstance() as ComponentInternalInstance;
 
-const dataList = ref([]);
+const dataList = ref<any[]>([]);
 const loading = ref(true);
-const total = ref(0);
 
 const formRef = ref<FormInstance>();
 
@@ -79,12 +83,11 @@ const data = reactive({
   form: {} as any,
   queryParams: {
     pageNum: 1,
-    pageSize: 10,
+    pageSize: 1000,
     navType: 'setting_footer'
   },
   rules: {
     navigationName: [{ required: true, message: '导航名称不能为空', trigger: 'blur' }],
-    url: [{ required: true, message: '链接地址不能为空', trigger: 'blur' }],
     sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
     isEnable: [{ required: true, message: '请选择是否启用', trigger: 'change' }]
   }
@@ -92,17 +95,63 @@ const data = reactive({
 
 const { queryParams, form, rules } = toRefs(data);
 
+/** 构建树形结构 */
+const treeData = computed(() => {
+  const list = dataList.value;
+  const map = new Map<number, any>();
+  const roots: any[] = [];
+
+  // 先将所有节点放入map
+  list.forEach((item: any) => {
+    map.set(item.id, { ...item, children: [] });
+  });
+
+  // 构建树
+  list.forEach((item: any) => {
+    const node = map.get(item.id);
+    if (item.parentId && item.parentId !== 0) {
+      const parent = map.get(item.parentId);
+      if (parent) {
+        parent.children.push(node);
+      } else {
+        roots.push(node);
+      }
+    } else {
+      roots.push(node);
+    }
+  });
+
+  // 移除空的children数组
+  const cleanChildren = (nodes: any[]) => {
+    nodes.forEach((node) => {
+      if (node.children && node.children.length === 0) {
+        delete node.children;
+      } else if (node.children) {
+        cleanChildren(node.children);
+      }
+    });
+  };
+  cleanChildren(roots);
+
+  return roots;
+});
+
+/** 上级导航选项(只显示顶级,最多二级) */
+const parentOptions = computed(() => {
+  return dataList.value.filter((item: any) => !item.parentId || item.parentId === 0);
+});
+
 const getList = async () => {
   loading.value = true;
   const res = await listNavigation(queryParams.value);
   dataList.value = res.rows;
-  total.value = res.total;
   loading.value = false;
 };
 
 const reset = () => {
   form.value = {
     id: undefined,
+    parentId: 0,
     navType: 'setting_footer',
     navigationName: undefined,
     url: undefined,
@@ -112,16 +161,23 @@ const reset = () => {
   formRef.value?.resetFields();
 };
 
-const handleAdd = () => {
+const handleAdd = (row?: any) => {
   reset();
+  if (row) {
+    form.value.parentId = row.id;
+  }
   dialog.visible = true;
-  dialog.title = '添加底部导航';
+  dialog.title = row ? '添加子级导航' : '添加底部导航';
 };
 
 const handleUpdate = async (row: any) => {
   reset();
   const res = await getNavigation(row.id);
   Object.assign(form.value, res.data);
+  // 确保parentId有值
+  if (!form.value.parentId) {
+    form.value.parentId = 0;
+  }
   dialog.visible = true;
   dialog.title = '修改底部导航';
 };
@@ -129,11 +185,16 @@ const handleUpdate = async (row: any) => {
 const submitForm = () => {
   formRef.value?.validate(async (valid: boolean) => {
     if (valid) {
+      // 如果parentId为0,传null给后端
+      const submitData = { ...form.value };
+      if (submitData.parentId === 0) {
+        submitData.parentId = null;
+      }
       if (form.value.id) {
-        await updateNavigation(form.value);
+        await updateNavigation(submitData);
         proxy?.$modal.msgSuccess('修改成功');
       } else {
-        await addNavigation(form.value);
+        await addNavigation(submitData);
         proxy?.$modal.msgSuccess('新增成功');
       }
       dialog.visible = false;
@@ -143,6 +204,12 @@ const submitForm = () => {
 };
 
 const handleDelete = async (row: any) => {
+  // 如果是顶级且有子级,提示不能删除
+  const hasChildren = dataList.value.some((item: any) => item.parentId === row.id);
+  if (hasChildren) {
+    proxy?.$modal.msgWarning('该导航下存在子级,请先删除子级');
+    return;
+  }
   await proxy?.$modal.confirm('是否确认删除?');
   await delNavigation(row.id);
   proxy?.$modal.msgSuccess('删除成功');