index.js 4.6 KB

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