index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {
  2. getProducts,
  3. getTasks,
  4. submitTask
  5. } from '~/api/global'
  6. import {
  7. buyNum,
  8. buyVip,
  9. getMyInfo,
  10. getVipInfo
  11. } from '~/api/user'
  12. const app = getApp()
  13. Component({
  14. data: {
  15. //弹窗显示控制
  16. showModalStatus: false,
  17. isIos: app.globalData.isIOS,
  18. userInfo: {},
  19. productNum: {},
  20. productVip: {},
  21. userInfo: {},
  22. vipTime: '',
  23. },
  24. lifetimes: {
  25. attached() {
  26. this.getProducts()
  27. this.setUserInfo()
  28. },
  29. },
  30. methods: {
  31. // 提交任务
  32. async submitTask({
  33. currentTarget
  34. }) {
  35. let id = currentTarget.dataset.type
  36. await submitTask({
  37. id
  38. })
  39. wx.showToast({
  40. title: id == '1' ? '签到成功!' : id == 3 ? "观看成功!" : "",
  41. icon: "none"
  42. })
  43. this.setUserInfo()
  44. },
  45. async getProducts() {
  46. let products = await getProducts()
  47. let productVip = products.find(item => {
  48. return item.type == 1
  49. })
  50. let productNum = products.find(item => {
  51. return item.type == 2
  52. })
  53. this.setData({
  54. productNum,
  55. productVip
  56. })
  57. },
  58. // 设置用户信息及vip状态和任务完成情况
  59. async setUserInfo() {
  60. let userInfo = await getMyInfo()
  61. let vipTime = await getVipInfo()
  62. this.getTasks()
  63. this.setData({
  64. userInfo,
  65. vipTime,
  66. })
  67. },
  68. // 调起广告
  69. rewardedVideo() {
  70. if (this.data.tasks.length != 3 || this.data.tasks[2].completed) {
  71. return
  72. }
  73. this.selectComponent('#advert').rewardedVideo();
  74. },
  75. async getTasks() {
  76. let tasks = await getTasks()
  77. this.setData({
  78. tasks
  79. })
  80. },
  81. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  82. async toBuy({
  83. currentTarget
  84. }) {
  85. let that = this
  86. let productId = currentTarget.dataset.type
  87. wx.showLoading({
  88. title: '提交中',
  89. mask: true
  90. })
  91. let res = ''
  92. if (productId == '1001') {
  93. res = await buyVip({
  94. productId
  95. }).finally(() => {
  96. wx.hideLoading()
  97. })
  98. } else if (productId == '1010') {
  99. res = await buyNum({
  100. productId
  101. }).finally(() => {
  102. wx.hideLoading()
  103. })
  104. } else {
  105. wx.hideLoading()
  106. wx.showToast({
  107. title: "支付失败,请重试",
  108. icon: "none"
  109. })
  110. }
  111. let {
  112. timeStamp,
  113. nonceStr,
  114. signType,
  115. paySign
  116. } = res
  117. // package保留字
  118. wx.requestPayment({
  119. timeStamp,
  120. nonceStr,
  121. package: res.package,
  122. signType,
  123. paySign,
  124. success(res) {
  125. wx.showToast({
  126. title: "支付成功",
  127. duration: 2500
  128. })
  129. setTimeout(() => {
  130. that.setUserInfo()
  131. }, 1500)
  132. },
  133. fail(res) {
  134. wx.showToast({
  135. title: "支付失败",
  136. icon: "none"
  137. })
  138. }
  139. })
  140. },
  141. //底部弹出框
  142. showModal: function () {
  143. // 背景遮罩层
  144. var animation = wx.createAnimation({
  145. duration: 200,
  146. timingFunction: "linear",
  147. delay: 0
  148. })
  149. animation.translateY(300).step()
  150. this.setData({
  151. animationData: animation.export(),
  152. showModalStatus: true
  153. })
  154. setTimeout(function () {
  155. animation.translateY(0).step()
  156. this.setData({
  157. animationData: animation.export()
  158. })
  159. }.bind(this), 200)
  160. },
  161. //点击背景面任意一处时,弹出框隐藏
  162. hideModal: function () {
  163. //弹出框消失动画
  164. var animation = wx.createAnimation({
  165. duration: 200,
  166. timingFunction: "linear",
  167. delay: 0
  168. })
  169. animation.translateY(300).step()
  170. this.setData({
  171. animationData: animation.export(),
  172. })
  173. setTimeout(function () {
  174. animation.translateY(0).step()
  175. this.setData({
  176. animationData: animation.export(),
  177. showModalStatus: false
  178. })
  179. }.bind(this), 200)
  180. },
  181. }
  182. })