UploadJsonFile.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <template>
  2. <div>
  3. <a-form-item :label="$t('cloudenv.text_113')">
  4. <a-radio-group @change="handleTypeChange" v-model="type">
  5. <a-radio-button :value="1">{{$t('cloudenv.text_114')}}</a-radio-button>
  6. <a-radio-button :value="2">{{$t('cloudenv.text_115')}}</a-radio-button>
  7. </a-radio-group>
  8. </a-form-item>
  9. <a-form-item :label="$t('cloudenv.text_116')" v-if="type === 1" class="google-account-file">
  10. <a-upload-dragger
  11. v-bind="uploadDraggerConfig"
  12. :beforeUpload="handleBeforeUpload"
  13. :remove="hanldeRemoveFile"
  14. :fileList="fileList">
  15. <div style="padding: 10px;">
  16. <p class="ant-upload-drag-icon">
  17. <a-icon type="inbox" />
  18. </p>
  19. <p class="ant-upload-text">{{$t('cloudenv.text_117')}}</p>
  20. <p class="ant-upload-hint">{{$t('cloudenv.text_118')}}</p>
  21. </div>
  22. </a-upload-dragger>
  23. <a-input v-show="false" v-decorator="decorators.file" type="text" />
  24. </a-form-item>
  25. <slot v-else />
  26. </div>
  27. </template>
  28. <script>
  29. export default {
  30. name: 'ObjectsUploadFileDialog',
  31. props: {
  32. fc: {
  33. type: Object,
  34. },
  35. },
  36. data () {
  37. return {
  38. type: 1,
  39. fileList: [],
  40. loading: false,
  41. }
  42. },
  43. computed: {
  44. decorators () {
  45. return {
  46. file: ['file', {
  47. rules: [{ required: true, message: this.$t('cloudenv.text_119') }],
  48. }],
  49. }
  50. },
  51. uploadDraggerConfig () {
  52. return {
  53. name: 'file',
  54. accept: '.json',
  55. headers: {
  56. Authorization: `Bearer ${this.$store.getters.userInfo.session}`,
  57. },
  58. }
  59. },
  60. },
  61. watch: {
  62. fileList (value, oldValue) {
  63. this.fc.setFieldsValue({
  64. file: (value && value.length > 0) || undefined,
  65. }, () => {
  66. this.fc.validateFields(['file'])
  67. })
  68. },
  69. },
  70. created () {
  71. this.fileList = []
  72. },
  73. methods: {
  74. handleTypeChange () {
  75. this.fileList = []
  76. },
  77. setValues (jsonStr) {
  78. if (!jsonStr || jsonStr === '') return false
  79. const json = JSON.parse(jsonStr)
  80. for (const key in json) {
  81. const k = ['project_id', 'private_key_id', 'private_key', 'client_email'].indexOf(key) > -1 ? `gcp_${key}` : key
  82. this.fc.getFieldDecorator(k, { preserve: true })
  83. this.fc.setFieldsValue({
  84. [k]: json[key],
  85. })
  86. }
  87. },
  88. handleBeforeUpload (file, fileList) {
  89. const isJson = file.name.endsWith('.json')
  90. const isLt2M = file.size / 1024 / 1024 < 2
  91. if (!isJson) {
  92. this.$message.error(this.$t('cloudenv.text_120'))
  93. return false
  94. }
  95. if (!isLt2M) {
  96. this.$message.error(this.$t('cloudenv.text_121'))
  97. return false
  98. }
  99. file.text().then(jsonStr => {
  100. this.setValues(jsonStr)
  101. })
  102. // eslint-disable-next-line vue/no-side-effects-in-computed-properties
  103. this.fileList = [file]
  104. return false
  105. },
  106. hanldeRemoveFile (file) {
  107. this.fileList = []
  108. return true
  109. },
  110. },
  111. }
  112. </script>
  113. <style>
  114. .google-account-file .ant-upload-drag-container {
  115. display: block !important;
  116. }
  117. .google-account-file .ant-form-item-label {
  118. visibility: hidden;
  119. }
  120. </style>