index.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import {
  2. buyVip,
  3. } from '~/api/user'
  4. import {
  5. getProducts
  6. } from '~/api/global'
  7. Page({
  8. data: {
  9. products: [],
  10. selected: {},
  11. activationModal: false,
  12. },
  13. onLoad(options) {
  14. this.getProducts()
  15. },
  16. onShow() {
  17. },
  18. async getProducts() {
  19. let products = await getProducts()
  20. console.log(products);
  21. this.setData({
  22. products,
  23. selected: products[0]
  24. })
  25. },
  26. checked({
  27. currentTarget
  28. }) {
  29. this.setData({
  30. selected: currentTarget.dataset.product
  31. })
  32. },
  33. activation() {
  34. this.setData({
  35. activationModal: true
  36. })
  37. },
  38. closeModal() {
  39. this.setData({
  40. activationModal: false
  41. })
  42. },
  43. async toBuy() {
  44. wx.showLoading({
  45. title: '提交中',
  46. mask: true
  47. })
  48. let res = await buyVip({
  49. productId: this.data.selected.id
  50. }).finally(() => {
  51. wx.hideLoading()
  52. })
  53. console.log(res);
  54. let {
  55. timeStamp,
  56. nonceStr,
  57. signType,
  58. paySign
  59. } = res
  60. // package保留字
  61. wx.requestPayment({
  62. timeStamp,
  63. nonceStr,
  64. package: res.package,
  65. signType,
  66. paySign,
  67. success(res) {
  68. wx.showToast({
  69. title: "支付成功",
  70. duration: 2500
  71. })
  72. setTimeout(() => {
  73. this.setUserInfo()
  74. }, 1500)
  75. },
  76. fail(res) {
  77. wx.showToast({
  78. title: "支付失败",
  79. icon: "none",
  80. duration: 3000
  81. })
  82. }
  83. })
  84. },
  85. })