charge-dialog.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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('章节解锁成功')
  79. })
  80. this.videoAd.offClose()
  81. } else {
  82. novelManager.closeChargeDialog()
  83. console.log('关闭解锁组件')
  84. }
  85. })
  86. }
  87. },
  88. // 激励视频展示
  89. rewardVideoPlayFn() {
  90. wx.showLoading({
  91. title: '解锁视频中',
  92. })
  93. this.videoAd.show().then(() => {
  94. console.log('激励视频播放成功')
  95. }).catch((err) => {
  96. console.log('激励视频播放失败', err)
  97. }).then(() => {
  98. wx.hideLoading()
  99. })
  100. },
  101. startCountdown() {
  102. this.setData({
  103. isCounting: true
  104. });
  105. let seconds = 3;
  106. this.countdownTimer = setInterval(() => {
  107. seconds--;
  108. this.setData({
  109. countdownNum: seconds
  110. });
  111. if (seconds <= 0) {
  112. this.stopCountdown();
  113. this.onCountdownEnd();
  114. }
  115. }, 1000);
  116. },
  117. stopCountdown() {
  118. clearInterval(this.countdownTimer);
  119. this.setData({
  120. isCounting: false
  121. });
  122. },
  123. onCountdownEnd() {
  124. this.rewardVideoPlayFn()
  125. }
  126. },
  127. })