product.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // import APIClient from '../../../utils/APIClient.js'
  2. import httpRequestApi from '../../utils/APIClient';
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. name: '朗读小咖秀',
  9. price: 9
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad: function (options) {
  15. console.log('产品页', options)
  16. if (options.price) {
  17. this.setData({
  18. price: options.price
  19. })
  20. }
  21. },
  22. /**
  23. * 生命周期函数--监听页面初次渲染完成
  24. */
  25. onReady: function () {
  26. },
  27. buy() {
  28. this.messageAuth();
  29. },
  30. // 授权
  31. messageAuth() {
  32. if (!wx.getStorageSync('message')) {
  33. wx.requestSubscribeMessage({
  34. tmplIds: ['KJ0YtcAacJNSXDBsE27JXqoaFrcJ1-N6Jcu85yTtQuY',
  35. '-2ZZpWFoyKvAtX1HwEIQLQ92LnN8cryamB94LqLGo98'
  36. ],
  37. success: (res) => {
  38. console.log(res)
  39. if (res['KJ0YtcAacJNSXDBsE27JXqoaFrcJ1-N6Jcu85yTtQuY'] === 'reject') {
  40. console.log('用户不同意订阅')
  41. wx.setStorageSync('message', false)
  42. } else if (res['KJ0YtcAacJNSXDBsE27JXqoaFrcJ1-N6Jcu85yTtQuY'] === 'accept') {
  43. console.log('订阅成功')
  44. wx.setStorageSync('message', true)
  45. }
  46. this.order();
  47. },
  48. fail: () => {
  49. this.order();
  50. }
  51. })
  52. } else {
  53. this.order();
  54. }
  55. },
  56. // 支付
  57. order() {
  58. // 渠道id
  59. const productId = wx.getStorageSync('productId');
  60. httpRequestApi.createOrder(productId).success(res => {
  61. console.log(res.data.data.package);
  62. const timeStamp = res.data.data.timeStamp;
  63. const nonceStr = res.data.data.nonceStr;
  64. const packages = res.data.data.package;
  65. const paySign = res.data.data.sign;
  66. wx.requestPayment({
  67. timeStamp,
  68. nonceStr,
  69. package: packages,
  70. signType: 'MD5',
  71. paySign,
  72. success: (res) => {
  73. if (this.data.iphoneType === 'iphone') {
  74. this.setData({
  75. tipsType: 'tips',
  76. tipsImg: 'http://reader-wx.ai160.com/images/reader/pay/success.png'
  77. })
  78. }
  79. httpRequestApi.getAuthActivity().success(res => {
  80. if (!res.data.data) {
  81. wx.setStorageSync('vip', false)
  82. wx.setStorageSync('date', '')
  83. } else {
  84. wx.setStorageSync('vip', true)
  85. wx.setStorageSync('date', res.data.data)
  86. // this.setData({
  87. // ['myData.isVIP']: true,
  88. // ['myData.date']: res.data.data
  89. // })
  90. const pages = getCurrentPages();
  91. const prevPage = pages[pages.length - 2];
  92. prevPage.setData({
  93. ['myData.isVIP']: true,
  94. ['myData.date']: res.data.data
  95. }, () => {
  96. wx.navigateBack({
  97. delta: 1
  98. })
  99. })
  100. // this.showGift();
  101. }
  102. }).fail(error => {
  103. console.log(error)
  104. })
  105. },
  106. fail(res) {}
  107. })
  108. })
  109. },
  110. // // 购买成功告诉首页显示邀请有礼
  111. // showGift() {
  112. // console.log('通知')
  113. // this.setData({
  114. // isVip: true
  115. // })
  116. // },
  117. /**
  118. * 生命周期函数--监听页面显示
  119. */
  120. onShow: function () {
  121. },
  122. })