upload.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. <template>
  2. <div v-loading="loading">
  3. <el-upload
  4. class="upload-demo"
  5. :drag="fileInfo.type !== 'btn'"
  6. multiple
  7. :limit="fileInfo.limit"
  8. ref="upload"
  9. action="string"
  10. :on-remove="handleRemove"
  11. :on-exceed="handleExceed"
  12. :file-list="attList"
  13. :auto-upload="true"
  14. :show-file-list="fileInfo.type !== 'btn'"
  15. :before-upload="deforeUp"
  16. :http-request="signUpload"
  17. :on-preview="defUp"
  18. :disabled="disabled"
  19. >
  20. <div v-if="fileInfo.type !== 'btn'">
  21. <i class="el-icon-upload"></i>
  22. <div class="el-upload__text">点击上传</div>
  23. </div>
  24. <div v-if="fileInfo.type === 'btn'">
  25. <el-button
  26. :size="fileInfo.size ? fileInfo.size : 'medium'"
  27. :type="fileInfo.btntype"
  28. >{{ fileInfo.typename }}</el-button
  29. >
  30. </div>
  31. </el-upload>
  32. </div>
  33. </template>
  34. <script>
  35. export default {
  36. // props: ["fileInfo", "fileList", "type"],
  37. props: {
  38. fileInfo: {
  39. type: Object,
  40. default: () => {}
  41. },
  42. fileList: {
  43. type: Array,
  44. default: () => []
  45. },
  46. type: {
  47. type: Array,
  48. default: () => []
  49. },
  50. disabled: {
  51. type: Boolean,
  52. default: () => {},
  53. },
  54. },
  55. data() {
  56. return {
  57. fileLists: [],
  58. attList: [
  59. // {
  60. // attchFileId: "",
  61. // fileCode: "202209271105276441",
  62. // name: "1231233333333333.xlsx",
  63. // id: "202209271105276441",
  64. // opName: "范岩",
  65. // opNo: "fanyan",
  66. // opTime: "2022-09-27 11:05:27",
  67. // type: 'success',
  68. // },
  69. ],
  70. loading: false,
  71. num: 0,
  72. s: false,
  73. };
  74. },
  75. // props:{
  76. // disabled: {
  77. // type: Boolean,
  78. // default: () => {},
  79. // },
  80. // },
  81. methods: {
  82. signUpload() {},
  83. //删除
  84. handleRemove(file) {
  85. console.log(file);
  86. console.log(this.attList);
  87. for (let i = 0; i < this.attList.length; i++) {
  88. if (this.attList[i].name === file.name) {
  89. this.attList.splice(i, 1);
  90. // this.$emit("clickDownload",this.attList);
  91. }
  92. }
  93. console.log(this.attList)
  94. this.$emit("uploadBack", this.attList);
  95. },
  96. //数量限制
  97. handleExceed(files, fileList) {
  98. this.$message.warning(
  99. `当前限制选择 ${this.fileInfo.limit} 个文件,本次选择了 ${
  100. files.length
  101. } 个文件,共选择了 ${files.length + fileList.length} 个文件`
  102. );
  103. },
  104. defUp(file){
  105. this.$emit("clickDownload",this.attList);
  106. },
  107. deforeUp(file) {
  108. const isLt2M = file.size / 1024 / 1024 < 100;
  109. if (!isLt2M) {
  110. this.$message.error("上传文件大小不能超过 20MB!");
  111. return false;
  112. }
  113. if (this.fileInfo.type == "img") {
  114. const isImg = file.type.split("/")[0] == "image";
  115. if (!isImg) {
  116. this.$message.error("请上传图片格式文件!");
  117. return false;
  118. }
  119. }
  120. if (this.fileInfo.typexz == "ppt") {
  121. const isImg = file.name.split(".")[1] == "pptx";
  122. console.log(file.name.split(".")[1] == "pptx");
  123. if (!isImg) {
  124. this.$message.error("请上传ppt格式文件!");
  125. return false;
  126. }
  127. }
  128. if (this.fileInfo.typexz == "csv") {
  129. const isImg = file.name.split(".")[1] == "csv";
  130. if (!isImg) {
  131. this.$message.error("请上传csv格式文件!");
  132. return false;
  133. }
  134. }
  135. if (this.fileInfo.typexz == "xlsx") {
  136. const isImg = file.name.split(".")[1] == "xlsx";
  137. if (!isImg) {
  138. this.$message.error("请上传xlsx格式文件!");
  139. return false;
  140. }
  141. }
  142. this.loading = true;
  143. let query = new FormData();
  144. query.append("file", file);
  145. if (this.fileInfo.typename == "新增素材") {
  146. query.append("advType", this.fileInfo.advType);
  147. query.append("advTypeName", this.fileInfo.advTypeName);
  148. }
  149. if (
  150. this.fileInfo.url == "/market/cIllegalCallTask/importTempByProv" ||
  151. this.fileInfo.url ==
  152. "/market/cChannelInfo/cIllegalCallTask/importTempByProv"
  153. ) {
  154. query.append("taskId", this.fileInfo.taskId);
  155. query.append("tempId", this.fileInfo.tempId);
  156. }
  157. //违规外呼
  158. if (this.fileInfo.uploadType == "outCall") {
  159. query.append("uploadType", "outCall");
  160. query.append("relId", this.fileInfo.relId);
  161. }
  162. if (this.fileInfo.uploadType == "train") {
  163. query.append("uploadType", "train");
  164. query.append("relId", this.fileInfo.relId);
  165. }
  166. //运营类项目考核及结算
  167. if (this.fileInfo.url == "/market/cmkAttachInfo/upload") {
  168. query.append("uploadType", this.fileInfo.uploadType);
  169. }
  170. if (this.fileInfo.url == "/market/cStoreOutWo/importData") {
  171. query.append("woNo", this.fileInfo.woNo);
  172. }
  173. if (this.fileInfo.url == "/market/cStoreScheTrainEva/importDataEva") {
  174. query.append("evaId", this.fileInfo.evaId);
  175. }
  176. this.num++;
  177. let _this = this;
  178. this.$http({
  179. url: this.fileInfo.url,
  180. method: "post",
  181. headers: {
  182. "Content-Type": "application/json",
  183. },
  184. data: query,
  185. })
  186. .then((res) => {
  187. if (this.fileInfo.type === "btn") {
  188. this.$refs["upload"].clearFiles();
  189. this.$emit("uploadBack", res);
  190. this.$emit("clickDownload",res);
  191. if (res.data.result == 0) {
  192. this.$emit("onSuccess", file);
  193. _this.$message({
  194. message: res.data.desc,
  195. type: "success",
  196. });
  197. } else if (res.data.result == 1) {
  198. _this.$message({
  199. message: res.data.desc,
  200. type: "error",
  201. });
  202. } else if (res.data.result == 2) {
  203. _this.$message({
  204. message: res.data.desc,
  205. type: "warning",
  206. });
  207. } else if (res.data.result == 3) {
  208. _this.$message({
  209. message: res.data.desc,
  210. type: "info",
  211. });
  212. } else {
  213. _this.$message({
  214. message: res.data.desc,
  215. type: "success",
  216. });
  217. }
  218. this.num--;
  219. if (this.num == 0) {
  220. this.loading = false;
  221. }
  222. return;
  223. }
  224. if (this.fileInfo.type === "voice") {
  225. this.attList = res.data;
  226. } else {
  227. this.attList.push({
  228. id: res.data.body.id,
  229. name: res.data.body.fileName,
  230. fileCode: res.data.body.fileCode,
  231. opName: res.data.body.opName,
  232. opNo: res.data.body.opNo,
  233. opTime: res.data.body.opTime,
  234. attchFileId: res.data.body.attchFileId,
  235. type: res.data.body.type,
  236. });
  237. }
  238. this.$emit("uploadBack", this.attList);
  239. // this.$emit("clickDownload",this.attList);
  240. this.num--;
  241. if (this.num == 0) {
  242. this.loading = false;
  243. }
  244. return true;
  245. })
  246. .catch((res) => {
  247. this.$message({
  248. message: file.name + "上传失败",
  249. type: "error",
  250. });
  251. this.num--;
  252. if (this.num == 0) {
  253. this.loading = false;
  254. }
  255. for (let i = 0; i < this.$refs["upload"].uploadFiles.length; i++) {
  256. if (file.name == this.$refs["upload"].uploadFiles[i].name) {
  257. this.$refs["upload"].uploadFiles.splice(i, 1);
  258. }
  259. }
  260. return false;
  261. });
  262. },
  263. },
  264. mounted() {},
  265. created() {
  266. this.attList = [];
  267. console.log(this.fileList);
  268. if(this.fileList){
  269. for (let i = 0; i < this.fileList.length; i++) {
  270. this.attList.push({
  271. id: this.fileList[i].id,
  272. name: this.fileList[i].fileName,
  273. fileCode: this.fileList[i].fileCode,
  274. opName: this.fileList[i].opName,
  275. opNo: this.fileList[i].opNo,
  276. opTime: this.fileList[i].opTime,
  277. attchFileId: this.fileList[i].attchFileId,
  278. type: this.fileList[i].type,
  279. })
  280. }
  281. }
  282. console.log(this.attList);
  283. },
  284. watch: {
  285. fileList() {
  286. this.attList = [];
  287. for (let i = 0; i < this.fileList.length; i++) {
  288. this.attList.push({
  289. id: this.fileList[i].id,
  290. name: this.fileList[i].fileName,
  291. fileCode: this.fileList[i].fileCode,
  292. opName: this.fileList[i].opName,
  293. opNo: this.fileList[i].opNo,
  294. opTime: this.fileList[i].opTime,
  295. attchFileId: this.fileList[i].attchFileId,
  296. type: this.fileList[i].type,
  297. });
  298. }
  299. },
  300. },
  301. };
  302. </script>
  303. <style scoped lang="scss">
  304. </style>