index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. this.setData({
  69. productVip,
  70. productNum
  71. })
  72. },
  73. async getVoucher() {
  74. console.log('zz');
  75. if (!this.data.voucherType) {
  76. await getVoucher({
  77. id: this.data.id
  78. })
  79. }
  80. this.toBuy()
  81. },
  82. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  83. async toBuy(productId) {
  84. wx.showLoading({
  85. title: '提交中',
  86. mask: true
  87. })
  88. let res = ''
  89. if (productId == '1001') {
  90. res = await buyVip({
  91. productId
  92. }).finally(() => {
  93. wx.hideLoading()
  94. })
  95. } else if (productId == '1010') {
  96. res = await buyNum({
  97. productId
  98. }).finally(() => {
  99. wx.hideLoading()
  100. })
  101. } else {
  102. wx.hideLoading()
  103. wx.showToast({
  104. title: "支付失败,请重试",
  105. icon: "none"
  106. })
  107. }
  108. let {
  109. timeStamp,
  110. nonceStr,
  111. signType,
  112. paySign
  113. } = res
  114. // package保留字
  115. wx.requestPayment({
  116. timeStamp,
  117. nonceStr,
  118. package: res.package,
  119. signType,
  120. paySign,
  121. success(res) {
  122. wx.showToast({
  123. title: "支付成功",
  124. duration: 2500
  125. })
  126. },
  127. fail(res) {
  128. wx.showToast({
  129. title: "支付失败",
  130. icon: "none"
  131. })
  132. }
  133. })
  134. },
  135. }
  136. })