index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. /**
  31. * 组件的方法列表
  32. */
  33. methods: {
  34. open(data) {
  35. this.getProducts()
  36. let {
  37. type,
  38. id,
  39. voucherType,
  40. preferential
  41. } = data
  42. this.setData({
  43. state: true,
  44. type,
  45. id,
  46. voucherType,
  47. preferential
  48. })
  49. },
  50. close() {
  51. this.setData({
  52. state: false
  53. })
  54. this.triggerEvent('reload')
  55. },
  56. async getProducts() {
  57. let products = await getProducts()
  58. let productVip = products.find(item => {
  59. return item.type == 1
  60. })
  61. let productNum = products.find(item => {
  62. return item.type == 2
  63. })
  64. this.setData({
  65. productVip,
  66. productNum
  67. })
  68. },
  69. async getVoucher() {
  70. console.log('zz', this.data.voucherType);
  71. if (!this.data.voucherType) {
  72. let res = await getVoucher({
  73. id: this.data.id
  74. })
  75. this.toBuy(res.type == 1 ? '1001' : '1010')
  76. } else {
  77. this.toBuy(this.data.voucherType == 1 ? '1001' : '1010')
  78. }
  79. },
  80. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  81. async toBuy(productId) {
  82. wx.showLoading({
  83. title: '提交中',
  84. mask: true
  85. })
  86. let res = ''
  87. if (productId == '1001') {
  88. res = await buyVip({
  89. productId
  90. }).finally(() => {
  91. wx.hideLoading()
  92. })
  93. } else if (productId == '1010') {
  94. res = await buyNum({
  95. productId
  96. }).finally(() => {
  97. wx.hideLoading()
  98. })
  99. } else {
  100. wx.hideLoading()
  101. wx.showToast({
  102. title: "支付失败,请重试",
  103. icon: "none"
  104. })
  105. }
  106. let {
  107. timeStamp,
  108. nonceStr,
  109. signType,
  110. paySign
  111. } = res
  112. // package保留字
  113. wx.requestPayment({
  114. timeStamp,
  115. nonceStr,
  116. package: res.package,
  117. signType,
  118. paySign,
  119. success: (res) => {
  120. wx.showToast({
  121. title: "支付成功",
  122. duration: 2500
  123. })
  124. this.close()
  125. },
  126. fail(res) {
  127. wx.showToast({
  128. title: "支付失败",
  129. icon: "none"
  130. })
  131. }
  132. })
  133. },
  134. }
  135. })