index.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <template>
  2. <view class="main">
  3. <block v-if="uploading">
  4. <view class="image-list">
  5. <view class="input_photo acea-row row-middle">
  6. <view class="pictrue" v-if="index<=19" v-for="(item, index) in imgList" :key="index">
  7. <easy-loadimage mode="widthFix" :image-src="item.src"></easy-loadimage>
  8. <view class="close_btn" @click="DelPic(index)"><text class="iconfont icon-guanbi4"></text></view>
  9. </view>
  10. </view>
  11. <view v-if="!imgList.length" class="acea-row row-center-wrapper row-column add" @click="add">
  12. <view><text class='iconfont icon-a-guanzhu'></text></view>
  13. <view class="text">点击选择图片</view>
  14. </view>
  15. </view>
  16. <view class='footer acea-row row-between-wrapper'>
  17. <view v-if="imgList.length">
  18. {{imgList.length}}/{{ limit }}张,{{ (allSize).toFixed(2) }} M
  19. </view>
  20. <view v-else></view>
  21. <view class="upload-btn acea-row row-between-wrapper">
  22. <button v-if="imgList.length < limit" class="btn" @click="add"> {{ imgList.length ? '继续选择' : '选择图片' }}</button>
  23. <button class="btn upload" :class="{ 'no-pic': !imgList.length }" @click="submitUpload">确认上传</button>
  24. </view>
  25. </view>
  26. </block>
  27. <block v-else>
  28. <view class="upload-success">
  29. <view class="success">
  30. <img class="image" :src="domain+'/static/images/success.jpg'" alt="" />
  31. </view>
  32. <view class="text">图片上传成功</view>
  33. <view class="again" @click="again">继续上传</view>
  34. </view>
  35. </block>
  36. </view>
  37. </template>
  38. <script>
  39. // +----------------------------------------------------------------------
  40. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  41. // +----------------------------------------------------------------------
  42. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  43. // +----------------------------------------------------------------------
  44. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  45. // +----------------------------------------------------------------------
  46. // | Author: CRMEB Team <admin@crmeb.com>
  47. // +----------------------------------------------------------------------
  48. import easyLoadimage from '@/components/easy-loadimage/easy-loadimage.vue';
  49. import {TOKENNAME,HTTP_REQUEST_URL} from '@/config/app.js';
  50. import {scanUpload} from '@/api/admin.js'
  51. import store from '@/store';
  52. export default {
  53. components: {
  54. easyLoadimage
  55. },
  56. data() {
  57. return {
  58. uploading: true,
  59. domain: HTTP_REQUEST_URL,
  60. imgList: [],
  61. fileList: [],
  62. token: "",
  63. images: [],
  64. mer_id: 0,
  65. allSize: 0,
  66. limit: 20,
  67. }
  68. },
  69. onLoad(options) {
  70. this.token = options.token
  71. this.mer_id = options.mer_id || 0
  72. },
  73. onShow() {
  74. },
  75. methods: {
  76. add() {
  77. let that = this
  78. uni.chooseImage({
  79. count: 100, //最多可以选择的图片总数
  80. sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
  81. sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
  82. success: (res)=> {
  83. let imageList = [];
  84. for (let i = 0; i < res.tempFiles.length; i++) {
  85. uni.getImageInfo({
  86. src:res.tempFilePaths[i],
  87. success: function(image) {
  88. let canvasWidth = image.width //图片原始长宽
  89. let canvasHeight = image.height;
  90. let base = canvasWidth/canvasHeight;
  91. if(canvasWidth>400){
  92. canvasWidth = 400;
  93. canvasHeight = Math.floor(canvasWidth/base);
  94. }
  95. let img = document.createElement('img');
  96. img.src = res.tempFilePaths[i]; // 要压缩的图片
  97. let canvas = document.createElement('canvas');
  98. let ctx = canvas.getContext('2d');
  99. canvas.width = canvasWidth;
  100. canvas.height = canvasHeight;
  101. // 将图片画到canvas上面 使用Canvas压缩
  102. ctx.drawImage(img, 0, 0, canvasWidth, canvasHeight);
  103. canvas.toBlob(function (blob) {
  104. let file = new File([blob], res.tempFiles[i].name, {type: blob.type})
  105. let imgSrc = window.URL.createObjectURL(blob);//原生JS生成文件路径
  106. that.images.push({img:imgSrc,name: res.tempFiles[i].name })
  107. console.log('压缩完图片路径',imgSrc)
  108. const sizeInBytes = blob.size //获取Blob对象的大小(以字节为单位)
  109. const sizeInKB = sizeInBytes / 1024 / 1024 //将大小转换为KB
  110. if(that.imgList.length<20){
  111. that.imgList.push({src:imgSrc,size:sizeInKB})
  112. that.allSize += sizeInKB
  113. }
  114. }, 'image/jpeg', 0.8);
  115. },fail(err) {
  116. console.log("读取图片失败:", err)
  117. }
  118. });
  119. }
  120. }
  121. })
  122. },
  123. async submitUpload(){
  124. let that = this;
  125. for (let i = 0; i < that.images.length; i++) {
  126. await that.uploadImages(that.images[i])
  127. if (i == that.imgList.length - 1) {
  128. that.uploading = false;
  129. that.loading = false;
  130. }
  131. }
  132. },
  133. uploadImages(data){
  134. let that = this
  135. return new Promise(async (resolve) => {
  136. //启动上传等待中...
  137. uni.showLoading({
  138. title: '上传中...',
  139. });
  140. uni.uploadFile({
  141. url: HTTP_REQUEST_URL + '/api/scan_upload/image/field/' + that.token,
  142. filePath: data.img,
  143. name: 'field',
  144. formData: {name: data.name, mer_id: that.mer_id},
  145. header: {
  146. [TOKENNAME]: 'Bearer ' + store.state.app.token
  147. },
  148. success: function(res) {
  149. uni.hideLoading();
  150. if (res.statusCode == 403) {
  151. that.$util.Tips({
  152. title: res.message
  153. });
  154. } else {
  155. let data = res.data ? JSON.parse(res.data) : {};
  156. if (data.status == 200) {
  157. resolve(data);
  158. } else {
  159. that.$util.Tips({
  160. title: res.message
  161. });
  162. }
  163. }
  164. },
  165. fail: function(res) {
  166. uni.hideLoading();
  167. that.$util.Tips({
  168. title: res
  169. });
  170. }
  171. })
  172. })
  173. },
  174. /**删除图片*/
  175. DelPic: function(index) {
  176. let that = this
  177. that.imgList.splice(index, 1);
  178. that.images.splice(index, 1);
  179. that.getImageSize();
  180. },
  181. /*计算总的图片大小*/
  182. getImageSize(){
  183. let s = 0;
  184. if (this.imgList.length) {
  185. this.imgList.map((e) => {
  186. s += e.size;
  187. });
  188. this.allSize = s;
  189. } else {
  190. this.allSize = 0;
  191. }
  192. },
  193. /*继续上传*/
  194. again(){
  195. this.uploading = true;
  196. this.imgList = [];
  197. this.images = [];
  198. this.allSize = 0;
  199. }
  200. },
  201. onHide() {
  202. }
  203. }
  204. </script>
  205. <style lang="scss" scoped>
  206. page{
  207. background: #ffffff;
  208. }
  209. .main {
  210. width: 100%;
  211. .image-list{
  212. padding: 20rpx;
  213. overflow: scroll;
  214. height: calc(100vh - 90rpx);
  215. }
  216. .add{
  217. height: 300rpx;
  218. background: #f9f9f9;
  219. border: 1px dashed #ebeef5;
  220. border-radius: 12rpx;
  221. color: #bbbbbb;
  222. .icon-a-guanzhu{
  223. font-size: 46rpx;
  224. font-weight: bold;
  225. }
  226. .text{
  227. font-size: 26rpx;
  228. color: #999;
  229. margin-top: 32rpx;
  230. }
  231. }
  232. /deep/.input_photo .easy-loadimage,/deep/.input_photo uni-image,/deep/.input_photo image{
  233. width: 230rpx;
  234. height: 230rpx;
  235. border-radius: 12rpx!important;
  236. }
  237. .pictrue{
  238. width: 220rpx;
  239. height: 220rpx;
  240. border-radius: 12rpx!important;
  241. margin: 0 15rpx 10rpx 0;
  242. position: relative;
  243. overflow: hidden;
  244. &:nth-child(3n){
  245. margin-right: 0;
  246. }
  247. .close_btn{
  248. width: 30rpx;
  249. height: 30rpx;
  250. background: rgba(0,0,0,.6);
  251. border-radius: 0 12rpx 0 12rpx;
  252. position: absolute;
  253. top: 0;
  254. right: 0;
  255. display: flex;
  256. align-items: center;
  257. justify-content: center;
  258. z-index: 10;
  259. .iconfont{
  260. color: #ffffff;
  261. font-size: 12rpx;
  262. }
  263. }
  264. }
  265. }
  266. .footer {
  267. padding: 0 20rpx 0 30rpx;
  268. position: fixed;
  269. bottom: 0;
  270. width: 100%;
  271. box-sizing: border-box;
  272. background-color: rgba(255, 255, 255, 0.85);
  273. backdrop-filter: blur(20rpx);
  274. z-index: 277;
  275. border-top: 2rpx solid #f0f0f0;
  276. height: 80rpx;
  277. height: calc(80rpx+ constant(safe-area-inset-bottom)); ///兼容 IOS<11.2/
  278. height: calc(80rpx + env(safe-area-inset-bottom)); ///兼容 IOS>11.2/
  279. display: flex;
  280. align-items: center;
  281. justify-content: space-between;
  282. color: #666666;
  283. font-size: 24rpx;
  284. .btn {
  285. border: 2rpx solid #cccccc;
  286. width: 170rpx;
  287. height: 60rpx;
  288. border-radius: 30rpx;
  289. color: #000;
  290. font-size: 26rpx;
  291. font-family: PingFang SC-Regular, PingFang SC;
  292. font-weight: 400;
  293. color: #666666;
  294. display: flex;
  295. align-items: center;
  296. justify-content: center;
  297. }
  298. .upload {
  299. background-color: #e93323;
  300. color: #fff;
  301. margin-left: 20rpx;
  302. }
  303. .upload.no-pic {
  304. background: #e93323;
  305. opacity: 0.3;
  306. }
  307. }
  308. .upload-success {
  309. display: flex;
  310. align-items: center;
  311. justify-content: center;
  312. flex-direction: column;
  313. height: 80vh;
  314. .success {
  315. width: 100rpx;
  316. height: 100rpx;
  317. background: #4bbc12;
  318. border-radius: 50%;
  319. display: flex;
  320. align-items: center;
  321. justify-content: center;
  322. margin-bottom: 40rpx;
  323. .image {
  324. width: 60%;
  325. }
  326. }
  327. .text {
  328. font-size: 32rpx;
  329. font-family: PingFang SC-Medium, PingFang SC;
  330. font-weight: 500;
  331. color: #282828;
  332. margin-bottom: 80rpx;
  333. }
  334. .again {
  335. width: 300rpx;
  336. height: 86rpx;
  337. border-radius: 42rpx;
  338. text-align: center;
  339. line-height: 86rpx;
  340. font-size: 30rpx;
  341. font-weight: 400;
  342. color: #333333;
  343. border: 2rpx solid #cccccc;
  344. }
  345. }
  346. </style>