index.vue 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <template>
  2. <div class="p-2">
  3. <el-card shadow="hover">
  4. <template #header>
  5. <div class="card-header">
  6. <span class="title">底部导航信息列表</span>
  7. <div>
  8. <el-button type="primary" @click="handleAdd()" v-hasPermi="['system:navigation:add']">+ 新增</el-button>
  9. <el-button @click="getList">刷新</el-button>
  10. </div>
  11. </div>
  12. </template>
  13. <el-table v-loading="loading" :data="treeData" border row-key="id" :tree-props="{ children: 'children' }" default-expand-all>
  14. <el-table-column label="导航标题" prop="navigationName" />
  15. <el-table-column label="链接地址" prop="url" show-overflow-tooltip />
  16. <el-table-column label="排序" prop="sort" width="100" align="center" />
  17. <el-table-column label="是否显示" prop="isEnable" width="100" align="center">
  18. <template #default="scope">
  19. {{ scope.row.isEnable === '0' ? '显示' : '不显示' }}
  20. </template>
  21. </el-table-column>
  22. <el-table-column label="操作" width="200" align="center">
  23. <template #default="scope">
  24. <el-button link type="primary" @click="handleAdd(scope.row)" v-if="!scope.row.parentId" v-hasPermi="['system:navigation:add']">新增子级</el-button>
  25. <el-button link type="primary" @click="handleUpdate(scope.row)" v-hasPermi="['system:navigation:edit']">编辑</el-button>
  26. <el-button link type="danger" @click="handleDelete(scope.row)" v-hasPermi="['system:navigation:remove']">删除</el-button>
  27. </template>
  28. </el-table-column>
  29. </el-table>
  30. </el-card>
  31. <!-- 添加或修改对话框 -->
  32. <el-dialog :title="dialog.title" v-model="dialog.visible" width="500px" append-to-body>
  33. <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
  34. <el-form-item label="上级导航" prop="parentId">
  35. <el-select v-model="form.parentId" placeholder="无(顶级导航)" clearable style="width: 100%">
  36. <el-option label="无(顶级导航)" :value="0" />
  37. <el-option v-for="item in parentOptions" :key="item.id" :label="item.navigationName" :value="item.id" />
  38. </el-select>
  39. </el-form-item>
  40. <el-form-item label="导航名称" prop="navigationName">
  41. <el-input v-model="form.navigationName" placeholder="请输入导航名称" />
  42. </el-form-item>
  43. <el-form-item label="链接地址" prop="url" required>
  44. <el-input v-model="form.url" placeholder="请输入链接地址" />
  45. </el-form-item>
  46. <el-form-item label="排序" prop="sort">
  47. <el-input-number v-model="form.sort" :min="0" />
  48. </el-form-item>
  49. <el-form-item label="是否启用" prop="isEnable">
  50. <el-radio-group v-model="form.isEnable">
  51. <el-radio value="0">是</el-radio>
  52. <el-radio value="1">否</el-radio>
  53. </el-radio-group>
  54. </el-form-item>
  55. </el-form>
  56. <template #footer>
  57. <div class="dialog-footer">
  58. <el-button type="primary" @click="submitForm">确 定</el-button>
  59. <el-button @click="cancel">取 消</el-button>
  60. </div>
  61. </template>
  62. </el-dialog>
  63. </div>
  64. </template>
  65. <script setup name="BottomNav" lang="ts">
  66. import { listNavigation, getNavigation, addNavigation, updateNavigation, delNavigation } from '@/api/system/navigation';
  67. import { ComponentInternalInstance, getCurrentInstance, ref, reactive, toRefs, computed } from 'vue';
  68. import type { FormInstance } from 'element-plus';
  69. const { proxy } = getCurrentInstance() as ComponentInternalInstance;
  70. const dataList = ref<any[]>([]);
  71. const loading = ref(true);
  72. const formRef = ref<FormInstance>();
  73. const dialog = reactive({ visible: false, title: '' });
  74. const data = reactive({
  75. form: {} as any,
  76. queryParams: {
  77. pageNum: 1,
  78. pageSize: 1000,
  79. navType: 'setting_footer'
  80. },
  81. rules: {
  82. navigationName: [{ required: true, message: '导航名称不能为空', trigger: 'blur' }],
  83. sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  84. isEnable: [{ required: true, message: '请选择是否启用', trigger: 'change' }]
  85. }
  86. });
  87. const { queryParams, form, rules } = toRefs(data);
  88. /** 构建树形结构 */
  89. const treeData = computed(() => {
  90. const list = dataList.value;
  91. const map = new Map<number, any>();
  92. const roots: any[] = [];
  93. // 先将所有节点放入map
  94. list.forEach((item: any) => {
  95. map.set(item.id, { ...item, children: [] });
  96. });
  97. // 构建树
  98. list.forEach((item: any) => {
  99. const node = map.get(item.id);
  100. if (item.parentId && item.parentId !== 0) {
  101. const parent = map.get(item.parentId);
  102. if (parent) {
  103. parent.children.push(node);
  104. } else {
  105. roots.push(node);
  106. }
  107. } else {
  108. roots.push(node);
  109. }
  110. });
  111. // 移除空的children数组
  112. const cleanChildren = (nodes: any[]) => {
  113. nodes.forEach((node) => {
  114. if (node.children && node.children.length === 0) {
  115. delete node.children;
  116. } else if (node.children) {
  117. cleanChildren(node.children);
  118. }
  119. });
  120. };
  121. cleanChildren(roots);
  122. return roots;
  123. });
  124. /** 上级导航选项(只显示顶级,最多二级) */
  125. const parentOptions = computed(() => {
  126. return dataList.value.filter((item: any) => !item.parentId || item.parentId === 0);
  127. });
  128. const getList = async () => {
  129. loading.value = true;
  130. const res = await listNavigation(queryParams.value);
  131. dataList.value = res.rows;
  132. loading.value = false;
  133. };
  134. const reset = () => {
  135. form.value = {
  136. id: undefined,
  137. parentId: 0,
  138. navType: 'setting_footer',
  139. navigationName: undefined,
  140. url: undefined,
  141. sort: 0,
  142. isEnable: '0'
  143. };
  144. formRef.value?.resetFields();
  145. };
  146. const handleAdd = (row?: any) => {
  147. reset();
  148. if (row) {
  149. form.value.parentId = row.id;
  150. }
  151. dialog.visible = true;
  152. dialog.title = row ? '添加子级导航' : '添加底部导航';
  153. };
  154. const handleUpdate = async (row: any) => {
  155. reset();
  156. const res = await getNavigation(row.id);
  157. Object.assign(form.value, res.data);
  158. // 确保parentId有值
  159. if (!form.value.parentId) {
  160. form.value.parentId = 0;
  161. }
  162. dialog.visible = true;
  163. dialog.title = '修改底部导航';
  164. };
  165. const submitForm = () => {
  166. formRef.value?.validate(async (valid: boolean) => {
  167. if (valid) {
  168. // 如果parentId为0,传null给后端
  169. const submitData = { ...form.value };
  170. if (submitData.parentId === 0) {
  171. submitData.parentId = null;
  172. }
  173. if (form.value.id) {
  174. await updateNavigation(submitData);
  175. proxy?.$modal.msgSuccess('修改成功');
  176. } else {
  177. await addNavigation(submitData);
  178. proxy?.$modal.msgSuccess('新增成功');
  179. }
  180. dialog.visible = false;
  181. getList();
  182. }
  183. });
  184. };
  185. const handleDelete = async (row: any) => {
  186. // 如果是顶级且有子级,提示不能删除
  187. const hasChildren = dataList.value.some((item: any) => item.parentId === row.id);
  188. if (hasChildren) {
  189. proxy?.$modal.msgWarning('该导航下存在子级,请先删除子级');
  190. return;
  191. }
  192. await proxy?.$modal.confirm('是否确认删除?');
  193. await delNavigation(row.id);
  194. proxy?.$modal.msgSuccess('删除成功');
  195. getList();
  196. };
  197. const cancel = () => {
  198. dialog.visible = false;
  199. reset();
  200. };
  201. getList();
  202. </script>
  203. <style scoped>
  204. .card-header {
  205. display: flex;
  206. justify-content: space-between;
  207. align-items: center;
  208. }
  209. .card-header .title {
  210. font-size: 16px;
  211. font-weight: bold;
  212. }
  213. </style>