Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

upload-car-img - 副本 (2).vue 5.6KB

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