main.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <template>
  2. <div>
  3. <input
  4. ref="inputer"
  5. :accept="accept"
  6. :multiple="multiple"
  7. class="fileUp"
  8. type="file"
  9. name="file"
  10. @change="handleChange($event)"
  11. />
  12. </div>
  13. </template>
  14. <script>
  15. import urlConfig from "@/apis/url-config";
  16. import { getToken } from "@/utils/auth";
  17. //Uploadcondition 用于判断个性化文件 //返回 false 就不会上传文件
  18. //UploadErrorEvent 上传成功
  19. //UploadErrorEvent 上传失败
  20. export default {
  21. name: "fileUploadPdf",
  22. props: {
  23. //@reject 默认值
  24. accept: String, //上传文件类型
  25. multiple: {
  26. //是否可以上传多张图片
  27. type: Boolean,
  28. default: false,
  29. },
  30. size: "mini",
  31. uploadcondition: {
  32. type: Function,
  33. default: null,
  34. },
  35. },
  36. data() {
  37. return {
  38. loading: false,
  39. imgAPI: urlConfig.baseURL,
  40. };
  41. },
  42. methods: {
  43. async handleChange(event) {
  44. let that = this;
  45. let inputDOM1 = that.$refs.inputer;
  46. const files = inputDOM1.files;
  47. let length = files.length;
  48. if (length === 0) return;
  49. that.loading = that.$loading({
  50. lock: true,
  51. text: "Loading",
  52. spinner: "el-icon-loading",
  53. background: "rgba(0, 0, 0, 0.7)",
  54. });
  55. for (let i = 0; i <= length; i++) {
  56. if (i === length) {
  57. this.loading.close();
  58. that.$refs.inputer.value = "";
  59. } else {
  60. let str = await that.httpupLoad(files, i);
  61. if (str === "error") {
  62. that.$emit("UploadErrorEvent", true);
  63. } else if (str === "break") {
  64. that.$emit("UploadErrorEvent", "break");
  65. } else {
  66. that.$emit("UploadSuccessEvent", str);
  67. }
  68. }
  69. }
  70. },
  71. async httpupLoad(files, i) {
  72. return new Promise((resolve, reject) => {
  73. let that = this;
  74. if (!this.uploadcondition(files[i])) {
  75. resolve("break");
  76. } else {
  77. let form = new FormData();
  78. form.append("files", files[i]);
  79. form.append("token", getToken());
  80. axios
  81. .post(`${that.imgAPI}admin/uploadfile`, form)
  82. .then((res) => {
  83. const { status } = res;
  84. if (status === 200) {
  85. let item = res.data;
  86. const { code, data } = item;
  87. if (code === 0) {
  88. const { url, name } = data[0];
  89. resolve({
  90. url: `${that.imgAPI}storage/${url}`,
  91. name: name,
  92. });
  93. } else {
  94. resolve({
  95. url: `noToken`,
  96. name: "",
  97. });
  98. }
  99. } else {
  100. reject();
  101. }
  102. })
  103. .catch((error) => {
  104. reject();
  105. });
  106. }
  107. });
  108. },
  109. },
  110. };
  111. </script>
  112. <style lang="scss" scoped>
  113. .fileUp {
  114. opacity: 0;
  115. width: 100%;
  116. height: 100%;
  117. outline: none;
  118. position: absolute;
  119. top: 0;
  120. left: 0;
  121. &:hover {
  122. cursor: pointer;
  123. }
  124. }
  125. </style>