DomainUserSelect.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="cloudprovider-cloudregion-zone-select-wrapper">
  3. <a-row :gutter="8">
  4. <a-col :span="12" v-if="isAdminMode">
  5. <a-form-item :wrapperCol="{ span: 24 }">
  6. <a-select :label-in-value="isAdminMode" :filterOption="filterOption" v-decorator="decorators.domain" @change="domainChange" dropdownClassName="oc-select-dropdown" :placeholder="$t('rules.domain')" show-search :loading="domainLoading">
  7. <a-select-option v-for="item in domainOpts" :key="item.id">
  8. <span class="text-color-secondary option-prefix">{{$t('dictionary.domain')}}: </span>{{item.value}}
  9. </a-select-option>
  10. </a-select>
  11. </a-form-item>
  12. </a-col>
  13. <a-col :span="!isAdminMode?24:12">
  14. <a-form-item :wrapperCol="{ span: 24 }">
  15. <div>
  16. <a-select style="width:calc(100% - 22px)" mode="multiple" :filterOption="filterOption" :label-in-value="labelInValue" v-decorator="decorators.users" @change="usersChange" dropdownClassName="oc-select-dropdown" :placeholder="$t('rules.user')" show-search :loading="usersLoading">
  17. <a-select-option v-for="item in usersOpts" :value="item.id" :key="item.id">
  18. <span class="text-color-secondary option-prefix">{{$t('dictionary.user')}}: </span>{{item.value}}
  19. </a-select-option>
  20. </a-select>
  21. <a-icon type="sync" class="ml-2 primary-color" :spin="usersLoading" @click="refresh" />
  22. </div>
  23. </a-form-item>
  24. </a-col>
  25. </a-row>
  26. <div slot="extra">{{$t('system.text_591',[$t('dictionary.user')])}}<help-link :href="userCreateLink">{{$t('system.text_440')}}</help-link></div>
  27. </div>
  28. </template>
  29. <script>
  30. import * as R from 'ramda'
  31. import _ from 'lodash'
  32. import { mapGetters } from 'vuex'
  33. import { Manager } from '@/utils/manager'
  34. export default {
  35. name: 'DomainUserSelect',
  36. props: {
  37. labelInValue: {
  38. type: Boolean,
  39. default: true,
  40. },
  41. projectDomainId: String,
  42. form: Object,
  43. decorators: {
  44. type: Object,
  45. validator: val => R.is(Array, val.users) && R.is(Array, val.project),
  46. },
  47. },
  48. data () {
  49. return {
  50. domainOpts: [],
  51. usersOpts: [],
  52. loading: {
  53. domain: false,
  54. users: false,
  55. },
  56. domainLoading: false,
  57. usersLoading: false,
  58. }
  59. },
  60. computed: {
  61. ...mapGetters(['isAdminMode', 'scope', 'isDomainMode', 'userInfo', 'l3PermissionEnable']),
  62. isOpenstack () {
  63. if (this.provider) {
  64. return this.provider.toLowerCase() === 'openstack'
  65. }
  66. return false
  67. },
  68. userCreateLink () {
  69. return this.$router.resolve('/systemuser/create').href
  70. },
  71. userParams () {
  72. const params = {
  73. scope: this.scope,
  74. limit: 0,
  75. }
  76. if (this.isAdminMode) {
  77. if (this.l3PermissionEnable) {
  78. params.domain_id = this.projectDomainId || this.userInfo.projectDomainId
  79. } else {
  80. params.domain_id = this.projectDomainId
  81. }
  82. }
  83. return params
  84. },
  85. },
  86. watch: {
  87. 'projectDomainId' (val) {
  88. if (val) {
  89. this.fetchUsers(val)
  90. }
  91. },
  92. },
  93. created () {
  94. this.dm = new Manager('domains', 'v1')
  95. this.um = new Manager('users', 'v1')
  96. if (this.isAdminMode) {
  97. this.fetchDomains()
  98. } else {
  99. this.fetchUsers('default')
  100. }
  101. },
  102. mounted () {
  103. },
  104. methods: {
  105. async fetchDomains () {
  106. if (!this.isAdminMode) {
  107. const data = [{
  108. key: this.userInfo.projectDomainId,
  109. label: this.userInfo.projectDomain,
  110. }]
  111. this.domainOpts = data
  112. this.domainChange(data[0])
  113. return
  114. }
  115. this.domainLoading = true
  116. try {
  117. const params = {
  118. scope: this.scope,
  119. limit: 0,
  120. filter: 'enabled.equals(1)', // 仅显示启用状态下的域
  121. }
  122. const response = await this.dm.list({ params })
  123. const data = response.data.data || []
  124. this.domainOpts = data.map(val => ({ ...val, value: val.name }))
  125. let defaultData = { id: this.userInfo.projectDomainId, value: this.userInfo.projectDomain }
  126. if (!this.domainOpts.find(val => val.id === this.userInfo.projectDomainId)) return // 如果下拉列表没有当前域值,return
  127. const initialValue = _.get(this.decorators, 'domain[1].initialValue')
  128. if (initialValue) {
  129. const findInitValue = this.domainOpts.find(val => val.id === (initialValue.id || initialValue))
  130. if (findInitValue) {
  131. defaultData = { key: findInitValue.id, label: findInitValue.value }
  132. }
  133. }
  134. if (!R.isNil(defaultData) && !R.isEmpty(defaultData)) {
  135. if (this.isAdminMode) {
  136. this.form.fc.setFieldsValue({
  137. domain: { key: defaultData.key, label: defaultData.label },
  138. })
  139. } else {
  140. this.form.fc.setFieldsValue({
  141. domain: defaultData.key,
  142. })
  143. }
  144. }
  145. this.domainChange(defaultData || {})
  146. } catch (error) {
  147. throw error
  148. } finally {
  149. this.domainLoading = false
  150. }
  151. },
  152. domainChange (domain) {
  153. const domainId = R.is(Object, domain) ? domain.key : domain
  154. if (domainId) {
  155. this.fetchUsers(domainId)
  156. this.form.fc.setFieldsValue({
  157. users: undefined,
  158. })
  159. this.$emit('domainChange', domainId)
  160. } else {
  161. this.form.fc.setFieldsValue({
  162. domain: undefined,
  163. users: undefined,
  164. })
  165. this.usersOpts = []
  166. }
  167. },
  168. async fetchUsers (domainId) {
  169. this.usersLoading = true
  170. try {
  171. const response = await this.um.list({
  172. params: this.userParams,
  173. })
  174. const data = response.data.data || []
  175. this.usersOpts = data.map(val => ({ ...val, value: val.name }))
  176. } catch (error) {
  177. throw error
  178. } finally {
  179. this.usersLoading = false
  180. }
  181. },
  182. refresh () {
  183. this.fetchUsers(this.projectDomainId)
  184. },
  185. filterOption (input, option) {
  186. return (
  187. option.componentOptions.children[1].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
  188. )
  189. },
  190. },
  191. }
  192. </script>