|
@@ -0,0 +1,227 @@
|
|
1
|
+<template>
|
|
2
|
+ <div>
|
|
3
|
+ <div class="page-bar">
|
|
4
|
+ <el-button icon="el-icon-plus" @click="insertRootMenu">顶级节点</el-button>
|
|
5
|
+ </div>
|
|
6
|
+ <div class="tree-box">
|
|
7
|
+ <div
|
|
8
|
+ v-for="(menus, inx) in treeData"
|
|
9
|
+ :key="inx">
|
|
10
|
+ <el-tree
|
|
11
|
+ :data="[menus]"
|
|
12
|
+ node-key="menuId"
|
|
13
|
+ :check-on-click-node="true"
|
|
14
|
+ :expand-on-click-node="false"
|
|
15
|
+ :default-expand-all="true"
|
|
16
|
+ @node-click="handleNodeClick"
|
|
17
|
+ @node-contextmenu="showContextMenu"
|
|
18
|
+ >
|
|
19
|
+ <el-popover
|
|
20
|
+ trigger="manual"
|
|
21
|
+ placement="right"
|
|
22
|
+ :value="checkMenu.menuId == data.menuId && contextMenuVisible"
|
|
23
|
+ slot-scope="{ data }"
|
|
24
|
+ >
|
|
25
|
+ <div>
|
|
26
|
+ <el-button type="text" icon="el-icon-plus" @click="() => insertSubMenu(data)">子菜单</el-button>
|
|
27
|
+ </div>
|
|
28
|
+ <div>
|
|
29
|
+ <el-button type="text" icon="el-icon-edit" @click="dialogVisible = true">编辑</el-button>
|
|
30
|
+ </div>
|
|
31
|
+ <div>
|
|
32
|
+ <el-button type="text" icon="el-icon-delete" @click="() => deleteNode(data)">删除</el-button>
|
|
33
|
+ </div>
|
|
34
|
+ <span slot="reference" class="custom-tree-node">
|
|
35
|
+ <span>{{data.menuName}} <small>({{data.menuId}})</small></span>
|
|
36
|
+ </span>
|
|
37
|
+ </el-popover>
|
|
38
|
+ </el-tree>
|
|
39
|
+ </div>
|
|
40
|
+ </div>
|
|
41
|
+ <el-dialog
|
|
42
|
+ title="提示"
|
|
43
|
+ :visible.sync="dialogVisible"
|
|
44
|
+ width="30%">
|
|
45
|
+ <el-form :model="checkMenu" label-width="80px">
|
|
46
|
+ <el-form-item label="菜单ID">
|
|
47
|
+ <el-input v-model="checkMenu.menuId" :disabled="!!checkMenu.createDate"></el-input>
|
|
48
|
+ </el-form-item>
|
|
49
|
+ <el-form-item label="菜单名称">
|
|
50
|
+ <el-input v-model="checkMenu.menuName"></el-input>
|
|
51
|
+ </el-form-item>
|
|
52
|
+ </el-form>
|
|
53
|
+ <div slot="footer">
|
|
54
|
+ <el-button @click="dialogVisible = false">取 消</el-button>
|
|
55
|
+ <el-button type="primary" @click="editNode">确 定</el-button>
|
|
56
|
+ </div>
|
|
57
|
+ </el-dialog>
|
|
58
|
+ </div>
|
|
59
|
+</template>
|
|
60
|
+
|
|
61
|
+<script>
|
|
62
|
+import { createNamespacedHelpers } from 'vuex'
|
|
63
|
+const { mapActions: mapMenuActions } = createNamespacedHelpers('sysmenu')
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+export default {
|
|
67
|
+ name: 'SysMenu',
|
|
68
|
+ data () {
|
|
69
|
+ return {
|
|
70
|
+ menusRaw: [],
|
|
71
|
+ menusWithChildren: [],
|
|
72
|
+ menuDetailTPL: {
|
|
73
|
+ menuId: '',
|
|
74
|
+ menuName: '',
|
|
75
|
+ menuPid: '',
|
|
76
|
+ menuParents: '',
|
|
77
|
+ status: 1,
|
|
78
|
+ sortNo: 0,
|
|
79
|
+ },
|
|
80
|
+ checkMenu: {},
|
|
81
|
+ contextMenuVisible: false,
|
|
82
|
+ dialogVisible: false,
|
|
83
|
+ }
|
|
84
|
+ },
|
|
85
|
+ computed: {
|
|
86
|
+ treeData () {
|
|
87
|
+ return (this.menusWithChildren || []).filter(m => m.menuPid === "-1") || []
|
|
88
|
+ },
|
|
89
|
+ },
|
|
90
|
+ created () {
|
|
91
|
+ this.initData()
|
|
92
|
+ },
|
|
93
|
+ methods: {
|
|
94
|
+ ...mapMenuActions([
|
|
95
|
+ 'getAllMenus',
|
|
96
|
+ 'deleteMenu',
|
|
97
|
+ 'insertMenu',
|
|
98
|
+ 'editMenu',
|
|
99
|
+ ]),
|
|
100
|
+ initData () {
|
|
101
|
+ this.getAllMenus().then((menus) => {
|
|
102
|
+ this.menusRaw = menus
|
|
103
|
+ this.menusWithChildren = menus.map((menu) => {
|
|
104
|
+ const parent = menus.filter((m) => m.menuId === menu.menuPid)[0]
|
|
105
|
+ if (parent) {
|
|
106
|
+ parent.children = (parent.children || []).concat(menu)
|
|
107
|
+ }
|
|
108
|
+ return menu
|
|
109
|
+ })
|
|
110
|
+ })
|
|
111
|
+ },
|
|
112
|
+ handleNodeClick (data) {
|
|
113
|
+ this.checkMenu = data
|
|
114
|
+ this.contextMenuVisible = false
|
|
115
|
+ },
|
|
116
|
+ showContextMenu (e, data) {
|
|
117
|
+ this.checkMenu = data
|
|
118
|
+ this.contextMenuVisible = true
|
|
119
|
+ },
|
|
120
|
+ insertRootMenu () {
|
|
121
|
+ this.checkMenu = {
|
|
122
|
+ ...this.menuDetailTPL,
|
|
123
|
+ menuPid: "-1",
|
|
124
|
+ menuParents: "",
|
|
125
|
+ }
|
|
126
|
+ this.dialogVisible = true
|
|
127
|
+ },
|
|
128
|
+ insertSubMenu (menu) {
|
|
129
|
+ const children = menu.children || []
|
|
130
|
+ const lastSortNo = (children[children.length - 1] || {}).sortNo || menu.sortNo
|
|
131
|
+
|
|
132
|
+ this.checkMenu = {
|
|
133
|
+ ...this.menuDetailTPL,
|
|
134
|
+ menuPid: menu.menuId,
|
|
135
|
+ menuParents: menu.menuParents.split(",").filter(x => x).concat(menu.menuId).join(","),
|
|
136
|
+ sortNo: lastSortNo + 1
|
|
137
|
+ }
|
|
138
|
+ this.dialogVisible = true
|
|
139
|
+ },
|
|
140
|
+ editNode () {
|
|
141
|
+ const newMenu = !this.checkMenu.createDate
|
|
142
|
+ const api = newMenu ? this.insertMenu : this.editMenu
|
|
143
|
+ api(this.checkMenu).then(data => {
|
|
144
|
+ this.checkMenu.createDate = data.createDate
|
|
145
|
+ const parent = this.menusWithChildren.filter(m => m.menuId === data.menuPid)[0]
|
|
146
|
+ if (parent) {
|
|
147
|
+ const exists = (parent.children || []).filter(m => m.menuId === data.menuId)[0]
|
|
148
|
+ if (!exists) {
|
|
149
|
+ parent.children = (parent.children || []).concat(data)
|
|
150
|
+ }
|
|
151
|
+ } else {
|
|
152
|
+ this.menusWithChildren = this.menusWithChildren.concat(data)
|
|
153
|
+ }
|
|
154
|
+
|
|
155
|
+ this.contextMenuVisible = false
|
|
156
|
+ this.dialogVisible = false
|
|
157
|
+ this.$notify({
|
|
158
|
+ title: '成功',
|
|
159
|
+ message: '数据更新成功',
|
|
160
|
+ type: 'success'
|
|
161
|
+ });
|
|
162
|
+ }).catch((err) => {
|
|
163
|
+ // this.contextMenuVisible = false
|
|
164
|
+ // this.dialogVisible = false
|
|
165
|
+ this.$notify.error({
|
|
166
|
+ title: '失败',
|
|
167
|
+ message: err.message || err,
|
|
168
|
+ });
|
|
169
|
+ })
|
|
170
|
+ },
|
|
171
|
+ deleteNode (menu) {
|
|
172
|
+ this.$confirm(`确认删除菜单 [${menu.menuName}] ?`).then(() => {
|
|
173
|
+ //
|
|
174
|
+ this.deleteMenu(menu).then(() => {
|
|
175
|
+ const parent = this.menusWithChildren.filter(m => m.menuId === menu.menuPid)[0]
|
|
176
|
+ if (parent) {
|
|
177
|
+ parent.children = (parent.children || []).filter(m => m.menuId !== menu.menuId)
|
|
178
|
+ } else {
|
|
179
|
+ this.menusWithChildren = this.menusWithChildren.filter(m => m.menuId !== menu.menuId)
|
|
180
|
+ }
|
|
181
|
+ this.contextMenuVisible = false
|
|
182
|
+ this.dialogVisible = false
|
|
183
|
+
|
|
184
|
+ this.$notify({
|
|
185
|
+ title: '成功',
|
|
186
|
+ message: '删除菜单成功',
|
|
187
|
+ type: 'success'
|
|
188
|
+ });
|
|
189
|
+ }).catch((err) => {
|
|
190
|
+ this.$notify.error({
|
|
191
|
+ title: '失败',
|
|
192
|
+ message: err.message || err,
|
|
193
|
+ });
|
|
194
|
+ })
|
|
195
|
+ }).catch(() => {
|
|
196
|
+ // 取消删除
|
|
197
|
+ this.contextMenuVisible = false
|
|
198
|
+ })
|
|
199
|
+ },
|
|
200
|
+ },
|
|
201
|
+}
|
|
202
|
+</script>
|
|
203
|
+
|
|
204
|
+<style lang="scss" scoped>
|
|
205
|
+.page-bar {
|
|
206
|
+ padding: 24px 0;
|
|
207
|
+}
|
|
208
|
+
|
|
209
|
+.tree-box {
|
|
210
|
+ display: flex;
|
|
211
|
+
|
|
212
|
+ & > div {
|
|
213
|
+ flex: auto;
|
|
214
|
+
|
|
215
|
+ & + div {
|
|
216
|
+ margin-left: 24px;
|
|
217
|
+ }
|
|
218
|
+ }
|
|
219
|
+}
|
|
220
|
+</style>
|
|
221
|
+
|
|
222
|
+
|
|
223
|
+<style lang="scss">
|
|
224
|
+ .custom-tree-node {
|
|
225
|
+ font-size: 14px;
|
|
226
|
+ }
|
|
227
|
+</style>
|