<template>
|
<div class="menuSettings_box">
|
<div class="menuSettings_tree">
|
<My-bread :list="['运维管理', '菜单设置']"></My-bread>
|
<el-button class="saveBtn" type="primary" size="mini" @click="sendChange"
|
>保存</el-button
|
>
|
<el-divider />
|
<div class="menuTreeBox">
|
<el-tree
|
ref="tree"
|
:props="defaultProps"
|
node-key="id"
|
:data="menuList"
|
:expand-on-click-node="false"
|
:default-expand-all="true"
|
draggable
|
@node-click="handleNodeClick"
|
@node-drag-start="handleDragStart"
|
@node-drag-end="handleDrop"
|
>
|
<span class="custom-tree-node" slot-scope="{ node, data }">
|
<span>{{ node.label }}</span>
|
</span>
|
</el-tree>
|
</div>
|
</div>
|
<div class="menuSettings">
|
<div class="title_box">
|
<h4>详细信息</h4>
|
</div>
|
<div class="form_box">
|
<el-form :model="itemdetail">
|
<el-form-item label="英文名称" :label-width="formLabelWidth">
|
<el-input v-model="itemdetail.enName" autocomplete="off"></el-input>
|
</el-form-item>
|
<el-form-item label="中文名称" :label-width="formLabelWidth">
|
<el-input v-model="itemdetail.cnName" autocomplete="off"></el-input>
|
</el-form-item>
|
<el-form-item label="图标" :label-width="formLabelWidth">
|
<el-input v-model="itemdetail.icon" autocomplete="off"></el-input>
|
</el-form-item>
|
<el-form-item label="是否显示" :label-width="formLabelWidth">
|
<el-input v-model="itemdetail.isShow" autocomplete="off"></el-input>
|
</el-form-item>
|
<el-form-item label="菜单Url" :label-width="formLabelWidth">
|
<el-input v-model="itemdetail.url" autocomplete="off"></el-input>
|
</el-form-item>
|
<el-form-item label="授权" :label-width="formLabelWidth">
|
<el-input v-model="itemdetail.perms" autocomplete="off"></el-input>
|
</el-form-item>
|
<el-form-item label="类型" :label-width="formLabelWidth">
|
<el-input v-model="itemdetail.type" autocomplete="off"></el-input>
|
</el-form-item>
|
<el-form-item label="备注" :label-width="formLabelWidth">
|
<el-input v-model="itemdetail.bak" autocomplete="off"></el-input>
|
</el-form-item>
|
<div class="btnBox">
|
<el-button type="primary" @click="updMenu">保存</el-button>
|
<el-button type="primary" @click="reset">取消</el-button>
|
</div>
|
</el-form>
|
</div>
|
</div>
|
</div>
|
</template>
|
|
<script>
|
import MyBread from "../../components/MyBread.vue";
|
import {
|
queryMenuTree,
|
updateMenuTree,
|
updateMenuTrees,
|
queryMaxId,
|
} from "../../api/api";
|
export default {
|
//import引入的组件需要注入到对象中才能使用
|
components: {
|
MyBread,
|
},
|
|
data() {
|
return {
|
defaultProps: {
|
children: "children",
|
label: "cnName",
|
},
|
oriData: [], //原始树数据
|
menuList: [], //el树数据
|
old_dirDat: [], //el树数据(拖动前)
|
newData: [], //拖动后原始数据
|
itemdetail: {
|
cnName: "",
|
enName: "",
|
icon: null,
|
isShow: null,
|
perms: null,
|
url: "",
|
type: null,
|
bak: "",
|
},
|
backUpData: {},
|
formLabelWidth: "170px",
|
};
|
},
|
methods: {
|
getMenuTree() {
|
//获取目录树最大ID,新建节点使用
|
// queryMaxId().then((res) => {
|
// this.id = res.data;
|
// });
|
queryMenuTree().then((res) => {
|
if (res.code == 200) {
|
this.menuList = this.treeData(res.result);
|
this.oriData = res.result;
|
this.newData = res.result;
|
} else {
|
console.log("接口报错");
|
}
|
});
|
},
|
treeData(source) {
|
let cloneData = JSON.parse(JSON.stringify(source)); // 对源数据深度克隆
|
return cloneData.filter((father) => {
|
// 循环所有项
|
let branchArr = cloneData.filter((child) => father.id == child.pid); // 对比ID,分别上下级菜单,并返回数据
|
branchArr.length > 0 ? (father.children = branchArr) : ""; // 给父级添加一个children属性,并赋值
|
return father.pid == 1; // 返回一级菜单
|
});
|
},
|
append(data) {
|
this.$prompt("请输入名称", "提示", {
|
confirmButtonText: "确定",
|
cancelButtonText: "取消",
|
})
|
.then(({ value }) => {
|
const newChild = {
|
id: this.id + 1,
|
name: value,
|
pid: data.id,
|
// children: [],
|
orderNum: data.children ? data.children.length + 1 : 1,
|
};
|
this.id = newChild.id; //修改新的最大I
|
console.log(newChild);
|
|
if (!data.children) {
|
this.$set(data, "children", []);
|
}
|
data.children.push(newChild);
|
this.newData.push(newChild);
|
// this.sendChange();
|
})
|
.catch(() => {
|
this.$message({
|
type: "info",
|
message: "取消输入",
|
});
|
});
|
},
|
remove(node, data) {
|
this.$confirm("此操作将删除该节点, 是否继续?", "提示", {
|
confirmButtonText: "确定",
|
cancelButtonText: "取消",
|
type: "warning",
|
})
|
.then(() => {
|
const parent = node.parent;
|
const children = parent.data.children || parent.data;
|
const index = children.findIndex((d) => d.id === data.id);
|
let res = children.splice(index, 1);
|
var std = [];
|
for (var i in res) {
|
std.push(res[i].id);
|
}
|
// deleteDirTree(std);
|
this.getDirTree();
|
this.$message({
|
type: "success",
|
message: "删除成功!",
|
});
|
})
|
.catch(() => {
|
this.$message({
|
type: "info",
|
message: "已取消删除",
|
});
|
});
|
// this.dialogMessage="是否删除"
|
// this.dialogFlag = 1;
|
// this.dialogFrom ={
|
// node:node,
|
// val:data
|
// }
|
// this.dialogVisible=true;//目录树更改弹窗
|
// const parent = node.parent;
|
// const children = parent.data.children || parent.data;
|
// const index = children.findIndex((d) => d.id === data.id);
|
// let res = children.splice(index, 1);
|
// // console.log(res);
|
// // console.log(data);
|
// console.log(this.flaten(res));
|
},
|
flaten(arr) {
|
return arr.reduce((p, v, i) => {
|
for (let i = 0; i < p.length; i++) {
|
if (p[i].children) {
|
delete p[i].children;
|
}
|
}
|
return p.concat(v.children ? this.flaten(v.children).concat(v) : v);
|
}, []);
|
},
|
handleDragStart(node, ev) {
|
this.old_dirDat = JSON.parse(JSON.stringify(this.menuList)); //将备份的dir重新赋值
|
},
|
handleDrop(draggingNode, dropNode, dropType, ev) {
|
this.$confirm("是否调整至该位置?", "提示", {
|
confirmButtonText: "确定",
|
cancelButtonText: "取消",
|
type: "warning",
|
})
|
.then(() => {
|
//父节点
|
let data = dropType != "inner" ? dropNode.parent.data : dropNode.data;
|
// 父节点中全部子节点
|
let nodeData =
|
dropNode.level == 1 && dropType != "inner" ? data : data.children;
|
//变更节点
|
// console.log(nodeData);
|
nodeData.forEach((item, i) => {
|
if (dropType != "inner") {
|
if (draggingNode.data.pid === dropNode.data.pid) {
|
item.pid = item.pid;
|
} else {
|
item.pid = dropNode.data.pid;
|
}
|
} else {
|
item.pid = data.id;
|
}
|
item.orderNum = i + 1;
|
});
|
console.log(nodeData);
|
//更新原始整体数据
|
let arr = [];
|
this.oriData.forEach((e) => {
|
nodeData.forEach((item) => {
|
if (item.id === e.id) e = item;
|
});
|
arr.push(e);
|
});
|
this.newData = arr;
|
})
|
.catch(() => {
|
this.$message({
|
type: "info",
|
message: "已取消更改",
|
});
|
this.menuList = this.old_dirDat; //将备份的dir重新赋值
|
});
|
},
|
sendChange() {
|
updateMenuTrees(this.newData).then((res) => {
|
if (res.code == 200) {
|
alert("更改完成。请及时刷新页面!");
|
return;
|
} else {
|
alert("修改失败,请重试!");
|
}
|
});
|
},
|
handleNodeClick(data) {
|
// console.log(data);
|
this.backUpData = JSON.stringify(data);
|
this.itemdetail = JSON.parse(JSON.stringify(data));
|
},
|
updMenu() {
|
updateMenuTree(this.itemdetail).then((res) => {
|
// console.log(res);
|
if (res.code == 200) {
|
alert("修改完成,请及时刷新页面!");
|
return;
|
} else {
|
alert("修改失败,请重试!");
|
}
|
});
|
},
|
reset() {
|
this.itemdetail = JSON.parse(this.backUpData);
|
},
|
},
|
mounted() {
|
this.getMenuTree();
|
},
|
};
|
</script>
|
<style lang="less" scoped>
|
//@import url(); 引入公共css类
|
.menuSettings_box {
|
// background: rgb(240, 242, 245);
|
border-radius: 10px;
|
height: 100%;
|
padding: 10px;
|
box-sizing: border-box;
|
display: flex;
|
.menuSettings_tree {
|
position: relative;
|
width: 344px;
|
height: 100%;
|
background: rgb(240, 242, 245);
|
padding: 20px;
|
border-radius: 10px;
|
box-sizing: border-box;
|
overflow: auto;
|
.saveBtn {
|
position: absolute;
|
left: 250px;
|
top: 23px;
|
}
|
.menuTreeBox {
|
height: 90%;
|
overflow: auto;
|
.el-tree {
|
background: transparent;
|
font-size: 15px;
|
font-family: Microsoft YaHei;
|
font-weight: 400;
|
color: #000000;
|
// /deep/ .el-tree-node__label {
|
// font-size: 18px;
|
// }
|
/deep/ .el-tree-node {
|
padding-top: 10px;
|
// padding-bottom: 10px;
|
}
|
/deep/ .el-tree-node:focus > .el-tree-node__content {
|
background-color: #b9b9b9;
|
}
|
/deep/ .el-tree-node__content:hover {
|
background-color: rgb(153, 153, 153);
|
}
|
.btnBox {
|
margin-left: 5px;
|
.el-button + .el-button {
|
margin-left: 5px;
|
}
|
}
|
}
|
}
|
}
|
.menuSettings {
|
width: calc(100% - 344px);
|
border-radius: 10px;
|
background: rgb(240, 242, 245);
|
margin-left: 10px;
|
height: 100%;
|
padding: 10px;
|
box-sizing: border-box;
|
.title_box {
|
background: #fff;
|
padding: 10px;
|
margin-bottom: 24px;
|
display: flex;
|
border-radius: 10px;
|
border: 1px solid rgb(202, 201, 204);
|
box-sizing: border-box;
|
}
|
.form_box {
|
border: 1px solid rgb(202, 201, 204);
|
border-radius: 10px;
|
background: #fff;
|
padding-top: 30px;
|
box-sizing: border-box;
|
width: 100%;
|
.el-input {
|
width: 400px;
|
}
|
.btnBox {
|
margin: 0 270px 20px;
|
width: 200px;
|
display: flex;
|
justify-content: space-between;
|
}
|
}
|
}
|
}
|
</style>
|