index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. async getProducts() {
  17. let products = await getProducts()
  18. console.log(products);
  19. this.setData({
  20. products,
  21. selected: products[0]
  22. })
  23. },
  24. checked({
  25. currentTarget
  26. }) {
  27. this.setData({
  28. selected: currentTarget.dataset.product
  29. })
  30. },
  31. closeModal() {
  32. this.setData({
  33. activationModal: false
  34. })
  35. wx.navigateBack()
  36. },
  37. async toBuy() {
  38. wx.showLoading({
  39. title: '提交中',
  40. mask: true
  41. })
  42. let res = await buyVip({
  43. productId: this.data.selected.id
  44. }).finally(() => {
  45. wx.hideLoading()
  46. })
  47. let {
  48. timeStamp,
  49. nonceStr,
  50. signType,
  51. paySign
  52. } = res
  53. // package保留字
  54. wx.requestPayment({
  55. timeStamp,
  56. nonceStr,
  57. package: res.package,
  58. signType,
  59. paySign,
  60. success: (res) => {
  61. console.log(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. /* // 设置用户信息及vip状态
  79. async setUserInfo() {
  80. let userInfo = await getMyInfo()
  81. let vipTime = await getVipInfo()
  82. this.setUser(userInfo.user)
  83. this.setData({
  84. userInfo,
  85. vipTime,
  86. })
  87. }, */
  88. })