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