選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

upload-car-img - 副本.vue 4.3KB

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