index.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. import {
  2. getMyInfo,
  3. androidbuyVip,
  4. buyVip,
  5. getVipInfo,
  6. getWxQrcode,
  7. getLearnCard
  8. } from '~/api/user'
  9. import event from '~/mixins/event'
  10. import {
  11. getProducts,
  12. userEvent
  13. } from '~/api/global'
  14. import {
  15. createStoreBindings
  16. } from 'mobx-miniprogram-bindings'
  17. import {
  18. store
  19. } from '~/store/index'
  20. const app = getApp()
  21. Page({
  22. behaviors: [event],
  23. data: {
  24. userInfo: {},
  25. vipTime: '',
  26. tasks: [],
  27. isIos: app.globalData.isIOS,
  28. qrCode: '',
  29. activationModal: false,
  30. activationRes: {},
  31. products: [],
  32. isPreferential: false
  33. },
  34. async onLoad() {
  35. // 手工绑定
  36. this.storeBindings = createStoreBindings(this, {
  37. store,
  38. actions: {
  39. setUser: 'setUser'
  40. }
  41. })
  42. if (this.data.isIos) {
  43. let qrCode = await getWxQrcode()
  44. this.setData({
  45. qrCode: qrCode.ticketUrl
  46. })
  47. }
  48. },
  49. async onShow() {
  50. if (typeof this.getTabBar === 'function') {
  51. this.getTabBar().setData({
  52. selected: 4
  53. })
  54. }
  55. let uid = wx.getStorageSync('uid') || ''
  56. if (!uid) {
  57. getApp().callBack = (res) => {
  58. this.setUserInfo()
  59. }
  60. } else {
  61. this.setUserInfo()
  62. }
  63. },
  64. // 设置用户信息及vip状态
  65. async setUserInfo() {
  66. let userInfo = await getMyInfo()
  67. let vipTime = await getVipInfo()
  68. this.setUser(userInfo.user)
  69. this.getProducts()
  70. this.setData({
  71. userInfo,
  72. vipTime,
  73. })
  74. },
  75. async getProducts() {
  76. let {
  77. productList: products,
  78. isPreferential
  79. } = await getProducts()
  80. console.log(products);
  81. this.setData({
  82. products,
  83. isPreferential
  84. })
  85. },
  86. activationCode() {
  87. wx.showModal({
  88. title: '请输入激活码',
  89. editable: true,
  90. success: async ({
  91. confirm,
  92. content
  93. }) => {
  94. if (confirm) {
  95. let regexp = /^[a-zA-Z0-9]{4}$/
  96. if (regexp.test(content)) {
  97. let activationRes = await getLearnCard({
  98. cardNo: content
  99. })
  100. if (activationRes.code == 200) {
  101. activationRes = {
  102. code: 200,
  103. message: '快去朗读挑战吧!'
  104. }
  105. }
  106. this.setUserInfo()
  107. if (typeof this.getTabBar === 'function') {
  108. this.getTabBar().setData({
  109. mask: true
  110. })
  111. }
  112. this.setData({
  113. activationModal: true,
  114. activationRes
  115. })
  116. } else {
  117. if (typeof this.getTabBar === 'function') {
  118. this.getTabBar().setData({
  119. mask: true
  120. })
  121. }
  122. this.setData({
  123. activationModal: true,
  124. activationRes: {
  125. code: 581,
  126. message: '请检查激活码输入是否正确'
  127. }
  128. })
  129. }
  130. }
  131. }
  132. })
  133. },
  134. toGzh() {
  135. this.selectComponent('#gzh').open()
  136. },
  137. async toBuy({
  138. currentTarget
  139. }) {
  140. if (currentTarget.dataset.isclick) {
  141. return
  142. }
  143. wx.showLoading({
  144. title: '提交中',
  145. mask: true
  146. })
  147. // #if MP
  148. userEvent({
  149. action: 'ANDROID_PAY_ACTIVITY',
  150. })
  151. let res = await buyVip({
  152. productId: currentTarget.dataset.id
  153. }).finally(() => {
  154. wx.hideLoading()
  155. })
  156. let {
  157. timeStamp,
  158. nonceStr,
  159. signType,
  160. paySign
  161. } = res
  162. // package保留字
  163. wx.requestPayment({
  164. timeStamp,
  165. nonceStr,
  166. package: res.package,
  167. signType,
  168. paySign,
  169. success: async (res) => {
  170. setTimeout(() => {
  171. this.setUserInfo()
  172. this.selectComponent('#vipModal').open()
  173. }, 1500)
  174. userEvent({
  175. action: 'ANDROID_PAY_SUCCESS',
  176. })
  177. },
  178. fail(res) {
  179. wx.showToast({
  180. title: "支付失败",
  181. icon: "none",
  182. duration: 3000
  183. })
  184. }
  185. })
  186. // #elif ANDROID
  187. let res = await androidbuyVip({
  188. productId: currentTarget.dataset.id
  189. }).finally(() => {
  190. wx.hideLoading()
  191. })
  192. wx.miniapp.requestPayment({
  193. timeStamp: res.timestamp,
  194. mchId: res.partnerid,
  195. prepayId: res.prepayid,
  196. package: res.package,
  197. nonceStr: res.noncestr,
  198. sign: res.sign,
  199. success: async (res) => {
  200. setTimeout(() => {
  201. this.setUserInfo()
  202. this.selectComponent('#vipModal').open()
  203. }, 1500)
  204. userEvent({
  205. action: 'ANDROID_PAY_SUCCESS',
  206. })
  207. },
  208. fail(res) {
  209. console.log(res);
  210. wx.showToast({
  211. title: "支付失败",
  212. icon: "none",
  213. duration: 3000
  214. })
  215. },
  216. complete: (res) => {
  217. console.error('wx.miniapp.requestPayment complete:', res)
  218. }
  219. })
  220. // #endif
  221. },
  222. jump({
  223. currentTarget
  224. }) {
  225. let url = currentTarget.dataset.url
  226. console.log(url);
  227. wx.navigateTo({
  228. url: url
  229. });
  230. },
  231. clipboar() {
  232. wx.setClipboardData({
  233. data: this.data.userInfo.user.eid,
  234. success: function (res) { //成功回调函数
  235. wx.showToast({
  236. title: '已复制',
  237. icon: "none"
  238. })
  239. }
  240. })
  241. },
  242. closeModal() {
  243. this.setData({
  244. activationModal: false
  245. })
  246. if (typeof this.getTabBar === 'function') {
  247. this.getTabBar().setData({
  248. mask: false
  249. })
  250. }
  251. },
  252. // 分享配置
  253. onShareAppMessage(res) {
  254. // #if MP
  255. return {
  256. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  257. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  258. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/375-300-1.jpg'
  259. }
  260. // #elif ANDROID
  261. return {
  262. title: '自从用了它,家里朗朗书声,美妙极了!你家孩子也快来试试!',
  263. userName: 'gh_50f61361ad1d',
  264. path: `/pages/index/index?uid=${wx.getStorageSync('uid')}`,
  265. imagePath: '/static/375-300-2.jpg',
  266. webpageUrl: 'http://www.efunbox.cn',
  267. withShareTicket: true,
  268. miniprogramType: 1,
  269. scene: 0,
  270. }
  271. // #endif
  272. },
  273. onShareTimeline: function () {
  274. return {
  275. title: '终于找到适合孩子的朗读神器了!动画配音,边玩边学!',
  276. query: `uid=${wx.getStorageSync('uid')}`,
  277. imageUrl: 'http://reader-wx.ai160.com/images/reader/v3/yuwen.jpg'
  278. }
  279. },
  280. })