video_detail.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <template>
  2. <view class="video_detail">
  3. <view class="videos" v-for="(item, index) in imgList" :key="item.id">
  4. <view>
  5. <view class="cover" @click="videoPlay(index)" v-if="index != curPlay"></view>
  6. <video :src="item.download_url" :id="'myVideo' + index" v-if="item.download_url" custom-cache="false"></video>
  7. </view>
  8. <view class="flexB state">
  9. <view class="flexC" @click="downloadVideo(item.download_url)">
  10. <image src="../../static/down_load.png" style="height:40rpx;width:40rpx;"></image>
  11. <text>下载</text>
  12. </view>
  13. <view @click="getCollect(item)" class="flexC">
  14. <image src="../../static/imgs/collected.png" class="collect" v-if="item.like"></image>
  15. <image src="../../static/imgs/my_collect.png" class="collect" v-else></image>
  16. <text>{{ item.like ? '已收藏' : '收藏' }}</text>
  17. </view>
  18. </view>
  19. </view>
  20. </view>
  21. </template>
  22. <script>
  23. import { getCollect, albumDetail, myCollect } from '../../api/album.js';
  24. export default {
  25. data() {
  26. return {
  27. imgList: [], //视频列表
  28. params: {
  29. id: '',
  30. activity_id: '',
  31. page_index: 1,
  32. page_size: 5
  33. },
  34. pages: '', //总页数
  35. title: '', //当前页面标题,
  36. curPlay: null
  37. };
  38. },
  39. onLoad(options) {
  40. this.title = options.title;
  41. this.params.id = options.id;
  42. this.params.activity_id = options.activity_id;
  43. },
  44. onShow() {
  45. uni.setNavigationBarTitle({
  46. title: this.title
  47. });
  48. },
  49. onReady() {
  50. this.getPhotos();
  51. },
  52. onReachBottom() {
  53. this.more();
  54. },
  55. methods: {
  56. videoPlay(index) {
  57. var that = this;
  58. if (that.curPlay != null) {
  59. //如果有正在播放的视频
  60. //停止正在播放的视频
  61. let videoContextPrev = wx.createVideoContext('myVideo' + that.curPlay);
  62. if (that.curPlay != index) {
  63. videoContextPrev.pause();
  64. }
  65. //开始播放当前点击的视频
  66. that.curPlay = index;
  67. let videoContextCurrent = wx.createVideoContext('myVideo' + index);
  68. videoContextCurrent.play();
  69. } else {
  70. //如果没有正在播放的视频
  71. that.curPlay = index;
  72. let videoContext = wx.createVideoContext('myVideo' + index);
  73. videoContext.play();
  74. }
  75. },
  76. //点击收藏当前视频
  77. getCollect(item) {
  78. if (item.like == true) {
  79. uni.showModal({
  80. content: '您已收藏过,请勿重复收藏',
  81. showCancel: false
  82. });
  83. return false;
  84. }
  85. let { id, activity_id, img_type } = item;
  86. getCollect({
  87. id,
  88. activity_id,
  89. img_type
  90. }).then(res => {
  91. if (res.code == 200) {
  92. let list = this.imgList;
  93. list.map(i => {
  94. if (item.id == i.id) {
  95. this.$set(i, 'like', true);
  96. }
  97. });
  98. this.imgList = list;
  99. uni.showToast({
  100. title: '收藏成功'
  101. });
  102. } else {
  103. uni.showModal({
  104. content: res.message || '请求失败',
  105. showCancel: false
  106. });
  107. }
  108. });
  109. },
  110. // 下载视频
  111. async downloadVideo(url) {
  112. await uni.showLoading({
  113. title: '下载中'
  114. });
  115. // 1.将远程文件下载到小程序的内存中,tempFilePath
  116. const result1 = await uni.downloadFile({
  117. url
  118. });
  119. const { tempFilePath } = result1[1];
  120. console.log(tempFilePath, 'tempFilePath');
  121. // 2.将小程序内存中的临时文件下载到本地上
  122. const result2 = await uni.saveVideoToPhotosAlbum({
  123. filePath: tempFilePath,
  124. success: () => {
  125. uni.showModal({
  126. content: '下载成功',
  127. showCancel: false
  128. });
  129. },
  130. fail: err => {
  131. console.log(err);
  132. if (err.errMsg === 'saveVideoToPhotosAlbum:fail auth deny' || err.errMsg === 'saveVideoToPhotosAlbum:fail authorize no response') {
  133. uni.showModal({
  134. title: '提示',
  135. content: '您好,请先授权,再保存此视频。',
  136. showCancel: false,
  137. success: res => {
  138. if (res.confirm) {
  139. uni.openSetting({
  140. success(settingdata) {
  141. if (settingdata.authSetting['scope.writePhotosAlbum']) {
  142. uni.showModal({
  143. title: '提示',
  144. content: '授权成功!请重新下载视频',
  145. showCancel: false
  146. });
  147. } else {
  148. uni.showModal({
  149. title: '提示',
  150. content: '获取相册权限失败',
  151. showCancel: false
  152. });
  153. }
  154. },
  155. fail: err => {
  156. console.log(err);
  157. }
  158. });
  159. }
  160. }
  161. });
  162. } else {
  163. uni.showModal({
  164. content: '下载失败',
  165. showCancel: false
  166. });
  167. }
  168. },
  169. complete: () => {
  170. uni.hideLoading();
  171. }
  172. });
  173. },
  174. /*获取视频列表
  175. * @params id 当前视频id
  176. * @params activity_id 当前活动id
  177. * @params page_index 第几页
  178. * @params page_size 每页显示数量
  179. */
  180. getPhotos() {
  181. this.params.page_index = 1;
  182. let { id, activity_id, page_index, page_size } = this.params;
  183. albumDetail({
  184. id,
  185. activity_id,
  186. page_index,
  187. page_size
  188. }).then(res => {
  189. if (res.code == 200) {
  190. this.imgList = res.data.list;
  191. this.pages = Math.ceil(res.data.total / this.params.page_size);
  192. } else {
  193. uni.showModal({
  194. content: res.message || '请求失败',
  195. showCancel: false
  196. });
  197. }
  198. });
  199. },
  200. //上拉加载更多
  201. more() {
  202. this.params.page_index++;
  203. if (this.params.page_index > this.pages) {
  204. uni.showToast({
  205. title: '没有更多啦~',
  206. icon: 'none'
  207. });
  208. return false;
  209. }
  210. let { id, activity_id, page_size, page_index } = this.params;
  211. albumDetail({
  212. id,
  213. activity_id,
  214. page_index,
  215. page_size
  216. }).then(res => {
  217. if (res.code == 200) {
  218. if (res.data.list.length > 0) {
  219. this.imgList = this.imgList.concat(res.data.list);
  220. }
  221. } else {
  222. uni.showModal({
  223. content: res.message || '请求失败',
  224. showCancel: false
  225. });
  226. }
  227. });
  228. }
  229. }
  230. };
  231. </script>
  232. <style lang="scss">
  233. .video_detail {
  234. padding: 20rpx 0;
  235. }
  236. .videos {
  237. width: 690rpx;
  238. margin: 0 auto;
  239. position: relative;
  240. video {
  241. width: 100%;
  242. height: 356rpx;
  243. border-radius: 32rpx;
  244. }
  245. .cover {
  246. width: 100%;
  247. height: 356rpx;
  248. position: absolute;
  249. top: 0;
  250. left: 0;
  251. z-index: 100;
  252. }
  253. .state {
  254. width: 75%;
  255. margin: 16rpx auto 34rpx;
  256. image {
  257. margin-right: 15rpx;
  258. }
  259. text {
  260. font-size: 28rpx;
  261. color: #333;
  262. }
  263. }
  264. .collect {
  265. width: 40rpx;
  266. height: 40rpx;
  267. }
  268. }
  269. </style>