main.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. uploadcondition: {
  31. type: Function,
  32. default: null,
  33. },
  34. },
  35. data() {
  36. return {
  37. loading: false,
  38. imgAPI: urlConfig.baseURL,
  39. };
  40. },
  41. methods: {
  42. async handleChange(event) {
  43. let that = this;
  44. let inputDOM1 = that.$refs.inputer;
  45. const files = inputDOM1.files;
  46. let length = files.length;
  47. if (length === 0) return;
  48. that.loading = that.$loading({
  49. lock: true,
  50. text: "Loading",
  51. spinner: "el-icon-loading",
  52. background: "rgba(0, 0, 0, 0.7)",
  53. });
  54. for (let i = 0; i <= length; i++) {
  55. if (i === length) {
  56. this.loading.close();
  57. that.$refs.inputer.value = "";
  58. } else {
  59. let str = await that.httpupLoad(files, i);
  60. if (str === "error") {
  61. that.$emit("UploadErrorEvent", true);
  62. } else if (str === "break") {
  63. that.$emit("UploadErrorEvent", "break");
  64. } else {
  65. that.$emit("UploadSuccessEvent", str);
  66. }
  67. }
  68. }
  69. },
  70. async httpupLoad(files, i) {
  71. return new Promise((resolve, reject) => {
  72. let that = this;
  73. if (!this.uploadcondition(files[i])) {
  74. resolve("break");
  75. } else {
  76. let form = new FormData();
  77. form.append("files", files[i]);
  78. form.append("token", getToken());
  79. axios
  80. .post(`${that.imgAPI}admin/uploadfile`, form)
  81. .then((res) => {
  82. const { status } = res;
  83. if (status === 200) {
  84. let item = res.data;
  85. const { code, data } = item;
  86. if (code === 0) {
  87. const { url, name } = data[0];
  88. resolve({
  89. url: `${that.imgAPI}storage/${url}`,
  90. name: name,
  91. });
  92. } else {
  93. resolve({
  94. url: `noToken`,
  95. name: "",
  96. });
  97. }
  98. } else {
  99. reject();
  100. }
  101. })
  102. .catch((error) => {
  103. reject();
  104. });
  105. }
  106. });
  107. },
  108. },
  109. };
  110. </script>
  111. <style lang="scss" scoped>
  112. .fileUp {
  113. opacity: 0;
  114. width: 100%;
  115. height: 100%;
  116. outline: none;
  117. position: absolute;
  118. top: 0;
  119. left: 0;
  120. &:hover {
  121. cursor: pointer;
  122. }
  123. }
  124. </style>