upload.vue 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <div class="upload_box">
  3. <el-button :disabled="existence" type="text" @click="upload"><i class="el-dropdown-link el-icon-download"></i>{{ typeName || '文件导入' }}</el-button>
  4. <input type="file" @change="fileChang" class="filesName" />
  5. <el-dialog :close-on-click-modal="false" :title="typeName" :visible.sync="dialogVisible" width="400px" @close="closes">
  6. <div>
  7. <el-input size="small" v-model="fileName" placeholder="请选择数据文件" disabled>
  8. <el-button slot="append" @click="selectFile">选择文件</el-button>
  9. </el-input>
  10. <div style="margin: 30px 0;"></div>
  11. <!-- <label v-if="strategy">数据处理策略:</label>
  12. <el-radio v-if="strategy" v-model="option" label="skip">跳过</el-radio>
  13. <el-radio v-if="strategy" v-model="option" label="overwrite">覆盖</el-radio> -->
  14. </div>
  15. <el-dropdown slot="footer" class="left-footer" size="small" trigger="click" @command="templates" v-if="template">
  16. <span class="el-dropdown-link"> 下载数据模板<i class="el-icon-arrow-down el-icon--right"></i> </span>
  17. <el-dropdown-menu slot="dropdown">
  18. <!-- <el-dropdown-item command="csv">文本文件(.csv)</el-dropdown-item> -->
  19. <el-dropdown-item command="excel">Excel文件(.xls)</el-dropdown-item>
  20. </el-dropdown-menu>
  21. </el-dropdown>
  22. <span slot="footer" class="dialog-footer">
  23. <el-button size="small" @click="dialogVisible = false">取 消</el-button>
  24. <el-button size="small" type="primary" @click="uploadFile">确 定</el-button>
  25. </span>
  26. </el-dialog>
  27. <el-dialog :close-on-click-modal="false" :title="successDate ? '数据导入进度' : '数据导入结果'" :visible.sync="progress" width="400px">
  28. <!-- 显示进度 -->
  29. <div v-if="successDate">
  30. <h3>{{ typeName }}</h3>
  31. <div style="margin: 30px 0;"></div>
  32. <el-progress :percentage="percentage"></el-progress>
  33. <div style="margin: 30px 0;"></div>
  34. </div>
  35. <!-- 显示成功后信息下载日志 -->
  36. <div v-else>
  37. <h3 class="success">{{ summary }}</h3>
  38. <div style="margin: 30px 0;"></div>
  39. <!-- <span slot="footer" class="dialog-footer">
  40. <el-button type="text" @click="btn">数据导入日志下载</el-button>
  41. </span> -->
  42. </div>
  43. </el-dialog>
  44. </div>
  45. </template>
  46. <script>
  47. import { mapActions } from 'vuex';
  48. export default {
  49. props: {
  50. // 数据导入类型 (user,vip)
  51. type: String,
  52. // 业务名称
  53. typeName: { type: String, default: '' },
  54. template: { type: Boolean, default: true },
  55. strategy: { type: Boolean, default: true }
  56. },
  57. data() {
  58. return {
  59. // 下载文件类型
  60. fileType: 'excel',
  61. // 选择信息弹出
  62. dialogVisible: false,
  63. // 按钮禁用
  64. existence: false,
  65. // 进度
  66. percentage: 0,
  67. // 上传前选择
  68. option: 'overwrite',
  69. // 进度弹出
  70. progress: false,
  71. // 成功后切换
  72. successDate: true,
  73. // 成功后文字
  74. summary: null,
  75. // 任务id
  76. id: null,
  77. // 导入模板命名
  78. fileName: null,
  79. // 文件流
  80. file: null
  81. };
  82. },
  83. methods: {
  84. ...mapActions(['uploads', 'progressInfo']),
  85. // 点击上传按钮
  86. upload() {
  87. this.dialogVisible = true;
  88. },
  89. // 选择完弹出信息确定
  90. selectFile() {
  91. document.getElementsByClassName('filesName')[0].value = '';
  92. document.getElementsByClassName('filesName')[0].click();
  93. },
  94. // 选择文件改变
  95. async fileChang() {
  96. this.file = document.getElementsByClassName('filesName')[0].files[0];
  97. this.fileName = this.file.name;
  98. },
  99. // 文件上传
  100. async uploadFile() {
  101. if (this.file == null) {
  102. this.$message.warning('请选择数据文件');
  103. return;
  104. }
  105. this.dialogVisible = false;
  106. this.summary = null;
  107. // 上传文件获取任务id参数format this.progress = true;弹出进度窗口成功后提示成功 切换显示成功后信息successDate = false
  108. const data = new FormData();
  109. data.append('file', this.file);
  110. data.append('type', this.type);
  111. data.append('option', this.option);
  112. const res = await this.uploads(data);
  113. if (res && !res.errcode) {
  114. this.progress = true;
  115. this.timeout(res.taskId);
  116. } else {
  117. document.getElementsByClassName('filesName')[0].value = '';
  118. this.$message.error(res.errmsg);
  119. }
  120. },
  121. timeout(res) {
  122. const Interval = setTimeout(async () => {
  123. const info = await this.progressInfo({ taskId: res });
  124. if (info && info.status !== 'error') {
  125. this.percentage = info.progress || 0;
  126. if (info.progress == 100) {
  127. this.id = info.taskId;
  128. this.successDate = false;
  129. this.summary = '导入成功';
  130. clearInterval(Interval);
  131. document.getElementsByClassName('filesName')[0].value = '';
  132. this.$emit('refreshData');
  133. this.percentage = 0;
  134. } else {
  135. this.timeout(info.taskId);
  136. }
  137. } else {
  138. clearInterval(Interval);
  139. this.$message.error(info?.errmsg || '错误');
  140. }
  141. }, 1000);
  142. },
  143. // 详情下载
  144. async btn() {
  145. window.open(`/api/xms/task/${this.id}/log?fileName=${this.typeName}日志`);
  146. },
  147. // 上传模板下载
  148. async templates(fileType) {
  149. // type 上传类型(user,vip)、fileType文件类型(现在只有excel)、fileName文件名称
  150. window.open(`/api/importandexport/handle/template?type=${this.type}&fileType=${fileType}&fileName=${this.typeName}模板`);
  151. },
  152. // 关闭窗口
  153. closes() {
  154. this.successDate = true;
  155. this.file = null;
  156. this.fileName = null;
  157. this.$emit('closes');
  158. }
  159. }
  160. };
  161. </script>
  162. <style lang="scss" scoped>
  163. input {
  164. display: none;
  165. }
  166. .el-button--text {
  167. padding: 0;
  168. }
  169. .upload_box {
  170. line-height: 0;
  171. }
  172. .success {
  173. width: 100%;
  174. color: #67c23a;
  175. }
  176. .error {
  177. width: 100%;
  178. color: #f56c6c;
  179. }
  180. .jump {
  181. width: 100%;
  182. color: #e6a23c;
  183. }
  184. .left-footer {
  185. float: left;
  186. margin-top: 10px;
  187. }
  188. </style>