index.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <view class="base-upload" @click="handleUpload">
  3. <slot>
  4. <view class="grid" hover-class="hover-active">
  5. <view class="icon"></view>
  6. </view>
  7. </slot>
  8. </view>
  9. </template>
  10. <script setup>
  11. import { computed, ref, unref } from 'vue';
  12. import { uploadFile } from "./upload.js"
  13. const props = defineProps({
  14. value: { type: [String, Array], default: () => [] },
  15. // 最多上传数量
  16. limit: { type: Number, default: 1 },
  17. showList: { type: Boolean, default: false },
  18. sourceType: { type: String }, // camera 打开相机
  19. type: { type: String, default: 'default' }
  20. })
  21. const emit = defineEmits(['update:value', 'change'])
  22. const getImgList = computed({
  23. get: () => props.value,
  24. set: (val) => emit('update:value', val)
  25. })
  26. function handleUpload(){
  27. uni.chooseImage({
  28. count: 1,
  29. sizeType: ["original"],
  30. extension: ["gif", "jpg", "jpeg", "png", "bmp", "webp"],
  31. sourceType: props.sourceType ? [props.sourceType] : undefined,
  32. success: res => {
  33. const tempFiles = res.tempFiles;
  34. const tempFilePaths = res.tempFilePaths;
  35. // console.log('上传 res', res)
  36. // return
  37. if (tempFiles && tempFiles.length > 0) {
  38. const fileItem = tempFiles[0];
  39. const { size } = fileItem;
  40. if (size / 1024 / 1024 > 8) {
  41. uni.showToast({ icon: "none", mask: true, title: "文件需要8M以内" });
  42. } else {
  43. // 上传方法
  44. uploadFile({
  45. type: props.type,
  46. filePath: tempFilePaths[0],
  47. }).then(url => {
  48. emit('change', url)
  49. if(props.limit>1){
  50. getImgList.value.push(url)
  51. }else{
  52. getImgList.value = url
  53. }
  54. })
  55. }
  56. }
  57. },
  58. fail: err => {
  59. console.log("handelSelectFile-fail", err);
  60. },
  61. });
  62. }
  63. </script>
  64. <style lang="scss" scoped>
  65. .base-upload{
  66. display: inline-block;
  67. .btn{
  68. width: 100%;
  69. height: 72rpx;
  70. border-radius: 8rpx;
  71. border: 1rpx solid #1677FF;
  72. color: #1677FF;
  73. font-size: 24rpx;
  74. background: #1677FF10;
  75. .img{
  76. width: 28rpx;
  77. height: 28rpx;
  78. margin-right: 12rpx;
  79. }
  80. }
  81. .grid{
  82. width: 118rpx;
  83. height: 118rpx;
  84. border-radius: 8rpx;
  85. background: #F7FAFC;
  86. position: relative;
  87. border: 2px solid #EBF1F5;
  88. &::after, &::before{
  89. position: absolute;
  90. left: 50%;
  91. top: 50%;
  92. transform: translate(-50%, -50%);
  93. content: ' ';
  94. clear: both;
  95. display: block;
  96. background: #C0C5CA;
  97. border-radius: 3rpx;
  98. }
  99. &::after{
  100. width: 3rpx;
  101. height: 40rpx;
  102. }
  103. &::before{
  104. width: 40rpx;
  105. height: 3rpx;
  106. }
  107. }
  108. }
  109. </style>