detail.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. <!-- html -->
  2. <template>
  3. <view style="overflow: hidden;">
  4. <u-checkbox-group @change="checkboxGroupChange" max=9>
  5. <view class="container">
  6. <view class="left">
  7. <view class="posive" v-for="(item,index) in leftList" :key="index">
  8. <image :src="item.url" alt="" mode="widthFix" @click="previewImg(item)" />
  9. <view v-if="upload" class="collected">
  10. <u-checkbox v-model="item.checked" :name="item.img_url" shape="circle" active-color="#3D6EF6" size="16" icon-size="8" />
  11. </view>
  12. </view>
  13. </view>
  14. <view class="right">
  15. <view class="posive" v-for="(item,index) in rightList" :key="index">
  16. <image :src="item.url" alt="" mode="widthFix" @click="previewImg(item)" />
  17. <view v-if="upload" class="collected">
  18. <u-checkbox v-model="item.checked" :name="item.img_url" shape="circle" active-color="#3D6EF6" size="16" icon-size="8" />
  19. </view>
  20. </view>
  21. </view>
  22. <!-- <view v-show="!upload" class="fixed" @click="upload = true, clearUp()">
  23. 下载
  24. </view> -->
  25. <view v-show="upload" class="fixed2 flexS">
  26. <view class="left">
  27. 已选择<text>{{ checkUrl.length}}</text>张
  28. </view>
  29. <view class="flex">
  30. <view class="upload" @click="downloadImgs">
  31. 立即下载
  32. </view>
  33. <view class="quxiao" @click="upload = false">
  34. 取消
  35. </view>
  36. </view>
  37. </view>
  38. </view>
  39. <u-loadmore
  40. v-if="nomore"
  41. dashed
  42. status="nomore"
  43. nomore-text="没有更多啦~"
  44. />
  45. </u-checkbox-group>
  46. <u-overlay :show="show" :opacity="1">
  47. <view @tap.stop="close()" class="close">
  48. <image src="../../static/img/close.png" mode="widthFix"></image>
  49. </view>
  50. <swiper :autoplay="false" :duration="350" :current="current" class="swiper">
  51. <swiper-item v-for="(item,index) in list">
  52. <movable-area class="image-view-area">
  53. <movable-view class="image-view-view" direction="all" :scale="true" scale-min="1" scale-max="3">
  54. <img :src="item.url" alt="" />
  55. </movable-view>
  56. </movable-area>
  57. <view v-if="item.master" class="btn" @tap.stop="showMaster(item, index)">
  58. 查看原图
  59. </view>
  60. </swiper-item>
  61. </swiper>
  62. </u-overlay>
  63. </view>
  64. </template>
  65. <script>
  66. import { GetImgDetail } from '../../api/index.js';
  67. export default {
  68. data() {
  69. return {
  70. upload: false,
  71. show: false,
  72. list: [
  73. // {
  74. // file_name: "IMG_20220723_185105.jpg",
  75. // url: "http://look.qiniuyun.jiuweiyun.cn/daweiboshi_szy/image_f7b92067678f0f3a29e8aa956214113f.jpg",
  76. // img_url: "http://look.qiniuyun.jiuweiyun.cn/daweiboshi_szy/image_f7b92067678f0f3a29e8aa956214113f.jpg",
  77. // checked: true
  78. // },
  79. // {
  80. // file_name: "IMG_20220723_185141.jpg",
  81. // url: "http://look.qiniuyun.jiuweiyun.cn/daweiboshi_szy/image_cc1ba9bc4e3e8e58459ed9daee128c9f.jpg",
  82. // img_url: "http://look.qiniuyun.jiuweiyun.cn/daweiboshi_szy/image_cc1ba9bc4e3e8e58459ed9daee128c9f.jpg",
  83. // checked: true
  84. // },
  85. // {
  86. // file_name: "IMG_20220723_185155.jpg",
  87. // url: "http://look.qiniuyun.jiuweiyun.cn/daweiboshi_szy/image_b21151a7aa4edfbc707140adac3d86b9.jpg",
  88. // img_url: "http://look.qiniuyun.jiuweiyun.cn/daweiboshi_szy/image_b21151a7aa4edfbc707140adac3d86b9.jpg",
  89. // checked: false
  90. // }
  91. ],
  92. checkUrl: [],
  93. filename: [],
  94. nomore: false,
  95. current: 0,
  96. leftList: [],// 初始化左右盒子
  97. rightList: [],
  98. leftH: 0,// 初始化左右盒子高度
  99. rightH: 0,
  100. params: {
  101. id: '',
  102. page_index: 1,
  103. page_size: 15,
  104. type: 1
  105. },
  106. totalPage: 0
  107. }
  108. },
  109. onLoad(option) {
  110. if (option.title) {
  111. uni.setNavigationBarTitle({
  112. title: option.title
  113. });
  114. }
  115. this.params.id = option.id
  116. this.getList()
  117. this.doList(this.list)
  118. console.log(this.$u.config.v);
  119. },
  120. onReachBottom() {
  121. this.more()
  122. },
  123. methods: {
  124. close() {
  125. this.show = false
  126. // this.move()
  127. },
  128. // 点击放大图片
  129. previewImg(item) {
  130. let i = this.list.findIndex(v => v.id == item.id)
  131. this.current = i
  132. this.show = true
  133. // this.stop()
  134. },
  135. showMaster(item, i) {
  136. this.list[i].url = item.img_url
  137. setTimeout(()=>{
  138. this.list[i].master = false
  139. },1000)
  140. },
  141. //禁止页面滑动
  142. stop(){
  143. var mo=function(e){e.preventDefault();};
  144. document.body.style.overflow='hidden';
  145. document.addEventListener("touchmove",mo,false);
  146. },
  147. /***取消滑动限制***/
  148. move(){
  149. var mo=function(e){e.preventDefault();};
  150. document.body.style.overflow='';//出现滚动条
  151. document.removeEventListener("touchmove",mo,false);
  152. },
  153. //获取列表
  154. getList(isMore) {
  155. uni.showLoading()
  156. GetImgDetail(this.params).then(res => {
  157. uni.hideLoading()
  158. if (res.code === 200) {
  159. const { list, total } = res.data
  160. if (list.length === 0) {
  161. uni.showToast({ title: '暂无内容', icon: 'none' })
  162. return false
  163. }
  164. this.totalPage = Math.ceil(Number(total) / 15)
  165. list.forEach(e=> {
  166. e.checked = false
  167. e.master = true
  168. })
  169. this.doList(list)
  170. this.list = isMore ? this.list.concat(list) : list
  171. } else {
  172. uni.showToast({ title: res.message || '获取列表失败', icon: 'none' })
  173. }
  174. })
  175. },
  176. //加载获取更多
  177. more() {
  178. if (this.params.page_index >= this.totalPage) {
  179. this.nomore = true
  180. return false
  181. }
  182. this.params.page_index++
  183. this.getList(true)
  184. },
  185. // 瀑布流
  186. doList(list) {
  187. const that = this
  188. list.forEach(res => {
  189. // 获取图片宽高
  190. uni.getImageInfo({
  191. src: res.url,
  192. success: (image) => {
  193. // 计算图片渲染高度
  194. let showH = (50 * image.height) / image.width
  195. // 判断左右盒子高度
  196. if(that.leftH <= that.rightH) {
  197. that.leftList.push(res)
  198. that.leftH += showH
  199. } else {
  200. that.rightList.push(res)
  201. that.rightH += showH
  202. }
  203. }
  204. })
  205. })
  206. },
  207. // 下载图片
  208. downloadImgs() {
  209. this.filename = []
  210. if (this.checkUrl.length === 0) {
  211. uni.showToast({
  212. title: '请选择需要保存的图片',
  213. icon: 'none'
  214. })
  215. return
  216. }
  217. let _this = this
  218. _this.checkUrl.forEach(c => {
  219. _this.list.forEach(v => {
  220. if (c == v.img_url) {
  221. _this.filename.push(v.file_name)
  222. }
  223. })
  224. })
  225. console.log(_this.checkUrl, _this.filename)
  226. // 调用保存图片promise队列
  227. _this.queue(_this.checkUrl, _this.filename).then(res => {
  228. uni.hideLoading()
  229. // _this.clearUp()
  230. // _this.checkUrl = []
  231. // _this.filename = []
  232. }).catch(err => {
  233. uni.hideLoading()
  234. })
  235. },
  236. // 队列
  237. queue(urls, filename) {
  238. console.log(2, urls, filename)
  239. let promise = Promise.resolve()
  240. urls.forEach((url, index) => {
  241. promise = promise.then(() => {
  242. return this.download(url, filename[index], index)
  243. })
  244. })
  245. return promise
  246. },
  247. // 下载
  248. download(url, filename, index) {
  249. console.log(url, filename)
  250. let length = this.checkUrl.length
  251. uni.downloadFile({
  252. url: url,
  253. success: function(res) {
  254. var temp = res.tempFilePath
  255. // var link = document.createElement('a')
  256. // link.href = temp
  257. // link.download = filename
  258. // document.body.appendChild(link)
  259. // link.click()
  260. let image = new Image();
  261. // 解决跨域 Canvas 污染问题
  262. image.setAttribute("crossOrigin", "anonymous");
  263. image.onload = function() {
  264. let canvas = document.createElement("canvas");
  265. canvas.width = image.width;
  266. canvas.height = image.height;
  267. let context = canvas.getContext("2d");
  268. context.drawImage(image, 0, 0, image.width, image.height);
  269. let img = canvas.toDataURL("image/png"); //得到图片的base64编码数据
  270. let a = document.createElement("a"); // 生成一个a元素
  271. let event = new MouseEvent("click"); // 创建一个单击事件
  272. a.download = filename|| "photo"; // 设置图片名称,-------------你传递的图片名称
  273. a.href = img; // 将生成的URL设置为a.href属性
  274. a.dispatchEvent(event); // 触发a的单击事件
  275. };
  276. image.src = temp;//你的图片地址
  277. },
  278. fail: function(err) {
  279. }
  280. })
  281. },
  282. // 清除已选项
  283. clearUp() {
  284. this.leftList.forEach((item, i) => {
  285. item.checked = false
  286. })
  287. this.rightList.forEach((item, i) => {
  288. item.checked = false
  289. })
  290. },
  291. // 选中任一checkbox时,由checkbox-group触发
  292. checkboxGroupChange(e) {
  293. this.checkUrl = e
  294. },
  295. }
  296. }
  297. </script>
  298. <style lang="scss" scoped>
  299. * { touch-action: pan-y; }
  300. page {
  301. background-color: #fff;
  302. padding-top: 20rpx;
  303. }
  304. .close {
  305. position: fixed;
  306. top: 20rpx;
  307. right: 20rpx;
  308. background: #6f727699;
  309. width: 80rpx;
  310. height: 80rpx;
  311. line-height: 88rpx;
  312. text-align: center;
  313. border-radius: 50%;
  314. z-index: 20000;
  315. image {
  316. width: 30rpx;
  317. }
  318. }
  319. .swiper {
  320. position: relative;
  321. left: 0;
  322. top: 0;
  323. width: 100%;
  324. height: 100%;
  325. overflow: hidden;
  326. .image-view-area, .image-view-view {
  327. position: absolute;
  328. left: 0;
  329. top: 0;
  330. width: 100%;
  331. height: 100%;
  332. }
  333. img {
  334. position: absolute;
  335. left: 50%;
  336. top: 50%;
  337. -webkit-transform: translate(-50%,-50%);
  338. transform: translate(-50%,-50%);
  339. max-height: 100%;
  340. max-width: 100%;
  341. width: 100%;
  342. }
  343. .btn {
  344. position: absolute;
  345. bottom: 60rpx;
  346. left: 285rpx;
  347. width: 180rpx;
  348. height: 60rpx;
  349. line-height: 60rpx;
  350. border-radius: 36rpx 36rpx 36rpx 36rpx;
  351. // border: 1rpx #FFFFFF solid;
  352. color: #FFFFFF;
  353. text-align: center;
  354. background-color: #333;
  355. }
  356. }
  357. .container {
  358. padding: 0 24rpx;
  359. font-size: 14rpx;
  360. line-height: 24rpx;
  361. display: flex;
  362. justify-content: space-between;
  363. .right, .left{
  364. display: inline-block;
  365. width: 340rpx;
  366. vertical-align: top;
  367. }
  368. .left image, .right image{
  369. border-radius: 16rpx;
  370. width: 100%;
  371. margin-bottom: 24rpx;
  372. }
  373. .posive {
  374. position: relative;
  375. }
  376. .collected {
  377. border: 2rpx solid #3D6EF6;
  378. position: absolute;
  379. background-color: rgba(0, 0, 0, 0.4);
  380. border-radius: 50%;
  381. bottom: 40rpx;
  382. right: 16rpx;
  383. z-index: 10;
  384. }
  385. .fixed {
  386. width: 80rpx;
  387. height: 80rpx;
  388. border-radius: 50%;
  389. text-align: center;
  390. line-height: 80rpx;
  391. background: #3D6EF6;
  392. position: fixed;
  393. right: 24rpx;
  394. bottom: 40rpx;
  395. color: #fff;
  396. z-index: 100;
  397. font-size: 28rpx;
  398. }
  399. .fixed2 {
  400. position: fixed;
  401. right: 0;
  402. bottom: 0;
  403. width: 684rpx;
  404. height: 96rpx;
  405. background: #000000;
  406. border-radius: 0px 0px 0px 0px;
  407. // opacity: 0.5;
  408. background-color: rgba(000,000,000,0.5);
  409. padding: 0 32rpx;
  410. color: #FFFFFF;
  411. font-size: 28rpx;
  412. z-index: 100;
  413. .left {
  414. font-size: 32rpx;
  415. font-weight: bold;
  416. text {
  417. color: #3D6EF6;
  418. margin: 0 10rpx;
  419. }
  420. }
  421. .upload {
  422. width: 176rpx;
  423. height: 64rpx;
  424. background: #3D6EF6;
  425. border-radius: 32rpx 32rpx 32rpx 32rpx;
  426. text-align: center;
  427. line-height: 64rpx;
  428. }
  429. .quxiao {
  430. width: 120rpx;
  431. height: 64rpx;
  432. margin-left: 24rpx;
  433. border-radius: 32rpx 32rpx 32rpx 32rpx;
  434. opacity: 1;
  435. border: 2rpx solid #F5F5F5;
  436. text-align: center;
  437. line-height: 64rpx;
  438. }
  439. }
  440. }
  441. </style>