index.vue 18.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<template>
  <div class="app-container">
    <Breadcrumb :items="['menu.system', 'menu.system.menu.list']" />
    <a-card class="general-card" :title="$t('menu.system.menu.list')">
      <!-- 头部区域 -->
      <div class="header">
        <!-- 搜索栏 -->
        <div v-if="showQuery" class="header-query">
          <a-form ref="queryRef" :model="queryParams" layout="inline">
            <a-form-item field="menuName" hide-label>
              <a-input
                v-model="queryParams.menuName"
                placeholder="输入菜单名称搜索"
                allow-clear
                style="width: 150px"
                @press-enter="handleQuery"
              />
            </a-form-item>
            <a-form-item field="status" hide-label>
              <a-select
                v-model="queryParams.status"
22
                :options="DisEnableStatusEnum"
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
                placeholder="状态搜索"
                allow-clear
                style="width: 150px"
              />
            </a-form-item>
            <a-form-item hide-label>
              <a-space>
                <a-button type="primary" @click="handleQuery">
                  <template #icon><icon-search /></template>查询
                </a-button>
                <a-button @click="resetQuery">
                  <template #icon><icon-refresh /></template>重置
                </a-button>
              </a-space>
            </a-form-item>
          </a-form>
        </div>
        <!-- 操作栏 -->
        <div class="header-operation">
          <a-row>
            <a-col :span="12">
              <a-space>
