index.vue 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <div>
  3. <div class="mb-4">
  4. <a-radio-group v-model="market" @change="chooseMaket">
  5. <a-radio-button value="cloud">{{$t('dictionary.server')}}</a-radio-button>
  6. <a-radio-button value="iso">ISO</a-radio-button>
  7. </a-radio-group>
  8. </div>
  9. <div>
  10. <a-radio-group v-model="imported" @change="chooseHandle">
  11. <a-radio-button :value="false">{{$t('compute.text_677')}}</a-radio-button>
  12. <a-radio-button :value="true">{{$t('compute.text_678')}}</a-radio-button>
  13. </a-radio-group>
  14. </div>
  15. <page-card-list
  16. :list="list"
  17. :card-fields="cardFields"
  18. :showPageer="false"
  19. :isRefreshed="false"
  20. :single-actions="singleActions" />
  21. </div>
  22. </template>
  23. <script>
  24. import { arrToObjByKey, sizestr } from '@/utils/utils'
  25. import WindowsMixin from '@/mixins/windows'
  26. const path = require('path')
  27. const imagesLogoFiles = require.context('@/assets/images/os-images', false, /.svg$/)
  28. const imagesLogos = []
  29. imagesLogoFiles.keys().forEach(key => {
  30. const name = path.basename(key, '.svg') // 返回文件名 不含后缀名
  31. imagesLogos.push(name)
  32. })
  33. export default {
  34. name: 'ImageImport',
  35. mixins: [WindowsMixin],
  36. data () {
  37. return {
  38. imported: false,
  39. market: 'cloud',
  40. list: {
  41. limit: 20,
  42. total: 0,
  43. offset: 0,
  44. data: [],
  45. loading: false,
  46. },
  47. cardFields: {
  48. url: 'os',
  49. title: 'title',
  50. description: 'description',
  51. desc: 'desc',
  52. },
  53. isImported: {},
  54. type: '',
  55. imagesLogos,
  56. }
  57. },
  58. computed: {
  59. singleActions () {
  60. return [
  61. {
  62. label: this.imported ? this.$t('compute.text_678') : this.$t('compute.text_679'),
  63. action: obj => {
  64. const data = {
  65. name: obj.name,
  66. copy_from: obj.url,
  67. disk_format: obj.format,
  68. is_public: true,
  69. is_standard: true,
  70. protected: true,
  71. properties: {
  72. os_type: obj.os_type,
  73. os_arch: obj.os_arch,
  74. os_description: obj.os_description,
  75. os_distribution: obj.os_distribution,
  76. os_version: obj.os_version,
  77. },
  78. }
  79. new this.$Manager('images', 'v1').create({ data })
  80. .then(() => {
  81. this.$set(this.isImported, obj.id, true)
  82. if (this.type === 'iso') {
  83. this.fetchData(this.type)
  84. } else {
  85. this.fetchData()
  86. }
  87. })
  88. },
  89. meta: (obj) => {
  90. let validate = true
  91. if (!this.imported && this.isImported[obj.id]) {
  92. validate = false
  93. } else {
  94. validate = !this.imported
  95. }
  96. return {
  97. buttonType: 'primary',
  98. validate,
  99. }
  100. },
  101. },
  102. ]
  103. },
  104. },
  105. created () {
  106. this.fetchData()
  107. },
  108. methods: {
  109. fetchData (market) {
  110. this.list.loading = true
  111. new this.$Manager('imageutils/imagesinfo', 'v1').list({ params: { market } }).then(({ data }) => {
  112. this.list.loading = false
  113. const osDesc = (image) => {
  114. return this.$t('compute.text_680', [
  115. image.format || '-',
  116. (image.disk || '-') + 'GiB',
  117. image.arch || '-',
  118. sizestr(image.size, 'B', 1024) || '-',
  119. image.create_at || '-',
  120. ])
  121. }
  122. var publicImages = []
  123. for (const image of data) {
  124. if (!image.hasOwnProperty('distribution')) {
  125. console.log('warnning, image missing key distribution', image)
  126. continue
  127. }
  128. if (!image.hasOwnProperty('type')) {
  129. console.log('warnning, image missing key type', image)
  130. continue
  131. }
  132. const os = image.distribution.split(' ')[0].toLowerCase()
  133. const o = {}
  134. o.id = image.id
  135. o.name = image.name
  136. o.url = image.url
  137. o.format = image.format
  138. o.os_type = image.type
  139. o.os_arch = image.arch
  140. o.os_description = image.os_description
  141. o.os_distribution = image.distribution
  142. o.os_version = image.version
  143. o.class = `fo-${os} os-logo ${image.type.toLowerCase()}`
  144. o.title = `${image.distribution} ${image.version}`
  145. o.desc = osDesc(image)
  146. o.imported = image.imported
  147. o.os = os || image.type.toLowerCase()
  148. o.description = image.description
  149. publicImages.push(o)
  150. }
  151. publicImages = publicImages.filter((item) => { return item.imported === this.imported.toString() })
  152. publicImages.forEach(item => {
  153. item.os = require(`@/assets/images/os-images/${this.imagesLogos.includes(item.os) ? item.os : 'unknow'}.svg`) || ''
  154. })
  155. this.list.data = arrToObjByKey(publicImages, 'id')
  156. }).catch((e) => {
  157. this.list.loading = false
  158. throw e
  159. })
  160. },
  161. chooseHandle () {
  162. if (this.type === 'iso') {
  163. this.fetchData(this.type)
  164. } else {
  165. this.fetchData()
  166. }
  167. },
  168. chooseMaket (e) {
  169. if (e.target.value === 'iso') {
  170. this.type = 'iso'
  171. this.fetchData(e.target.value)
  172. } else {
  173. this.type = ''
  174. this.fetchData()
  175. }
  176. },
  177. },
  178. }
  179. </script>