index.vue 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <script>
  2. export default {
  3. name:'BatchImport',
  4. props:{
  5. disabled:{
  6. type:Boolean,
  7. default:false,
  8. },
  9. limit:{
  10. type:Number,
  11. default:1,
  12. },
  13. fileType:{
  14. type:Array,
  15. default:()=>['xls', 'xlsx'],
  16. },
  17. size:{
  18. type:String,
  19. default:'mini',
  20. },
  21. isShowTip:{
  22. type:Boolean,
  23. default:true,
  24. },
  25. isTemplate:{
  26. type:Boolean,
  27. default:true,
  28. },
  29. fileSize: {
  30. type: Number,
  31. default: 100,
  32. },
  33. },
  34. computed:{
  35. // 是否显示提示
  36. showTip() {
  37. return this.isShowTip && (this.fileType || this.fileSize);
  38. },
  39. accept(){
  40. return this.fileType.map(item =>(`.${item}`)).join(',');
  41. }
  42. },
  43. data(){
  44. return {
  45. title:'批量导入',
  46. visible:false,
  47. loading:false,
  48. fileList:[],
  49. number: 0,
  50. }
  51. },
  52. methods:{
  53. // 确认上传
  54. async submitUpload(){
  55. if(this.fileList.length){
  56. this.$emit('import',this.fileList);
  57. }else{
  58. this.$notify.warning({
  59. title:'警告',
  60. message: '请上传文件之后在确认!',
  61. });
  62. }
  63. },
  64. // 模板下载
  65. async templateDownload(){
  66. this.$emit('temDownload')
  67. // 放父组件
  68. // this.download('/system/material/download', {}, `物料基本信息模板.xlsx`);
  69. },
  70. //
  71. beforeClose(done){
  72. this.fileList = [];
  73. // 关闭前清空
  74. done();
  75. },
  76. // 预览
  77. handlePreview(){},
  78. // 移除
  79. handleRemove(file, fileList){
  80. this.fileList = fileList;
  81. },
  82. // 文件改变
  83. onChange(file, fileList){
  84. this.fileList = fileList;
  85. },
  86. setVisible(prop){
  87. this.visible = prop;
  88. if(!prop){
  89. this.fileList = [];
  90. }
  91. },
  92. open(){
  93. this.setVisible(true);
  94. },
  95. // 取消
  96. cancal(){
  97. this.setVisible(false);
  98. },
  99. // 上传前校检格式和大小
  100. handleBeforeUpload(file) {
  101. // 校检文件类型
  102. if (this.fileType) {
  103. const fileName = file.name.split(".");
  104. const fileExt = fileName[fileName.length - 1];
  105. const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
  106. if (!isTypeOk) {
  107. this.$modal.msgError(
  108. `文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`
  109. );
  110. return false;
  111. }
  112. }
  113. // 校检文件大小
  114. if (this.fileSize) {
  115. const isLt = file.size / 1024 / 1024 < this.fileSize;
  116. if (!isLt) {
  117. this.$modal.msgError(`上传文件大小不能超过 ${this.fileSize} MB!`);
  118. return false;
  119. }
  120. }
  121. this.$modal.loading("正在上传文件,请稍候...");
  122. this.number++;
  123. return true;
  124. },
  125. },
  126. created(){},
  127. }
  128. </script>
  129. <template>
  130. <el-button
  131. :disabled="disabled"
  132. :size="size"
  133. @click="open"
  134. type="primary"
  135. v-bind="$attrs"
  136. v-on="$listeners"
  137. >
  138. {{ title }}
  139. <el-dialog
  140. :title="title"
  141. :visible.sync="visible"
  142. v-loading="loading"
  143. width="35%"
  144. center
  145. :before-close="beforeClose"
  146. append-to-body
  147. >
  148. <el-upload
  149. ref="upload"
  150. action="#"
  151. :on-preview="handlePreview"
  152. :accept="accept"
  153. :before-upload="handleBeforeUpload"
  154. :on-remove="handleRemove"
  155. :file-list="fileList"
  156. :auto-upload="false"
  157. :on-change="onChange"
  158. :limit="limit"
  159. :disabled="disabled"
  160. style="text-align: center;"
  161. >
  162. <el-button
  163. slot="trigger"
  164. :size="size"
  165. type="primary"
  166. >选取文件</el-button>
  167. <el-button
  168. v-if="isTemplate"
  169. style="margin-left: 10px;"
  170. :size="size"
  171. type="success"
  172. @click="templateDownload"
  173. >下载模板</el-button>
  174. <div slot="tip" class="el-upload__tip" v-if="isShowTip">
  175. 请上传
  176. <template v-if="fileSize">
  177. 大小不超过 <b style="color: #f56c6c">{{ fileSize }}MB</b>
  178. </template>
  179. <template v-if="fileType">
  180. 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b>
  181. </template>
  182. </div>
  183. </el-upload>
  184. <div slot="footer" class="dialog-footer">
  185. <el-button type="primary" @click="submitUpload" :size="size">确 定</el-button>
  186. <el-button @click="cancal" :size="size">取 消</el-button>
  187. </div>
  188. </el-dialog>
  189. </el-button>
  190. </template>