45
                <a-button type="primary" @click="toAdd">
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
                  <template #icon><icon-plus /></template>新增
                </a-button>
                <a-button
                  type="primary"
                  status="success"
                  :disabled="single"
                  :title="single ? '请选择一条要修改的数据' : ''"
                  @click="toUpdate(ids[0])"
                >
                  <template #icon><icon-edit /></template>修改
                </a-button>
                <a-button
                  type="primary"
                  status="danger"
                  :disabled="multiple"
                  :title="multiple ? '请选择要删除的数据' : ''"
                  @click="handleBatchDelete"
                >
                  <template #icon><icon-delete /></template>删除
                </a-button>
                <a-button
                  :loading="exportLoading"
                  type="primary"
                  status="warning"
                  @click="handleExport"
                >
                  <template #icon><icon-download /></template>导出
                </a-button>
                <a-button type="secondary" @click="handleExpandAll">
                  <template #icon><icon-swap :rotate="90" /></template>展开/折叠
                </a-button>
              </a-space>
            </a-col>
            <a-col :span="12">
              <right-toolbar
                v-model:show-query="showQuery"
                @refresh="getList"
              />
            </a-col>
          </a-row>
        </div>
      </div>

      <!-- 列表区域 -->
      <a-table
        ref="tableRef"
        :data="menuList"
        :row-selection="{
          type: 'checkbox',
          showCheckedAll: true,
          onlyCurrent: false,
        }"
        :pagination="false"
        :default-expand-all-rows="true"
        :hide-expand-button-on-empty="true"
        row-key="menuId"
        :bordered="false"
        :stripe="true"
        :loading="loading"
        size="large"
        @select="handleSelect"
        @selection-change="handleSelectionChange"
      >
        <template #columns>
          <a-table-column title="菜单名称" data-index="menuName" />
          <a-table-column title="图标" align="center">
            <template #cell="{ record }">
              <svg-icon :icon-class="record.icon ? record.icon : ''" />
            </template>
          </a-table-column>
          <a-table-column title="排序" align="center" data-index="menuSort" />
          <a-table-column title="权限标识" data-index="permission" />
          <a-table-column title="组件路径" data-index="component" />
          <a-table-column title="状态" align="center">
            <template #cell="{ record }">
              <a-switch
                v-model="record.status"
                :checked-value="1"
                :unchecked-value="2"
                @change="handleChangeStatus(record)"
              />
            </template>
          </a-table-column>
          <a-table-column title="外链" align="center">
            <template #cell="{ record }">
              <a-tag v-if="record.isExternal" color="green"></a-tag>
              <a-tag v-else color="red"></a-tag>
            </template>
          </a-table-column>
          <a-table-column title="缓存" align="center">
            <template #cell="{ record }">
              <a-tag v-if="record.isCache" color="green"></a-tag>
              <a-tag v-else color="red"></a-tag>
            </template>
          </a-table-column>
          <a-table-column title="隐藏" align="center">
            <template #cell="{ record }">
              <a-tag v-if="record.isHidden" color="green"></a-tag>
              <a-tag v-else color="red"></a-tag>
            </template>
          </a-table-column>
          <a-table-column title="创建时间" data-index="createTime" />
          <a-table-column title="操作" align="center">
            <template #cell="{ record }">
              <a-button
                v-permission="['admin']"
                type="text"
                size="small"
                title="修改"
                @click="toUpdate(record.menuId)"
              >
                <template #icon><icon-edit /></template>修改
              </a-button>
              <a-popconfirm
                content="确定要删除当前选中的数据吗?如果存在下级菜单则一并删除,此操作不能撤销!"
                type="warning"
                @ok="handleDelete([record.menuId])"
              >
                <a-button
                  v-permission="['admin']"
                  type="text"
                  size="small"
                  title="删除"
                >
                  <template #icon><icon-delete /></template>删除
                </a-button>
              </a-popconfirm>
            </template>
          </a-table-column>
        </template>
      </a-table>

      <!-- 表单区域 -->
      <a-modal
        :title="title"
        :visible="visible"
        :width="625"
        :mask-closable="false"
        unmount-on-close
        render-to-body
        @ok="handleOk"
        @cancel="handleCancel"
      >
        <a-form
          ref="formRef"
          :model="form"
          :rules="rules"
          layout="inline"
          :label-col-style="{ width: '85px' }"
          size="large"
        >
          <a-form-item label="菜单类型" field="menuType" lab>
            <a-radio-group v-model="form.menuType" type="button">
              <a-radio :value="1" style="width: 57px">目录</a-radio>
              <a-radio :value="2" style="width: 57px">菜单</a-radio>
              <a-radio :value="3" style="width: 57px">按钮</a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item v-if="form.menuType !== 3" label="菜单图标" field="icon">
            <a-popover
              v-model:popup-visible="showChooseIcon"
              position="bottom"
              trigger="click"
            >
              <a-input
                v-model="form.icon"
                placeholder="点击选择菜单图标"
                readonly
                allow-clear
                style="width: 473px"
              >
                <template #prefix>
                  <svg-icon
                    v-if="form.icon"
                    :icon-class="form.icon"
                    style="height: 32px; width: 16px"
                  />
                  <icon-search v-else />
                </template>
              </a-input>
              <template #content>
                <icon-select ref="iconSelectRef" @selected="selected" />
              </template>
            </a-popover>
          </a-form-item>
          <a-form-item label="菜单名称" field="menuName">
            <a-input
              v-model="form.menuName"
              placeholder="请输入菜单名称"
              style="width: 182px"
            />
          </a-form-item>
          <a-form-item label="菜单排序" field="menuSort">
            <a-input-number
              v-model="form.menuSort"
              placeholder="请输入菜单排序"
              :min="1"
              mode="button"
              style="width: 182px"
            />
          </a-form-item>
          <a-form-item
            v-if="form.menuType !== 1"
            label="权限标识"
            field="permission"
          >
            <a-input
              v-model="form.permission"
              placeholder="请输入权限标识"
              style="width: 182px"
            />
          </a-form-item>
          <a-form-item v-if="form.menuType !== 3" label="路由地址" field="path">
            <a-input
              v-model="form.path"
              placeholder="请输入路由地址"
              style="width: 473px"
            />
          </a-form-item>
          <a-form-item
            v-if="!form.isExternal && form.menuType === 2"
            label="组件名称"
            field="name"
          >
            <a-input
              v-model="form.name"
              placeholder="请输入组件名称"
              style="width: 182px"
            />
          </a-form-item>
          <a-form-item
            v-if="!form.isExternal && form.menuType === 2"
            label="组件路径"
            field="component"
          >
            <a-input
              v-model="form.component"
              placeholder="请输入组件路径"
              style="width: 182px"
            />
          </a-form-item>
          <br />
          <a-form-item
            v-if="form.menuType !== 3"
            label="是否外链"
            field="isExternal"
          >
            <a-radio-group v-model="form.isExternal" type="button">
              <a-radio :value="true"></a-radio>
              <a-radio :value="false"></a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item
            v-if="form.menuType === 2"
            label="是否缓存"
            field="isCache"
          >
            <a-radio-group v-model="form.isCache" type="button">
              <a-radio :value="true"></a-radio>
              <a-radio :value="false"></a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item
            v-if="form.menuType !== 3"
            label="是否隐藏"
            field="isHidden"
          >
            <a-radio-group v-model="form.isHidden" type="button">
              <a-radio :value="true"></a-radio>
              <a-radio :value="false"></a-radio>
            </a-radio-group>
          </a-form-item>
          <a-form-item label="上级菜单" field="parentId">
            <a-tree-select
              v-model="form.parentId"
              :data="treeData"
              placeholder="请选择上级菜单"
              allow-clear
              allow-search
              :filter-tree-node="filterMenuTree"
              :fallback-option="false"
              style="width: 473px"
            />
          </a-form-item>
        </a-form>
      </a-modal>
    </a-card>
  </div>
