ModifyMenu.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <el-dialog
  3. class="modify-menu"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. top="10vh"
  7. width="500px"
  8. :close-on-click-modal="false"
  9. :close-on-press-escape="false"
  10. append-to-body
  11. @opened="visibleChange"
  12. >
  13. <div class="part-box part-box-pad part-box-border">
  14. <el-form
  15. ref="modalFormComp"
  16. :model="modalForm"
  17. :rules="rules"
  18. label-width="100px"
  19. >
  20. <el-form-item prop="name" label="菜单名称:">
  21. <el-input
  22. style="width:100%;"
  23. v-model.trim="modalForm.name"
  24. placeholder="请输入菜单名称"
  25. clearable
  26. ></el-input>
  27. </el-form-item>
  28. <el-form-item prop="url" label="URL地址:">
  29. <el-input
  30. style="width:100%;"
  31. v-model.trim="modalForm.url"
  32. placeholder="请输入URL地址"
  33. clearable
  34. ></el-input>
  35. </el-form-item>
  36. <el-form-item label="上级菜单:">
  37. <el-input
  38. style="width:100%;"
  39. v-model.trim="modalForm.parentName"
  40. disabled
  41. ></el-input>
  42. </el-form-item>
  43. <el-form-item label="菜单类型:">
  44. <el-select
  45. v-model="modalForm.type"
  46. style="width: 100px;"
  47. placeholder="请选择"
  48. >
  49. <el-option
  50. v-for="(val, key) in PRIVILEGE_TYPE"
  51. :key="key"
  52. :value="key"
  53. :label="val"
  54. ></el-option>
  55. </el-select>
  56. </el-form-item>
  57. <el-form-item label="备注:">
  58. <el-input
  59. style="width:100%;"
  60. v-model.trim="modalForm.remark"
  61. ></el-input>
  62. </el-form-item>
  63. </el-form>
  64. </div>
  65. <div slot="footer">
  66. <el-button type="primary" :disabled="isSubmit" @click="submit"
  67. >确认</el-button
  68. >
  69. <el-button type="danger" @click="cancel" plain>取消</el-button>
  70. </div>
  71. </el-dialog>
  72. </template>
  73. <script>
  74. import { PRIVILEGE_TYPE } from "@/constants/enumerate";
  75. import { updateMenu } from "../api";
  76. const initModalForm = {
  77. id: null,
  78. name: "",
  79. url: "",
  80. parentId: null,
  81. parentName: "",
  82. sortNumber: 1,
  83. type: "M",
  84. remark: ""
  85. };
  86. export default {
  87. name: "modify-menu",
  88. props: {
  89. instance: {
  90. type: Object,
  91. default() {
  92. return {};
  93. }
  94. }
  95. },
  96. computed: {
  97. isEdit() {
  98. return !!this.instance.id;
  99. },
  100. title() {
  101. return (this.isEdit ? "编辑" : "新增") + "权限";
  102. }
  103. },
  104. data() {
  105. return {
  106. modalIsShow: false,
  107. isSubmit: false,
  108. modalForm: {},
  109. PRIVILEGE_TYPE,
  110. rules: {
  111. name: [
  112. {
  113. required: true,
  114. pattern: /^[\u4E00-\u9FA5/]{1,30}$/,
  115. message: "菜单名称只能输入汉字和斜线,长度不能超过30",
  116. trigger: "change"
  117. }
  118. ],
  119. url: [
  120. {
  121. required: true,
  122. pattern: /^[0-9a-zA-Z_-]{3,30}$/,
  123. message:
  124. "URL地址只能由字母、数字、短横线和下划线组成,长度在3-30之间",
  125. trigger: "change"
  126. }
  127. ]
  128. }
  129. };
  130. },
  131. methods: {
  132. initData(val) {
  133. this.modalForm = this.$objAssign(initModalForm, val);
  134. },
  135. visibleChange() {
  136. this.initData(this.instance);
  137. this.$nextTick(() => {
  138. this.$refs.modalFormComp.clearValidate();
  139. });
  140. },
  141. cancel() {
  142. this.modalIsShow = false;
  143. },
  144. open() {
  145. this.modalIsShow = true;
  146. },
  147. async submit() {
  148. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  149. if (!valid) return;
  150. if (this.isSubmit) return;
  151. this.isSubmit = true;
  152. const datas = { ...this.modalForm };
  153. const data = await updateMenu(datas).catch(() => {});
  154. this.isSubmit = false;
  155. if (!data) return;
  156. this.$emit("confirm", this.modalForm);
  157. this.cancel();
  158. }
  159. }
  160. };
  161. </script>