my_collect.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. <template>
  2. <view class="collect">
  3. <view class="tab">
  4. <view @click="getCollect(0)" :class="params.img_type == 0 ? 'active' : ''">照片</view>
  5. <view @click="getCollect(1)" :class="params.img_type == 1 ? 'active' : ''">视频</view>
  6. </view>
  7. <view class="scroll">
  8. <scroll-view scroll-y="true" @scrolltolower="more">
  9. <view class="scroll_con">
  10. <view v-if="params.img_type == 0" class="photos">
  11. <view class="photo" v-for="(item, index) in collectList" :key="index" @click="previewImg($event, item)">
  12. <view class="collect flexC"><image src="../../static/imgs/ph_collected.png"></image></view>
  13. <image :src="item.thumbnail"></image>
  14. </view>
  15. </view>
  16. <view v-if="params.img_type == 1 && collectList.length > 0">
  17. <view class="videos" v-for="(item, index) in collectList" :key="item.id">
  18. <view>
  19. <view class="cover" @click="videoPlay(index)" v-if="index != curPlay"></view>
  20. <video :src="item.img_url" :id="'myVideo' + index"></video>
  21. </view>
  22. <view class="flexB state" @click="downloadVideo(item.download_url)">
  23. <view class="flexC">
  24. <image src="../../static/imgs/down_load.png" style="width: 44rpx;height:42rpx;"></image>
  25. <text>下载</text>
  26. </view>
  27. <view @click="getCollect(item)" class="flexC">
  28. <image src="../../static/imgs/collected.png" style="height:40rpx;width:40rpx;"></image>
  29. <text>已收藏</text>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. <view class="no_data" v-if="collectList.length == 0"><image src="../../static/imgs/no_collect.png"></image></view>
  35. </view>
  36. </scroll-view>
  37. </view>
  38. </view>
  39. </template>
  40. <script>
  41. import { myCollect } from '../../api/album.js';
  42. export default {
  43. data() {
  44. return {
  45. params: {
  46. page_size: 20,
  47. page_index: 1,
  48. img_type: 0 //0 照片 1 视频
  49. },
  50. pages: '', //总页数
  51. collectList: [], //收藏列表
  52. preList: [], //点击放大的图片
  53. curPlay: null //当前播放的视频
  54. };
  55. },
  56. onLoad() {
  57. this.getCollect(0);
  58. },
  59. onReachBottom() {
  60. this.more();
  61. },
  62. methods: {
  63. //点击下一个视频时,上一个视频停止播放
  64. videoPlay(index) {
  65. var that = this;
  66. if (that.curPlay != null) {
  67. //如果有正在播放的视频
  68. //停止正在播放的视频
  69. let videoContextPrev = wx.createVideoContext('myVideo' + that.curPlay);
  70. if (that.curPlay != index) {
  71. videoContextPrev.pause();
  72. }
  73. //开始播放当前点击的视频
  74. that.curPlay = index;
  75. let videoContextCurrent = wx.createVideoContext('myVideo' + index);
  76. videoContextCurrent.play();
  77. } else {
  78. //如果没有正在播放的视频
  79. that.curPlay = index;
  80. let videoContext = wx.createVideoContext('myVideo' + index);
  81. videoContext.play();
  82. }
  83. },
  84. // 下载视频
  85. async downloadVideo(url) {
  86. await uni.showLoading({
  87. title: '下载中'
  88. });
  89. // 1.将远程文件下载到小程序的内存中,tempFilePath
  90. const result1 = await uni.downloadFile({
  91. url
  92. });
  93. const { tempFilePath } = result1[1];
  94. // 2.将小程序内存中的临时文件下载到本地上
  95. const result2 = await uni.saveVideoToPhotosAlbum({
  96. filePath: tempFilePath,
  97. success: () => {
  98. uni.showModal({
  99. content: '下载成功',
  100. showCancel: false
  101. });
  102. },
  103. fail: err => {
  104. console.log(err);
  105. if (err.errMsg === 'saveVideoToPhotosAlbum:fail auth deny' || err.errMsg === 'saveVideoToPhotosAlbum:fail authorize no response') {
  106. uni.showModal({
  107. title: '提示',
  108. content: '您好,请先授权,再保存此视频。',
  109. showCancel: false,
  110. success: res => {
  111. if (res.confirm) {
  112. uni.openSetting({
  113. success(settingdata) {
  114. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  115. uni.showModal({
  116. title: '提示',
  117. content: '授权成功!请重新下载视频',
  118. showCancel: false
  119. });
  120. } else {
  121. uni.showModal({
  122. title: '提示',
  123. content: '获取相册权限失败',
  124. showCancel: false
  125. });
  126. }
  127. },
  128. fail: err => {
  129. console.log(err);
  130. }
  131. });
  132. }
  133. }
  134. });
  135. } else {
  136. uni.showModal({
  137. content: '下载失败',
  138. showCancel: false
  139. });
  140. }
  141. },
  142. complete: () => {
  143. uni.hideLoading();
  144. }
  145. });
  146. },
  147. //点击放大图片
  148. previewImg(e, item) {
  149. uni.previewImage({
  150. current: item.img_url,
  151. urls: this.preList
  152. });
  153. },
  154. /* 获取收藏列表
  155. * @params page_index 当前是第几页
  156. * @params page_size 当前显示几个
  157. * @params img_type 判断照片还是视频 0 照片 1 视频
  158. */
  159. getCollect(type) {
  160. this.collectList = '';
  161. this.params.page_index = 1;
  162. this.params.img_type = type;
  163. let { page_index, page_size, img_type } = this.params;
  164. myCollect({
  165. page_index,
  166. page_size,
  167. img_type
  168. }).then(res => {
  169. if (res.code == 200) {
  170. this.collectList = res.data.list;
  171. this.pages = Math.ceil(res.data.total / this.params.page_size);
  172. let list = res.data.list;
  173. if (this.preList.length < list.length) {
  174. list.map(i => {
  175. this.preList.push(i.img_url);
  176. });
  177. }
  178. } else {
  179. uni.showModal({
  180. content: res.message || '请求失败',
  181. showCancel: false
  182. });
  183. }
  184. });
  185. },
  186. //上拉加载更多
  187. more() {
  188. this.params.page_index++;
  189. if (this.params.page_index > this.pages) {
  190. uni.showToast({
  191. title: '没有更多啦~',
  192. icon: 'none'
  193. });
  194. return false;
  195. }
  196. let { page_size, img_type, page_index } = this.params;
  197. myCollect({
  198. page_index,
  199. page_size,
  200. img_type
  201. }).then(res => {
  202. if (res.code == 200) {
  203. if (res.data.list.length > 0) {
  204. this.collectList = this.collectList.concat(res.data.list);
  205. let list = res.data.list;
  206. if (this.preList.length < this.collectList.length) {
  207. list.map(i => {
  208. this.preList.push(i.img_url);
  209. });
  210. }
  211. }
  212. } else {
  213. uni.showModal({
  214. content: res.message || '请求失败',
  215. showCancel: false
  216. });
  217. }
  218. });
  219. }
  220. }
  221. };
  222. </script>
  223. <style lang="scss">
  224. .collect {
  225. .tab {
  226. width: 100%;
  227. height: 7vh;
  228. display: flex;
  229. justify-content: space-around;
  230. align-items: center;
  231. border-top: 1rpx solid #f2f2f2;
  232. border-bottom: 1rpx solid #f2f2f2;
  233. margin-bottom: 20rpx;
  234. background: #fff;
  235. view {
  236. height: 85rpx;
  237. line-height: 85rpx;
  238. font-size: 32rpx;
  239. letter-spacing: 5rpx;
  240. font-weight: 500;
  241. color: #666;
  242. &::after {
  243. content: '';
  244. display: block;
  245. width: 130rpx;
  246. height: 4rpx;
  247. border-radius: 2rpx;
  248. margin-left: -32rpx;
  249. }
  250. }
  251. .active {
  252. color: #ea4a41;
  253. &::after {
  254. background-color: #ea4a41;
  255. }
  256. }
  257. }
  258. .scroll {
  259. height: 93vh;
  260. scroll-view {
  261. height: 100%;
  262. }
  263. .scroll_con {
  264. padding-bottom: 20px;
  265. }
  266. .photos {
  267. width: 100%;
  268. display: flex;
  269. align-items: center;
  270. justify-content: flex-start;
  271. flex-wrap: wrap;
  272. .photo {
  273. position: relative;
  274. width: 33.33%;
  275. text-align: center;
  276. image {
  277. height: 236rpx;
  278. width: 236rpx;
  279. border-radius: 24rpx;
  280. }
  281. .collect {
  282. width: 58rpx;
  283. height: 49rpx;
  284. position: absolute;
  285. background-color: rgba(0, 0, 0, .4);
  286. border-radius: 24rpx 0 24rpx 0;
  287. bottom: 13rpx;
  288. right: 9rpx;
  289. z-index: 9999;
  290. image {
  291. height: 30rpx;
  292. width: 30rpx;
  293. }
  294. }
  295. }
  296. }
  297. .videos {
  298. width: 690rpx;
  299. margin: 0 auto;
  300. padding-top: 20rpx;
  301. position: relative;
  302. video {
  303. width: 100%;
  304. height: 356rpx;
  305. border-radius: 32rpx;
  306. }
  307. .cover {
  308. width: 100%;
  309. height: 356rpx;
  310. position: absolute;
  311. top: 0;
  312. left: 0;
  313. z-index: 100;
  314. }
  315. .state {
  316. width: 75%;
  317. margin: 16rpx auto 34rpx;
  318. image {
  319. margin-right: 15rpx;
  320. }
  321. text {
  322. font-size: 28rpx;
  323. color: #333;
  324. }
  325. }
  326. .collect {
  327. width: 40rpx;
  328. height: 40rpx;
  329. }
  330. }
  331. .no_data {
  332. position: absolute;
  333. top: 50%;
  334. left: 50%;
  335. transform: translate(-50%, -50%);
  336. image {
  337. width: 170rpx;
  338. height: 186rpx;
  339. }
  340. }
  341. }
  342. }
  343. </style>