upload.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <template>
  2. <div class="upload-wrap">
  3. <div class="tips tips-text" v-if="tipText?.length>0">
  4. <span>{{tipText}}</span>
  5. </div>
  6. <div class="tips" v-if="!textShow">
  7. <el-icon class="icon" size="16"><Warning /></el-icon>
  8. <span>上传照片可精准预测最佳病虫防治时间</span>
  9. </div>
  10. <uploader class="uploader" v-model="fileList" multiple :max-count="3" :after-read="afterRead" @delete="deleteImg">
  11. <template v-if="exampleImg">
  12. <slot v-if="!fileList.length"></slot>
  13. <img class="plus" v-else src="@/assets/img/home/plus.png" alt="">
  14. </template>
  15. <img class="plus" v-else src="@/assets/img/home/plus.png" alt="">
  16. </uploader>
  17. </div>
  18. </template>
  19. <script setup>
  20. import { onMounted, ref} from "vue";
  21. import { Uploader } from "vant";
  22. import eventBus from "@/api/eventBus";
  23. import { base_img_url2 } from "@/api/config";
  24. import { getFileExt } from "@/utils/util";
  25. import { ElMessage } from "element-plus";
  26. import UploadFile from "@/utils/upliadFile";
  27. import 'vant/lib/uploader/style';
  28. import { useStore } from "vuex";
  29. const props = defineProps({
  30. tipText: {
  31. type: String,
  32. default: ''
  33. },
  34. fullPath:{
  35. type: Boolean,
  36. default: true
  37. },
  38. textShow: {
  39. type: Boolean,
  40. default: true
  41. },
  42. exampleImg: {
  43. type: Boolean,
  44. default: false
  45. },
  46. })
  47. const store = useStore();
  48. const miniUserId = store.state.home.miniUserId;
  49. const emit = defineEmits(['handleUpload'])
  50. //上传照片
  51. const fileList = ref([]);
  52. const fileArr = ref([])
  53. const imgArr = ref([])
  54. const uploadFileObj = new UploadFile();
  55. const afterRead = async (files) => {
  56. if (!Array.isArray(files)) {
  57. files = [files];
  58. }
  59. for(let file of files){
  60. // 将文件上传至服务器
  61. let fileVal = file.file;
  62. file.status = "uploading";
  63. file.message = "上传中...";
  64. let ext = getFileExt(fileVal.name);
  65. let key = `birdseye-look-mini/${miniUserId}/${new Date().getTime()}.${ext}`;
  66. let resFilename = await uploadFileObj.put(key, fileVal)
  67. file.status = "done";
  68. file.message = "";
  69. if(resFilename){
  70. fileArr.value.push(props.fullPath ? base_img_url2 + resFilename : resFilename)
  71. imgArr.value.push(resFilename)
  72. eventBus.emit('upload:change',fileArr.value)
  73. eventBus.emit('upload:changeArr',imgArr.value)
  74. emit('handleUpload',{imgArr:imgArr.value,fileList:fileArr.value})
  75. }else{
  76. fileList.value.pop()
  77. ElMessage.error('图片上传失败,请稍后再试!')
  78. }
  79. }
  80. };
  81. const deleteImg = (file,e) => {
  82. fileArr.value.splice(e.index,1)
  83. imgArr.value.splice(e.index,1)
  84. eventBus.emit('upload:change',fileArr.value)
  85. eventBus.emit('upload:changeArr',imgArr.value)
  86. emit('handleUpload',{imgArr:imgArr.value,fileList:fileArr.value})
  87. }
  88. function uploadReset(){
  89. fileList.value = []
  90. fileArr.value = []
  91. imgArr.value = []
  92. }
  93. onMounted(()=>{
  94. eventBus.off('upload:reset',uploadReset)
  95. eventBus.on('upload:reset',uploadReset)
  96. })
  97. </script>
  98. <style lang="scss" scoped>
  99. .upload-wrap {
  100. .tips {
  101. display: flex;
  102. align-items: center;
  103. color: #2199F8;
  104. background: #E9F5FF;
  105. border-radius: 4px;
  106. padding: 2px 10px;
  107. box-sizing: border-box;
  108. margin-bottom: 10px;
  109. .icon {
  110. margin-right: 2px;
  111. }
  112. }
  113. .tips-text{
  114. background: linear-gradient(240deg,#FFFFFF 22%,rgba(33, 153, 248,0.2) 100%);
  115. border-radius: 20px 0 0 20px;
  116. }
  117. ::v-deep {
  118. .van-uploader__input-wrapper{
  119. text-align: center;
  120. }
  121. .el-select__wrapper:hover {
  122. box-shadow: 0 0 0 1px #dcdfe6 inset;
  123. }
  124. .avatar-uploader .el-upload {
  125. width: 100%;
  126. border: 1px dashed #dddddd;
  127. border-radius: 6px;
  128. cursor: pointer;
  129. position: relative;
  130. overflow: hidden;
  131. }
  132. .el-icon.avatar-uploader-icon {
  133. font-size: 28px;
  134. color: #8c939d;
  135. width: 100%;
  136. height: 128px;
  137. text-align: center;
  138. background: #f6f6f6;
  139. }
  140. }
  141. .uploader{
  142. .plus{
  143. width: 80px;
  144. height: 80px;
  145. }
  146. }
  147. }
  148. </style>