menu-edit.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <script setup lang="ts">
  2. import { reactive, ref } from "vue";
  3. import {
  4. MenuTypeOptions,
  5. ifNumberOptions,
  6. ifOptions,
  7. ifEnableOptions
  8. } from "@/constants/constants";
  9. import * as permissionApi from "@/api/sys/permission";
  10. import FromQuestion from "@/components/form-question/index.vue";
  11. import { IconSelect } from "@/components/icon-select";
  12. import type { FormInstance } from "element-plus";
  13. import { message } from "@/utils/message";
  14. import { cloneDeep } from "@pureadmin/utils";
  15. const formRef = ref<FormInstance>();
  16. const pageData: any = reactive({
  17. isShow: false,
  18. title: "新增菜单/权限",
  19. formLoading: false,
  20. treeMenus: [],
  21. menuIcon: "",
  22. formData: {
  23. menuType: 0,
  24. isFrame: 0,
  25. showLink: true,
  26. enable: true,
  27. keepAlive: false,
  28. showParent: true
  29. },
  30. formRules: {},
  31. dirFormRules: {
  32. menuType: [
  33. { required: true, message: "请选择菜单类型", trigger: "change" }
  34. ],
  35. title: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
  36. rank: [{ required: true, message: "请输入排序", trigger: "blur" }],
  37. path: [{ required: true, message: "请输入路由地址", trigger: "blur" }]
  38. },
  39. menuFormRules: {
  40. menuType: [
  41. { required: true, message: "请选择菜单类型", trigger: "change" }
  42. ],
  43. title: [{ required: true, message: "请输入菜单名称", trigger: "blur" }],
  44. rank: [{ required: true, message: "请输入排序", trigger: "blur" }],
  45. path: [{ required: true, message: "请输入路由地址", trigger: "blur" }]
  46. },
  47. permissionFormRules: {
  48. menuType: [
  49. { required: true, message: "请选择菜单类型", trigger: "change" }
  50. ],
  51. title: [{ required: true, message: "请输入权限名称", trigger: "blur" }],
  52. perms: [{ required: true, message: "请输入权限标识", trigger: "blur" }],
  53. rank: [{ required: true, message: "请输入排序", trigger: "blur" }]
  54. }
  55. });
  56. const emits = defineEmits(["close", "ok"]);
  57. const open = (action: "save" | "update", data?: any, title?: string) => {
  58. pageData.title = title;
  59. const menuType = data?.menuType | 0;
  60. if (menuType === 0) {
  61. pageData.formRules = pageData.dirFormRules;
  62. } else if (menuType === 1) {
  63. pageData.formRules = pageData.menuFormRules;
  64. } else {
  65. pageData.formRules = pageData.permissionFormRules;
  66. }
  67. queryTreeMenu();
  68. switch (action) {
  69. case "save":
  70. pageData.formData = {
  71. menuType: 0,
  72. isFrame: 0,
  73. showLink: true,
  74. enable: true,
  75. keepAlive: false,
  76. showParent: true
  77. };
  78. if (data) {
  79. pageData.formData = Object.assign(pageData.formData, data);
  80. }
  81. break;
  82. case "update":
  83. pageData.formData = cloneDeep(data);
  84. break;
  85. }
  86. pageData.isShow = true;
  87. };
  88. /**
  89. * 查询菜单树形列表
  90. */
  91. const queryTreeMenu = () => {
  92. pageData.formLoading = true;
  93. permissionApi
  94. .treeMenus({})
  95. .then((res: any) => {
  96. if (res.success) {
  97. pageData.treeMenus = res.result;
  98. }
  99. })
  100. .finally(() => {
  101. pageData.formLoading = false;
  102. });
  103. };
  104. const handleChangeMenuType = (val: number) => {
  105. setTimeout(function () {
  106. formRef.value!.clearValidate([
  107. "menuType",
  108. "title",
  109. "rank",
  110. "path",
  111. "perms"
  112. ]);
  113. }, 30);
  114. if (val === 0) {
  115. pageData.formRules = pageData.dirFormRules;
  116. } else if (val === 1) {
  117. pageData.formRules = pageData.menuFormRules;
  118. } else {
  119. pageData.formRules = pageData.permissionFormRules;
  120. }
  121. };
  122. const handleConfirm = () => {
  123. formRef.value!.validate((isValid: boolean) => {
  124. if (isValid) {
  125. if (pageData.formData.id) {
  126. _update();
  127. } else {
  128. _save();
  129. }
  130. } else {
  131. message("表单校验失败", { type: "warning" });
  132. }
  133. });
  134. };
  135. const _update = () => {
  136. pageData.formLoading = true;
  137. const data = cloneDeep(pageData.formData);
  138. permissionApi
  139. .updatePermission(pageData.formData.id, data)
  140. .then((res: any) => {
  141. if (res.success) {
  142. _handleConfirm();
  143. } else {
  144. message(res.message, { type: "warning" });
  145. }
  146. })
  147. .finally(() => {
  148. pageData.formLoading = false;
  149. });
  150. };
  151. const _save = () => {
  152. pageData.formLoading = true;
  153. const data = cloneDeep(pageData.formData);
  154. permissionApi
  155. .savePermission(data)
  156. .then((res: any) => {
  157. if (res.success) {
  158. _handleConfirm();
  159. } else {
  160. message(res.message, { type: "warning" });
  161. }
  162. })
  163. .finally(() => {
  164. pageData.formLoading = false;
  165. });
  166. };
  167. /**
  168. * 关闭之前会调用
  169. */
  170. const _handleClose = () => {
  171. pageData.isShow = false;
  172. emits("close");
  173. };
  174. const _handleConfirm = () => {
  175. pageData.isShow = false;
  176. emits("ok");
  177. };
  178. defineOptions({ name: "MenuEdit" });
  179. defineExpose({ open });
  180. </script>
  181. <template>
  182. <el-drawer
  183. size="40%"
  184. v-model="pageData.isShow"
  185. destroy-on-close
  186. modal
  187. :close-on-click-modal="false"
  188. :title="pageData.title"
  189. :before-close="_handleClose"
  190. >
  191. <el-form
  192. ref="formRef"
  193. :model="pageData.formData"
  194. v-loading="pageData.formLoading"
  195. :rules="pageData.formRules"
  196. label-position="left"
  197. >
  198. <el-form-item label="菜单类型" prop="menuType">
  199. <el-radio-group
  200. v-model="pageData.formData.menuType"
  201. @change="handleChangeMenuType"
  202. >
  203. <el-radio-button
  204. v-for="(item, index) in MenuTypeOptions"
  205. :label="item.value"
  206. :key="index"
  207. >{{ item.label }}</el-radio-button
  208. >
  209. </el-radio-group>
  210. </el-form-item>
  211. <el-form-item
  212. label="菜单图标"
  213. prop="icon"
  214. v-if="pageData.formData.menuType !== 2"
  215. >
  216. <icon-select v-model="pageData.formData.icon" style="width: 100%" />
  217. </el-form-item>
  218. <el-form-item
  219. label="是否外链"
  220. prop="isFrame"
  221. v-if="pageData.formData.menuType !== 2"
  222. >
  223. <template #label>
  224. <from-question
  225. label="是否外链"
  226. description="选择是外链则路由地址需要以`http(s)://`开头"
  227. />
  228. </template>
  229. <el-radio-group v-model="pageData.formData.isFrame">
  230. <el-radio-button
  231. v-for="(item, index) in ifNumberOptions"
  232. :label="item.value"
  233. :key="index"
  234. >{{ item.label }}</el-radio-button
  235. >
  236. </el-radio-group>
  237. </el-form-item>
  238. <el-form-item
  239. label="菜单可见"
  240. props="showLink"
  241. v-if="pageData.formData.menuType !== 2"
  242. >
  243. <template #label>
  244. <from-question
  245. label="菜单可见"
  246. description="选择隐藏则路由将不会出现在侧边栏,但仍然可以访问"
  247. />
  248. </template>
  249. <el-radio-group v-model="pageData.formData.showLink">
  250. <el-radio-button
  251. v-for="(item, index) in ifOptions"
  252. :label="item.value"
  253. :key="index"
  254. >{{ item.label }}</el-radio-button
  255. >
  256. </el-radio-group>
  257. </el-form-item>
  258. <el-form-item
  259. label="菜单名称"
  260. prop="title"
  261. v-if="pageData.formData.menuType !== 2"
  262. >
  263. <el-input
  264. v-model="pageData.formData.title"
  265. clearable
  266. placeholder="请输入菜单名称"
  267. />
  268. </el-form-item>
  269. <el-form-item
  270. label="是否缓存"
  271. prop="keepAlive"
  272. v-if="pageData.formData.menuType === 1"
  273. >
  274. <template #label>
  275. <from-question
  276. label="是否缓存"
  277. description="选择是则会被`keep-alive`缓存,需要匹配组件的`name`和地址保持一致"
  278. />
  279. </template>
  280. <el-radio-group v-model="pageData.formData.keepAlive">
  281. <el-radio-button
  282. v-for="(item, index) in ifOptions"
  283. :label="item.value"
  284. :key="index"
  285. >{{ item.label }}</el-radio-button
  286. >
  287. </el-radio-group>
  288. </el-form-item>
  289. <el-form-item
  290. label="显示父级菜单"
  291. prop="showParent"
  292. v-if="pageData.formData.menuType === 1"
  293. >
  294. <el-radio-group v-model="pageData.formData.showParent">
  295. <el-radio-button
  296. v-for="(item, index) in ifOptions"
  297. :label="item.value"
  298. :key="index"
  299. >{{ item.label }}</el-radio-button
  300. >
  301. </el-radio-group>
  302. </el-form-item>
  303. <el-form-item
  304. label="组件名称"
  305. prop="name"
  306. v-if="pageData.formData.menuType === 1"
  307. >
  308. <el-input
  309. v-model="pageData.formData.name"
  310. clearable
  311. placeholder="组件名称"
  312. />
  313. </el-form-item>
  314. <el-form-item
  315. label="组件路径"
  316. prop="component"
  317. v-if="pageData.formData.menuType === 1"
  318. >
  319. <template #label>
  320. <from-question
  321. label="组件路径"
  322. description="访问的组件路径,如:`system/user/index`,默认在`views`目录下"
  323. />
  324. </template>
  325. <el-input
  326. v-model="pageData.formData.component"
  327. clearable
  328. placeholder="请输入组件路径"
  329. />
  330. </el-form-item>
  331. <el-form-item
  332. label="路由地址"
  333. prop="path"
  334. v-if="pageData.formData.menuType !== 2"
  335. >
  336. <template #label>
  337. <from-question
  338. label="路由地址"
  339. description="访问的路由地址,如:`user`,如外网地址需内链访问则以`http(s)://`开头"
  340. />
  341. </template>
  342. <el-input
  343. v-model="pageData.formData.path"
  344. clearable
  345. placeholder="请输入路由地址"
  346. />
  347. </el-form-item>
  348. <el-form-item
  349. label="菜单状态"
  350. prop="enable"
  351. v-if="pageData.formData.menuType !== 2"
  352. >
  353. <template #label>
  354. <from-question
  355. label="菜单状态"
  356. description="选择停用则路由将不会出现在侧边栏,也不能被访问"
  357. />
  358. </template>
  359. <el-radio-group v-model="pageData.formData.enable">
  360. <el-radio-button
  361. v-for="(item, index) in ifEnableOptions"
  362. :label="item.value"
  363. :key="index"
  364. >{{ item.label }}</el-radio-button
  365. >
  366. </el-radio-group>
  367. </el-form-item>
  368. <el-form-item
  369. label="权限名称"
  370. prop="title"
  371. v-if="pageData.formData.menuType === 2"
  372. >
  373. <el-input
  374. v-model="pageData.formData.title"
  375. clearable
  376. placeholder="请输入权限名称"
  377. />
  378. </el-form-item>
  379. <el-form-item
  380. label="权限标识"
  381. prop="perms"
  382. v-if="pageData.formData.menuType === 2"
  383. >
  384. <template #label>
  385. <from-question
  386. label="权限标识"
  387. description="控制器中定义的权限字符,如:@PreAuthorize(`@permission.hashPermission('menu:query')`)"
  388. />
  389. </template>
  390. <el-input
  391. v-model="pageData.formData.perms"
  392. clearable
  393. placeholder="请输入权限标识"
  394. />
  395. </el-form-item>
  396. <el-form-item label="显示排序" prop="rank">
  397. <el-input-number
  398. style="width: 100%"
  399. v-model="pageData.formData.rank"
  400. :min="0"
  401. :max="999"
  402. controls-position="right"
  403. />
  404. </el-form-item>
  405. <el-form-item label="上级菜单" prop="parentId">
  406. <el-tree-select
  407. v-model="pageData.formData.parentId"
  408. :data="pageData.treeMenus"
  409. :props="{ children: 'children', label: 'title' }"
  410. node-key="id"
  411. accordion
  412. filterable
  413. check-strictly
  414. clearable
  415. style="width: 100%"
  416. />
  417. </el-form-item>
  418. </el-form>
  419. <template #footer>
  420. <div class="demo-drawer__footer">
  421. <el-button @click="_handleClose">取消</el-button>
  422. <el-button type="primary" @click="handleConfirm">确认</el-button>
  423. </div>
  424. </template>
  425. </el-drawer>
  426. </template>
  427. <style scoped></style>