15815213711
2025-07-29 4c82fc90c9942245f1bd787d820d3738f1a6bc89
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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
<template>
  <basicContainer>
    <avue-crud
        :option="option"
        :table-loading="pageF.loading"
        :data="tableData"
        :page="page"
        :permission="permissionList"
        :before-open="beforeOpen"
        v-model="form"
        ref="crudRef"
        @row-update="rowUpdate"
        @row-save="rowSave"
        @row-del="rowDel"
        @search-change="searchChange"
        @search-reset="searchReset"
        @selection-change="selectionChange"
        @current-change="currentChange"
        @size-change="sizeChange"
        @refresh-change="refreshChange"
        @on-load="onLoad"
    >
      <template #menu-left>
        <el-button
            type="info"
            plain
            icon="Sort"
            @click="toggleExpandAll"
        >展开/折叠
        </el-button>
      </template>
      <template #menu-before="scope">
        <el-link
            type="primary"
            icon="Plus" :underline="false"
            size="small" v-hasPermi="['system:menu:add']"
            @click.stop="handleAdd(scope.row)"
        ><span style="margin-left: 5px">新 增</span>
        </el-link>
      </template>
      <template #status="scope">
        <dict-tag v-if="scope.dic" :options="scope.dic" :value="scope.row.status" />
      </template>
      <template #icon="scope">
        <svg-icon :icon-class="scope.row.icon"/>
      </template>
      <template #icon-form>
        <el-popover
            placement="bottom-start"
            :width="460"
            trigger="click"
            @show="showSelectIcon"
        >
          <template #reference>
            <el-input v-model="form.icon" placeholder="点击选择图标" @click="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"/>
        </el-popover>
      </template>
    </avue-crud>
  </basicContainer>
</template>
 
<script setup name="menu" lang="ts">
import {MenuI, addMenu, delMenu, exportMenu, getMenu, listMenu, updateMenu} from "@/api/system/menu";
import useCurrentInstance from "@/utils/useCurrentInstance";
import {computed, reactive, ref, toRefs} from "vue";
import {PagesInterface, PageQueryInterface} from "@/utils/globalInterface";
import {usePagePlus} from "@/hooks/usePagePlus";
import {hasPermission} from "@/utils/permissionUtils";
import SvgIcon from "@/components/SvgIcon/index.vue";
import IconSelect from "@/components/IconSelect/index.vue";
 
 
const {proxy} = useCurrentInstance();
const crudRef = ref();
 
const permissionList = computed(() => {
  return {
    addBtn: hasPermission(["system:menu:add"]),
    delBtn: hasPermission(["system:menu:remove"]),
    editBtn: hasPermission(["system:menu:edit"]),
    viewBtn: hasPermission(["system:menu:query"]),
  }
})
 
const data = reactive({
  form: <MenuI>{},
  queryParams: <MenuI & PageQueryInterface>{},
  page: <PagesInterface>{
    pageSize: 10,
    total: 0,
    currentPage: 1,
  },
  selectionList: [],
  rowAdd: false,
})
const {queryParams, form, page, selectionList, rowAdd} = toRefs(data);
 
 
 
