index.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. closeModal() {
  34. this.setData({
  35. activationModal: false
  36. })
  37. },
  38. async toBuy() {
  39. wx.showLoading({
  40. title: '提交中',
  41. mask: true
  42. })
  43. let res = await buyVip({
  44. productId: this.data.selected.id
  45. }).finally(() => {
  46. wx.hideLoading()
  47. })
  48. let {
  49. timeStamp,
  50. nonceStr,
  51. signType,
  52. paySign
  53. } = res
  54. // package保留字
  55. wx.requestPayment({
  56. timeStamp,
  57. nonceStr,
  58. package: res.package,
  59. signType,
  60. paySign,
  61. success: (res) => {
  62. this.setData({
  63. activationModal: true
  64. })
  65. setTimeout(() => {
  66. this.setUserInfo()
  67. }, 1500)
  68. },
  69. fail(res) {
  70. wx.showToast({
  71. title: "支付失败",
  72. icon: "none",
  73. duration: 3000
  74. })
  75. }
  76. })
  77. },
  78. })