RoleModal.vue 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <template>
  2. <a-modal :visible="true" :maskClosable="false" centered :destroyOnClose="true" :title="title"
  3. :wrapClassName="wrapClassName" :width="width" @cancel="onClose" @ok="onSubmit">
  4. <div class="content">
  5. <a-form ref="formRef" :model="form" :rules="rules" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
  6. <a-form-item label="角色名称" name="groupName">
  7. <a-input v-model:value="form.groupName" style="width: 100%" placeholder="请输入名称" />
  8. </a-form-item>
  9. <a-form-item label="系统功能" name="checkedKeys">
  10. <div class="tree-box">
  11. <a-tree checkable :tree-data="menuTreeData" v-model:checkedKeys="form.checkedKeys"
  12. class="tree-list" :replaceFields="{ children: 'children', key: 'id', title: 'name' }">
  13. </a-tree>
  14. </div>
  15. </a-form-item>
  16. </a-form>
  17. </div>
  18. <template #footer>
  19. <div class="btns" style="display: flex;justify-content: center;">
  20. <a-button class="btn" @click="onClose"
  21. style="width: 64px;height: 34px;margin-right: 25px;color: #333333;">取消</a-button>
  22. <a-button class="btn" @click="onSubmit"
  23. style="width: 64px;height: 34px;background: #0671DD;color: #ffffff;">确定</a-button>
  24. </div>
  25. </template>
  26. </a-modal>
  27. </template>
  28. <script>
  29. import { defineComponent, reactive, ref, onMounted, watch, toRefs } from 'vue';
  30. import { message } from 'ant-design-vue';
  31. import moment from 'moment';
  32. import { v4 as uuidv4 } from 'uuid';
  33. //菜单树api
  34. import { getMenuList } from '/@/api/sys/menu';
  35. //获取与保存角色绑定的菜单数据
  36. import { saveRoleMenu } from '/@/api/sys/user';
  37. //保存角色信息
  38. import { saveOrUpdateRoleInfoWithMenu } from '/@/api/system/system';
  39. const props = {
  40. }
  41. export default defineComponent({
  42. name: 'modal',
  43. components: {},
  44. props,
  45. setup(props, { emit }) {
  46. const data = reactive({
  47. width: '546px',
  48. wrapClassName: 'modal-wrap',
  49. })
  50. const form = reactive({
  51. groupName: '',
  52. sort: '1',
  53. groupid: '',
  54. checkedKeys: []
  55. });
  56. const rules = {
  57. groupName: [{
  58. required: true,
  59. message: '请输入角色名称',
  60. trigger: 'blur'
  61. }]
  62. };
  63. const title = ref('新增角色')
  64. const formRef = ref()
  65. const menuTreeData = ref([])
  66. onMounted(() => {
  67. //此处请求菜单树
  68. getMenuList().then(res => {
  69. menuTreeData.value = res
  70. })
  71. })
  72. // 关闭请求弹窗
  73. const onClose = () => {
  74. emit('closeModal')
  75. resetForm()
  76. };
  77. const onSubmit = () => {
  78. formRef.value.validate().then(() => {
  79. let newId = uuidv4()
  80. let params = {
  81. groupName: form.groupName,
  82. sort: '1',
  83. groupid: newId
  84. }
  85. saveOrUpdateRoleInfoWithMenu(params).then((res) => {
  86. if (res === 1) {
  87. let menusIds = [...new Set([].concat(form.checkedKeys))]
  88. saveRoleMenu({
  89. menusId: menusIds.join(","),
  90. systemId: "1",
  91. roleId: newId
  92. }).then(r => {
  93. if (r) {
  94. message.success('角色新增成功')
  95. } else {
  96. message.error('权限绑定失败')
  97. }
  98. emit('onSubmit',true)
  99. formRef.value.resetFields();
  100. })
  101. } else {
  102. message.error('角色新增失败')
  103. formRef.value.resetFields();
  104. emit('onSubmit',false)
  105. }
  106. });
  107. }).catch((error) => {
  108. console.log('error', error);
  109. });
  110. };
  111. const resetForm = () => {
  112. formRef.value.resetFields();
  113. };
  114. return {
  115. form,
  116. rules,
  117. title,
  118. formRef,
  119. menuTreeData,
  120. labelCol: { span: 6 },
  121. wrapperCol: { span: 12 },
  122. ...toRefs(data),
  123. //func
  124. onClose,
  125. onSubmit
  126. };
  127. },
  128. });
  129. </script>
  130. <style lang="less" scoped>
  131. .modal-wrap {
  132. .content {
  133. padding: 20px 20px 10px 20px;
  134. margin-top: 10px;
  135. ::v-deep .tree-box {
  136. width: 100%;
  137. height: 450px;
  138. overflow: auto;
  139. border-radius: 2px;
  140. box-sizing: border-box;
  141. border: 1px solid #D9D9D9;
  142. &::-webkit-scrollbar {
  143. width: 3px;
  144. }
  145. &::-webkit-scrollbar-thumb {
  146. border-radius: 2px;
  147. background: #ccd5df;
  148. }
  149. &::-webkit-scrollbar-track {
  150. display: none;
  151. }
  152. }
  153. }
  154. ::v-deep .ant-modal-footer {
  155. .btn {
  156. width: 64px;
  157. height: 34px;
  158. background-color: red;
  159. }
  160. }
  161. }
  162. </style>