ModifyRole.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <template>
  2. <el-dialog
  3. class="modify-role"
  4. :visible.sync="modalIsShow"
  5. :title="title"
  6. top="10vh"
  7. width="600px"
  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="type" label="角色类型:">
  29. <el-select
  30. v-model="modalForm.type"
  31. style="width: 100%;"
  32. placeholder="请选择"
  33. >
  34. <el-option
  35. v-for="(val, key) in roleTypes"
  36. :key="key"
  37. :value="key"
  38. :label="val"
  39. ></el-option>
  40. </el-select>
  41. </el-form-item>
  42. <el-form-item
  43. v-show="modalForm.type === 'CUSTOM'"
  44. prop="privilegeIds"
  45. label="角色权限:"
  46. >
  47. <div class="part-box part-box-pad part-box-border">
  48. <el-tree
  49. :data="menus"
  50. show-checkbox
  51. default-expand-all
  52. node-key="id"
  53. ref="MenuTree"
  54. highlight-current
  55. :props="defaultProps"
  56. check-on-click-node
  57. :expand-on-click-node="false"
  58. @check-change="checkChange"
  59. >
  60. </el-tree>
  61. </div>
  62. </el-form-item>
  63. </el-form>
  64. </div>
  65. <div slot="footer">
  66. <el-button type="primary" @click="submit">确认</el-button>
  67. <el-button type="danger" @click="cancel" plain>取消</el-button>
  68. </div>
  69. </el-dialog>
  70. </template>
  71. <script>
  72. import { updateRole, roleBoundPrivileges } from "../api";
  73. const initModalForm = {
  74. id: null,
  75. name: "",
  76. type: "",
  77. privilegeIds: []
  78. };
  79. export default {
  80. name: "modify-role",
  81. props: {
  82. instance: {
  83. type: Object,
  84. default() {
  85. return {};
  86. }
  87. },
  88. menus: {
  89. type: Array,
  90. default() {
  91. return [];
  92. }
  93. },
  94. roleTypes: {
  95. type: Object,
  96. default() {
  97. return {};
  98. }
  99. }
  100. },
  101. computed: {
  102. isEdit() {
  103. return !!this.instance.id;
  104. },
  105. title() {
  106. return (this.isEdit ? "编辑" : "新增") + "角色";
  107. }
  108. },
  109. data() {
  110. return {
  111. modalIsShow: false,
  112. modalForm: {},
  113. defaultProps: {
  114. label: "name"
  115. },
  116. rules: {
  117. name: [
  118. {
  119. required: true,
  120. pattern: /^[0-9a-zA-Z\u4E00-\u9FA5]{1,10}$/,
  121. message: "角色名称只能输入汉字、字母和数字,长度不能超过10",
  122. trigger: "change"
  123. }
  124. ],
  125. type: [
  126. {
  127. required: true,
  128. message: "请选择角色类型",
  129. trigger: "change"
  130. }
  131. ],
  132. privilegeIds: [
  133. {
  134. required: true,
  135. validator: (rule, value, callback) => {
  136. if (this.modalForm.type !== "CUSTOM" || value.length) {
  137. callback();
  138. } else {
  139. callback(new Error("请选择扩展字段"));
  140. }
  141. },
  142. trigger: "change"
  143. }
  144. ]
  145. }
  146. };
  147. },
  148. methods: {
  149. async visibleChange() {
  150. if (this.instance.id) {
  151. this.modalForm = this.$objAssign(initModalForm, this.instance);
  152. if (this.instance.type === "CUSTOM") {
  153. let privilegeIds = await roleBoundPrivileges(this.instance.id);
  154. let checkedIds = [];
  155. const getCheckedIds = list => {
  156. list.forEach(item => {
  157. if (item["children"] && item["children"].length) {
  158. getCheckedIds(item.children);
  159. } else {
  160. const isChecked = privilegeIds.includes(item.id);
  161. if (isChecked) checkedIds.push(item.id);
  162. }
  163. });
  164. };
  165. getCheckedIds(this.menus);
  166. this.$refs.MenuTree.setCheckedKeys(checkedIds);
  167. this.modalForm.privilegeIds = checkedIds;
  168. }
  169. } else {
  170. this.modalForm = { ...initModalForm };
  171. this.$refs.MenuTree.setCheckedKeys([]);
  172. }
  173. this.$nextTick(() => {
  174. this.$refs.modalFormComp.clearValidate();
  175. });
  176. },
  177. cancel() {
  178. this.modalIsShow = false;
  179. },
  180. open() {
  181. this.modalIsShow = true;
  182. },
  183. checkChange() {
  184. this.modalForm.privilegeIds = this.$refs.MenuTree.getCheckedKeys();
  185. this.$refs.modalFormComp.validateField("privilegeIds");
  186. },
  187. async submit() {
  188. const valid = await this.$refs.modalFormComp.validate().catch(() => {});
  189. if (!valid) return;
  190. if (this.isSubmit) return;
  191. this.isSubmit = true;
  192. const datas = { ...this.modalForm };
  193. const privilegeIds = [
  194. ...this.$refs.MenuTree.getCheckedKeys(),
  195. ...this.$refs.MenuTree.getHalfCheckedKeys()
  196. ];
  197. datas.privilegeIds = privilegeIds;
  198. const data = await updateRole(datas).catch(() => {});
  199. this.isSubmit = false;
  200. if (!data) return;
  201. this.$emit("modified", this.modalForm);
  202. this.cancel();
  203. }
  204. }
  205. };
  206. </script>