ChangeProject.vue 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{title}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :count="params.data.length" :action="title" :name="name" />
  6. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  7. <a-form-model
  8. ref="form"
  9. :model="fd"
  10. :rules="rules">
  11. <a-form-model-item prop="project" :label="params.projectLabel || $t('res.project')" v-bind="formItemLayout">
  12. <a-row :gutter="8">
  13. <a-col :span="12">
  14. <a-select v-model="domain" show-search @search="getConditionDomains" :filter-option="false" allow-clear dropdownClassName="oc-select-dropdown">
  15. <template v-for="item of domains">
  16. <a-select-option :key="item.id" :value="item.id">
  17. <span class="text-color-secondary option-prefix">{{ $t('res.domain') }}: </span>{{ item.name }}
  18. </a-select-option>
  19. </template>
  20. </a-select>
  21. </a-col>
  22. <a-col :span="12">
  23. <a-select v-model="fd.project" show-search @search="fetchProjects" :filter-option="false" allow-clear dropdownClassName="oc-select-dropdown">
  24. <template v-for="item of projects">
  25. <a-select-option :key="item.id" :value="item.id">
  26. <span class="text-color-secondary option-prefix">{{ $t('res.project') }}: </span>{{ item.name }}
  27. </a-select-option>
  28. </template>
  29. </a-select>
  30. </a-col>
  31. </a-row>
  32. </a-form-model-item>
  33. </a-form-model>
  34. </div>
  35. <div slot="footer">
  36. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  37. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  38. </div>
  39. </base-dialog>
  40. </template>
  41. <script>
  42. import * as R from 'ramda'
  43. import { mapGetters } from 'vuex'
  44. import DialogMixin from '@/mixins/dialog'
  45. import WindowsMixin from '@/mixins/windows'
  46. export default {
  47. name: 'ExternalProjectSwitchLocalDialog',
  48. mixins: [DialogMixin, WindowsMixin],
  49. data () {
  50. return {
  51. loading: false,
  52. domain: '',
  53. domains: [],
  54. domainLoading: false,
  55. projects: [],
  56. fd: {
  57. project: '',
  58. },
  59. rules: {
  60. project: [
  61. { required: true, message: this.$t('rules.project') },
  62. ],
  63. },
  64. formItemLayout: this.params.formItemLayout || {
  65. wrapperCol: {
  66. span: 21,
  67. },
  68. labelCol: {
  69. span: 3,
  70. },
  71. },
  72. }
  73. },
  74. computed: {
  75. ...mapGetters(['userInfo', 'l3PermissionEnable', 'isAdminMode']),
  76. isBatch () {
  77. return this.params.data.length > 1
  78. },
  79. name () {
  80. return this.params.name || this.$t('common.text00006')
  81. },
  82. title () {
  83. return this.params.title || this.$t('common_641', [this.$t('dictionary.project')])
  84. },
  85. },
  86. watch: {
  87. domain (val, oldVal) {
  88. if (val !== oldVal) {
  89. this.fd.project = ''
  90. this.fetchProjects()
  91. }
  92. },
  93. },
  94. destroyed () {
  95. this.dm = null
  96. this.pm = null
  97. },
  98. created () {
  99. this.dm = new this.$Manager('domains', 'v1')
  100. this.pm = new this.$Manager('projects', 'v1')
  101. this.getConditionDomains()
  102. },
  103. methods: {
  104. async getConditionDomains (query) {
  105. // 域管理后台只能选自己所在的domain
  106. // 全局共享 -> all scope domain
  107. // 多域共享 -> account domain + share_domains
  108. const cloudaccountDomain = {
  109. id: this.params.cloudaccount.domain_id,
  110. name: this.params.cloudaccount.project_domain,
  111. }
  112. if (!this.l3PermissionEnable) {
  113. const domains = [cloudaccountDomain]
  114. this.domains = domains
  115. this.domain = this.domains[0].id
  116. }
  117. if (!this.isAdminMode) {
  118. const domains = [
  119. {
  120. name: this.userInfo.projectDomain,
  121. id: this.userInfo.projectDomainId,
  122. },
  123. ]
  124. this.domains = domains
  125. this.domain = this.domains[0].id
  126. return
  127. }
  128. const { public_scope = 'none', shared_domains = [] } = this.params.cloudaccount
  129. let domains = []
  130. if (public_scope === 'none') {
  131. domains = [cloudaccountDomain]
  132. }
  133. if (public_scope === 'domain') {
  134. domains = shared_domains
  135. domains.push(cloudaccountDomain)
  136. }
  137. if (public_scope === 'system') {
  138. this.domainLoading = true
  139. try {
  140. const params = {
  141. scope: this.scope,
  142. limit: 20,
  143. }
  144. if (query) {
  145. params.filter = `name.contains(${query})`
  146. }
  147. const response = await this.dm.list({
  148. params,
  149. })
  150. const data = response.data.data || []
  151. domains = data
  152. } catch (error) {
  153. throw error
  154. } finally {
  155. this.domainLoading = false
  156. }
  157. }
  158. let initDomainId = ''
  159. if (!this.isBatch && !this.initDomainIdFlag) {
  160. if (this.params.data[0].domain_id && this.params.data[0].project_domain) {
  161. const selectedDomain = {
  162. id: this.params.data[0].domain_id,
  163. name: this.params.data[0].project_domain,
  164. }
  165. const fined = !!R.find(R.propEq('id', selectedDomain.id))(domains)
  166. if (!fined) {
  167. if (!query || (query && selectedDomain.name.includes(query))) {
  168. domains.push(selectedDomain)
  169. initDomainId = selectedDomain.id
  170. this.initDomainIdFlag = true
  171. }
  172. } else {
  173. initDomainId = selectedDomain.id
  174. this.initDomainIdFlag = true
  175. }
  176. }
  177. }
  178. this.domains = domains
  179. this.domain = initDomainId || ((this.domains[0] && this.domains[0].id) || '')
  180. },
  181. async fetchProjects (query) {
  182. const params = {
  183. scope: this.scope,
  184. domain_id: this.domain,
  185. limit: 20,
  186. }
  187. if (query) {
  188. params.filter = `name.contains(${query})`
  189. }
  190. try {
  191. const response = await this.pm.list({
  192. params,
  193. })
  194. const projects = response.data.data || []
  195. let initProjectId = ''
  196. if (!this.isBatch && !this.initProjectIdFlag) {
  197. if (this.params.data[0].tenant_id && this.params.data[0].project) {
  198. const selectedProject = {
  199. id: this.params.data[0].tenant_id,
  200. name: this.params.data[0].project,
  201. }
  202. const finded = !!R.find(R.propEq('id', selectedProject.id))(projects)
  203. if (!finded) {
  204. if (!query || (query && selectedProject.name.includes(query))) {
  205. projects.push(selectedProject)
  206. initProjectId = selectedProject.id
  207. this.initProjectIdFlag = true
  208. }
  209. } else {
  210. initProjectId = selectedProject.id
  211. this.initProjectIdFlag = true
  212. }
  213. }
  214. }
  215. this.projects = projects
  216. this.fd.project = initProjectId || ((this.params.data[0].tenant_id) || '')
  217. } catch (error) {
  218. throw error
  219. }
  220. },
  221. async doChangeProject (values, ids) {
  222. return this.params.onManager('batchPerformAction', {
  223. id: ids,
  224. steadyStatus: this.params.steadyStatus || ['running', 'ready'],
  225. managerArgs: {
  226. action: this.params.action || 'change-project',
  227. data: values,
  228. },
  229. })
  230. },
  231. async handleConfirm () {
  232. this.loading = true
  233. try {
  234. await this.$refs.form.validate()
  235. const ids = this.params.data.map(item => item.id)
  236. this.loading = true
  237. await this.doChangeProject(this.fd, ids)
  238. this.loading = false
  239. this.cancelDialog()
  240. } catch (error) {
  241. this.loading = false
  242. throw error
  243. }
  244. },
  245. },
  246. }
  247. </script>