</template>

<script lang="ts" setup>
  import { getCurrentInstance, ref, toRefs, reactive } from 'vue';
338
  import { TreeNodeData, TableData } from '@arco-design/web-vue';
339 340 341 342 343
  import {
    MenuRecord,
    MenuParam,
    listMenu,
    getMenu,
344
    addMenu,
345 346 347 348 349 350
    updateMenu,
    deleteMenu,
  } from '@/api/system/menu';
  import { listMenuTree } from '@/api/common';

  const { proxy } = getCurrentInstance() as any;
351
  const { DisEnableStatusEnum } = proxy.useDict('DisEnableStatusEnum');
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406

  const menuList = ref<MenuRecord[]>([]);
  const ids = ref<Array<number>>([]);
  const title = ref('');
  const single = ref(true);
  const multiple = ref(true);
  const showQuery = ref(true);
  const loading = ref(false);
  const exportLoading = ref(false);
  const expandAll = ref(false);
  const visible = ref(false);
  const showChooseIcon = ref(false);
  const treeData = ref<TreeNodeData[]>();

  const data = reactive({
    // 查询参数
    queryParams: {
      menuName: undefined,
      status: undefined,
      sort: ['parentId,asc', 'menuSort,asc', 'createTime,desc'],
    },
    // 表单数据
    form: {} as MenuRecord,
    // 表单验证规则
    rules: {
      menuName: [{ required: true, message: '请输入菜单名称' }],
      path: [{ required: true, message: '请输入路由地址' }],
      name: [{ required: true, message: '请输入组件名称' }],
      component: [{ required: true, message: '请输入组件路径' }],
      permission: [{ required: true, message: '请输入权限标识' }],
      menuSort: [{ required: true, message: '请输入菜单排序' }],
    },
  });
  const { queryParams, form, rules } = toRefs(data);

  /**
   * 查询列表
   *
   * @param params 查询参数
   */
  const getList = (params: MenuParam = { ...queryParams.value }) => {
    loading.value = true;
    listMenu(params)
      .then((res) => {
        menuList.value = res.data;
      })
      .finally(() => {
        loading.value = false;
      });
  };
  getList();

  /**
   * 打开新增对话框
   */
