裂变星小程序-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.

245 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. },
  86. onShareAppMessage(res) {
  87. const {
  88. headTitle,
  89. indexImage,
  90. } = this.detail
  91. let o = {
  92. title: headTitle,
  93. imageUrl: indexImage,
  94. query: `id=${this.id}&state=1&shareId=${this.userInfo.id}`,
  95. }
  96. //调用增加分享次数的方法
  97. const params = {
  98. id: this.id,
  99. state: "1",
  100. }
  101. // this.$fetch('addLogShareInfo', params)
  102. // 将分享记录移到回调成功里面,避免分享取消也计数
  103. shareLog.insert(this.id)
  104. return o
  105. },
  106. methods: {
  107. async fetchDetails(id) {
  108. try {
  109. this.detail = await this.$fetch('getVideoShareInfo', {
  110. id
  111. })
  112. // 确保num和timeNum是数字类型
  113. if (this.detail) {
  114. this.detail.num = parseInt(this.detail.num) || 0;
  115. this.detail.timeNum = parseFloat(this.detail.timeNum) || 0;
  116. }
  117. } catch (err) {
  118. }
  119. },
  120. async initData() {
  121. this.isLocked = true
  122. await this.fetchDetails(this.id)
  123. this.videoContext = uni.createVideoContext('video');
  124. // 初始加载时检查是否已经解锁
  125. const result = await this.fetchCheckShare()
  126. if (result.open) {
  127. this.isLocked = false
  128. this.timeIsUp = false
  129. }
  130. },
  131. async refreshLockStatus() {
  132. // 不要在这里设置timeIsUp = false,这会导致弹窗被关闭
  133. // this.timeIsUp = false
  134. setTimeout(async () => {
  135. const result = await this.fetchCheckShare()
  136. const {
  137. open,
  138. need_num,
  139. num
  140. } = result
  141. if (open) { // 转发已达标
  142. this.videoContext.play()
  143. this.isLocked = false
  144. this.timeIsUp = false // 只有在达标时才关闭弹窗
  145. return
  146. }else{
  147. if(this.timeIsUp){
  148. this.onPlay()
  149. }
  150. }
  151. // 如果未达标,只显示提示,不改变弹窗状态
  152. uni.showToast({
  153. title: `还需转发${need_num - num}`,
  154. icon: 'none',
  155. })
  156. })
  157. },
  158. async fetchCheckShare() {
  159. try {
  160. // 确保detail.num被转换为数字,因为API返回的是字符串类型
  161. const numValue = parseInt(this.detail.num) || 0;
  162. return await shareLog.check(this.id, numValue)
  163. } catch (err) {
  164. return {}
  165. }
  166. },
  167. async onPlay() {
  168. if (!this.isLocked) {
  169. return
  170. }
  171. // 先检查是否已经达到分享条件
  172. const result = await this.fetchCheckShare()
  173. if (result.open) {
  174. this.isLocked = false
  175. this.timeIsUp = false
  176. this.videoContext.play()
  177. return
  178. }
  179. // 如果未达到条件,则暂停视频并显示弹窗
  180. this.videoContext.pause()
  181. this.timeIsUp = true
  182. this.$refs.popupUnlock.open();
  183. },
  184. async onTimeupdate(e) {
  185. const {
  186. currentTime
  187. } = e.target
  188. if (currentTime >= this.detail.timeNum && this.isLocked) {
  189. this.onPlay()
  190. }
  191. },
  192. onTimeEnd() {
  193. this.$refs.popupQrCode.open()
  194. },
  195. },
  196. }
  197. </script>
  198. <style scoped lang="scss">
  199. .video {
  200. width: 100%;
  201. height: calc(100vh - #{$navbar-height} - var(--status-bar-height) - 20rpx);
  202. }
  203. .info {
  204. color: #FFFFFF;
  205. font-size: 28rpx;
  206. position: fixed;
  207. left: 40rpx;
  208. bottom: 100rpx;
  209. .title {
  210. font-size: 32rpx;
  211. margin: 5rpx 0;
  212. }
  213. }
  214. </style>