UploadCreate.vue 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <template>
  2. <a-form
  3. class="mt-3"
  4. :form="form.fc">
  5. <a-form-item
  6. :label="$t('k8s.text_19')"
  7. v-bind="formItemLayout">
  8. <cluster-select
  9. @input="setCluster"
  10. :clusterObj.sync="clusterObj"
  11. v-decorator="decorators.cluster"
  12. style="width: 140px;" />
  13. </a-form-item>
  14. <a-form-item
  15. :label="$t('k8s.text_290')"
  16. :extra="$t('k8s.text_291')"
  17. v-bind="formItemLayout">
  18. <a-upload
  19. name="file"
  20. :beforeUpload="beforeUpload"
  21. :fileList="files"
  22. v-decorator="decorators.yaml"
  23. @change="handleFileChange"
  24. accept="application/x-yaml,application/json">
  25. <a-button> <a-icon type="upload" />{{$t('k8s.text_292')}}</a-button>
  26. </a-upload>
  27. </a-form-item>
  28. <a-form-item :wrapper-col="{ span: 20, offset: 3 }">
  29. <a-button class="mr-2" type="primary" @click="handleConfirm" :loading="loading">{{$t('k8s.text_290')}}</a-button>
  30. <a-button @click="cancel">{{$t('k8s.text_162')}}</a-button>
  31. </a-form-item>
  32. </a-form>
  33. </template>
  34. <script>
  35. import 'codemirror/mode/yaml/yaml.js'
  36. import 'codemirror/theme/material.css'
  37. import ClusterSelect from '@K8S/sections/ClusterSelect'
  38. import k8sCreateMixin from '@K8S/mixins/create'
  39. export default {
  40. name: 'UpdateCreate',
  41. components: {
  42. ClusterSelect,
  43. },
  44. mixins: [k8sCreateMixin],
  45. data () {
  46. return {
  47. loading: false,
  48. form: {
  49. fc: this.$form.createForm(this),
  50. },
  51. decorators: {
  52. cluster: [
  53. 'cluster',
  54. {
  55. rules: [
  56. { required: true, message: this.$t('k8s.text_30') },
  57. ],
  58. },
  59. ],
  60. yaml: [
  61. 'yaml',
  62. {
  63. rules: [
  64. { required: true, message: this.$t('k8s.text_289') },
  65. ],
  66. },
  67. ],
  68. },
  69. formItemLayout: {
  70. wrapperCol: { span: 20 },
  71. labelCol: { span: 3 },
  72. },
  73. clusterObj: {},
  74. files: [],
  75. }
  76. },
  77. created () {
  78. this.uploadM = new this.$Manager('appfromfiles', 'v1')
  79. this.form.fc.getFieldDecorator('yaml', { preserve: true, initialValue: '' })
  80. },
  81. destroyed () {
  82. this.uploadM = null
  83. },
  84. methods: {
  85. beforeUpload (file) {
  86. this.files = [file]
  87. return false
  88. },
  89. handleFileChange ({ fileList }) {
  90. this.files = [fileList[fileList.length - 1]]
  91. this.getYamlContent()
  92. },
  93. getYamlContent () {
  94. if (!('FileReader' in window)) {
  95. return null
  96. }
  97. const file = this.files[0]
  98. if (!file) {
  99. return null
  100. }
  101. const fr = new FileReader()
  102. fr.readAsText(file.originFileObj)
  103. fr.onload = (evt) => {
  104. this.form.fc.setFieldsValue({ yaml: evt.target.result })
  105. }
  106. fr.onerror = () => {
  107. this.$message.error(this.$t('k8s.text_293'))
  108. }
  109. },
  110. doCreate (data) {
  111. return this.uploadM.create({
  112. data: {
  113. content: data.yaml,
  114. cluster: data.cluster,
  115. },
  116. })
  117. },
  118. async handleConfirm () {
  119. this.loading = true
  120. try {
  121. const values = await this.form.fc.validateFields()
  122. await this.doCreate(values)
  123. this.loading = false
  124. this.$message.success(this.$t('k8s.text_184'))
  125. this.$router.push('/k8s-namespace')
  126. } catch (error) {
  127. this.loading = false
  128. throw error
  129. }
  130. },
  131. cancel () {
  132. this.$router.push('/k8s-namespace')
  133. },
  134. },
  135. }
  136. </script>