Import.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <template>
  2. <base-dialog @cancel="cancelDialog">
  3. <div slot="header">{{this.params.title}}</div>
  4. <div slot="body">
  5. <a-form :form="form.fc" v-bind="formItemLayout">
  6. <a-form-item>
  7. <a-upload-dragger
  8. name="file"
  9. v-decorator="decorators.file"
  10. accept=".ocdb"
  11. :fileList="fileList"
  12. :multiple="false"
  13. :beforeUpload="handleBeforeUpload"
  14. @change="handleChange">
  15. <p class="ant-upload-drag-icon">
  16. <a-icon type="inbox" />
  17. </p>
  18. <p class="ant-upload-text">{{$t('dashboard.text_112')}}</p>
  19. </a-upload-dragger>
  20. </a-form-item>
  21. </a-form>
  22. </div>
  23. <div slot="footer">
  24. <a-button type="primary" @click="handleConfirm" :loading="loading">{{ $t("dialog.ok") }}</a-button>
  25. <a-button @click="cancelDialog">{{ $t('dialog.cancel') }}</a-button>
  26. </div>
  27. </base-dialog>
  28. </template>
  29. <script>
  30. import { mapGetters } from 'vuex'
  31. import { Base64 } from 'js-base64'
  32. import DialogMixin from '@/mixins/dialog'
  33. import WindowsMixin from '@/mixins/windows'
  34. import { uuid } from '@/utils/utils'
  35. export default {
  36. name: 'DashboardImport',
  37. mixins: [DialogMixin, WindowsMixin],
  38. data () {
  39. return {
  40. loading: false,
  41. form: {
  42. fc: this.$form.createForm(this),
  43. },
  44. fileList: [],
  45. decorators: {
  46. file: [
  47. 'file',
  48. {
  49. rules: [
  50. { required: true, message: this.$t('dashboard.text_113') },
  51. ],
  52. },
  53. ],
  54. },
  55. formItemLayout: {
  56. wrapperCol: {
  57. span: 24,
  58. },
  59. },
  60. }
  61. },
  62. computed: {
  63. ...mapGetters(['scope']),
  64. },
  65. destroyed () {
  66. this.pm = null
  67. },
  68. created () {
  69. this.pm = new this.$Manager('parameters', 'v1')
  70. },
  71. methods: {
  72. readFileAsync (file) {
  73. return new Promise((resolve, reject) => {
  74. const reader = new FileReader()
  75. reader.onload = () => {
  76. resolve(reader.result)
  77. }
  78. reader.onerror = reject
  79. reader.readAsDataURL(file)
  80. })
  81. },
  82. handleBeforeUpload () {
  83. return false
  84. },
  85. handleChange (info) {
  86. let fileList = [...info.fileList]
  87. fileList = fileList.slice(-1)
  88. this.fileList = fileList
  89. },
  90. async handleConfirm () {
  91. this.loading = true
  92. try {
  93. const values = await this.form.fc.validateFields()
  94. const fileData = await this.readFileAsync(values.file.file)
  95. const fileDataArr = fileData.split(',')
  96. const objStr = Base64.decode(Base64.decode(fileDataArr[1]))
  97. const data = JSON.parse(objStr)
  98. if (data.scope !== this.scope) {
  99. this.$message.warning(this.$t('dashboard.text_114', [this.$t(`policyScopeLabel.${data.scope}`), this.$t(`policyScopeLabel.${this.scope}`)]))
  100. this.loading = false
  101. return
  102. }
  103. // 检查是否已经创建了dashboard配置
  104. const optionsCreated = await this.params.checkOptionsCreated()
  105. if (!optionsCreated) {
  106. await this.params.initOptions()
  107. }
  108. // 更新options
  109. const options = [...this.params.options]
  110. const id = `dashboard-${this.scope}-panel-${uuid(16)}`
  111. const item = {
  112. name: this.params.genName(data.name),
  113. id,
  114. }
  115. options.push(item)
  116. await this.params.updateOptions(options)
  117. // 创建panel数据配置
  118. const panelData = {}
  119. for (let i = 0, len = data.items.length; i < len; i++) {
  120. panelData[`dashboard-item-${uuid(32)}`] = data.items[i]
  121. }
  122. await this.pm.create({
  123. data: {
  124. name: id,
  125. value: panelData,
  126. },
  127. })
  128. this.cancelDialog()
  129. this.params.selectOption(item)
  130. } catch (error) {
  131. throw error
  132. } finally {
  133. this.loading = false
  134. }
  135. },
  136. },
  137. }
  138. </script>