index.js 5.2 KB

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