index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import {
  2. buyVip,
  3. buyNum,
  4. } from '~/api/user'
  5. Component({
  6. /**
  7. * 组件的属性列表
  8. */
  9. properties: {
  10. },
  11. /**
  12. * 组件的初始数据
  13. */
  14. data: {
  15. state: false
  16. },
  17. /**
  18. * 组件的方法列表
  19. */
  20. methods: {
  21. open() {
  22. this.setData({
  23. state: true
  24. })
  25. },
  26. close() {
  27. this.setData({
  28. state: false
  29. })
  30. },
  31. //购买vip和购买次数不是一个接口 type 1001是vip,1010是次数
  32. async toBuy({
  33. currentTarget
  34. }) {
  35. let productId = currentTarget.dataset.type
  36. wx.showLoading({
  37. title: '提交中',
  38. mask: true
  39. })
  40. let res = ''
  41. if (productId == '1001') {
  42. res = await buyVip({
  43. productId
  44. }).finally(() => {
  45. wx.hideLoading()
  46. })
  47. } else if (productId == '1010') {
  48. res = await buyNum({
  49. productId
  50. }).finally(() => {
  51. wx.hideLoading()
  52. })
  53. } else {
  54. wx.hideLoading()
  55. wx.showToast({
  56. title: "支付失败,请重试",
  57. icon: "none"
  58. })
  59. }
  60. let {
  61. timeStamp,
  62. nonceStr,
  63. signType,
  64. paySign
  65. } = res
  66. // package保留字
  67. wx.requestPayment({
  68. timeStamp,
  69. nonceStr,
  70. package: res.package,
  71. signType,
  72. paySign,
  73. success(res) {
  74. wx.showToast({
  75. title: "支付成功",
  76. duration: 2500
  77. })
  78. },
  79. fail(res) {
  80. wx.showToast({
  81. title: "支付失败",
  82. icon: "none"
  83. })
  84. }
  85. })
  86. },
  87. }
  88. })