裂变星小程序-25.03.04
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

246 lines
5.3 KiB

  1. <template>
  2. <view class="page">
  3. <navbar leftClick @leftClick="$utils.navigateBack" />
  4. <view class="content">
  5. <video class="video" id="video" :src="detail.vio" autoplay play-btn-position="center" :controls="!timeIsUp"
  6. :show-fullscreen-btn="false" :show-center-play-btn="true" @timeupdate="onTimeupdate"
  7. @ended="onTimeEnd"></video>
  8. <view class="info">
  9. <view class="author">{{ detail.author || '' }}</view>
  10. <view class="title">{{ detail.headTitle || '' }}</view>
  11. <view class="desc">{{ detail.textDetails || '' }}</view>
  12. </view>
  13. </view>
  14. <uv-overlay :show="timeIsUp" @click="onPlay" zIndex="998">
  15. <popupUnlock ref="popupUnlock" src="../static/sharing/unlock-video.png"></popupUnlock>
  16. </uv-overlay>
  17. <popupQrCode ref="popupQrCode" :src="detail.wxCodeImage"></popupQrCode>
  18. </view>
  19. </template>
  20. <script>
  21. import {
  22. mapState
  23. } from 'vuex'
  24. import popupUnlock from '../components/popupUnlock.vue'
  25. import popupQrCode from '../components/popupQrCode.vue'
  26. import shareLog from '@/utils/shareLog'
  27. export default {
  28. components: {
  29. popupUnlock,
  30. popupQrCode,
  31. },
  32. data() {
  33. return {
  34. id: null,
  35. detail: {
  36. id: null,
  37. headTitle: null,
  38. indexImage: null,
  39. vio: null,
  40. timeNum: 0,
  41. num: 0,
  42. wxCodeImage: null,
  43. textDetails: null,
  44. },
  45. timeIsUp: false,
  46. isLocked: true,
  47. videoContext: null,
  48. }
  49. },
  50. computed: {
  51. ...mapState(['userInfo']),
  52. },
  53. onShow() {
  54. // if (this.id && uni.getStorageSync('token')) {
  55. // this.detail.id ? this.refreshLockStatus() : this.initData()
  56. // }
  57. if (this.detail.id) { // 转发后返回页面的场景
  58. this.refreshLockStatus()
  59. }
  60. },
  61. async onLoad(option) {
  62. const {
  63. id,
  64. state,
  65. shareId
  66. } = option
  67. if (shareId) {
  68. uni.setStorageSync('shareId', shareId)
  69. }
  70. if (state) {
  71. uni.setStorageSync('state', state)
  72. }
  73. if (id) {
  74. uni.setStorageSync('id', id)
  75. }
  76. this.id = id
  77. // if(uni.getStorageSync('token')){
  78. // this.initData()
  79. // }else{
  80. // uni.navigateTo({
  81. // url: '/pages_order/auth/wxLogin'
  82. // })
  83. // }
  84. this.initData()
  85. shareLog.clear()
  86. },
  87. onShareAppMessage(res) {
  88. const {
  89. headTitle,
  90. indexImage,
  91. } = this.detail
  92. let o = {
  93. title: headTitle,
  94. imageUrl: indexImage,
  95. query: `id=${this.id}&state=1&shareId=${this.userInfo.id}`,
  96. }
  97. //调用增加分享次数的方法
  98. const params = {
  99. id: this.id,
  100. state: "1",
  101. }
  102. // this.$fetch('addLogShareInfo', params)
  103. // 将分享记录移到回调成功里面,避免分享取消也计数
  104. shareLog.insert(this.id)
  105. return o
  106. },
  107. methods: {
  108. async fetchDetails(id) {
  109. try {
  110. this.detail = await this.$fetch('getVideoShareInfo', {
  111. id
  112. })
  113. // 确保num和timeNum是数字类型
  114. if (this.detail) {
  115. this.detail.num = parseInt(this.detail.num) || 0;
  116. this.detail.timeNum = parseFloat(this.detail.timeNum) || 0;
  117. }
  118. } catch (err) {
  119. }
  120. },
  121. async initData() {
  122. this.isLocked = true
  123. await this.fetchDetails(this.id)
  124. this.videoContext = uni.createVideoContext('video');
  125. // 初始加载时检查是否已经解锁
  126. const result = await this.fetchCheckShare()
  127. if (result.open) {
  128. this.isLocked = false
  129. this.timeIsUp = false
  130. }
  131. },
  132. async refreshLockStatus() {
  133. // 不要在这里设置timeIsUp = false,这会导致弹窗被关闭
  134. // this.timeIsUp = false
  135. setTimeout(async () => {
  136. const result = await this.fetchCheckShare()
  137. const {
  138. open,
  139. need_num,
  140. num
  141. } = result
  142. if (open) { // 转发已达标
  143. this.videoContext.play()
  144. this.isLocked = false
  145. this.timeIsUp = false // 只有在达标时才关闭弹窗
  146. return
  147. }else{
  148. if(this.timeIsUp){
  149. this.onPlay()
  150. }
  151. }
  152. // 如果未达标,只显示提示,不改变弹窗状态
  153. uni.showToast({
  154. title: `还需转发${need_num - num}`,
  155. icon: 'none',
  156. })
  157. })
  158. },
  159. async fetchCheckShare() {
  160. try {
  161. // 确保detail.num被转换为数字,因为API返回的是字符串类型
  162. const numValue = parseInt(this.detail.num) || 0;
  163. return await shareLog.check(this.id, numValue)
  164. } catch (err) {
  165. return {}
  166. }
  167. },
  168. async onPlay() {
  169. if (!this.isLocked) {
  170. return
  171. }
  172. // 先检查是否已经达到分享条件
  173. const result = await this.fetchCheckShare()
  174. if (result.open) {
  175. this.isLocked = false
  176. this.timeIsUp = false
  177. this.videoContext.play()
  178. return
  179. }
  180. // 如果未达到条件,则暂停视频并显示弹窗
  181. this.videoContext.pause()
  182. this.timeIsUp = true
  183. this.$refs.popupUnlock.open();
  184. },
  185. async onTimeupdate(e) {
  186. const {
  187. currentTime
  188. } = e.target
  189. if (currentTime >= this.detail.timeNum && this.isLocked) {
  190. this.onPlay()
  191. }
  192. },
  193. onTimeEnd() {
  194. this.$refs.popupQrCode.open()
  195. },
  196. },
  197. }
  198. </script>
  199. <style scoped lang="scss">
  200. .video {
  201. width: 100%;
  202. height: calc(100vh - #{$navbar-height} - var(--status-bar-height) - 20rpx);
  203. }
  204. .info {
  205. color: #FFFFFF;
  206. font-size: 28rpx;
  207. position: fixed;
  208. left: 40rpx;
  209. bottom: 100rpx;
  210. .title {
  211. font-size: 32rpx;
  212. margin: 5rpx 0;
  213. }
  214. }
  215. </style>