index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import {
  2. getProducts,
  3. getTasks,
  4. submitTask,
  5. } from '~/api/global'
  6. import {
  7. buyVip,
  8. buyNum,
  9. getMyInfo,
  10. getVipInfo,
  11. exchangePhone,
  12. bindPhone
  13. } from '~/api/user'
  14. const app = getApp()
  15. Page({
  16. data: {
  17. userInfo: {},
  18. vipTime: '',
  19. tasks: [],
  20. isIos: app.globalData.isIOS,
  21. productNum: {},
  22. productVip: {}
  23. },
  24. onLoad() {
  25. this.getProducts()
  26. },
  27. async onShow() {
  28. if (typeof this.getTabBar === 'function') {
  29. this.getTabBar().setData({
  30. selected: 3
  31. })
  32. }
  33. let uid = wx.getStorageSync('uid') || ''
  34. if (!uid) {
  35. getApp().callBack = (res) => {
  36. this.setUserInfo()
  37. }
  38. } else {
  39. this.setUserInfo()
  40. }
  41. },
  42. // 设置用户信息及vip状态和任务完成情况
  43. async setUserInfo() {
  44. let userInfo = await getMyInfo()
  45. let vipTime = await getVipInfo()
  46. this.getTasks()
  47. this.setData({
  48. userInfo,
  49. vipTime,
  50. })
  51. },
  52. async getTasks() {
  53. let tasks = await getTasks()
  54. this.setData({
  55. tasks
  56. })
  57. },
  58. async getProducts() {
  59. let products = await getProducts()
  60. let productVip = products.find(item => {
  61. return item.type == 1
  62. })
  63. let productNum = products.find(item => {
  64. return item.type == 2
  65. })
  66. this.setData({
  67. productNum,
  68. productVip
  69. })
  70. },
  71. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  72. async toBuy({
  73. currentTarget
  74. }) {
  75. let productId = currentTarget.dataset.type
  76. wx.showLoading({
  77. title: '提交中',
  78. mask: true
  79. })
  80. let res = ''
  81. if (productId == '1001') {
  82. res = await buyVip({
  83. productId
  84. }).finally(() => {
  85. wx.hideLoading()
  86. })
  87. } else if (productId == '1010') {
  88. res = await buyNum({
  89. productId
  90. }).finally(() => {
  91. wx.hideLoading()
  92. })
  93. } else {
  94. wx.hideLoading()
  95. wx.showToast({
  96. title: "支付失败,请重试",
  97. icon: "none",
  98. duration: 3000
  99. })
  100. }
  101. let {
  102. timeStamp,
  103. nonceStr,
  104. signType,
  105. paySign
  106. } = res
  107. // package保留字
  108. wx.requestPayment({
  109. timeStamp,
  110. nonceStr,
  111. package: res.package,
  112. signType,
  113. paySign,
  114. success(res) {
  115. wx.showToast({
  116. title: "支付成功",
  117. duration: 2500
  118. })
  119. setTimeout(() => {
  120. this.setUserInfo()
  121. }, 1500)
  122. },
  123. fail(res) {
  124. wx.showToast({
  125. title: "支付失败",
  126. icon: "none",
  127. duration: 3000
  128. })
  129. }
  130. })
  131. },
  132. // 提交任务
  133. async submitTask({
  134. currentTarget
  135. }) {
  136. let id = currentTarget.dataset.type
  137. await submitTask({
  138. id
  139. })
  140. wx.showToast({
  141. title: id == '1' ? '签到成功!' : id == 3 ? "观看成功!" : "",
  142. icon: "none",
  143. duration: 2000
  144. })
  145. this.setUserInfo()
  146. },
  147. async getPhoneNumber({
  148. detail
  149. }) {
  150. let mobile = await exchangePhone({
  151. code: detail.code
  152. })
  153. await bindPhone({
  154. mobile
  155. })
  156. this.setUserInfo()
  157. wx.showToast({
  158. title: '绑定成功!已获得7天VIP',
  159. icon: "none",
  160. duration: 2000
  161. })
  162. },
  163. jump({
  164. currentTarget
  165. }) {
  166. let url = currentTarget.dataset.url
  167. wx.navigateTo({
  168. url: url
  169. });
  170. },
  171. // 调起广告
  172. rewardedVideo() {
  173. if (this.data.tasks.length != 3 || this.data.tasks[2].completed) {
  174. return
  175. }
  176. this.selectComponent('#advert').rewardedVideo();
  177. },
  178. clipboar() {
  179. wx.setClipboardData({
  180. data: this.data.userInfo.user.eid,
  181. success: function (res) { //成功回调函数
  182. wx.showToast({
  183. title: '已复制',
  184. icon: "none"
  185. })
  186. }
  187. })
  188. },
  189. // 分享配置
  190. onShareAppMessage: function (res) {
  191. const user = wx.getStorageSync('uid');
  192. return {
  193. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  194. path: `/pages/index/index?uid=${user}`,
  195. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  196. }
  197. },
  198. onShareTimeline: function () {
  199. return {
  200. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  201. query: `uid=${wx.getStorageSync('uid')}`,
  202. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  203. }
  204. },
  205. })