ModifyRole.vue 4.2 KB

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