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