index.js 4.1 KB

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