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 5.9KB

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