index.js 4.4 KB

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