index.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <template>
  2. <view class="body">
  3. <view v-for="(item,i) in list" :key="i">
  4. <image v-if="item.cover" class="imgStryle"
  5. :style=" item.style ? 'width:'+ item.style.w +'px;height:' + item.style.h + 'px' : '' "
  6. @load="v=>loadIndex(v,i)" @click="() => tonext(item)" :src="item.cover" mode="scaleToFill" />
  7. <swiper @change="e=>change(i + '-' + e.detail.current)" v-if="item.swiper_list && item.swiper_list.length"
  8. :indicator-dots="true" :autoplay="autoplay"
  9. :style=" item.swiper_list[0].style ? 'height:' + item.swiper_list[0].style.h + 'px' : '' "
  10. :interval="3000" :duration="1000">
  11. <swiper-item v-for="(v, o) in item.swiper_list" :key="'swiper-item' + i">
  12. <video class="imgStryle videos" :id="`${i}-${o}`"
  13. :style=" v.style ? 'width:'+ v.style.w +'px;height:' + v.style.h + 'px' : '' " :src="v.url"
  14. :autoplay="item.current == o"
  15. @loadedmetadata="video_item=>videoLoadIndex(video_item.detail, i, o)" initial-time="0.01"
  16. @play="play" @ended="ended" :poster="v.cover" :enable-progress-gesture="false" controls></video>
  17. </swiper-item>
  18. </swiper>
  19. </view>
  20. </view>
  21. </template>
  22. <script setup>
  23. import {
  24. ref
  25. } from "vue";
  26. import config from "../../config/index.js";
  27. import {
  28. onShareAppMessage,
  29. onShareTimeline,
  30. onReady
  31. } from '@dcloudio/uni-app'
  32. const info = uni.getSystemInfoSync();
  33. const list = ref([]);
  34. const autoplay = ref(true);
  35. let lastVideoId = undefined;
  36. onShareAppMessage(() => {})
  37. onShareTimeline(() => {})
  38. onReady(() => {
  39. })
  40. uni.request({
  41. url: config.base + "home.json?" + Date.now(),
  42. success: r => {
  43. if (r.statusCode !== 200) return
  44. const list_json = r.data.list || [];
  45. list.value = list_json;
  46. }
  47. })
  48. function loadIndex(v, i) {
  49. const w = info.windowWidth - 16;
  50. const h = v.detail.height / v.detail.width * w;
  51. list.value[i].style = {
  52. w,
  53. h
  54. };
  55. }
  56. function videoLoadIndex(v, i, o) {
  57. const w = info.windowWidth - 16;
  58. const h = v.height / v.width * w;
  59. list.value[i].swiper_list[o].style = {
  60. w,
  61. h
  62. }
  63. }
  64. function tonext(T) {
  65. T.pageType === "detail" && uni.navigateTo({
  66. url: "/pages/brief/brief?title=" + T.subTitle + "&page=" + T.page
  67. });
  68. T.pageType === "single_row" && uni.navigateTo({
  69. url: "/pages/single_row/single_row?title=" + T.title + "&url=" + T.headCover + "&page=" + T.page
  70. })
  71. T.pageType === "single_list" && uni.navigateTo({
  72. url: "/pages/single_list/single_list?title=" + T.title + "&url=" + T.headCover + "&page=" + T.page
  73. })
  74. }
  75. function play(e) {
  76. autoplay.value = false;
  77. if (e.currentTarget.id === lastVideoId || !lastVideoId) {
  78. lastVideoId = e.currentTarget.id
  79. return
  80. }
  81. const oldVideo = wx.createVideoContext(lastVideoId, this);
  82. oldVideo.pause();
  83. lastVideoId = e.currentTarget.id
  84. }
  85. function ended() {
  86. autoplay.value = true;
  87. }
  88. function change(id) {
  89. if (!wx || !id || lastVideoId === id) return
  90. const nowVideo = wx.createVideoContext(id, this);
  91. nowVideo.play();
  92. }
  93. </script>
  94. <style lang="scss">
  95. .body {
  96. padding: 1em 0;
  97. .imgStryle {
  98. margin: 2px 8px;
  99. border-radius: 10px;
  100. }
  101. }
  102. </style>