<template>
|
<div class="app-container">
|
<el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
|
<el-form-item label="菜单名称" prop="name">
|
<el-input
|
v-model="queryParams.name"
|
placeholder="请输入菜单名称"
|
clearable
|
style="width: 200px"
|
@keyup.enter="handleQuery"
|
/>
|
</el-form-item>
|
<el-form-item>
|
<el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
|
<el-button icon="Refresh" @click="resetQuery">重置</el-button>
|
</el-form-item>
|
</el-form>
|
|
<el-row :gutter="10" class="mb8">
|
<el-col :span="1.5">
|
<el-button
|
type="primary"
|
plain
|
icon="Plus"
|
@click="handleAdd"
|
>新增
|
</el-button>
|
</el-col>
|
<el-col :span="1.5">
|
<el-button
|
type="info"
|
plain
|
icon="Sort"
|
@click="toggleExpandAll"
|
>展开/折叠
|
</el-button>
|
</el-col>
|
</el-row>
|
|
<el-table
|
v-if="refreshTable"
|
v-loading="loading"
|
:data="menuList"
|
row-key="id"
|
align="center"
|
:default-expand-all="isExpandAll"
|
:tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
|
:fit="true"
|
>
|
<el-table-column prop="name" label="菜单名称" :show-overflow-tooltip="true"></el-table-column>
|
<el-table-column prop="icon" label="图标" align="center">
|
<template #default="scope">
|
<svg-icon :icon-class="scope.row.icon"/>
|
</template>
|
</el-table-column>
|
<el-table-column prop="sort" label="排序"></el-table-column>
|
<el-table-column label="操作" align="center" class-name="small-padding fixed-width">
|
<template #default="scope">
|
<el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
|
v-has="['update',route]">修改
|
</el-button>
|
<el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-has="['add',route]">
|
新增
|
</el-button>
|
<el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
|
v-has="['delete',route]">删除
|
</el-button>
|
</template>
|
</el-table-column>
|
</el-table>
|
|
<!-- 添加或修改菜单对话框 -->
|
<el-dialog :title="title" v-model="open" append-to-body center>
|
<el-form class="form-box" ref="menuRef" :model="form" :rules="rules" label-width='auto'>
|
<el-form-item label="上级菜单">
|
<el-tree-select
|
v-model="form.parentId"
|
:data="menuOptions"
|
:props="{ value: 'id', label: 'name', children: 'children' }"
|
value-key="id"
|
placeholder="选择上级菜单"
|
check-strictly
|
/>
|
</el-form-item>
|
<el-form-item label="菜单标题" prop="title">
|
<el-input v-model="form.title" placeholder="请输入菜单标题"/>
|
</el-form-item>
|
<el-form-item label="菜单名称" prop="name">
|
<el-input v-model="form.name" placeholder="请输入菜单名称"/>
|
</el-form-item>
|
<el-form-item label="菜单图标" prop="icon">
|
<el-popover
|
placement="bottom-start"
|
:width="540"
|
trigger="click"
|
>
|
<template #reference>
|
<el-input v-model="form.icon" placeholder="点击选择图标" @blur="showSelectIcon"
|
readonly>
|
<template #prefix>
|
<svg-icon
|
v-if="form.icon"
|
:icon-class="form.icon"
|
class="el-input__icon"
|
style="height: 32px;width: 16px;"
|
/>
|
<el-icon v-else style="height: 32px;width: 16px;">
|
<search/>
|
</el-icon>
|
</template>
|
</el-input>
|
</template>
|
<icon-select ref="iconSelectRef" @selected="selected" :active-icon="form.icon"/>
|
</el-popover>
|
</el-form-item>
|
<el-form-item label="前端资源路径" prop="url">
|
<el-input v-model="form.url" placeholder="请输入前端资源路径"/>
|
</el-form-item>
|
<el-form-item label="类型" placeholder="请选择菜单类型" prop="type">
|
<el-select v-model="form.type">
|
<el-option
|
v-for="(item,index) in typeList"
|
:label="item.name"
|
:value="item.value"
|
:key="index"
|
></el-option>
|
</el-select>
|
</el-form-item>
|
<el-form-item label="显示排序" prop="sort">
|
<el-input-number v-model="form.sort" controls-position="right" :min="0"/>
|
</el-form-item>
|
<el-form-item></el-form-item>
|
</el-form>
|
<template #footer>
|
<div class="dialog-footer">
|
<el-button type="primary" @click="submitForm">确 定</el-button>
|
<el-button @click="cancel">取 消</el-button>
|
</div>
|
</template>
|
</el-dialog>
|
</div>
|
</template>
|
|
<script setup name="Menu">
|
import menu from "@/api/system/menu";
|
import {DictType} from "@/api/system/dict";
|
import SvgIcon from "@/components/SvgIcon";
|
import IconSelect from "@/components/IconSelect";
|
import {useRoute} from 'vue-router';
|
|
const {proxy} = getCurrentInstance();
|
let route = useRoute()
|
const menuList = ref([]);
|
const open = ref(false);
|
const loading = ref(false);
|
const showSearch = ref(true);
|
const title = ref("");
|
const menuOptions = ref([]);
|
const isExpandAll = ref(false);
|
const refreshTable = ref(true);
|
const iconSelectRef = ref(null);
|
|
const data = reactive({
|
form: {},
|
queryParams: {
|
name: undefined,
|
},
|
rules: {
|
name: [{required: true, message: "菜单名称不能为空", trigger: "blur"}],
|
title: [{required: true, message: "菜单标题不能为空", trigger: "blur"}],
|
url: [{required: true, message: "前端资源路径不能为空", trigger: "blur"}],
|
icon: [{required: true, message: "请选择菜单图标", trigger: "blur"}]
|
},
|
});
|
|
const {queryParams, form, rules} = toRefs(data);
|
|
/**
|
* 搜索相关
|
*/
|
/** 搜索按钮操作 */
|
function handleQuery() {
|
getList({keywords: queryParams.value.name, page: 1})
|
}
|
|
/** 重置按钮操作 */
|
function resetQuery() {
|
proxy.resetForm("queryRef");
|
handleQuery();
|
}
|
|
/**
|
* Table表格菜单数据列表相关
|
*/
|
|
/** 获取菜单列表 */
|
async function getList(val) {
|
loading.value = true;
|
if (val) {
|
val.limit = 100
|
} else {
|
val = {limit: 100}
|
}
|
await menu().get(val).then((res) => {
|
menuList.value = proxy.handleTree(res.data.list, "id");
|
})
|
loading.value = false;
|
}
|
|
/** 展开/折叠table操作 */
|
function toggleExpandAll() {
|
refreshTable.value = false;
|
isExpandAll.value = !isExpandAll.value;
|
nextTick(() => {
|
refreshTable.value = true;
|
});
|
}
|
|
/** 新增按钮操作 */
|
async function handleAdd(row) {
|
reset();
|
form.value = {
|
name: "",
|
title: '',
|
url: "",
|
icon: "",
|
sort: 1,
|
parentId: "",
|
};
|
let res = await menu().getListTree()
|
menuOptions.value = res.data
|
if (row != null && row.id) {
|
form.value.parentId = row.id;
|
} else {
|
form.value.parentId = "";
|
}
|
open.value = true;
|
title.value = "添加菜单";
|
}
|
|
/** 修改按钮操作 */
|
async function handleUpdate(row) {
|
reset();
|
form.value = Object.assign({}, row)
|
form.value.type = form.value.type.toString()
|
if (form.value.parentId == 0) {
|
form.value.parentId = ""
|
}
|
await menu().getListTree().then((res) => {
|
menuOptions.value = res.data
|
})
|
console.log('formmodify', form.value);
|
open.value = true;
|
title.value = "修改菜单";
|
}
|
|
/** 删除按钮操作 */
|
function handleDelete(row) {
|
proxy.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项?').then(function () {
|
return menu().remove(row.id);
|
}).then(() => {
|
getList();
|
proxy.$modal.msgSuccess("删除成功");
|
}).catch(() => {
|
});
|
}
|
|
|
/**
|
* 新增/修改弹窗Form表单相关
|
*/
|
|
/** icon下拉图标 */
|
function showSelectIcon() {
|
iconSelectRef.value.reset();
|
}
|
|
/** 选中图标 */
|
function selected(name) {
|
form.value.icon = name;
|
}
|
|
//获取菜单类型
|
const typeList = ref()
|
const getType = async () => {
|
await DictType().getEnum('EMenuType').then((res) => {
|
typeList.value = res.data
|
console.log('typeList.value ', typeList.value);
|
})
|
}
|
|
/** 提交按钮 */
|
function submitForm() {
|
proxy.$refs["menuRef"].validate(valid => {
|
if (valid) {
|
if (form.value.parentId == "") {
|
form.value.parentId = 0
|
}
|
if (form.value.id != undefined) {
|
menu().modify(form.value).then(res => {
|
proxy.$modal.msgSuccess("修改成功");
|
open.value = false;
|
getList();
|
}).catch(() => {
|
open.value = false;
|
proxy.$modal.msgError("修改失败");
|
});
|
} else {
|
menu().create(form.value).then(res => {
|
proxy.$modal.msgSuccess("新增成功");
|
open.value = false;
|
getList();
|
}).catch(() => {
|
open.value = false;
|
proxy.$modal.msgError("新增失败");
|
});
|
;
|
}
|
}
|
});
|
}
|
|
/** 取消按钮 */
|
function cancel() {
|
open.value = false;
|
reset();
|
}
|
|
/** 表单重置 */
|
function reset() {
|
form.value = {
|
id: undefined,
|
type: undefined,
|
parentId: 0,
|
name: undefined,
|
icon: undefined,
|
sort: undefined,
|
url: undefined,
|
};
|
proxy.resetForm("menuRef");
|
}
|
|
getType()
|
getList();
|
</script>
|
|