ca8c9a5f8ebfe7b275b552ced40cc5133dad190c.svn-base 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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) {
  80. this.allurl = this.url + id;
  81. if (this.length) {
  82. this.$nextTick(() => {
  83. console.log(this.$refs)
  84. let upload = this.$refs.upload
  85. upload.submit();
  86. });
  87. return false;
  88. } else {
  89. return true;
  90. }
  91. },
  92. // 上传出错,向上触发结束
  93. upError(err, file, fileList) {
  94. this.$message.error("上传出错,请修改文件!");
  95. this.$emit("uploadOver");
  96. this.init();
  97. },
  98. // 全部上传成功,向上触发结束
  99. uploadSuccess(response, file, fileList) {
  100. if(!response.success){
  101. this.$message.error("上传出错,请修改文件!");
  102. this.$emit("uploadOver");
  103. this.init();
  104. return
  105. }
  106. this.resList.push(response.data);
  107. this.sum = this.sum + 1;
  108. if (this.sum == this.length) {
  109. this.$emit("uploadOver", this.resList);
  110. this.init();
  111. }
  112. },
  113. // 及时更新列表长度
  114. changeFileList(file, fileList) {
  115. this.length = fileList.length;
  116. }
  117. }
  118. };
  119. </script>