uploadKh.vue 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <el-upload class="upload-demo" multiple :limit="fileInfo.limit" ref="upload"
  3. action="string" :on-remove="handleRemove" :on-exceed="handleExceed" :file-list="attList"
  4. :auto-upload="true" :show-file-list="fileInfo.type !== 'btn'"
  5. :before-upload="deforeUp" :http-request="signUpload">
  6. <!-- <div v-if="fileInfo.type !== 'btn'">
  7. <i class="el-icon-upload"></i>
  8. <div class="el-upload__text">点击上传</div>
  9. </div>-->
  10. <el-button size="small" type="primary">点击上传</el-button>
  11. </el-upload>
  12. </template>
  13. <script>
  14. export default {
  15. props: ['fileInfo', 'fileList'],
  16. data() {
  17. return {
  18. fileLists: [],
  19. attList: [],
  20. loading: false,
  21. num: 0,
  22. s: false
  23. }
  24. },
  25. methods: {
  26. signUpload(){},
  27. //删除
  28. handleRemove(file) {
  29. for (let i = 0; i < this.attList.length; i++) {
  30. if (this.attList[i].fileName === file.name) {
  31. this.attList.splice(i, 1);
  32. this.$emit('uploadBack', this.attList)
  33. }
  34. }
  35. },
  36. //数量限制
  37. handleExceed(files, fileList) {
  38. this.$message.warning(
  39. `当前限制选择 ${this.fileInfo.limit} 个文件,本次选择了 ${files.length} 个文件,共选择了 ${files.length + fileList.length} 个文件`
  40. );
  41. },
  42. deforeUp(file) {
  43. const isLt2M = file.size / 1024 / 1024 < 100;
  44. if (!isLt2M) {
  45. this.$message.error('上传文件大小不能超过 20MB!');
  46. return false
  47. }
  48. if(this.fileInfo.type == 'img'){
  49. const isImg = file.type.split("/")[0] == 'image';
  50. if (!isImg) {
  51. this.$message.error('请上传图片格式文件!');
  52. return false
  53. }
  54. }
  55. this.loading = true;
  56. let query = new FormData();
  57. query.append("file", file);
  58. if(this.fileInfo.typename == '新增素材'){
  59. query.append("advType", this.fileInfo.advType);
  60. query.append("advTypeName", this.fileInfo.advTypeName);
  61. }
  62. this.num++;
  63. let _this = this;
  64. this.$http({
  65. url: this.fileInfo.url,
  66. method: "post",
  67. headers: {
  68. "Content-Type": "application/json",
  69. },
  70. data: query,
  71. }).then((res) => {
  72. if (this.fileInfo.type === 'btn') {
  73. this.$refs['upload'].clearFiles();
  74. this.$emit('uploadBack',res)
  75. _this.$message({
  76. message: res.data.desc,
  77. type: 'success'
  78. });
  79. this.num--;
  80. if (this.num == 0) {
  81. this.loading = false;
  82. }
  83. return
  84. }
  85. if (this.fileInfo.type === 'voice') {
  86. this.attList = res.data;
  87. } else {
  88. this.attList.push({
  89. id: res.data.body.id,
  90. fileName: res.data.body.fileName,
  91. fileCode: res.data.body.fileCode,
  92. opName: res.data.body.opName,
  93. opNo: res.data.body.opNo,
  94. opTime: res.data.body.opTime,
  95. attchFileId: res.data.body.attchFileId,
  96. type: res.data.body.type,
  97. });
  98. }
  99. this.$emit('uploadBack', this.attList);
  100. this.num--;
  101. if (this.num == 0) {
  102. this.loading = false;
  103. }
  104. return true
  105. }).catch((res) => {
  106. this.$message({
  107. message: file.name+'上传失败',
  108. type: 'error'
  109. });
  110. this.num--;
  111. if (this.num == 0) {
  112. this.loading = false;
  113. }
  114. for (let i = 0; i < this.$refs['upload'].uploadFiles.length; i++) {
  115. if(file.name == this.$refs['upload'].uploadFiles[i].name){
  116. this.$refs['upload'].uploadFiles.splice(i,1)
  117. }
  118. }
  119. return false
  120. })
  121. },
  122. },
  123. mounted() {
  124. },
  125. created() {
  126. this.attList = [];
  127. for (let i = 0; i < this.fileList.length; i++) {
  128. console.info("--created--"+this.fileList[i]);
  129. this.attList.push({
  130. id: this.fileList[i].id,
  131. fileName: this.fileList[i].fileName,
  132. name:this.fileList[i].fileName,
  133. fileCode: this.fileList[i].fileCode,
  134. opName: this.fileList[i].opName,
  135. opNo: this.fileList[i].opNo,
  136. opTime: this.fileList[i].opTime,
  137. attchFileId: this.fileList[i].attchFileId,
  138. type: this.fileList[i].type,
  139. })
  140. }
  141. },
  142. watch: {
  143. fileList() {
  144. console.info('watch.fileList->'+JSON.stringify(this.fileList));
  145. this.attList = [];
  146. for (let i = 0; i < this.fileList.length; i++) {
  147. this.attList.push({
  148. id: this.fileList[i].id,
  149. fileName: this.fileList[i].fileName,
  150. name:this.fileList[i].fileName,
  151. opName: this.fileList[i].opName,
  152. opNo: this.fileList[i].opNo,
  153. opTime: this.fileList[i].opTime,
  154. attchFileId: this.fileList[i].attchFileId,
  155. type: this.fileList[i].type,
  156. })
  157. }
  158. }
  159. }
  160. }
  161. </script>
  162. <style scoped lang="scss">
  163. </style>