BatchImportModal.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <template>
  2. <el-dialog
  3. v-model="visible"
  4. title="批量导入事件"
  5. width="500px"
  6. :before-close="handleClose"
  7. >
  8. <el-form :model="form" label-width="80px">
  9. <el-form-item label="选择应用" required>
  10. <el-select
  11. v-model="form.selectedApps"
  12. multiple
  13. placeholder="请选择应用"
  14. style="width: 100%"
  15. >
  16. <el-option
  17. v-for="app in appOptions"
  18. :key="app.value"
  19. :label="app.label"
  20. :value="app.value"
  21. />
  22. </el-select>
  23. </el-form-item>
  24. <el-form-item label="选择文件">
  25. <el-button @click="selectFile">
  26. <el-icon><Upload /></el-icon>
  27. 选择Excel文件
  28. </el-button>
  29. <br>
  30. <div v-if="form.fileName" class="file-name">{{ form.fileName }}</div>
  31. </el-form-item>
  32. <el-form-item label=" ">
  33. <el-button @click="downloadTemplate">
  34. <el-icon><Download /></el-icon>
  35. Excel模板下载
  36. </el-button>
  37. </el-form-item>
  38. </el-form>
  39. <div class="tips">
  40. <div class="tips-title">说明:</div>
  41. <div class="tips-content">
  42. <p>① 命名规范:事件标识符(event id)、事件属性标识符(key)命名支持字母(建议小写,避免使用中文)、数字、下划线 (_)、中划线 (-)、小数点 (.)及加号(+) 。event id和key长度不能超过128个字节。</p>
  43. <p>② 事件类型,1表示计算事件,0表示多参数类型事件</p>
  44. <p>③ 属性类型:number数值型(可计算),string字符串</p>
  45. </div>
  46. </div>
  47. <template #footer>
  48. <span class="dialog-footer">
  49. <el-button @click="handleClose">取消</el-button>
  50. <el-button type="primary" @click="handleImport" :disabled="!canImport">导入</el-button>
  51. </span>
  52. </template>
  53. </el-dialog>
  54. </template>
  55. <script setup lang="ts">
  56. import { ref, computed, reactive } from 'vue'
  57. import { Upload, Download } from '@element-plus/icons-vue'
  58. interface Props {
  59. modelValue: boolean
  60. }
  61. interface Emits {
  62. (e: 'update:modelValue', value: boolean): void
  63. (e: 'import', data: any): void
  64. }
  65. const props = defineProps<Props>()
  66. const emit = defineEmits<Emits>()
  67. // 可见性绑定
  68. const visible = computed({
  69. get: () => props.modelValue,
  70. set: (value) => emit('update:modelValue', value)
  71. })
  72. // 表单数据
  73. const form = reactive({
  74. selectedApps: [] as string[],
  75. fileName: '',
  76. file: null as File | null
  77. })
  78. // 应用选项
  79. const appOptions = [
  80. { label: '教育行业Demo', value: 'education' },
  81. { label: '游戏行业Demo', value: 'game' },
  82. { label: '通用行业Demo', value: 'general' }
  83. ]
  84. // 是否可以导入(已选择应用和文件)
  85. const canImport = computed(() => {
  86. return form.selectedApps.length > 0 && form.file !== null
  87. })
  88. // 关闭弹窗
  89. const handleClose = () => {
  90. visible.value = false
  91. // 重置表单
  92. form.selectedApps = []
  93. form.fileName = ''
  94. form.file = null
  95. }
  96. // 选择文件
  97. const selectFile = () => {
  98. const input = document.createElement('input')
  99. input.type = 'file'
  100. input.accept = '.xlsx,.xls'
  101. input.onchange = (e) => {
  102. const file = (e.target as HTMLInputElement).files?.[0]
  103. if (file) {
  104. form.file = file
  105. form.fileName = file.name
  106. }
  107. }
  108. input.click()
  109. }
  110. // 下载模板
  111. const downloadTemplate = () => {
  112. // 这里可以实现实际的模板下载逻辑
  113. console.log('下载模板')
  114. // 示例:创建一个虚拟的下载链接
  115. const link = document.createElement('a')
  116. link.href = 'data:application/vnd.ms-excel;base64,UEsDBAoAAAAAALJdHFYAAAAA' // 示例base64
  117. link.download = '事件导入模板.xlsx'
  118. link.click()
  119. }
  120. // 处理导入
  121. const handleImport = () => {
  122. if (!canImport.value) return
  123. emit('import', {
  124. apps: form.selectedApps,
  125. file: form.file
  126. })
  127. handleClose()
  128. }
  129. </script>
  130. <style scoped lang="scss">
  131. .file-name {
  132. margin-top: 8px;
  133. font-size: 14px;
  134. color: #606266;
  135. }
  136. .tips {
  137. background-color: #f5f7fa;
  138. border-radius: 4px;
  139. padding: 12px;
  140. margin-top: 20px;
  141. .tips-title {
  142. font-weight: bold;
  143. margin-bottom: 8px;
  144. color: #303133;
  145. }
  146. .tips-content {
  147. font-size: 12px;
  148. color: #606266;
  149. line-height: 1.5;
  150. p {
  151. margin: 0 0 4px 0;
  152. }
  153. }
  154. }
  155. .dialog-footer {
  156. display: flex;
  157. justify-content: flex-end;
  158. gap: 10px;
  159. }
  160. </style>