index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. console.log(userInfo);
  70. this.getTasks()
  71. this.setData({
  72. userInfo,
  73. vipTime,
  74. })
  75. },
  76. // 调起广告
  77. rewardedVideo() {
  78. if (this.data.tasks.length != 3 || this.data.tasks[2].completed) {
  79. return
  80. }
  81. this.selectComponent('#advert').rewardedVideo();
  82. },
  83. async getTasks() {
  84. let tasks = await getTasks()
  85. this.setData({
  86. tasks
  87. })
  88. },
  89. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  90. async toBuy({
  91. currentTarget
  92. }) {
  93. let that = this
  94. let productId = currentTarget.dataset.type
  95. wx.showLoading({
  96. title: '提交中',
  97. mask: true
  98. })
  99. let res = ''
  100. if (productId == '1001') {
  101. res = await buyVip({
  102. productId
  103. }).finally(() => {
  104. wx.hideLoading()
  105. })
  106. } else if (productId == '1010') {
  107. res = await buyNum({
  108. productId
  109. }).finally(() => {
  110. wx.hideLoading()
  111. })
  112. } else {
  113. wx.hideLoading()
  114. wx.showToast({
  115. title: "支付失败,请重试",
  116. icon: "none"
  117. })
  118. }
  119. let {
  120. timeStamp,
  121. nonceStr,
  122. signType,
  123. paySign
  124. } = res
  125. // package保留字
  126. wx.requestPayment({
  127. timeStamp,
  128. nonceStr,
  129. package: res.package,
  130. signType,
  131. paySign,
  132. success(res) {
  133. wx.showToast({
  134. title: "支付成功",
  135. duration: 2500
  136. })
  137. setTimeout(() => {
  138. that.setUserInfo()
  139. }, 1500)
  140. },
  141. fail(res) {
  142. wx.showToast({
  143. title: "支付失败",
  144. icon: "none"
  145. })
  146. }
  147. })
  148. },
  149. //底部弹出框
  150. showModal: function() {
  151. // 背景遮罩层
  152. var animation = wx.createAnimation({
  153. duration: 200,
  154. timingFunction: "linear",
  155. delay: 0
  156. })
  157. animation.translateY(300).step()
  158. this.setData({
  159. animationData: animation.export(),
  160. showModalStatus: true
  161. })
  162. setTimeout(function() {
  163. animation.translateY(0).step()
  164. this.setData({
  165. animationData: animation.export()
  166. })
  167. }.bind(this), 200)
  168. },
  169. //点击背景面任意一处时,弹出框隐藏
  170. hideModal: function() {
  171. //弹出框消失动画
  172. var animation = wx.createAnimation({
  173. duration: 200,
  174. timingFunction: "linear",
  175. delay: 0
  176. })
  177. animation.translateY(300).step()
  178. this.setData({
  179. animationData: animation.export(),
  180. })
  181. setTimeout(function() {
  182. animation.translateY(0).step()
  183. this.setData({
  184. animationData: animation.export(),
  185. showModalStatus: false
  186. })
  187. }.bind(this), 200)
  188. },
  189. }
  190. })