Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

upload-car-img.vue 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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'], //从相机查询选择
  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 = (300 * 1024) / size;
  61. // let scall = (2048 * 1024) / size;
  62. let scall = 0.8;
  63. let types = handleToTypes();
  64. console.log(types, size / 1024 + 'K', size, name, '图片所有信息');
  65. console.log(res.tempFiles[0], '图片', res.tempFiles);
  66. if (scall > 1) {
  67. scall = 0.8;
  68. // uploadUrl(res.tempFiles[0], name, uploadimges.value);
  69. } else if (scall < 0.02) {
  70. scall = 0.02;
  71. }
  72. // else {
  73. // translate(uploadimges.value, scall, name, (imgURL, file) => {
  74. // uploadimges.value = imgURL;
  75. // console.log(file, '压缩后的图片');
  76. // uploadUrl(file, name, imgURL);
  77. // });
  78. // }
  79. console.log('压缩比', scall);
  80. translate(uploadimges.value, scall, name, (imgURL, file) => {
  81. uploadimges.value = imgURL;
  82. console.log(file, '压缩后的图片');
  83. uploadUrl(file, name, imgURL);
  84. });
  85. // uni.showLoading({
  86. // title: '正在进行识别',
  87. // mask: true
  88. // });
  89. }
  90. });
  91. };
  92. // h5 下载图片
  93. const plusDownloadImg = (opts) => {
  94. let {
  95. url,
  96. token,
  97. fileName // 文件名需要传入并且带上后缀
  98. } = opts;
  99. if (!fileName) fileName = '未命名.jpg';
  100. if (url) {
  101. let dtask = plus.downloader.createDownload(
  102. url,
  103. {
  104. filename: '_downloads/' + fileName
  105. },
  106. (d, status) => {
  107. if (status === 200) {
  108. console.log('下载成功:' + d.filename);
  109. // 需要转换成绝对路径
  110. let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
  111. plusSaveImg(fileSaveUrl);
  112. } else {
  113. console.log('下载失败:' + status);
  114. }
  115. }
  116. );
  117. dtask.setRequestHeader('token', token);
  118. dtask.start();
  119. }
  120. };
  121. // h5+ 保存图片到相册
  122. const plusSaveImg = (filename) => {
  123. plus.gallery.save(
  124. filename,
  125. () => {
  126. console.log(filename);
  127. // this.$showToast.ok('已保存到系统相册');
  128. },
  129. (e) => {
  130. // this.$showToast.error('图片保存失败');
  131. console.log(e.message);
  132. }
  133. );
  134. };
  135. const uploadUrl = (files, fileName, imgURL) => {
  136. // WARNING: For POST requests, body is set to null by browsers.
  137. var data = new FormData();
  138. data.append('bucket', 'default-bucket');
  139. data.append('file', files);
  140. var xhr = new XMLHttpRequest();
  141. console.log(xhr, 'xhr请求');
  142. xhr.withCredentials = true;
  143. xhr.timeout = 60000; // 超时时间,单位是毫秒
  144. xhr.addEventListener('readystatechange', function () {
  145. if (this.readyState === 4) {
  146. // uni.hideLoading()
  147. const data = JSON.parse(this.responseText).data;
  148. let url = data.ossFilePath;
  149. let pathDomain = data.ossFilePath;
  150. if (import.meta.env.VITE_APP_TYPE === 'production' || true) {
  151. url = 'http://100.64.2.113:9000/default-bucket/' + url;
  152. pathDomain = 'https://qtzl.etcjz.cn/default-bucket/' + pathDomain;
  153. } else {
  154. url = 'http://192.168.101.145:9000/default-bucket/' + url;
  155. }
  156. console.log(url, '图片地址');
  157. uni.hideLoading()
  158. emits('uploadImgHandle', {
  159. path: imgURL,
  160. url,
  161. fileName,
  162. pathDomain
  163. });
  164. }
  165. });
  166. if (import.meta.env.DEV) {
  167. xhr.open('POST', '/minIo/upload');
  168. } else {
  169. xhr.open('POST', apiUrl + 'minIo/upload');
  170. }
  171. xhr.send(data);
  172. };
  173. </script>
  174. <style lang="scss" scoped>
  175. .picture-wrapper {
  176. margin: 10rpx 30rpx;
  177. .bg {
  178. background: #ffffff;
  179. box-shadow: 0rpx 4rpx 13rpx 3rpx rgba(223, 223, 223, 0.8);
  180. border-radius: 20rpx;
  181. padding: 40rpx;
  182. display: flex;
  183. // align-items: center;
  184. justify-content: space-between;
  185. .name {
  186. font-size: 34rpx;
  187. font-family: Microsoft YaHei;
  188. font-weight: 400;
  189. color: #000000;
  190. line-height: 34rpx;
  191. }
  192. .value {
  193. margin-top: 20rpx;
  194. font-size: 24rpx;
  195. font-family: Microsoft YaHei;
  196. font-weight: 400;
  197. color: #999999;
  198. line-height: 24rpx;
  199. }
  200. .tip {
  201. margin-top: 20rpx;
  202. text-align: center;
  203. width: 110rpx;
  204. height: 40rpx;
  205. background: rgba(33, 190, 177, 0.2);
  206. border-radius: 6rpx;
  207. .tip-value {
  208. font-size: 20rpx;
  209. font-family: Microsoft YaHei;
  210. font-weight: 400;
  211. color: #0a8f8a;
  212. line-height: 40rpx;
  213. opacity: 1;
  214. }
  215. }
  216. }
  217. .icon {
  218. width: 294rpx;
  219. height: 188rpx;
  220. }
  221. }
  222. .upload-car-bottom {
  223. font-size: 28rpx;
  224. }
  225. </style>