index.vue 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <template>
  2. <div>
  3. <van-uploader
  4. ref="uploader"
  5. v-bind="$attrs"
  6. v-model="fileList"
  7. :before-read="beforeRead"
  8. :after-read="afterRead"
  9. multiple
  10. :max-count="maxCount"
  11. :max-size="maxSize * 1024*1024"/>
  12. </div>
  13. </template>
  14. <script>
  15. import {upload} from "@/api/public";
  16. import ImageCompressor from "js-image-compressor";
  17. import config from '@/config/index';
  18. export default {
  19. props:{
  20. value:{
  21. type: Array,
  22. },
  23. //最大上传数量
  24. maxCount:{
  25. type: Number,
  26. default: 6,
  27. },
  28. //文件大小,单位MB
  29. maxSize:{
  30. type: Number,
  31. default: 5,
  32. },
  33. },
  34. data() {
  35. return {
  36. fileList:[]
  37. };
  38. },
  39. mounted() {
  40. },
  41. watch:{
  42. value:{
  43. handler(val){
  44. this.fileList = val.map(v=>{
  45. console.log( window.origin ,' window.origin ')
  46. let imgUrl =
  47. process.env.NODE_ENV === "development"
  48. ? config.baseUrl + v.imgPath
  49. : window.origin + v.imgPath;
  50. return {
  51. name: v.name,
  52. url: imgUrl,
  53. path:v.imgPath,
  54. }
  55. })
  56. },
  57. deep:true,
  58. immediate:true
  59. }
  60. },
  61. methods: {
  62. //上传前压缩
  63. beforeRead(file){
  64. console.log(file,'图片压缩前')
  65. return new Promise((resolve, reject) => {
  66. new ImageCompressor({
  67. file,
  68. quality: 0.6,
  69. success: (result) => {
  70. //this.$toast('图片压缩成功')
  71. console.log(result,'图片压缩后')
  72. let img = new File([result], result.name, {
  73. width: result.width,
  74. height: result.height,
  75. type: result.type,
  76. });
  77. console.log(img,'后')
  78. resolve(img);
  79. },
  80. error: (e) => {
  81. console.log('imageError:'+e);
  82. this.$toast('图片压缩失败')
  83. reject(e);
  84. },
  85. });
  86. });
  87. },
  88. chooseFiles(){
  89. console.log( this.$refs.uploader,'子组件1111')
  90. this.$refs.uploader.chooseFile({sourceType: ['album']})
  91. },
  92. //上传到服务器
  93. afterRead(fileObj){
  94. console.log(fileObj,'flie');
  95. let formData = new FormData();
  96. formData.append("file", fileObj.file);
  97. upload(formData, "image").then((res) => {
  98. console.log( config,'222')
  99. /*上传成功*/
  100. let imgUrl =
  101. process.env.NODE_ENV === "development"
  102. ? config.baseUrl + res.data.url
  103. : window.origin + res.data.url;
  104. let arr = [];
  105. let emitList=[]
  106. console.log(imgUrl,'imgUrl');
  107. arr.push({ name: res.data.name, url: imgUrl,imgPath:res.data.url});
  108. //emitList.push(res.data.url)
  109. this.fileList = arr;
  110. console.log(this.fileList,arr,'tttttttt')
  111. //emit完整图片URL路径
  112. this.$emit("input",arr);
  113. // //非完整图片URL路径
  114. // this.$emit("imgUrl", res.data.url);
  115. }).catch((err) => {
  116. /*上传失败*/
  117. console.log(this.fileList,'this.fileList')
  118. //this.$refs.imageUpload.handleRemove(fileObj.file);
  119. });
  120. },
  121. },
  122. model:{
  123. prop:'value',
  124. event:'input'
  125. }
  126. };
  127. </script>
  128. <style lang="scss" scoped>
  129. .upload-text{
  130. font-size: 18px;
  131. color: #999;
  132. margin-top: 5px;
  133. }
  134. </style>