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. 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. },
  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. await getVoucher({
  73. id: this.data.id
  74. })
  75. }
  76. this.toBuy(this.data.voucherType == 1 ? '1001' : '1010')
  77. },
  78. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  79. async toBuy(productId) {
  80. console.log(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. console.log(res);
  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. },
  125. fail(res) {
  126. wx.showToast({
  127. title: "支付失败",
  128. icon: "none"
  129. })
  130. }
  131. })
  132. },
  133. }
  134. })