Export.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <template>
  2. <base-dialog @cancel="cancelDialog" v-show="show">
  3. <div slot="header">{{$t('dashboard.text_105')}}</div>
  4. <div slot="body">
  5. <dialog-selected-tips :name="$route.path.includes('app-package') ? $t('dictionary.app_package') : $t('dictionary.image')" :count="params.data.length" :action="$t('dashboard.text_105')" />
  6. <dialog-table :data="params.data" :columns="params.columns.slice(0, 3)" />
  7. <a-spin :spinning="loading">
  8. <a-form :form="form" v-bind="formItemLayout">
  9. <a-form-item :label="$t('compute.text_640')">
  10. <a-radio-group v-decorator="decorators.format">
  11. <a-radio
  12. v-for="o in formatOptions"
  13. :key="o.key"
  14. :value="o.key">
  15. {{ o.label }}
  16. </a-radio>
  17. </a-radio-group>
  18. </a-form-item>
  19. <a-form-item :label="$t('compute.image.export')">
  20. <a-radio-group v-model="isDownload">
  21. <a-radio :value="true">
  22. {{ $t('compute.image.export.file') }}
  23. </a-radio>
  24. <a-radio :value="false">
  25. {{ $t('compute.image.export.url') }}
  26. </a-radio>
  27. </a-radio-group>
  28. </a-form-item>
  29. </a-form>
  30. </a-spin>
  31. <a ref="imageButton" download :id="params.data[0].id" v-show="false" />
  32. </div>
  33. <div slot="footer">
  34. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t('dialog.ok') }}</a-button>
  35. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  36. </div>
  37. </base-dialog>
  38. </template>
  39. <script>
  40. import DialogMixin from '@/mixins/dialog'
  41. import WindowsMixin from '@/mixins/windows'
  42. export default {
  43. name: 'ImageExportDialog',
  44. mixins: [DialogMixin, WindowsMixin],
  45. data () {
  46. const m = new this.$Manager('images', 'v1')
  47. return {
  48. loading: true,
  49. isDownload: true,
  50. manager: m,
  51. show: true,
  52. formatOptions: [],
  53. form: this.$form.createForm(this),
  54. formItemLayout: {
  55. wrapperCol: {
  56. span: 20,
  57. },
  58. labelCol: {
  59. span: 4,
  60. },
  61. },
  62. decorators: {
  63. format: [
  64. 'format',
  65. {
  66. rules: [{ required: true, message: this.$t('compute.text_148', [this.$t('compute.text_640')]) }],
  67. },
  68. ],
  69. },
  70. }
  71. },
  72. created () {
  73. this.fetchFormats()
  74. },
  75. methods: {
  76. fetchFormats () {
  77. try {
  78. this.loading = true
  79. this.manager.getSpecific({
  80. id: this.params.data[0].id,
  81. spec: 'subformats',
  82. }).then(({ data }) => {
  83. this.formatOptions = data.filter((item) => { return item.status === 'active' }).map((item) => {
  84. return { key: item.format, label: item.format.toUpperCase() }
  85. })
  86. if (this.formatOptions.length >= 1) {
  87. this.form.setFieldsValue({ format: this.formatOptions[0].key })
  88. }
  89. this.$nextTick(() => {
  90. this.loading = false
  91. })
  92. })
  93. } catch (error) {
  94. this.loading = false
  95. throw error
  96. }
  97. },
  98. async getDownloadUrl (params) {
  99. const { data } = await new this.$Manager('imageutils', 'v1').getSpecific({ id: 'download', spec: this.params.data[0].id, params })
  100. let name = this.params.data[0].name || this.params.data[0].id
  101. if (params && params.format && !name.endsWith(params.format)) {
  102. name = name + '.' + params.format
  103. }
  104. return '/api/v1/imageutils/image/' + name + '?signature=' + data.signature
  105. },
  106. async fetchUrl (params) {
  107. try {
  108. const path = await this.getDownloadUrl(params)
  109. this.createDialog('ImageExportUrlDialog', {
  110. url: window.location.protocol + '//' + window.location.host + path,
  111. cancel: this.cancelDialog,
  112. })
  113. this.show = false
  114. } catch (error) {
  115. throw error
  116. }
  117. },
  118. async download (params) {
  119. const path = await this.getDownloadUrl(params)
  120. this.$refs.imageButton.href = path
  121. this.$refs.imageButton.click()
  122. this.cancelDialog()
  123. },
  124. async handleConfirm () {
  125. try {
  126. this.loading = true
  127. const values = await this.form.validateFields()
  128. if (this.isDownload) {
  129. await this.download(values)
  130. } else {
  131. await this.fetchUrl(values)
  132. }
  133. this.loading = false
  134. } catch (error) {
  135. this.loading = false
  136. throw error
  137. }
  138. },
  139. },
  140. }
  141. </script>
  142. <style scoped>
  143. </style>