index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <template>
  2. <div class="server-password">
  3. <a-form-item class="mb-0">
  4. <a-radio-group :disabled="disabled" v-decorator="decorators.loginType" @change="loginTypeChange">
  5. <a-radio-button v-for="item of loginTypeMap" :value="item.key" :key="item.key" :disabled="disabledLoginTypes.includes(item.key)">
  6. {{ item.label }}
  7. <help-tooltip v-if="['image', 'keypair'].includes(item.key)" :name="`${item.key}Password`" class="ml-2" />
  8. </a-radio-button>
  9. </a-radio-group>
  10. </a-form-item>
  11. <a-form-item v-if="(loginTypeMap && loginTypeMap.keypair) && vmLoginType === loginTypeMap.keypair.key" class="mb-0">
  12. <template #extra>
  13. {{$t('compute.text_201')}}<help-link :href="href">{{$t('compute.text_202')}}</help-link>
  14. </template>
  15. <base-select
  16. class="w-50"
  17. v-decorator="decorators.keypair"
  18. resource="keypairs"
  19. :isDefaultSelect="true"
  20. :showSync="true"
  21. :select-props="{ allowClear: true, placeholder: $t('compute.text_203') }" />
  22. </a-form-item>
  23. <a-form-item v-if="(loginTypeMap && loginTypeMap.password) && vmLoginType === loginTypeMap.password.key" class="mb-0">
  24. <a-input-password
  25. class="w-50"
  26. v-decorator="decorators.password"
  27. :placeholder="$t('compute.text_204')" />
  28. </a-form-item>
  29. </div>
  30. </template>
  31. <script>
  32. import * as R from 'ramda'
  33. import { LOGIN_TYPES_MAP } from '@Compute/constants'
  34. import { passwordValidator } from '@/utils/validate'
  35. import i18n from '@/locales'
  36. const DEFAULT_DECORATOR = {
  37. password: [
  38. 'password',
  39. {
  40. validateFirst: true,
  41. rules: [
  42. { required: true, message: i18n.t('compute.text_204') },
  43. { validator: passwordValidator },
  44. ],
  45. },
  46. ],
  47. }
  48. export default {
  49. name: 'ServerPassword',
  50. props: {
  51. loginTypes: {
  52. type: Array,
  53. },
  54. decorator: {
  55. type: Object,
  56. required: true,
  57. // validator: val => R.is(Array, val.loginType) && R.is(Array, val.keypair) && R.is(Array, val.password),
  58. },
  59. form: {
  60. type: Object,
  61. },
  62. isSnapshotImageType: { // 表单的镜像类型是否是主机快照
  63. type: Boolean,
  64. default: false,
  65. },
  66. disabledLoginTypes: {
  67. type: Array,
  68. default: () => ([]),
  69. },
  70. },
  71. data () {
  72. return {
  73. vmLoginType: 'random',
  74. disabled: false,
  75. }
  76. },
  77. computed: {
  78. loginTypeMap () {
  79. if (this.loginTypes && this.loginTypes.length > 0) {
  80. const _ = {}
  81. for (const k in LOGIN_TYPES_MAP) {
  82. if (this.loginTypes.includes(k)) {
  83. _[k] = LOGIN_TYPES_MAP[k]
  84. }
  85. }
  86. return _
  87. }
  88. return LOGIN_TYPES_MAP
  89. },
  90. decorators () {
  91. return {
  92. ...DEFAULT_DECORATOR,
  93. ...this.decorator,
  94. }
  95. },
  96. href () {
  97. const url = this.$router.resolve('/keypair')
  98. return url.href
  99. },
  100. },
  101. watch: {
  102. isSnapshotImageType (val) {
  103. if (val) {
  104. this.disabled = true
  105. const v = LOGIN_TYPES_MAP.image.key
  106. this.form.fc.setFieldsValue({
  107. [this.decorators.loginType[0]]: v,
  108. })
  109. this.vmLoginType = v
  110. } else {
  111. this.disabled = false
  112. }
  113. },
  114. loginTypeMap (val, oldv) {
  115. if (!R.equals(val, oldv)) {
  116. this.setLoginType()
  117. }
  118. },
  119. },
  120. mounted () {
  121. this.setLoginType()
  122. },
  123. methods: {
  124. loginTypeChange (e) {
  125. this.vmLoginType = e.target.value
  126. },
  127. setLoginType () {
  128. if (this.loginTypeMap && !R.isEmpty(this.loginTypeMap)) {
  129. const loginTypeInitailValue = this.decorator.loginType[1].initialValue
  130. const keys = Object.keys(this.loginTypeMap)
  131. let vmLoginType = loginTypeInitailValue
  132. if (!keys.includes(loginTypeInitailValue) || this.isSnapshotImageType) { // 如果表单中的初始值不在 loginTypeMap 中, 主机快照只支持保留镜像设置
  133. if (keys.includes(LOGIN_TYPES_MAP.image.key)) { // 如果maps中有"保留镜像设置",则设置
  134. vmLoginType = LOGIN_TYPES_MAP.image.key
  135. } else { // 否则设置第一项
  136. vmLoginType = keys[0]
  137. }
  138. }
  139. if (this.form && this.form.fc) {
  140. this.form.fc.setFieldsValue({
  141. [this.decorators.loginType[0]]: vmLoginType,
  142. })
  143. }
  144. this.vmLoginType = vmLoginType
  145. }
  146. },
  147. },
  148. }
  149. </script>