import-dialog.vue 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <!-- 上传 -->
  3. <el-dialog title="导入文件上传" :visible.sync="dialogVisible" width="40%">
  4. <el-form label-width="110px" class="zw-criterion">
  5. <el-form-item label="选择文件">
  6. <el-upload
  7. class="avatar-uploader"
  8. action="#"
  9. :show-file-list="false"
  10. :http-request="handlSuccess"
  11. :multiple="true"
  12. >
  13. <el-button icon="el-icon-plus" size="small" type="primary"
  14. >文件上传</el-button
  15. >
  16. <div slot="tip" class="el-upload__tip" v-if="fileUrl">
  17. 只能上传excel文件,点击
  18. <el-link
  19. type="primary"
  20. :underline="false"
  21. @click="downLoadTemplate()"
  22. >
  23. 下载模板</el-link
  24. >
  25. </div>
  26. </el-upload>
  27. </el-form-item>
  28. <el-form-item label="上传列表">
  29. <div class="imgs-box">
  30. <p v-for="(item, index) in attaments" :key="index" class="imgs-p">
  31. <span> {{ item.name }}</span>
  32. <el-link @click="delFileList(index)" type="primary">删除</el-link>
  33. </p>
  34. </div>
  35. </el-form-item>
  36. </el-form>
  37. <div slot="footer" class="dialog-footer">
  38. <el-button size="small" @click="dialogVisible = false">关 闭</el-button>
  39. <el-button size="small" @click="upload" type="primary">上 传</el-button>
  40. </div>
  41. <div id="progress" v-if="isProgress">
  42. <el-progress
  43. type="circle"
  44. :percentage="percentage"
  45. text-color="#fff"
  46. ></el-progress>
  47. </div>
  48. </el-dialog>
  49. </template>
  50. <script>
  51. import { importBatch,downLoadTemplate } from '@/api/system/file/index.js';
  52. import { download1 } from '@/utils/file';
  53. import { number } from 'echarts';
  54. export default {
  55. props: {
  56. // eslint-disable-next-line vue/require-prop-type-constructor
  57. defModule: '',
  58. fileUrl: '',
  59. fileName: '',
  60. apiUrl: '',
  61. isWeb: {
  62. type: Boolean,
  63. default: true
  64. } ,
  65. fileKeyName:'file'
  66. },
  67. //注册组件
  68. data() {
  69. return {
  70. showViewer: false, // 显示查看器
  71. dialogVisible: false,
  72. uploadShow: false,
  73. isProgress: false,
  74. module: '',
  75. percentage: 0,
  76. attaments: [], //上传文件
  77. file: ''
  78. };
  79. },
  80. created() {},
  81. methods: {
  82. open() {
  83. this.attaments = [];
  84. this.module = '';
  85. this.dialogVisible = true;
  86. },
  87. //删除附件
  88. delFileList(index) {
  89. this.attaments.splice(index, 1);
  90. },
  91. //上传限制
  92. beforeUpload(file) {
  93. const isLt10M = file.size / 1024 / 1024 < 10;
  94. if (!isLt10M) {
  95. this.$message.error('导入单文件大小不能超过 10MB!');
  96. }
  97. return isLt10M;
  98. },
  99. //图片上传
  100. handlSuccess(param) {
  101. this.file = param.file;
  102. this.attaments.push(param.file);
  103. },
  104. // 文件上传
  105. async upload() {
  106. if (this.attaments.length == 0) {
  107. return this.$message.warning('文件不能为空!');
  108. }
  109. this.module = this.$props.defModule;
  110. this.isProgress = true;
  111. this.percentage = 0;
  112. await importBatch(
  113. {
  114. module: this.module,
  115. multiPartFiles: this.attaments
  116. },
  117. this.apiUrl,
  118. (data) => {
  119. console.log(data, 'data');
  120. this.percentage = parseFloat(
  121. ((data.loaded * 100) / data.total).toFixed(2)
  122. );
  123. if (this.percentage >= 100) {
  124. setTimeout(() => {
  125. this.isProgress = false;
  126. }, 500);
  127. }
  128. },
  129. this.fileKeyName
  130. )
  131. .then((res) => {
  132. this.$message.success('操作成功!');
  133. this.dialogVisible = false;
  134. this.isProgress = false;
  135. this.$emit('success');
  136. })
  137. .catch(() => {
  138. this.isProgress = false;
  139. });
  140. },
  141. //下载模板
  142. downLoadTemplate() {
  143. if(this.isWeb){
  144. download1(window.location.origin + this.fileUrl, this.fileName);
  145. }else{
  146. downLoadTemplate(this.fileUrl,this.fileName);
  147. }
  148. }
  149. }
  150. };
  151. </script>
  152. <style lang="scss">
  153. .zw-table-header {
  154. float: right;
  155. }
  156. .imgs-box .imgs-p {
  157. height: 30px;
  158. background: #f0f3f3;
  159. line-height: 30px;
  160. width: 372px;
  161. margin-bottom: 5px;
  162. padding: 0 10px;
  163. display: flex;
  164. justify-content: space-between;
  165. }
  166. .zw-criterion-normal {
  167. padding: 20px 0 0 0;
  168. }
  169. .el-main {
  170. overflow: hidden;
  171. }
  172. #progress {
  173. background: rgba($color: #000000, $alpha: 0.5);
  174. position: fixed;
  175. width: 100%;
  176. height: 100%;
  177. top: 0;
  178. left: 0;
  179. display: flex;
  180. justify-content: center;
  181. align-items: center;
  182. }
  183. </style>