const option = ref({
  pageKey: 'menu',
  rowKey: 'menuId',
  rowParentKey: 'parentId',
  tree: true,
  selection: false,
  defaultExpandAll: false,
  viewBtn: false,
  column: {
 
    parentId: {
      label: '上级菜单', value: 0, hide: true, type: 'tree', row: true,
      addDisabled: false,
      dicData: [],
      rules: [
        {required: true, message: "上级菜单不能为空", trigger: "blur"}
      ],
      props: {
        label: "menuName",
        value: "menuId"
      },
    },
    menuType: {
      label: '菜单类型', value: "M", hide: true, type: 'radio',
      dicData: [
        {dictLabel: '目录', dictValue: 'M',},
        {dictLabel: '菜单', dictValue: 'C',},
        {dictLabel: '按钮', dictValue: 'F',},
      ],
      change: ({value}: { value: string }) => {
        // 定义一个默认的显示状态对象
        const defaultDisplayState = {
          perms: false,
          status: false,
          isCache: false,
          component: false,
          icon: false,
        };
 
        // 初始化所有属性为默认的显示状态
        const displayState = {...defaultDisplayState};
 
        // 根据不同的 value 更新显示状态
        if (value === 'M') {
          Object.assign(displayState, {
            status: true,
            icon: true,
          });
        } else if (value === 'C') {
          Object.assign(displayState, {
            isCache: true,
            status: true,
            perms: true,
            component: true,
            icon: true,
          });
        } else if (value === 'F') {
          Object.assign(displayState, {
            perms: true,
            component: true,
            isCache: true,
          });
        }
 
        // 更新 option.value.column 的显示状态
        Object.keys(displayState).forEach((key: string) => {
          //@ts-ignore
          option.value.column[key].display = displayState[key];
        });
      }
 
    },
    menuName: {
      label: '菜单名称', width: 180,search: true,
      rules: [
        {
          required: true,
          message: "菜单名称不能为空", trigger: "blur"
        }
      ],
    },
    icon: {
      label: '菜单图标', width: 80, display: true,
 
    },
    orderNum: {
      label: '显示顺序', width: 80, type: 'number',
      min: 0, value: 1,
      rules: [
        {required: true, message: "菜单顺序不能为空", trigger: "blur"}
      ]
    },
 
    isFrame: {
      label: '是否外链', value: "1", hide: true, display: true,
      type: 'radio',
      tip: '选择是外链则路由地址需要以`http(s)://`开头',
      dicData: [
        {dictLabel: '是', dictValue: '0',},
        {dictLabel: '否', dictValue: '1',},
      ],
    },
    path: {
      label: '路由地址', hide: true, display: true,
      tip: '访问的路由地址,如:`user`,如外网地址需内链访问则以`http(s)://`开头',
      rules: [
        {
          required: true,
          message: "路由地址不能为空", trigger: "blur"
        }
      ],
    },
 
    query: {
      label: '路由参数', hide: true, display: true,
      tip: '访问路由的默认传递参数,如:`{"id": 1, "name": "ry"}`'
    },
    component: {
      label: '组件路径', display: true,
      tip: '访问的组件路径,如:`system/user/index`,默认在`views`目录下'
    },
    perms: {
      label: '权限标识', display: true,
      tip: '控制器中定义的权限字符,如:@PreAuthorize(`@ss.hasPermi(\'system:user:list\')`)'
    },
 
    isCache: {
      label: '是否缓存', value: "0", hide: true, display: true,
      type: 'radio',
      tip: '选择是则会被`keep-alive`缓存,需要匹配组件的`name`和地址保持一致',
      dicData: [
        {dictLabel: '缓存', dictValue: '0',},
        {dictLabel: '不缓存', dictValue: '1',},
      ],
    },
 
    visible: {
      tip: '选择隐藏则路由将不会出现在侧边栏,但仍然可以访问',
      label: '显示状态', value: "0", hide: true, display: true,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_show_hide',
 
    },
    status: {
      tip: '选择停用则路由将不会出现在侧边栏,也不能被访问',
      label: '菜单状态', value: "0", width: 80, display: true,search: true,
      type: 'radio', dicUrl: '/system/dict/data/type/sys_normal_disable',
    },
 
 
    createTime: {
      label: '创建时间', width: 180, addDisplay: false, editDisplay: false
    },
 
  }
})
 
const {
  tableData, pageF, rowSave, rowUpdate, rowDel, beforeOpen, searchChange,
  searchReset, selectionChange, onLoad, currentChange, sizeChange, handleDelete, handleExport, handleUpdate,refreshChange
} = usePagePlus({
  form: form,
  option:option,
  queryParams: queryParams,
  idKey: 'menuId',
  page: page.value,
  getListApi: listMenu,
  getDetailApi: getMenu,
  exportApi: exportMenu,
  deleteApi: delMenu,
  addApi: addMenu,
  updateApi: updateMenu,
  handleUpdateFunc: () => {
    crudRef.value.rowEdit(selectionList.value[0]);
  },
  handleSelectionChangeFunc: (selection: any) => {
    selectionList.value = selection;
  },
  getListFunc: (res: any) => {
    tableData.value = proxy.handleTree(res.data, "menuId");
  },
  handleBeforeOpenFunc: () => {
    listMenu().then(res => {
      res.data.push({menuId: 0, menuName: '主类目'});
      option.value.column.parentId.dicData = proxy.handleTree(res.data, "menuId");
    })
  }
})
 
 
/** 展开/折叠操作 */
function toggleExpandAll() {
  option.value.defaultExpandAll = !option.value.defaultExpandAll;
  crudRef.value.doLayout();
  crudRef.value.refreshTable();
}
 
const handleAdd = (row: any) => {
  option.value.column.parentId.value = row.menuId;
  option.value.column.parentId.addDisabled = true;
  form.value.parentId = row.menuId;
  rowAdd.value = true;
  crudRef.value.rowAdd();
}
 
const iconSelectRef = ref();
const showSelectIcon = ()=>{
  iconSelectRef.value.reset();
}
const selected = (name: string )=>{
  form.value.icon = name;
}
 
</script>