index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <div>
  3. <page-header :title="$t('system.text_487')" />
  4. <steps class="my-3" v-model="step" />
  5. <page-body needMarginBottom>
  6. <component :is="stepComponent" :domain="domain" ref="stepForm" />
  7. </page-body>
  8. <page-footer>
  9. <template v-slot:right>
  10. <a-tooltip v-if="disabledJoinProject">
  11. <template slot="title">
  12. {{ $t('iam.disabled_join_project.tips') }}
  13. </template>
  14. <a-button type="primary" :loading="loading" :disabled="disabledJoinProject" @click="handleSubmit">{{ createTxt }}</a-button>
  15. </a-tooltip>
  16. <a-button v-else type="primary" :loading="loading" @click="handleSubmit">{{ createTxt }}</a-button>
  17. <a-button class="ml-3" @click="handleCancel">{{ cancelTxt }}</a-button>
  18. </template>
  19. </page-footer>
  20. </div>
  21. </template>
  22. <script>
  23. import { mapGetters } from 'vuex'
  24. import step from '@/mixins/step'
  25. import { hasPermission } from '@/utils/auth'
  26. import CreateUser from './form/CreateUser'
  27. import JoinProject from './form/JoinProject'
  28. export default {
  29. name: 'UserCreate',
  30. components: {
  31. CreateUser,
  32. JoinProject,
  33. },
  34. mixins: [step],
  35. data () {
  36. return {
  37. loading: false,
  38. userMsg: {},
  39. step: {
  40. steps: [
  41. { title: this.$t('system.text_488'), key: 'create-user' },
  42. { title: this.$t('system.text_489'), key: 'join-project' },
  43. ],
  44. currentStep: 0,
  45. },
  46. domain: { key: 'default', label: 'Default' },
  47. }
  48. },
  49. computed: {
  50. ...mapGetters(['isAdminMode', 'l3PermissionEnable', 'userInfo', 'permission']),
  51. stepComponent () {
  52. return this.step.steps[this.step.currentStep].key
  53. },
  54. cancelTxt () {
  55. const _ = {
  56. 'create-user': this.$t('system.text_491'),
  57. 'join-project': this.$t('system.text_492'),
  58. }
  59. return _[this.stepComponent] || this.$t('system.text_491')
  60. },
  61. createTxt () {
  62. return this.step.currentStep ? this.$t('system.text_498', []) : this.$t('common.create')
  63. },
  64. disabledJoinProject () {
  65. if (this.step.currentStep === 1) {
  66. return !hasPermission({ key: 'projects_perform_join', permissionData: this.permission })
  67. }
  68. return false
  69. },
  70. },
  71. created () {
  72. this.manager = new this.$Manager('users', 'v1')
  73. if (this.l3PermissionEnable) {
  74. this.domain = {
  75. key: this.userInfo.projectDomainId,
  76. label: this.userInfo.projectDomain,
  77. }
  78. }
  79. },
  80. methods: {
  81. async handleSubmit () {
  82. this.loading = true
  83. try {
  84. const values = await this.$refs.stepForm.validateForm()
  85. if (this.step.currentStep === 0) {
  86. const { data } = await this.createUser(values)
  87. if (this.l3PermissionEnable) {
  88. this.domain = values.domain
  89. }
  90. this.userMsg = data
  91. this.step.currentStep = this.step.currentStep + 1
  92. } else if (this.step.currentStep === 1) {
  93. await this.joinProject(values)
  94. this.handleCancel()
  95. }
  96. } catch (error) {
  97. throw error
  98. } finally {
  99. this.loading = false
  100. }
  101. },
  102. async createUser (values) {
  103. try {
  104. const data = { ...values, skip_password_complexity_check: true }
  105. if (data.domain && data.domain.key) {
  106. data.domain_id = data.domain.key
  107. delete data.domain
  108. }
  109. if (!this.isAdminMode) {
  110. delete data.domain_id
  111. }
  112. const ret = await this.manager.create({
  113. data,
  114. })
  115. this.$message.success(this.$t('system.text_493'))
  116. return ret
  117. } catch (error) {
  118. throw error
  119. }
  120. },
  121. async joinProject (values) {
  122. try {
  123. delete values.domain
  124. await this.manager.performAction({
  125. id: this.userMsg.id,
  126. action: 'join',
  127. data: values,
  128. })
  129. this.$message.success(this.$t('system.text_494'))
  130. } catch (error) {
  131. throw error
  132. }
  133. },
  134. handleCancel () {
  135. this.$router.push('/systemuser')
  136. },
  137. },
  138. }
  139. </script>