index.js 2.6 KB

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