ModifySystemRole.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <el-dialog
  3. class="modify-system-role"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. :close-on-click-modal="false"
  7. :close-on-press-escape="false"
  8. append-to-body
  9. fullscreen
  10. @opened="visibleChange"
  11. >
  12. <el-form
  13. ref="modalFormComp"
  14. :model="modalForm"
  15. :rules="rules"
  16. label-position="top"
  17. @submit.native.prevent
  18. >
  19. <el-form-item prop="name" label="角色名称:">
  20. <el-input
  21. v-model.trim="modalForm.name"
  22. style="width:282px;"
  23. placeholder="请输入角色名称"
  24. clearable
  25. ></el-input>
  26. </el-form-item>
  27. <el-form-item label="角色权限:" required></el-form-item>
  28. </el-form>
  29. <privilege-set
  30. v-if="menus && menus.length"
  31. ref="PrivilegeSet"
  32. :menus="menus"
  33. ></privilege-set>
  34. <div slot="footer">
  35. <el-button type="primary" :disabled="isSubmit" @click="submit"
  36. >确认</el-button
  37. >
  38. <el-button @click="cancel">取消</el-button>
  39. </div>
  40. </el-dialog>
  41. </template>
  42. <script>
  43. import { updateRole, roleBoundPrivileges } from "../../base/api";
  44. import { schoolMenuTree } from "../api";
  45. import PrivilegeSet from "../../base/components/PrivilegeSet";
  46. const initModalForm = {
  47. id: null,
  48. name: "",
  49. privilegeIds: []
  50. };
  51. export default {
  52. name: "modify-system-role",
  53. components: { PrivilegeSet },
  54. props: {
  55. instance: {
  56. type: Object,
  57. default() {
  58. return {};
  59. }
  60. }
  61. },
  62. computed: {
  63. isEdit() {
  64. return !!this.instance.id;
  65. },
  66. title() {
  67. return (this.isEdit ? "编辑" : "新增") + "角色";
  68. }
  69. },
  70. data() {
  71. return {
  72. modalIsShow: false,
  73. isSubmit: false,
  74. menus: [],
  75. modalForm: {},
  76. rules: {
  77. name: [
  78. {
  79. required: true,
  80. pattern: /^[0-9a-zA-Z\u4E00-\u9FA5]{1,20}$/,
  81. message: "角色名称只能输入汉字、字母和数字,长度不能超过20",
  82. trigger: "change"
  83. }
  84. ]
  85. }
  86. };
  87. },
  88. created() {
  89. this.getMenus();
  90. },
  91. methods: {
  92. async getMenus() {
  93. const needHideModules = ["common", "customer"];
  94. const data = await schoolMenuTree();
  95. const menus = data.customPrivilegeList || [];
  96. this.menus = menus
  97. .filter(item => !needHideModules.includes(item.url))
  98. .map(item => {
  99. item.parentId = null;
  100. return item;
  101. });
  102. },
  103. async visibleChange() {
  104. let privilegeIds = [];
  105. if (this.instance.id) {
  106. this.modalForm = this.$objAssign(initModalForm, this.instance);
  107. privilegeIds = await roleBoundPrivileges(this.instance.id);
  108. privilegeIds = privilegeIds || [];
  109. this.modalForm.privilegeIds = privilegeIds;
  110. } else {
  111. this.modalForm = { ...initModalForm };
  112. }
  113. this.$nextTick(() => {
  114. this.$refs.modalFormComp.clearValidate();
  115. this.$refs.PrivilegeSet &&
  116. this.$refs.PrivilegeSet.buildTableData(privilegeIds);
  117. });
  118. },
  119. cancel() {
  120. this.modalIsShow = false;
  121. },
  122. open() {
  123. this.modalIsShow = true;
  124. },
  125. async submit() {
  126. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  127. if (!valid) return;
  128. const privilegeIds = this.$refs.PrivilegeSet.getSelectedPrivilegeIds();
  129. if (!privilegeIds.length) {
  130. this.$emit("请设置角色权限!");
  131. return;
  132. }
  133. if (this.isSubmit) return;
  134. this.isSubmit = true;
  135. const datas = { ...this.modalForm };
  136. datas.privilegeIds = privilegeIds;
  137. const data = await updateRole(datas).catch(() => {});
  138. this.isSubmit = false;
  139. if (!data) return;
  140. this.$emit("modified", this.modalForm);
  141. this.cancel();
  142. }
  143. }
  144. };
  145. </script>