xiangpei
2025-05-06 5c929cfb5286a31a4e067cbc61e8774f4e7d42ae
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
 
<template>
  <div>
    <el-dialog
      :visible.sync="fileDialogVisible"
      ref="formDialogRef"
      width="35%"
      append-to-body
      close-on-click-modal
      @close="closeDialog"
    >
      <template slot="title">
        <span style="padding-bottom: 18px">
          {{ isImportOrExport ? '请按照需求导出目标内容' : '请按照模板样式上传项目文件' }}
        </span>
      </template>
      <template slot="default">
        <div v-if="!isImportOrExport" class="dialog-content">
          <el-upload
            ref="uploadRef"
            class="upload-demo"
            :action="uploadUrl"
            :limit="1"
            :accept="accept"
            :headers="uploadHeaders"
            :disabled="uploadIsUploading"
            :on-progress="handleFileUploadProgress"
            :on-success="handleFileSuccess"
            :auto-upload="false"
            drag
          >
            <i class="el-icon-upload"></i>
            <div class="el-upload__text">请上传<em>Zip</em>文件,大小在<em>100M</em>以内</div>
          </el-upload>
          <span>仅允许导入zip格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size: 12px; vertical-align: baseline" @click="handleDownloadFile">下载模板</el-link>
        </div>
        <div v-else-if="isImportOrExport" class="dialog-content">
          <el-button class="export-button" @click="handleDownloadTargetList">导出目标列表内容</el-button>
          <el-button class="export-button">导出所有列表内容</el-button>
          <el-button class="export-button">导出目标项目详情</el-button>
          <el-button class="export-button">导出所有项目详情</el-button>
        </div>
      </template>
      <template slot="footer">
        <div v-if="!isImportOrExport" class="dialog-footer">
          <el-button type="primary" @click="submitFileForm">确 定</el-button>
          <el-button @click="closeDialog">取 消</el-button>
        </div>
        <div v-else-if="isImportOrExport"></div>
      </template>
    </el-dialog>
  </div>
</template>
 
<script>
import { globalHeaders } from '@/utils/request';
import {getToken} from "@/utils/auth";
 
export default {
  name: 'FileDialog',
  props: {
    isImportOrExport: {
      type: Boolean,
      default: false
    },
    fileDialogVisible: {
      type: Boolean,
      default: false
    },
    currentColumns: {
      type: Array,
      default: function () {
        return [];
      }
    }
  },
  data() {
    return {
      uploadRef: null,
      targetColumn: [],
      accept: `.zip`,
      uploadUrl: '/project/import',
      uploadHeaders: {},
      uploadIsUploading: false
    };
  },
  methods: {
    submit() {
      console.log("子组件submit")
    },
    handleFileUploadProgress() {
      this.uploadIsUploading = true;
    },
    handleFileSuccess(response, file) {
      this.uploadIsUploading = false;
      if (this.uploadRef) {
        this.uploadRef.handleRemove(file);
      }
      if (response.code === 200) {
        this.$emit('fileDialogCancel');
        this.$message({
          message: response.msg,
          type: 'success'
        });
      } else {
        this.$message.error(response.msg);
      }
    },
    handleDownloadFile() {
      // fetch(`${process.env.VITE_APP_BASE_API}/project/info/export/template`, {
      //   method: 'GET',
      //   headers: self.upload.headers
      // })
      //   .then(response => response.blob())
      //   .then(blob => {
      //     const url = window.URL.createObjectURL(blob);
      //     const a = document.createElement('a');
      //     a.style.display = 'none';
      //     a.href = url;
      //     a.download = `项目文件模板_${new Date().getTime()}.zip`;
      //     document.body.appendChild(a);
      //     a.click();
      //     window.URL.revokeObjectURL(url);
      //   })
      //   .catch(error => {
      //     console.error('文件下载失败:', error);
      //   });
 
      const url = process.env.VUE_APP_BASE_API + '/project/info/export/template';
      axios.post(url, [], { // 发送一个空数组而不是空对象
        responseType: 'blob', // 告诉axios期望服务器返回的是blob类型
        headers: {
          'Content-Type': 'application/json',
          Authorization: "Bearer " + getToken()
        }
      })
        .then(response => {
          // 处理文件下载
          const blob = new Blob([response.data], { type: 'application/zip' }); // 指定MIME类型为zip
          const url = window.URL.createObjectURL(blob);
          const a = document.createElement('a');
          a.style.display = 'none';
          a.href = url;
          a.download = `项目文件模板_${new Date().getTime()}.zip`;
          document.body.appendChild(a);
          a.click();
          document.body.removeChild(a);
          window.URL.revokeObjectURL(url);
        })
        .catch(error => {
          console.error('There was an error!', error);
        });
    },
    submitFileForm() {
      if (this.uploadRef) {
        this.uploadRef.submit();
      }
    },
    closeDialog() {
      this.$emit('fileDialogCancel');
    },
    handleDownloadTargetList() {
      console.log('导出目标列表内容', this.currentColumns);
      this.targetColumn = this.currentColumns.filter(item => item.visible);
    }
  },
  mounted() {
    this.uploadRef = this.$refs.uploadRef;
  },
  created() {
 
    this.isFileDialogVisible = this.fileDialogVisible;
  }
};
</script>
 
<style scoped lang="scss">
.dialog-content {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 300px;
 
  .export-button {
    margin-left: 0;
    margin-bottom: 10px;
  }
}
.dialog-footer {
  display: flex;
  justify-content: center;
}
.upload-demo {
  width: 100%;
}
::v-deep  .el-upload{
  width: 100%;
}
::v-deep .el-upload .el-upload-dragger{
  width: 100%;
}
</style>