excelImportWithData.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <template>
  2. <div style="">
  3. <!--
  4. v-loading="uploadConfig.loading"
  5. element-loading-text="正在上传..."-->
  6. <el-upload
  7. :action="uploadConfig.uploadFileApiUrl+flag"
  8. :show-file-list="false"
  9. :on-success="handleImageSuccess"
  10. :before-upload="beforeImageUpload"
  11. :headers="myHeaders"
  12. :title="title"
  13. ref="uploadCtl"
  14. :data="data"
  15. >
  16. <el-button type="primary" round><slot>{{this.title}}</slot></el-button>
  17. </el-upload>
  18. </div>
  19. </template>
  20. <script>
  21. import { getToken } from '@/utils/auth'
  22. import { Loading } from 'element-ui'
  23. export default {
  24. data () {
  25. return {
  26. allowFileTypes:'.xls|.xlsx',
  27. //上传文件配置属性
  28. uploadConfig:{
  29. loading:false,
  30. uploadFileApiUrl: process.env.VUE_APP_BASE_API,
  31. dialogVisible:false,
  32. },
  33. myHeaders: {
  34. 'token': getToken()
  35. },
  36. loadingInstance: false,
  37. }
  38. },
  39. //接收参数
  40. props:{
  41. data:{
  42. type:Object,
  43. required:false,
  44. default:null
  45. },
  46. //初始化文件列表 默认为空
  47. fileList:{
  48. type:String,
  49. required:false,
  50. default:""
  51. },
  52. //按钮名字
  53. title:{
  54. type:String,
  55. required:true
  56. },
  57. //文件标识
  58. flag:{
  59. type:String,
  60. required:true
  61. },
  62. //是否支持多选文件 默认为false
  63. multiple:{
  64. type:Boolean,
  65. required:false,
  66. default:false
  67. },
  68. //样式类型(1:图片类型 2.附件类型) 默认1
  69. styleType:{
  70. type:Number,
  71. required:false,
  72. default:1
  73. },
  74. //tip备注提示
  75. tipMessage:{
  76. type:String,
  77. required:false
  78. },
  79. //缩略图大小(宽高 例如:100x100)
  80. thumbnailSize:{
  81. type:String,
  82. required:false,
  83. default:"146x146"
  84. }
  85. },
  86. watch: {
  87. 'uploadConfig.loading'(val) {
  88. if (val) {
  89. this.loadingInstance = Loading.service({
  90. text: '正在导入请等待...'
  91. })
  92. } else {
  93. this.loadingInstance.close()
  94. }
  95. }
  96. },
  97. created(){
  98. var that = this;
  99. },
  100. methods:{
  101. handleImageSuccess(res, file) {
  102. var that = this;
  103. that.uploadConfig.loading = false;
  104. if(res.code == 200){
  105. this.$notify({
  106. title: '成功',
  107. message: '导入成功',
  108. type: 'success',
  109. duration: 2000
  110. })
  111. this.$emit('fath')
  112. }
  113. else{
  114. this.$notify({
  115. title: '失败',
  116. message: res.msg,
  117. type: 'error',
  118. duration: 2000
  119. })
  120. }
  121. },
  122. handleImagefail(res, file)
  123. {
  124. var that = this;
  125. this.$notify({
  126. title: '成功',
  127. message: '导入失败',
  128. type: 'success',
  129. duration: 2000
  130. })
  131. },
  132. beforeImageUpload(file) {
  133. var that= this;
  134. var checkFile = false;
  135. var fileSize = 100 // MB
  136. const fileNameSuffix = file.name.substring(file.name.lastIndexOf("."));
  137. const allowFileTypes = that.allowFileTypes.split("|");
  138. const fileSizeLimit = file.size / 1024 / 1024 < fileSize;
  139. if(allowFileTypes && allowFileTypes.length>0){
  140. for(var i =0; i<allowFileTypes.length;i++){
  141. if(allowFileTypes[i].toUpperCase() == fileNameSuffix.toUpperCase()){
  142. checkFile = true;
  143. break;
  144. }
  145. }
  146. }
  147. if(!checkFile){
  148. that.$message.error('上传文件格式错误!');
  149. return false;
  150. }
  151. if (that.styleType == 1 && !fileSizeLimit) {
  152. that.$message.error(`上传图片大小不能超过 ${fileSize}MB!`);
  153. return false;
  154. }
  155. that.uploadConfig.loading = true;
  156. return true;
  157. }
  158. }
  159. }
  160. </script>
  161. <style>
  162. </style>