index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <template>
  2. <div class="image-select">
  3. <a-form-item class="mb-0">
  4. <a-radio-group v-decorator="decorator.imageType" @change="change">
  5. <a-tooltip v-for="item in mirrorTypeOptions" :key="item.key" :title="item.tooltip" :mouseEnterDelay="0.5">
  6. <a-radio-button :value="item.key" :disabled="item.disabled">{{ item.label }}</a-radio-button>
  7. </a-tooltip>
  8. </a-radio-group>
  9. </a-form-item>
  10. <image-select
  11. :cloud-type="type"
  12. :uefi="uefi"
  13. :vgaPci="vgaPci"
  14. :image-type="imageType"
  15. :decorator="decorator"
  16. @input="imageInput"
  17. @updateImageMsg="updateImageMsg"
  18. :imageParams="imageParams"
  19. :cacheImageParams="cacheImageParams"
  20. :osType="osType"
  21. :osArch="osArch"
  22. :cloudproviderParamsExtra="cloudproviderParamsExtra"
  23. :cloudaccountId="cloudaccountId"
  24. :imageCloudproviderDisabled="imageCloudproviderDisabled"
  25. :sys-disk-size="sysDiskSize"
  26. :form="form"
  27. :edit="edit"
  28. :hypervisor="hypervisor" />
  29. </div>
  30. </template>
  31. <script>
  32. import * as R from 'ramda'
  33. import { IMAGES_TYPE_MAP } from '@/constants/compute'
  34. import { HYPERVISORS_MAP } from '@/constants'
  35. import storage from '@/utils/storage'
  36. import ImageSelect from './ImageSelect'
  37. export default {
  38. name: 'OsSelect',
  39. components: {
  40. ImageSelect,
  41. },
  42. props: {
  43. types: {
  44. type: Array,
  45. },
  46. decorator: {
  47. type: Object,
  48. required: true,
  49. validator: val => R.is(Array, val.imageType) && R.is(Array, val.os) && R.is(Array, val.image),
  50. },
  51. imageParams: {
  52. type: Object,
  53. },
  54. cacheImageParams: {
  55. type: Object,
  56. },
  57. type: {
  58. type: String,
  59. validator: val => ['public', 'private', 'idc', 'baremetal'].includes(val),
  60. required: true,
  61. },
  62. hypervisor: {
  63. type: String,
  64. },
  65. ignoreOptions: {
  66. type: Array,
  67. default: () => [],
  68. },
  69. osType: {
  70. type: String,
  71. },
  72. osArch: {
  73. type: String,
  74. },
  75. uefi: {
  76. type: Boolean,
  77. required: false,
  78. },
  79. vgaPci: {
  80. type: Boolean,
  81. required: false,
  82. },
  83. cloudproviderParamsExtra: {
  84. type: Object,
  85. default: () => ({}),
  86. },
  87. imageCloudproviderDisabled: {
  88. type: Boolean,
  89. default: false,
  90. },
  91. cloudaccountId: {
  92. type: String,
  93. },
  94. form: {
  95. type: Object,
  96. required: true,
  97. },
  98. sysDiskSize: {
  99. type: Number,
  100. },
  101. imageTypeMap: {
  102. type: Object,
  103. default: () => ({}),
  104. },
  105. edit: {
  106. type: Boolean,
  107. default: false,
  108. },
  109. },
  110. data () {
  111. return {
  112. imageType: this.decorator.imageType[1].initialValue,
  113. isFirstLoad: true,
  114. }
  115. },
  116. computed: {
  117. isPublic () {
  118. return this.type === 'public'
  119. },
  120. isPrivate () {
  121. return this.type === 'private'
  122. },
  123. isIDC () {
  124. return this.type === 'idc'
  125. },
  126. isBaremetal () {
  127. return this.type === 'baremetal'
  128. },
  129. mirrorTypeOptions () {
  130. let ret = [IMAGES_TYPE_MAP.standard, IMAGES_TYPE_MAP.customize]
  131. if (this.isIDC && this.hypervisor === HYPERVISORS_MAP.kvm.key) {
  132. ret.push(IMAGES_TYPE_MAP.iso, IMAGES_TYPE_MAP.host, { ...IMAGES_TYPE_MAP.snapshot, label: this.$t(IMAGES_TYPE_MAP.snapshot.t) }, IMAGES_TYPE_MAP.backup)
  133. } else if (this.hypervisor === HYPERVISORS_MAP.esxi.key) {
  134. ret.unshift(IMAGES_TYPE_MAP.vmware)
  135. ret.push(IMAGES_TYPE_MAP.iso)
  136. ret.push({ ...IMAGES_TYPE_MAP.snapshot, label: this.$t(IMAGES_TYPE_MAP.snapshot.t) })
  137. } else if (this.hypervisor === HYPERVISORS_MAP.proxmox.key) {
  138. ret = [IMAGES_TYPE_MAP.private, IMAGES_TYPE_MAP.iso, IMAGES_TYPE_MAP.private_iso]
  139. } else if (this.hypervisor === HYPERVISORS_MAP.uis.key) {
  140. ret = [IMAGES_TYPE_MAP.private_iso]
  141. } else if (this.hypervisor === HYPERVISORS_MAP.sangfor.key) {
  142. ret = [IMAGES_TYPE_MAP.private_iso]
  143. } else if (this.isPublic) {
  144. ret.unshift(IMAGES_TYPE_MAP.public_customize)
  145. ret.unshift(IMAGES_TYPE_MAP.public)
  146. } else if (this.isPrivate) {
  147. ret.unshift(IMAGES_TYPE_MAP.private)
  148. } else if (this.isBaremetal) {
  149. ret.push(IMAGES_TYPE_MAP.iso)
  150. }
  151. ret = ret.filter((item) => {
  152. return !this.ignoreOptions.includes(item.key)
  153. })
  154. if (this.types && !R.isEmpty(this.types)) {
  155. ret = ret.filter(({ key }) => {
  156. return this.types.indexOf(key) > -1
  157. })
  158. }
  159. if (!R.isEmpty(this.imageTypeMap)) {
  160. ret = ret.map(val => {
  161. const imageTypeMapItem = this.imageTypeMap[val.key] // 如果传了外部的 imageTypeMap,采用外部
  162. if (R.is(Object, imageTypeMapItem)) {
  163. return { ...val, ...imageTypeMapItem }
  164. }
  165. return val
  166. })
  167. }
  168. return ret
  169. },
  170. },
  171. watch: {
  172. hypervisor () {
  173. const imageType = this.decorator.imageType[1].initialValue || this.mirrorTypeOptions[0].key
  174. this.imageType = imageType
  175. this.form.fc.setFieldsValue({
  176. [this.decorator.imageType[0]]: imageType,
  177. })
  178. },
  179. 'form.fd.image.key': {
  180. handler () {
  181. const lastSelectedImageInfo = storage.get('oc_selected_image') || {}
  182. const { imageType = lastSelectedImageInfo.imageType } = this.$route.query
  183. if (this.isFirstLoad && imageType) {
  184. setTimeout(() => {
  185. this.form.fc.setFieldsValue({ imageType })
  186. }, 0)
  187. this.imageType = imageType
  188. }
  189. },
  190. immediate: true,
  191. },
  192. },
  193. methods: {
  194. imageInput (image) {
  195. this.$emit('change', image)
  196. },
  197. change (e) {
  198. this.isFirstLoad = false
  199. this.imageType = e.target.value
  200. this.$emit('update:imageType', e.target.value)
  201. },
  202. updateImageMsg (...ret) {
  203. const lastSelectedImageInfo = storage.get('oc_selected_image') || {}
  204. const image = ret[0].imageMsg
  205. if (image?.properties) {
  206. let os_distribution = image.properties.os_distribution
  207. const os_type = image.properties.os_type
  208. if (os_distribution) {
  209. os_distribution = os_distribution.includes('Windows') ? 'Windows' : os_distribution
  210. storage.set('oc_selected_image', { ...lastSelectedImageInfo, imageOs: os_distribution, imageId: image.id })
  211. } else if (os_type) {
  212. storage.set('oc_selected_image', { ...lastSelectedImageInfo, imageOs: os_type, imageId: image.id })
  213. }
  214. }
  215. this.$emit('updateImageMsg', ...ret)
  216. },
  217. },
  218. }
  219. </script>