Newer
Older
KaiFengPC / src / views / system / menu / index.vue
@zhangdeliang zhangdeliang on 3 Sep 16 KB update
  1. <template>
  2. <!-- 菜单管理 -->
  3. <div class="publicContainer menuPage">
  4. <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
  5. <el-form-item label="菜单名称" prop="menuName">
  6. <el-input v-model="queryParams.menuName" placeholder="请输入菜单名称" clearable style="width: 200px" @keyup.enter="handleQuery" />
  7. </el-form-item>
  8. <el-form-item label="状态" prop="status">
  9. <el-select v-model="queryParams.status" placeholder="菜单状态" clearable style="width: 200px">
  10. <el-option v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.label" :value="dict.value" />
  11. </el-select>
  12. </el-form-item>
  13. <el-form-item>
  14. <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
  15. <el-button icon="Refresh" @click="resetQuery">重置</el-button>
  16. </el-form-item>
  17. </el-form>
  18.  
  19. <el-row :gutter="10" class="mb8">
  20. <el-col :span="1.5">
  21. <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:menu:add']">新增</el-button>
  22. </el-col>
  23. <el-col :span="1.5">
  24. <el-button type="info" plain icon="Sort" @click="toggleExpandAll">展开/折叠</el-button>
  25. </el-col>
  26. <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
  27. </el-row>
  28.  
  29. <el-table
  30. v-if="refreshTable"
  31. v-loading="loading"
  32. :data="menuList"
  33. row-key="menuId"
  34. :default-expand-all="isExpandAll"
  35. :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
  36. class="specialLeft"
  37. max-height="800"
  38. >
  39. <el-table-column prop="menuName" label="菜单名称" show-overflow-tooltip width="330"></el-table-column>
  40. <el-table-column prop="icon" label="图标" width="100">
  41. <template #default="scope">
  42. <svg-icon :icon-class="scope.row.icon" />
  43. </template>
  44. </el-table-column>
  45. <el-table-column prop="orderNum" label="排序" width="60"></el-table-column>
  46. <el-table-column prop="perms" label="权限标识" show-overflow-tooltip></el-table-column>
  47. <el-table-column prop="component" label="组件路径" show-overflow-tooltip></el-table-column>
  48. <el-table-column prop="status" label="状态" width="80">
  49. <template #default="scope">
  50. <dict-tag :options="sys_normal_disable" :value="scope.row.status" />
  51. </template>
  52. </el-table-column>
  53. <el-table-column prop="status" label="是否缓存" width="90">
  54. <template #default="scope">
  55. <span>{{ scope.row.isCache == 1 ? '不缓存' : '缓存' }}</span>
  56. </template>
  57. </el-table-column>
  58. <el-table-column label="创建时间" width="170" prop="createTime">
  59. <template #default="scope">
  60. <span>{{ parseTime(scope.row.createTime) }}</span>
  61. </template>
  62. </el-table-column>
  63. <el-table-column label="操作" width="240" class-name="small-padding fixed-width">
  64. <template #default="scope">
  65. <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:menu:edit']">修改</el-button>
  66. <el-button link type="primary" icon="Plus" @click="handleAdd(scope.row)" v-hasPermi="['system:menu:add']">新增</el-button>
  67. <el-button link type="warning" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:menu:remove']">删除</el-button>
  68. </template>
  69. </el-table-column>
  70. </el-table>
  71.  
  72. <!-- 添加或修改菜单对话框 -->
  73. <el-dialog :title="title" v-model="open" width="750px" append-to-body>
  74. <el-form ref="menuRef" :model="form" :rules="rules" label-width="100px">
  75. <el-row>
  76. <el-col :span="24">
  77. <el-form-item label="上级菜单">
  78. <el-tree-select
  79. v-model="form.parentId"
  80. :data="menuOptions"
  81. :props="{
  82. value: 'menuId',
  83. label: 'menuName',
  84. children: 'children',
  85. }"
  86. value-key="menuId"
  87. placeholder="选择上级菜单"
  88. check-strictly
  89. />
  90. </el-form-item>
  91. </el-col>
  92. <el-col :span="24">
  93. <el-form-item label="菜单类型" prop="menuType">
  94. <el-radio-group v-model="form.menuType">
  95. <el-radio label="M">目录</el-radio>
  96. <el-radio label="C">菜单</el-radio>
  97. <el-radio label="F">按钮</el-radio>
  98. </el-radio-group>
  99. </el-form-item>
  100. </el-col>
  101. <el-col :span="24">
  102. <el-form-item label="所属菜单" prop="moduleKey">
  103. <el-select v-model="form.moduleKey" placeholder="请选择所属菜单" clearable style="width: 240px">
  104. <el-option v-for="dict in sysModuleList" :key="dict.moduleKey" :label="dict.moduleName" :value="dict.moduleKey" />
  105. </el-select>
  106. </el-form-item>
  107. </el-col>
  108. <el-col :span="24" v-if="form.menuType != 'F'">
  109. <el-form-item label="菜单图标" prop="icon">
  110. <el-popover placement="bottom-start" :width="540" v-model:visible="showChooseIcon" trigger="click" @show="showSelectIcon">
  111. <template #reference>
  112. <el-input v-model="form.icon" placeholder="点击选择图标" @blur="showSelectIcon" v-click-outside="hideSelectIcon" readonly>
  113. <template #prefix>
  114. <svg-icon v-if="form.icon" :icon-class="form.icon" class="el-input__icon" style="height: 32px; width: 16px" />
  115. <el-icon v-else style="height: 32px; width: 16px"><search /></el-icon>
  116. </template>
  117. </el-input>
  118. </template>
  119. <icon-select ref="iconSelectRef" @selected="selected" />
  120. </el-popover>
  121. </el-form-item>
  122. </el-col>
  123. <el-col :span="12">
  124. <el-form-item label="菜单名称" prop="menuName">
  125. <el-input v-model="form.menuName" placeholder="请输入菜单名称" />
  126. </el-form-item>
  127. </el-col>
  128. <el-col :span="12">
  129. <el-form-item label="显示排序" prop="orderNum">
  130. <el-input-number v-model="form.orderNum" controls-position="right" :min="0" />
  131. </el-form-item>
  132. </el-col>
  133. <el-col :span="12" v-if="form.menuType != 'F'">
  134. <el-form-item>
  135. <template #label>
  136. <span>
  137. <el-tooltip content="选择是外链则路由地址需要以`http(s)://`开头" placement="top">
  138. <el-icon><question-filled /></el-icon>
  139. </el-tooltip>
  140. 是否外链
  141. </span>
  142. </template>
  143. <el-radio-group v-model="form.isFrame">
  144. <el-radio label="0"></el-radio>
  145. <el-radio label="1"></el-radio>
  146. </el-radio-group>
  147. </el-form-item>
  148. </el-col>
  149. <el-col :span="12" v-if="form.menuType != 'F'">
  150. <el-form-item prop="path">
  151. <template #label>
  152. <el-tooltip content="访问的路由地址,如:`user`,如外网地址需内链访问则以`http(s)://`开头" placement="top">
  153. <el-icon><question-filled /></el-icon>
  154. </el-tooltip>
  155. 路由地址
  156. </template>
  157. <el-input v-model="form.path" placeholder="请输入路由地址" />
  158. </el-form-item>
  159. </el-col>
  160. <el-col :span="24" v-if="form.menuType == 'C'">
  161. <el-form-item prop="component">
  162. <template #label>
  163. <span>
  164. <el-tooltip content="访问的组件路径,如:`system/user/index`,默认在`views`目录下" placement="top">
  165. <el-icon><question-filled /></el-icon>
  166. </el-tooltip>
  167. 组件路径
  168. </span>
  169. </template>
  170. <el-input v-model="form.component" placeholder="请输入组件路径" />
  171. </el-form-item>
  172. </el-col>
  173. <el-col :span="24" v-if="form.menuType != 'M'">
  174. <el-form-item>
  175. <el-input v-model="form.perms" placeholder="请输入权限标识" maxlength="100" />
  176. <template #label>
  177. <span>
  178. <el-tooltip content="控制器中定义的权限字符,如:@PreAuthorize(`@ss.hasPermi('system:user:list')`)" placement="top">
  179. <el-icon><question-filled /></el-icon>
  180. </el-tooltip>
  181. 权限字符
  182. </span>
  183. </template>
  184. </el-form-item>
  185. </el-col>
  186. <el-col :span="12" v-if="form.menuType == 'C'">
  187. <el-form-item>
  188. <el-input v-model="form.query" placeholder="请输入路由参数" maxlength="255" />
  189. <template #label>
  190. <span>
  191. <el-tooltip content='访问路由的默认传递参数,如:`{"id": 1, "name": "ry"}`' placement="top">
  192. <el-icon><question-filled /></el-icon>
  193. </el-tooltip>
  194. 路由参数
  195. </span>
  196. </template>
  197. </el-form-item>
  198. </el-col>
  199. <el-col :span="12" v-if="form.menuType == 'C'">
  200. <el-form-item>
  201. <template #label>
  202. <span>
  203. <el-tooltip content="选择是则会被`keep-alive`缓存,需要匹配组件的`name`和地址保持一致" placement="top">
  204. <el-icon><question-filled /></el-icon>
  205. </el-tooltip>
  206. 是否缓存
  207. </span>
  208. </template>
  209. <el-radio-group v-model="form.isCache">
  210. <el-radio label="0">缓存</el-radio>
  211. <el-radio label="1">不缓存</el-radio>
  212. </el-radio-group>
  213. </el-form-item>
  214. </el-col>
  215. <el-col :span="12" v-if="form.menuType != 'F'">
  216. <el-form-item>
  217. <template #label>
  218. <span>
  219. <el-tooltip content="选择隐藏则路由将不会出现在侧边栏,但仍然可以访问" placement="top">
  220. <el-icon><question-filled /></el-icon>
  221. </el-tooltip>
  222. 显示状态
  223. </span>
  224. </template>
  225. <el-radio-group v-model="form.visible">
  226. <el-radio v-for="dict in sys_show_hide" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
  227. </el-radio-group>
  228. </el-form-item>
  229. </el-col>
  230. <el-col :span="12" v-if="form.menuType != 'F'">
  231. <el-form-item>
  232. <template #label>
  233. <span>
  234. <el-tooltip content="选择停用则路由将不会出现在侧边栏,也不能被访问" placement="top">
  235. <el-icon><question-filled /></el-icon>
  236. </el-tooltip>
  237. 菜单状态
  238. </span>
  239. </template>
  240. <el-radio-group v-model="form.status">
  241. <el-radio v-for="dict in sys_normal_disable" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
  242. </el-radio-group>
  243. </el-form-item>
  244. </el-col>
  245. <el-col :span="24">
  246. <el-form-item label="菜单描述" prop="description">
  247. <el-input type="textarea" v-model="form.description" placeholder="请输入菜单描述" />
  248. </el-form-item>
  249. </el-col>
  250. </el-row>
  251. </el-form>
  252. <template #footer>
  253. <div class="dialog-footer">
  254. <el-button type="info" @click="cancel">取 消</el-button>
  255. <el-button type="primary" @click="submitForm">确 定</el-button>
  256. </div>
  257. </template>
  258. </el-dialog>
  259. </div>
  260. </template>
  261.  
  262. <script setup name="Menu">
  263. import { addMenu, delMenu, getMenu, listMenu, updateMenu } from '@/api/system/menu';
  264. import { listsysModule } from '@/api/system/sysModule';
  265. import SvgIcon from '@/components/SvgIcon';
  266. import IconSelect from '@/components/IconSelect';
  267. import { ClickOutside as vClickOutside } from 'element-plus';
  268.  
  269. const { proxy } = getCurrentInstance();
  270. const { sys_show_hide, sys_normal_disable } = proxy.useDict('sys_show_hide', 'sys_normal_disable');
  271.  
  272. const menuList = ref([]);
  273. const open = ref(false);
  274. const loading = ref(true);
  275. const showSearch = ref(true);
  276. const title = ref('');
  277. const menuOptions = ref([]);
  278. const isExpandAll = ref(false);
  279. const refreshTable = ref(true);
  280. const showChooseIcon = ref(false);
  281. const iconSelectRef = ref(null);
  282. const sysModuleList = ref([]);
  283. const data = reactive({
  284. form: {},
  285. queryParams: {
  286. menuName: undefined,
  287. visible: undefined,
  288. },
  289. rules: {
  290. menuName: [{ required: true, message: '菜单名称不能为空', trigger: 'blur' }],
  291. orderNum: [{ required: true, message: '菜单顺序不能为空', trigger: 'blur' }],
  292. path: [{ required: true, message: '路由地址不能为空', trigger: 'blur' }],
  293. moduleKey: [{ required: true, message: '所属菜单不能为空', trigger: 'blur' }],
  294. },
  295. });
  296.  
  297. const { queryParams, form, rules } = toRefs(data);
  298.  
  299. /** 搜索菜单列表 */
  300. function getList() {
  301. loading.value = true;
  302. listMenu(queryParams.value).then(response => {
  303. menuList.value = proxy.handleTree(response.data, 'menuId');
  304. loading.value = false;
  305. });
  306. }
  307. //获取所属菜单
  308. function getSysModuleList() {
  309. listsysModule().then(res => {
  310. sysModuleList.value = res.data;
  311. });
  312. }
  313. /** 搜索菜单下拉树结构 */
  314. function getTreeselect() {
  315. menuOptions.value = [];
  316. listMenu().then(response => {
  317. const menu = { menuId: 0, menuName: '主类目', children: [] };
  318. menu.children = proxy.handleTree(response.data, 'menuId');
  319. menuOptions.value.push(menu);
  320. });
  321. }
  322. /** 取消按钮 */
  323. function cancel() {
  324. open.value = false;
  325. reset();
  326. }
  327. /** 表单重置 */
  328. function reset() {
  329. form.value = {
  330. menuId: undefined,
  331. parentId: 0,
  332. menuName: undefined,
  333. icon: undefined,
  334. menuType: 'M',
  335. orderNum: undefined,
  336. moduleKey: undefined,
  337. isFrame: '1',
  338. isCache: '1',
  339. visible: '0',
  340. status: '0',
  341. };
  342. proxy.resetForm('menuRef');
  343. }
  344. /** 展示下拉图标 */
  345. function showSelectIcon() {
  346. iconSelectRef.value.reset();
  347. showChooseIcon.value = true;
  348. }
  349. /** 选择图标 */
  350. function selected(name) {
  351. form.value.icon = name;
  352. showChooseIcon.value = false;
  353. }
  354. /** 图标外层点击隐藏下拉列表 */
  355. function hideSelectIcon(event) {
  356. var elem = event.relatedTarget || event.srcElement || event.target || event.currentTarget;
  357. var className = elem.className;
  358. if (className !== 'el-input__inner') {
  359. showChooseIcon.value = false;
  360. }
  361. }
  362. /** 搜索按钮操作 */
  363. function handleQuery() {
  364. getList();
  365. }
  366. /** 重置按钮操作 */
  367. function resetQuery() {
  368. proxy.resetForm('queryRef');
  369. handleQuery();
  370. }
  371. /** 新增按钮操作 */
  372. function handleAdd(row) {
  373. reset();
  374. getTreeselect();
  375. if (row != null && row.menuId) {
  376. form.value.parentId = row.menuId;
  377. } else {
  378. form.value.parentId = 0;
  379. }
  380. open.value = true;
  381. title.value = '添加菜单';
  382. }
  383. /** 展开/折叠操作 */
  384. function toggleExpandAll() {
  385. refreshTable.value = false;
  386. isExpandAll.value = !isExpandAll.value;
  387. nextTick(() => {
  388. refreshTable.value = true;
  389. });
  390. }
  391. /** 修改按钮操作 */
  392. async function handleUpdate(row) {
  393. reset();
  394. await getTreeselect();
  395. getMenu(row.menuId).then(response => {
  396. form.value = response.data;
  397. open.value = true;
  398. title.value = '修改菜单';
  399. });
  400. }
  401. /** 提交按钮 */
  402. function submitForm() {
  403. proxy.$refs['menuRef'].validate(valid => {
  404. if (valid) {
  405. if (form.value.menuId != undefined) {
  406. updateMenu(form.value).then(response => {
  407. proxy.$modal.msgSuccess('修改成功');
  408. open.value = false;
  409. getList();
  410. });
  411. } else {
  412. addMenu(form.value).then(response => {
  413. proxy.$modal.msgSuccess('新增成功');
  414. open.value = false;
  415. getList();
  416. });
  417. }
  418. }
  419. });
  420. }
  421. /** 删除按钮操作 */
  422. function handleDelete(row) {
  423. proxy.$modal
  424. .confirm('是否确认删除名称为"' + row.menuName + '"的数据项?')
  425. .then(function () {
  426. return delMenu(row.menuId);
  427. })
  428. .then(() => {
  429. getList();
  430. proxy.$modal.msgSuccess('删除成功');
  431. })
  432. .catch(() => {});
  433. }
  434.  
  435. getList();
  436. getSysModuleList();
  437. </script>
  438.  
  439. <style lang="scss">
  440. .menuPage {
  441. .specialLeft {
  442. th:nth-of-type(1).el-table__cell,
  443. td:nth-of-type(1).el-table__cell {
  444. text-align: left;
  445. }
  446. }
  447. }
  448. </style>