index.vue 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <!-- 图片上传 -->
  2. <script>
  3. export default {
  4. name: "ImageUpload",
  5. props: {
  6. value: [String, Object, Array],
  7. maxCount: {
  8. type: [Number, String],
  9. default: 9,
  10. },
  11. fileType: {
  12. type: Array,
  13. default: () => ["jpg", "jpeg", "png"],
  14. },
  15. // fileSize: {
  16. // type: Number,
  17. // },
  18. },
  19. data() {
  20. return {
  21. fileList: [],
  22. };
  23. },
  24. computed: {
  25. accept: {
  26. get() {
  27. const { fileType } = this.$props;
  28. const typeList = fileType.reduce((str, item) => {
  29. return (str += "." + item + ",");
  30. }, "");
  31. return typeList;
  32. },
  33. set() {},
  34. },
  35. },
  36. methods: {
  37. afterRead(file) {
  38. let {
  39. fileList,
  40. $props: { maxCount },
  41. } = this;
  42. this.$emit("input", fileList);
  43. if (fileList.length === maxCount) {
  44. this.$toast(`最多可上传${maxCount}个文件`);
  45. }
  46. },
  47. useDelete(file) {
  48. this.$emit("input", this.fileList);
  49. },
  50. beforeRead(file, detail) {
  51. const fileTypeList = file.type.split("/");
  52. const fileExt = fileTypeList[fileTypeList.length - 1];
  53. const isTypeOk = this.fileType.indexOf(fileExt) >= 0;
  54. console.log(isTypeOk, "isTypeOk");
  55. if (!isTypeOk) {
  56. this.$toast(
  57. `文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`
  58. );
  59. return false;
  60. }
  61. return true;
  62. // const { fileSize, accept } = this.$props;
  63. // const imgformat = /image\/(png|jpg|jpeg)$/;
  64. // if (!imgformat.test(file.type)) {
  65. // this.$toast.fail(`请上传 ${accept} 格式图片`);
  66. // return false;
  67. // }
  68. },
  69. },
  70. created() {},
  71. };
  72. </script>
  73. <template>
  74. <div class="upload-image">
  75. <van-uploader
  76. v-model="fileList"
  77. multiple
  78. preview-size="60px"
  79. :accept="accept"
  80. :max-count="maxCount"
  81. :after-read="afterRead"
  82. :before-read="beforeRead"
  83. @delete="useDelete"
  84. />
  85. </div>
  86. </template>