Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

upload-car-img - 副本 (4).vue 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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" @load='handleLoad'></image>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script lang="ts" setup>
  16. import { ref, onBeforeMount, nextTick, onBeforeUnmount } 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. import imageCompression from 'browser-image-compression'
  22. const props = defineProps({
  23. dataList: {
  24. type: Object,
  25. default: () => {
  26. return {};
  27. }
  28. },
  29. imgUrl: {
  30. type: String,
  31. default: ''
  32. },
  33. });
  34. onBeforeMount(() => {
  35. if (props.imgUrl) {
  36. uploadimges.value = props.imgUrl
  37. }
  38. })
  39. const handleImg = (url) => {
  40. // 判断如果头部为http开头
  41. const reg = /^https?:\/\/.*$/;
  42. if (reg.test(url)) {
  43. return url;
  44. } else {
  45. return fileURL + url;
  46. }
  47. };
  48. const emits = defineEmits(['uploadImgHandle']);
  49. let uploadimges = ref('');
  50. let uploadObj = { scall: 1, name: "", size: "" }
  51. const cardImageOcr = () => {
  52. uni.chooseImage({
  53. count: 1, //只能选取一张照片
  54. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  55. sourceType: ['camera', 'album'], //从相机查询选择
  56. success: (res) => {
  57. // console.log(res.tempFiles[0]);
  58. console.time('translate')
  59. uni.showLoading({
  60. title: '正在识别图片',
  61. mask: true
  62. });
  63. console.log(res, '图片地址');
  64. if (!res.tempFiles[0].path) {
  65. console.log(res, '没有图片地址');
  66. uni.hideLoading()
  67. return;
  68. }
  69. uploadimges.value = res.tempFiles[0].path;
  70. let size = res.tempFiles[0].size;
  71. let name = res.tempFiles[0].name;
  72. let scall = (2 * 1024 * 1024) / size;
  73. // let scall = (2048 * 1024) / size;
  74. console.log(size / 1024 / 1024 + 'M', size, name, '图片所有信息');
  75. console.log(res.tempFiles[0], '图片', res.tempFiles);
  76. console.log('压缩比', scall, uploadimges.value);
  77. if (scall >= 1) {
  78. scall = 1;
  79. ocrUpload(res.tempFiles[0].path)
  80. return
  81. } else if (scall < 0.2) {
  82. scall = 0.2
  83. }
  84. uploadObj.scall = scall
  85. uploadObj.name = name
  86. uploadObj.size = size
  87. // else {
  88. // translate(uploadimges.value, scall, name, (imgURL, file) => {
  89. // uploadimges.value = imgURL;
  90. // console.log(file, '压缩后的图片');
  91. // uploadUrl(file, name, imgURL);
  92. // });
  93. // }
  94. // uni.showLoading({
  95. // title: '正在进行识别',
  96. // mask: true
  97. // });
  98. // ocrUpload(res.tempFiles[0].path)
  99. }
  100. });
  101. };
  102. let timer = null
  103. function handleTimer() {
  104. clearTimeout(timer)
  105. timer = setTimeout(() => {
  106. console.log('运行定时器');
  107. uni.hideLoading()
  108. }, 10 * 1000)
  109. }
  110. onBeforeUnmount(() => {
  111. clearTimeout(timer)
  112. })
  113. const handleLoad = () => {
  114. console.log('onload了');
  115. let scall = uploadObj.scall
  116. let name = uploadObj.name
  117. handleTimer()
  118. setTimeout(() => {
  119. translate(uploadimges.value, scall, name, (imgURL, file) => {
  120. let twoScall = 2 * 1024 * 1024 - file.size;
  121. console.log(twoScall, 'twoScalltwoScall');
  122. if (twoScall > 0) {
  123. twoScall = twoScall / (300 * 1024) * 0.1 + scall
  124. } else if (file.size > 2 * 1024 * 1024) {
  125. twoScall = scall - 0.1
  126. }
  127. console.log(file, file.size / 1024 / 1024 + 'M', scall, '压缩后的图片1', twoScall,);
  128. if (file.size < 1.5 * 1024 * 1024 || file.size > 2 * 1024 * 1024) {
  129. if (twoScall > 1) {
  130. twoScall = 0.95
  131. }
  132. console.log('压缩后的图片1twoScall', twoScall,);
  133. translate(uploadimges.value, twoScall, name, (imgURL, file) => {
  134. console.timeEnd('translate')
  135. uploadUrl(file, name, imgURL);
  136. clearTimeout(timer)
  137. console.log(file, file.size / 1024 / 1024 + 'm', '压缩后的图片');
  138. })
  139. } else {
  140. uploadUrl(file, name, imgURL);
  141. console.timeEnd('translate')
  142. }
  143. });
  144. }, 120)
  145. }
  146. const ocrUpload = (filePath) => {
  147. // uni.showLoading({
  148. // title: '文件上传中',
  149. // mask: true
  150. // })
  151. // let url = 'https://wayetc.com/upload/v1/file/uploadFile'
  152. let urls = ''
  153. if (import.meta.env.DEV) {
  154. urls = '/minIo/upload'
  155. } else {
  156. urls = apiUrl + 'minIo/upload'
  157. }
  158. uni.uploadFile({
  159. url: urls, //仅为示例,非真实的接口地址 accountNum= secretKey
  160. filePath: filePath,
  161. name: 'file',
  162. formData: {
  163. 'user': 'test',
  164. "bucket": 'default-bucket'
  165. },
  166. success: (uploadFileRes) => {
  167. const data = JSON.parse(uploadFileRes.data)
  168. let url = data.data.ossFilePath
  169. const fileName = data.data.originalFileName
  170. console.log(uploadFileRes, data, url, fileName, 'uploadFileRes');
  171. uni.hideLoading()
  172. let pathDomain = url;
  173. if (import.meta.env.VITE_APP_TYPE === 'production' || true) {
  174. url = 'http://100.64.2.113:9000/default-bucket/' + url;
  175. pathDomain = 'https://qtzl.etcjz.cn/default-bucket/' + pathDomain;
  176. } else {
  177. url = 'http://192.168.101.145:9000/default-bucket/' + url;
  178. }
  179. console.log(url, '图片上传完地址');
  180. // uni.hideLoading()
  181. emits('uploadImgHandle', {
  182. path: '',
  183. url,
  184. fileName,
  185. pathDomain
  186. });
  187. },
  188. fail(err) {
  189. console.log(err);
  190. uni.hideLoading()
  191. }
  192. });
  193. }
  194. // h5 下载图片
  195. const plusDownloadImg = (opts) => {
  196. let {
  197. url,
  198. token,
  199. fileName // 文件名需要传入并且带上后缀
  200. } = opts;
  201. if (!fileName) fileName = '未命名.jpg';
  202. if (url) {
  203. let dtask = plus.downloader.createDownload(
  204. url,
  205. {
  206. filename: '_downloads/' + fileName
  207. },
  208. (d, status) => {
  209. if (status === 200) {
  210. console.log('下载成功:' + d.filename);
  211. // 需要转换成绝对路径
  212. let fileSaveUrl = plus.io.convertLocalFileSystemURL(d.filename);
  213. plusSaveImg(fileSaveUrl);
  214. } else {
  215. console.log('下载失败:' + status);
  216. }
  217. }
  218. );
  219. dtask.setRequestHeader('token', token);
  220. dtask.start();
  221. }
  222. };
  223. // h5+ 保存图片到相册
  224. const plusSaveImg = (filename) => {
  225. plus.gallery.save(
  226. filename,
  227. () => {
  228. console.log(filename);
  229. // this.$showToast.ok('已保存到系统相册');
  230. },
  231. (e) => {
  232. // this.$showToast.error('图片保存失败');
  233. console.log(e.message);
  234. }
  235. );
  236. };
  237. const uploadUrl = (files, fileName, imgURL) => {
  238. // WARNING: For POST requests, body is set to null by browsers.
  239. var data = new FormData();
  240. data.append('bucket', 'default-bucket');
  241. data.append('file', files);
  242. var xhr = new XMLHttpRequest();
  243. console.log(xhr, 'xhr请求');
  244. xhr.withCredentials = true;
  245. xhr.timeout = 60000; // 超时时间,单位是毫秒
  246. xhr.addEventListener('readystatechange', function () {
  247. if (this.readyState === 4) {
  248. uni.hideLoading()
  249. const data = JSON.parse(this.responseText).data;
  250. let url = data.ossFilePath;
  251. let pathDomain = data.ossFilePath;
  252. if (import.meta.env.VITE_APP_TYPE === 'production' || true) {
  253. url = 'http://100.64.2.113:9000/default-bucket/' + url;
  254. pathDomain = 'https://qtzl.etcjz.cn/default-bucket/' + pathDomain;
  255. } else {
  256. url = 'http://192.168.101.145:9000/default-bucket/' + url;
  257. }
  258. console.log(url, '图片上传完地址');
  259. // uni.hideLoading()
  260. emits('uploadImgHandle', {
  261. path: imgURL,
  262. url,
  263. fileName,
  264. pathDomain
  265. });
  266. }
  267. });
  268. if (import.meta.env.DEV) {
  269. xhr.open('POST', '/minIo/upload');
  270. } else {
  271. xhr.open('POST', apiUrl + 'minIo/upload');
  272. }
  273. xhr.send(data);
  274. };
  275. </script>
  276. <style lang="scss" scoped>
  277. .picture-wrapper {
  278. margin: 10rpx 30rpx;
  279. .bg {
  280. background: #ffffff;
  281. box-shadow: 0rpx 4rpx 13rpx 3rpx rgba(223, 223, 223, 0.8);
  282. border-radius: 20rpx;
  283. padding: 40rpx;
  284. display: flex;
  285. // align-items: center;
  286. justify-content: space-between;
  287. .name {
  288. font-size: 34rpx;
  289. font-family: Microsoft YaHei;
  290. font-weight: 400;
  291. color: #000000;
  292. line-height: 34rpx;
  293. }
  294. .value {
  295. margin-top: 20rpx;
  296. font-size: 24rpx;
  297. font-family: Microsoft YaHei;
  298. font-weight: 400;
  299. color: #999999;
  300. line-height: 24rpx;
  301. }
  302. .tip {
  303. margin-top: 20rpx;
  304. text-align: center;
  305. width: 110rpx;
  306. height: 40rpx;
  307. background: rgba(33, 190, 177, 0.2);
  308. border-radius: 6rpx;
  309. .tip-value {
  310. font-size: 20rpx;
  311. font-family: Microsoft YaHei;
  312. font-weight: 400;
  313. color: #0a8f8a;
  314. line-height: 40rpx;
  315. opacity: 1;
  316. }
  317. }
  318. }
  319. .icon {
  320. width: 294rpx;
  321. height: 188rpx;
  322. }
  323. }
  324. .upload-car-bottom {
  325. font-size: 28rpx;
  326. }
  327. </style>