index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import {
  2. buyVip,
  3. buyNum,
  4. } from '~/api/user'
  5. import {
  6. getProducts,
  7. getVoucher
  8. } from '~/api/global'
  9. Component({
  10. /**
  11. * 组件的属性列表
  12. */
  13. properties: {
  14. },
  15. /**
  16. * 组件的初始数据
  17. */
  18. data: {
  19. state: false,
  20. // 红包类型
  21. type: '',
  22. // 优惠金额
  23. preferential: '',
  24. productVip: 0,
  25. productNum: 0,
  26. // 红包id
  27. id: '',
  28. voucherType: ''
  29. },
  30. pageLifetimes: {
  31. show() {
  32. this.getProducts()
  33. },
  34. },
  35. /**
  36. * 组件的方法列表
  37. */
  38. methods: {
  39. open(data) {
  40. let {
  41. type,
  42. id,
  43. voucherType,
  44. preferential
  45. } = data
  46. console.log(data);
  47. this.setData({
  48. state: true,
  49. type,
  50. id,
  51. voucherType,
  52. preferential
  53. })
  54. },
  55. close() {
  56. this.setData({
  57. state: false
  58. })
  59. },
  60. async getProducts() {
  61. let products = await getProducts()
  62. let productVip = products.find(item => {
  63. return item.type == 1
  64. })
  65. let productNum = products.find(item => {
  66. return item.type == 2
  67. })
  68. console.log(productVip,
  69. productNum);
  70. this.setData({
  71. productVip,
  72. productNum
  73. })
  74. },
  75. async getVoucher() {
  76. console.log('zz');
  77. if (!this.data.voucherType) {
  78. await getVoucher({
  79. id: this.data.id
  80. })
  81. }
  82. this.toBuy()
  83. },
  84. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  85. async toBuy(productId) {
  86. wx.showLoading({
  87. title: '提交中',
  88. mask: true
  89. })
  90. let res = ''
  91. if (productId == '1001') {
  92. res = await buyVip({
  93. productId
  94. }).finally(() => {
  95. wx.hideLoading()
  96. })
  97. } else if (productId == '1010') {
  98. res = await buyNum({
  99. productId
  100. }).finally(() => {
  101. wx.hideLoading()
  102. })
  103. } else {
  104. wx.hideLoading()
  105. wx.showToast({
  106. title: "支付失败,请重试",
  107. icon: "none"
  108. })
  109. }
  110. let {
  111. timeStamp,
  112. nonceStr,
  113. signType,
  114. paySign
  115. } = res
  116. // package保留字
  117. wx.requestPayment({
  118. timeStamp,
  119. nonceStr,
  120. package: res.package,
  121. signType,
  122. paySign,
  123. success(res) {
  124. wx.showToast({
  125. title: "支付成功",
  126. duration: 2500
  127. })
  128. },
  129. fail(res) {
  130. wx.showToast({
  131. title: "支付失败",
  132. icon: "none"
  133. })
  134. }
  135. })
  136. },
  137. }
  138. })