您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

upload-car-img.vue 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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: async (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. // const files = await fileRename(res.tempFiles[0], res.tempFiles[0].name.replaceAll(' ', ''))
  70. uploadimges.value = res.tempFiles[0].path;
  71. let size = res.tempFiles[0].size;
  72. let name = res.tempFiles[0].name.replaceAll(' ', '');
  73. let scall = (2 * 1024 * 1024) / size;
  74. // let scall = (2048 * 1024) / size;
  75. console.log(size / 1024 / 1024 + 'M', size, name, '图片所有信息');
  76. console.log(res.tempFiles[0], '图片', res.tempFiles);
  77. console.log('压缩比', scall, uploadimges.value);
  78. if (scall >= 1) {
  79. // scall = 1;
  80. // const files = await filePathToFile(res.tempFiles[0].path)
  81. // console.log(res.tempFiles[0].file, 'files');
  82. // uploadUrl(res.tempFiles[0], name, res.tempFiles[0].path);
  83. scall = 0.95;
  84. // ocrUpload(res.tempFiles[0].path)
  85. } else if (scall < 0.2) {
  86. scall = 0.2
  87. }
  88. uploadObj.scall = scall
  89. uploadObj.name = name
  90. uploadObj.size = size
  91. }
  92. });
  93. };
  94. let timer = null
  95. function handleTimer() {
  96. clearTimeout(timer)
  97. timer = setTimeout(() => {
  98. console.log('运行定时器');
  99. uni.hideLoading()
  100. }, 10 * 1000)
  101. }
  102. onBeforeUnmount(() => {
  103. clearTimeout(timer)
  104. })
  105. const handleLoad = () => {
  106. console.log('onload了');
  107. let scall = uploadObj.scall
  108. let name = uploadObj.name
  109. if (scall >= 1) {
  110. return
  111. }
  112. handleTimer()
  113. setTimeout(() => {
  114. translate(uploadimges.value, scall, name, (imgURL, file) => {
  115. let twoScall = 2 * 1024 * 1024 - file.size;
  116. console.log(twoScall, 'twoScalltwoScall');
  117. if (twoScall > 0) {
  118. twoScall = twoScall / (300 * 1024) * 0.1 + scall
  119. } else if (file.size > 2 * 1024 * 1024) {
  120. twoScall = scall - 0.1
  121. }
  122. console.log(file, file.size / 1024 / 1024 + 'M', scall, '压缩后的图片1', twoScall,);
  123. if (file.size < 1.5 * 1024 * 1024 || file.size > 2 * 1024 * 1024) {
  124. if (twoScall > 1) {
  125. twoScall = 0.95
  126. }
  127. console.log('压缩后的图片1twoScall', twoScall,);
  128. translate(uploadimges.value, twoScall, name, (imgURL, file) => {
  129. console.timeEnd('translate')
  130. uploadUrl(file, name, imgURL);
  131. clearTimeout(timer)
  132. console.log(file, file.size / 1024 / 1024 + 'm', '压缩后的图片');
  133. })
  134. } else {
  135. uploadUrl(file, name, imgURL);
  136. console.timeEnd('translate')
  137. }
  138. });
  139. }, 120)
  140. }
  141. const ocrUpload = (filePath) => {
  142. // uni.showLoading({
  143. // title: '文件上传中',
  144. // mask: true
  145. // })
  146. // let url = 'https://wayetc.com/upload/v1/file/uploadFile'
  147. let urls = ''
  148. if (import.meta.env.DEV) {
  149. urls = '/minIo/upload'
  150. } else {
  151. urls = apiUrl + 'minIo/upload'
  152. }
  153. uni.uploadFile({
  154. url: urls, //仅为示例,非真实的接口地址 accountNum= secretKey
  155. filePath: filePath,
  156. name: 'file',
  157. formData: {
  158. 'user': 'test',
  159. "bucket": 'default-bucket'
  160. },
  161. success: (uploadFileRes) => {
  162. const data = JSON.parse(uploadFileRes.data)
  163. let url = data.data.ossFilePath
  164. const fileName = data.data.originalFileName.replaceAll(' ', '');
  165. console.log(uploadFileRes, data, url, fileName, 'uploadFileRes');
  166. uni.hideLoading()
  167. let pathDomain = url;
  168. if (import.meta.env.VITE_APP_TYPE === 'production' || true) {
  169. url = 'http://100.64.2.113:9000/default-bucket/' + url;
  170. pathDomain = 'https://qtzl.etcjz.cn/default-bucket/' + pathDomain;
  171. } else {
  172. url = 'http://192.168.101.145:9000/default-bucket/' + url;
  173. }
  174. console.log(url, '图片上传完地址');
  175. // uni.hideLoading()
  176. emits('uploadImgHandle', {
  177. path: '',
  178. url,
  179. fileName,
  180. pathDomain
  181. });
  182. },
  183. fail(err) {
  184. console.log(err);
  185. uni.hideLoading()
  186. }
  187. });
  188. }
  189. const uploadUrl = (files, fileName, imgURL) => {
  190. // WARNING: For POST requests, body is set to null by browsers.
  191. var data = new FormData();
  192. data.append('busCode', 'h5');
  193. data.append('file', files);
  194. // data.append('fileName', fileName);
  195. var xhr = new XMLHttpRequest();
  196. console.log(xhr, 'xhr请求');
  197. xhr.withCredentials = true;
  198. xhr.timeout = 60000; // 超时时间,单位是毫秒
  199. xhr.addEventListener('readystatechange', function () {
  200. if (this.readyState === 4) {
  201. uni.hideLoading()
  202. const data = JSON.parse(this.responseText).data;
  203. let url = 'http://100.64.2.113:9000/default-bucket/' + data.ossFilePath;
  204. let pathDomain = 'https://qtzl.etcjz.cn/default-bucket/' + data.ossFilePath;
  205. console.log(url,pathDomain, '图片上传完地址');
  206. // uni.hideLoading()
  207. emits('uploadImgHandle', {
  208. path: imgURL,
  209. url,
  210. fileName,
  211. pathDomain
  212. });
  213. }
  214. });
  215. if (import.meta.env.DEV) {
  216. xhr.open('POST', '/newDev/minIo/upload');
  217. } else {
  218. xhr.open('POST', apiUrl + 'newDev/minIo/upload');
  219. }
  220. xhr.send(data);
  221. };
  222. </script>
  223. <style lang="scss" scoped>
  224. .picture-wrapper {
  225. margin: 10rpx 30rpx;
  226. .bg {
  227. background: #ffffff;
  228. box-shadow: 0rpx 4rpx 13rpx 3rpx rgba(223, 223, 223, 0.8);
  229. border-radius: 20rpx;
  230. padding: 40rpx;
  231. display: flex;
  232. // align-items: center;
  233. justify-content: space-between;
  234. .name {
  235. font-size: 34rpx;
  236. font-family: Microsoft YaHei;
  237. font-weight: 400;
  238. color: #000000;
  239. line-height: 34rpx;
  240. }
  241. .value {
  242. margin-top: 20rpx;
  243. font-size: 24rpx;
  244. font-family: Microsoft YaHei;
  245. font-weight: 400;
  246. color: #999999;
  247. line-height: 24rpx;
  248. }
  249. .tip {
  250. margin-top: 20rpx;
  251. text-align: center;
  252. width: 110rpx;
  253. height: 40rpx;
  254. background: rgba(33, 190, 177, 0.2);
  255. border-radius: 6rpx;
  256. .tip-value {
  257. font-size: 20rpx;
  258. font-family: Microsoft YaHei;
  259. font-weight: 400;
  260. color: #0a8f8a;
  261. line-height: 40rpx;
  262. opacity: 1;
  263. }
  264. }
  265. }
  266. .icon {
  267. width: 294rpx;
  268. height: 188rpx;
  269. }
  270. }
  271. .upload-car-bottom {
  272. font-size: 28rpx;
  273. }
  274. </style>