1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<template>
  <BaseCard
    size="small"
    direction="top-left"
    borderless="t"
    v-loading="loading"
  >
    <template #content>
      <div class="download">
        <el-button
          @click="downloadTemplate"
          type="primary"
          class="el-button-custom"
          size="small"
          >下载模板</el-button
        >
      </div>
      <el-form :model="formInfo" :rules="rules" ref="formRef">
        <el-form-item label="时间" prop="dateTime">
          <el-date-picker
            v-model="formInfo.dateTime"
            type="date"
            placeholder="选择时间"
            size="small"
          />
        </el-form-item>
        <el-form-item label="">
          <label
            ><el-checkbox
              v-model="formInfo.update"
              label=""
              size="small"
            />覆盖旧数据</label
          >
        </el-form-item>
        <el-form-item>
          <el-upload
            v-model:file-list="formInfo.file"
            accept=".xlsx"
            :limit="1"
            :auto-upload="false"
          >
            <el-button
              :disabled="!formInfo.dateTime && !formInfo.groupId"
              type="primary"
              class="el-button-custom select-file-button"
              size="small"
              >选择文件</el-button
            >
          </el-upload>
        </el-form-item>
 
        <el-form-item>
          <el-button
            @click="handleImportClick"
            type="primary"
            class="el-button-custom import-button"
            size="small"
          >
            导入
          </el-button>
        </el-form-item>
      </el-form>
    </template>
  </BaseCard>
</template>
<script>
import gridApi from '@/api/gridApi';
import { dayjs, ElMessage } from 'element-plus';
export default {
  props: {
    gridGroup: {
      type: Object,
      default: () => {}
    }
  },
  emits: ['submit'],
  methods: {
    downloadTemplate() {
      gridApi.downloadTemplate();
    },
    handleImportClick() {
      this.$refs.formRef.validate((valid) => {
        if (valid) {
          this.loading = true;
          const isUpdate = this.formInfo.update ? 1 : 0;
          const type = 0;
 
          const formData = new FormData();
          // 文件转换为二进制
          const reader = new FileReader();
          reader.readAsArrayBuffer(this.formInfo.file[0].raw);
          reader.onload = async (theFile) => {
            const binary = new Blob([theFile.target.result], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'});
            formData.append('excel', binary);
            formData.append('groupId', this.gridGroup.id);
            formData.append('type', type);
            formData.append(
              'dateTime',
              dayjs(this.formInfo.dateTime).format('YYYY-MM-DD HH:mm:ss')
            );
            formData.append('update', isUpdate);
            gridApi
              .importData(formData)
              .then((res) => {
                this.loading = false;
                if (res && res.data.success) {
                  // 导入成功,触发父组件刷新事件
                  this.$emit('submit');
                  ElMessage({
                    message: res.data.result,
                    type: 'success'
                  });
                } else {
                  ElMessage({
                    message: res.data.result,
                    type: 'warning'
                  });
                }
              })
          };
        }
      });
    }
  },
  data() {
    return {
      loading: false,
      formInfo: {
        dateTime: undefined,
        groupId: undefined,
        update: false,
        file: []
      },
      rules: {
        dateTime: [{ required: true, message: '时间不能为空', trigger: 'blur' }]
      }
    };
  }
};
</script>
<style scoped>
.download {
  display: flex;
  justify-content: flex-end;
}
.select-file-button {
  margin-left: 5px;
}
.import-button {
  margin-left: 5px;
  margin-top: -7px;
}
::v-deep .el-upload-list__item-file-name {
  max-width: 50% !important;
}
</style>