EditRolesDialog.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{params.title}}</div>
  4. <div slot="body">
  5. <a-alert :message="$t('iam.project.alter_role_tips')" class="mb-2" />
  6. <dialog-selected-tips :name="$t('common_578')" :count="params.data.length" :action="params.title" />
  7. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  8. <a-form
  9. :form="form.fc"
  10. v-bind="formItemLayout">
  11. <a-form-item :label="$t('dictionary.role')">
  12. <a-select
  13. :filterOption="filterOption"
  14. :loading="roleLoading"
  15. v-decorator="decorators.roles"
  16. mode="multiple"
  17. :placeholder="$t('rules.role')"
  18. @deselect="handleDeselect"
  19. @select="handleSelect">
  20. <a-select-option v-for="item in roleList" :key="item.id">
  21. {{item.name}}
  22. </a-select-option>
  23. </a-select>
  24. </a-form-item>
  25. </a-form>
  26. </div>
  27. <div slot="footer">
  28. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  29. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  30. </div>
  31. </base-dialog>
  32. </template>
  33. <script>
  34. import * as R from 'ramda'
  35. import DialogMixin from '@/mixins/dialog'
  36. import WindowsMixin from '@/mixins/windows'
  37. export default {
  38. name: 'ProjectEditRolesDialog',
  39. mixins: [DialogMixin, WindowsMixin],
  40. data () {
  41. return {
  42. loading: false,
  43. domainId: this.params.data[0].domain_id,
  44. roleList: [],
  45. roleLoading: false,
  46. initialValue: this.params.data[0].roles.map(item => { return item.id }),
  47. deselectList: [],
  48. selectList: [],
  49. form: {
  50. fc: this.$form.createForm(this),
  51. },
  52. decorators: {
  53. roles: [
  54. 'roles',
  55. {
  56. rules: [
  57. { required: true, message: this.$t('rules.role') },
  58. ],
  59. },
  60. ],
  61. },
  62. formItemLayout: {
  63. wrapperCol: {
  64. span: 21,
  65. },
  66. labelCol: {
  67. span: 3,
  68. },
  69. },
  70. type: this.params.data[0].groupId && this.params.data[0].groupName ? 'group' : 'user',
  71. }
  72. },
  73. created () {
  74. this.manager = new this.$Manager('projects', 'v1')
  75. this.fetchRoles()
  76. },
  77. methods: {
  78. async fetchRoles () {
  79. const manager = new this.$Manager('roles', 'v1')
  80. const params = {
  81. domain_id: this.domainId,
  82. scope: this.$store.getters.scope,
  83. }
  84. this.roleLoading = true
  85. try {
  86. const { data = {} } = await manager.list({ params })
  87. this.roleList = data.data || []
  88. this.form.fc.getFieldDecorator('roles', { initialValue: this.initialValue })
  89. } catch (err) {
  90. throw err
  91. } finally {
  92. this.roleLoading = false
  93. }
  94. },
  95. filterOption (input, option) {
  96. return (
  97. option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  98. )
  99. },
  100. handleDeselect (value, option) {
  101. if (R.includes(value, this.initialValue) && !R.includes(value, this.deselectList)) this.deselectList.push(value)
  102. if (!R.includes(value, this.initialValue) && R.includes(value, this.selectList)) {
  103. for (let i = 0; i < this.selectList.length; i++) {
  104. if (this.selectList[i] === value) {
  105. this.selectList.splice(i, 1)
  106. }
  107. }
  108. }
  109. },
  110. handleSelect (val) {
  111. if (!R.includes(val, this.initialValue) && !R.includes(val, this.selectList)) this.selectList.push(val)
  112. if (R.includes(val, this.initialValue) && R.includes(val, this.deselectList)) {
  113. for (let i = 0; i < this.deselectList.length; i++) {
  114. if (this.deselectList[i] === val) {
  115. this.deselectList.splice(i, 1)
  116. }
  117. }
  118. }
  119. },
  120. doJoint (roles) {
  121. const data = {
  122. [this.type + 's']: [this.params.data[0].groupId ? this.params.data[0].groupId : this.params.data[0].id],
  123. roles: roles,
  124. }
  125. return this.manager.performAction({
  126. id: this.params.uid,
  127. action: 'join',
  128. data,
  129. })
  130. },
  131. doLeave (roles) {
  132. const data = {
  133. group_roles: [],
  134. user_roles: [],
  135. }
  136. if (this.type === 'group') {
  137. data.group_roles = roles
  138. } else {
  139. data.user_roles = roles
  140. }
  141. return this.manager.performAction({
  142. id: this.params.uid,
  143. action: 'leave',
  144. data,
  145. })
  146. },
  147. async handleConfirm () {
  148. this.loading = true
  149. try {
  150. if (this.selectList.length > 0) {
  151. const { roles } = await this.form.fc.validateFields()
  152. await this.doJoint(roles)
  153. }
  154. if (this.deselectList.length > 0) {
  155. const roles = this.deselectList.map(item => ({
  156. [this.type]: this.params.data[0].groupId ? this.params.data[0].groupId : this.params.data[0].id,
  157. role: item,
  158. }))
  159. await this.doLeave(roles)
  160. }
  161. this.cancelDialog()
  162. this.params.success()
  163. } catch (error) {
  164. this.loading = false
  165. throw error
  166. }
  167. },
  168. },
  169. }
  170. </script>