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