20cc6fb3036522ca1bb133802207c8abda3f20c1.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <!--
  2. <upload ref="uploadFile" url="/biz/hzz/basic/file/upload?bizId=" @uploadOver="uploadOver"></upload>
  3. if (this.$refs.uploadFile.submitUpload(id)) {
  4. // true上传列表为空,直接执行结束操作
  5. this.insertOver()
  6. }
  7. -->
  8. <template>
  9. <div style="width: 360px;margin: 20px auto;">
  10. <el-upload
  11. v-if="!uploadText"
  12. ref="upload"
  13. drag
  14. :action="allurl"
  15. :headers="header"
  16. :accept="accept"
  17. :auto-upload="false"
  18. multiple
  19. :on-change="changeFileList"
  20. :on-success="uploadSuccess"
  21. :on-error="upError"
  22. >
  23. <div>
  24. <i class="el-icon-upload"></i>
  25. <div class="el-upload__text">
  26. 将文件拖到此处,或
  27. <em>点击添加</em>
  28. </div>
  29. </div>
  30. </el-upload>
  31. <el-upload
  32. v-if="uploadText"
  33. ref="upload"
  34. :action="allurl"
  35. :headers="header"
  36. :accept="accept"
  37. :auto-upload="false"
  38. multiple
  39. :on-change="changeFileList"
  40. :on-success="uploadSuccess"
  41. :on-error="upError"
  42. >
  43. <el-button size="small" type="primary" style="margin: 0">上传</el-button>
  44. </el-upload>
  45. </div>
  46. </template>
  47. <script>
  48. export default {
  49. props: ["url","accept","uploadText"], // 上传基础路径
  50. data() {
  51. return {
  52. fileList: [],
  53. resList: [],
  54. length: 0, // 上传列表长度
  55. sum: 0, // 上传成功次数
  56. allurl: "", // 上传完整路径
  57. header: {
  58. 'persId': localStorage.getItem('userid'),
  59. 'accessToken' : localStorage.getItem('accessToken'),
  60. 'orgId': localStorage.getItem('currentOrgId')
  61. },
  62. };
  63. },
  64. computed:{
  65. userid(){
  66. return localStorage.getItem('userid') ? localStorage.getItem('userid') : ''
  67. }
  68. },
  69. methods: {
  70. // 初始化,
  71. init() {
  72. this.$refs.upload.clearFiles();
  73. this.length = 0;
  74. this.sum = 0;
  75. this.allurl = "";
  76. this.resList = [];
  77. },
  78. // 开始上传,文件列表为空直接返回turn表示结束,不为空触发上传并返回false,由uploadOver判断结束时间
  79. submitUpload(id,type) {
  80. this.allurl = `${this.url}${id}&bizType=${type}`;
  81. if (this.length>0) {
  82. this.$nextTick(() => {
  83. this.$refs.upload.submit();
  84. });
  85. return false;
  86. } else {
  87. return true;
  88. }
  89. },
  90. // 上传出错,向上触发结束
  91. upError(err, file, fileList) {
  92. this.$message.error("上传出错,请修改文件!");
  93. this.$emit("uploadOver");
  94. this.init();
  95. },
  96. // 全部上传成功,向上触发结束
  97. uploadSuccess(response, file, fileList) {
  98. if(!response.success){
  99. this.$message.error("上传出错,请修改文件!");
  100. this.$emit("uploadOver");
  101. this.init();
  102. return
  103. }
  104. this.resList.push(response.data);
  105. this.sum = this.sum + 1;
  106. if (this.sum == this.length) {
  107. this.$emit("uploadOver", this.resList);
  108. this.init();
  109. }
  110. },
  111. // 及时更新列表长度
  112. changeFileList(file, fileList) {
  113. this.length = fileList.length;
  114. }
  115. }
  116. };
  117. </script>