charge-dialog.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import {
  2. unlockChapter
  3. } from '../../api/api.js'
  4. import {
  5. apple
  6. } from '../../config/config'
  7. const novelPlugin = requirePlugin('novel-plugin')
  8. Component({
  9. properties: {
  10. novelManagerId: {
  11. type: Number,
  12. value: -1,
  13. },
  14. bookId: {
  15. type: String,
  16. value: '',
  17. },
  18. chapterIndex: {
  19. type: Number,
  20. value: -1,
  21. },
  22. chapterId: {
  23. type: String,
  24. value: '',
  25. },
  26. originalId: {
  27. type: String,
  28. value: '',
  29. },
  30. },
  31. data: {
  32. countdownNum: 3,
  33. isCounting: false
  34. },
  35. created() {
  36. this.rewardVideoLoad()
  37. },
  38. ready() {
  39. setTimeout(() => {
  40. this.startCountdown()
  41. }, 500)
  42. },
  43. detached() {
  44. this.stopCountdown(); // 页面卸载时清除定时器
  45. },
  46. methods: {
  47. unlock() {
  48. this.stopCountdown();
  49. this.rewardVideoPlayFn()
  50. },
  51. // 激励视频加载
  52. rewardVideoLoad() {
  53. if (wx.createRewardedVideoAd) {
  54. let adId = apple.ads.rewardAdId
  55. this.videoAd = wx.createRewardedVideoAd({
  56. adUnitId: adId
  57. })
  58. this.videoAd.offClose()
  59. this.videoAd.onLoad(() => {
  60. console.log('激励加载成功')
  61. })
  62. this.videoAd.onError((err) => {
  63. console.log('激励失败', err)
  64. })
  65. this.videoAd.onClose((res) => {
  66. const novelManager = novelPlugin.getNovelManager(this.properties.novelManagerId)
  67. if (res && res.isEnded) {
  68. // for (let index = 0; index < getApp().globalData.unlockChapterNum; index++) {
  69. // }
  70. let params = {
  71. chapterNo: this.properties.chapterIndex,
  72. novelId: getApp().globalData.novelId,
  73. type: 1
  74. }
  75. unlockChapter(params).then(res => {
  76. console.log(res)
  77. novelManager.paymentCompleted()
  78. console.log('章节解锁成功', this.properties.chapterIndex)
  79. wx.showToast({
  80. title: '解锁成功',
  81. icon: 'none'
  82. })
  83. })
  84. this.videoAd.offClose()
  85. } else {
  86. novelManager.closeChargeDialog()
  87. console.log('关闭解锁组件')
  88. wx.showToast({
  89. title: '广告解锁失败,请重试',
  90. icon: 'none'
  91. })
  92. }
  93. })
  94. }
  95. },
  96. // 激励视频展示
  97. rewardVideoPlayFn() {
  98. wx.showLoading({
  99. title: '解锁视频中',
  100. })
  101. this.videoAd.show().then(() => {
  102. console.log('激励视频播放成功')
  103. }).catch((err) => {
  104. console.log('激励视频播放失败', err)
  105. wx.showToast({
  106. title: '观看人数太多,请稍后再试',
  107. icon: 'none'
  108. })
  109. }).then(() => {
  110. wx.hideLoading()
  111. })
  112. },
  113. startCountdown() {
  114. this.setData({
  115. isCounting: true
  116. });
  117. let seconds = 3;
  118. this.countdownTimer = setInterval(() => {
  119. seconds--;
  120. this.setData({
  121. countdownNum: seconds
  122. });
  123. if (seconds <= 0) {
  124. this.stopCountdown();
  125. this.onCountdownEnd();
  126. }
  127. }, 1000);
  128. },
  129. stopCountdown() {
  130. clearInterval(this.countdownTimer);
  131. this.setData({
  132. isCounting: false
  133. });
  134. },
  135. onCountdownEnd() {
  136. this.rewardVideoPlayFn()
  137. }
  138. },
  139. })