407
  const toAdd = () => {
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
    reset();
    listMenuTree({}).then((res) => {
      treeData.value = res.data;
    });
    title.value = '新增菜单';
    visible.value = true;
  };

  /**
   * 打开修改对话框
   *
   * @param id ID
   */
  const toUpdate = (id: number) => {
    reset();
    listMenuTree({}).then((res) => {
      treeData.value = res.data;
    });

    getMenu(id).then((res) => {
      form.value = res.data;
      title.value = '修改菜单';
      visible.value = true;
    });
  };

  /**
   * 重置表单
   */
  const reset = () => {
    form.value = {
      menuId: undefined,
      menuName: '',
      parentId: undefined,
      menuType: 1,
      path: undefined,
      name: undefined,
      component: undefined,
      icon: undefined,
      isExternal: false,
      isCache: false,
      isHidden: false,
      permission: undefined,
      menuSort: 999,
      status: 1,
    };
    proxy.$refs.formRef?.resetFields();
  };

  /**
   * 取消
   */
  const handleCancel = () => {
    visible.value = false;
    proxy.$refs.formRef.resetFields();
  };

  /**
   * 确定
   */
  const handleOk = () => {
    proxy.$refs.formRef.validate((valid: any) => {
      if (!valid) {
        if (form.value.menuId !== undefined) {
          updateMenu(form.value).then((res) => {
            handleCancel();
            getList();
            proxy.$message.success(res.msg);
          });
        } else {
478
          addMenu(form.value).then((res) => {
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656
            handleCancel();
            getList();
            proxy.$message.success(res.msg);
          });
        }
      }
    });
  };

  /**
   * 批量删除
   */
  const handleBatchDelete = () => {
    if (ids.value.length === 0) {
      proxy.$message.info('请选择要删除的数据');
    } else {
      proxy.$modal.warning({
        title: '警告',
        titleAlign: 'start',
        content:
          '确定要删除当前选中的数据吗?如果存在下级菜单则一并删除,此操作不能撤销!',
        hideCancel: false,
        onOk: () => {
          handleDelete(ids.value);
        },
      });
    }
  };

  /**
   * 删除
   *
   * @param ids ID 列表
   */
  const handleDelete = (ids: Array<number>) => {
    deleteMenu(ids).then((res) => {
      proxy.$message.success(res.msg);
      getList();
    });
  };

  /**
   * 点击行选择器
   */
  const handleSelect = (rowKeys: any, rowKey: any, record: TableData) => {
    if (rowKeys.find((key: any) => key === rowKey)) {
      if (record.children) {
        record.children.forEach((r) => {
          proxy.$refs.tableRef.select(r.menuId);
          rowKeys.push(r.menuId);
          if (r.children) {
            handleSelect(rowKeys, rowKey, r);
          }
        });
      }
    } else if (record.children) {
      record.children.forEach((r) => {
        rowKeys.splice(
          rowKeys.findIndex((key: number | undefined) => key === r.menuId),
          1
        );
        proxy.$refs.tableRef.select(r.menuId, false);
        if (r.children) {
          handleSelect(rowKeys, rowKey, r);
        }
      });
    }
  };

  /**
   * 已选择的数据行发生改变
   *
   * @param rowKeys ID 列表
   */
  const handleSelectionChange = (rowKeys: Array<any>) => {
    ids.value = rowKeys;
    single.value = rowKeys.length !== 1;
    multiple.value = !rowKeys.length;
  };

  /**
   * 导出
   */
  const handleExport = () => {
    if (exportLoading.value) return;
    exportLoading.value = true;
    proxy
      .download('/system/menu/export', { ...queryParams.value }, '菜单数据')
      .finally(() => {
        exportLoading.value = false;
      });
  };

  /**
   * 展开/折叠
   */
  const handleExpandAll = () => {
    expandAll.value = !expandAll.value;
    proxy.$refs.tableRef.expandAll(expandAll.value);
  };

  /**
   * 修改状态
   *
   * @param record 记录信息
   */
  const handleChangeStatus = (record: MenuRecord) => {
    const tip = record.status === 1 ? '启用' : '禁用';
    updateMenu(record)
      .then(() => {
        proxy.$message.success(`${tip}成功`);
      })
      .catch(() => {
        record.status = record.status === 1 ? 2 : 1;
      });
  };

  /**
   * 过滤菜单树
   *
   * @param searchValue 搜索值
   * @param nodeData 节点值
   */
  const filterMenuTree = (searchValue: string, nodeData: TreeNodeData) => {
    if (nodeData.title) {
      return (
        nodeData.title.toLowerCase().indexOf(searchValue.toLowerCase()) > -1
      );
    }
    return false;
  };

  /**
   * 展示下拉图标
   */
  const showSelectIcon = () => {
    proxy.$refs.iconSelectRef.reset();
    showChooseIcon.value = true;
  };

  /**
   * 选择图标
   *
   * @param name 图标名称
   */
  const selected = (name: string) => {
    form.value.icon = name;
    showChooseIcon.value = false;
  };

  const hideSelectIcon = () => {
    showChooseIcon.value = false;
  };

  /**
   * 查询
   */
  const handleQuery = () => {
    getList();
  };

  /**
   * 重置
   */
  const resetQuery = () => {
    proxy.$refs.queryRef.resetFields();
    handleQuery();
  };
</script>

<script lang="ts">
  export default {
    // eslint-disable-next-line vue/no-reserved-component-names
    name: 'Menu',
  };
</script>

<style scoped lang="less"></style>