You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

upload-car-img.vue 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <template>
  2. <view class="upload-car-img">
  3. <view class="picture-wrapper" @click="cardImageOcr">
  4. <view class="bg">
  5. <view class="">
  6. <view class="name"> {{dataList.title}} </view>
  7. <view class="value"> {{dataList.hint}} </view>
  8. </view>
  9. <image v-if="!uploadimges" class="icon" :src="handleImg(dataList.placeholderImg)">
  10. </image>
  11. <image v-else class="icon" :src="uploadimges"></image>
  12. </view>
  13. </view>
  14. </view>
  15. </template>
  16. <script lang='ts' setup>
  17. import {
  18. ref
  19. } from 'vue'
  20. import {
  21. translate
  22. } from '@/utils/getLessLimitSizeImage'
  23. import {
  24. fileURL
  25. } from '@/utils/network/api.js';
  26. const props = defineProps({
  27. dataList: {
  28. type: Object,
  29. default: () => {
  30. return {}
  31. }
  32. }
  33. })
  34. const handleImg = (url) => {
  35. // 判断如果头部为http开头
  36. const reg = /^https?:\/\/.*$/
  37. if (reg.test(url)) {
  38. return url
  39. } else {
  40. return fileURL + url
  41. }
  42. }
  43. const emits = defineEmits(['uploadImgHandle'])
  44. let uploadimges = ref('')
  45. const cardImageOcr = () => {
  46. uni.chooseImage({
  47. count: 1, //只能选取一张照片
  48. sizeType: ["original", "compressed"], //可以指定是原图还是压缩图,默认二者都有
  49. sourceType: ["camera", "album"], //从相册选择
  50. success: (res) => {
  51. // console.log(res.tempFiles[0]);
  52. uni.showLoading({
  53. title: '正在识别图片',
  54. mask: true
  55. });
  56. uploadimges.value = res.tempFiles[0].path
  57. let size = res.tempFiles[0].size
  58. let name = res.tempFiles[0].name
  59. console.log(res.tempFiles);
  60. let scall = 200 * 1024 / size
  61. if (scall > 1) {
  62. scall = 1
  63. }
  64. // uni.showLoading({
  65. // title: '正在进行识别',
  66. // mask: true
  67. // });
  68. // 压缩图片
  69. translate(res.tempFiles[0].path, scall, name, (imgURL, file) => {
  70. uploadimges.value = imgURL
  71. console.log(file);
  72. uploadUrl(file, name, imgURL)
  73. })
  74. }
  75. })
  76. }
  77. const uploadUrl = (files, fileName, imgURL) => {
  78. // WARNING: For POST requests, body is set to null by browsers.
  79. var data = new FormData();
  80. data.append("bucket", "default-bucket");
  81. data.append("file", files);
  82. var xhr = new XMLHttpRequest();
  83. xhr.withCredentials = true;
  84. xhr.addEventListener("readystatechange", function() {
  85. if (this.readyState === 4) {
  86. const data = JSON.parse(this.responseText).data
  87. let url = data.ossFilePath
  88. if (import.meta.env.VITE_APP_TYPE === 'production') {
  89. url = 'http://100.64.2.113:9000/default-bucket/' + url
  90. } else {
  91. url = 'http://192.168.101.145:9000/default-bucket/' + url
  92. }
  93. console.log(url, '图片地址');
  94. emits('uploadImgHandle', {
  95. path: imgURL,
  96. url,
  97. fileName,
  98. })
  99. uni.hideLoading()
  100. }
  101. });
  102. xhr.open("POST", "/minIo/upload");
  103. xhr.send(data);
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. .picture-wrapper {
  108. margin: 10rpx 30rpx;
  109. .bg {
  110. background: #ffffff;
  111. box-shadow: 0rpx 4rpx 13rpx 3rpx rgba(223, 223, 223, 0.8);
  112. border-radius: 20rpx;
  113. padding: 40rpx;
  114. display: flex;
  115. // align-items: center;
  116. justify-content: space-between;
  117. .name {
  118. font-size: 34rpx;
  119. font-family: Microsoft YaHei;
  120. font-weight: 400;
  121. color: #000000;
  122. line-height: 34rpx;
  123. }
  124. .value {
  125. margin-top: 20rpx;
  126. font-size: 24rpx;
  127. font-family: Microsoft YaHei;
  128. font-weight: 400;
  129. color: #999999;
  130. line-height: 24rpx;
  131. }
  132. .tip {
  133. margin-top: 20rpx;
  134. text-align: center;
  135. width: 110rpx;
  136. height: 40rpx;
  137. background: rgba(33, 190, 177, 0.2);
  138. border-radius: 6rpx;
  139. .tip-value {
  140. font-size: 20rpx;
  141. font-family: Microsoft YaHei;
  142. font-weight: 400;
  143. color: #0a8f8a;
  144. line-height: 40rpx;
  145. opacity: 1;
  146. }
  147. }
  148. }
  149. .icon {
  150. width: 294rpx;
  151. height: 188rpx;
  152. }
  153. }
  154. .upload-car-bottom {
  155. font-size: 28rpx;
  156. }
  157. </style>