index.js 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // pages/commodity/index.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. activationModal: false,
  8. checkedId: '1'
  9. },
  10. onLoad(options) {
  11. },
  12. onShow() {
  13. },
  14. checked({
  15. currentTarget
  16. }) {
  17. console.log(currentTarget);
  18. this.setData({
  19. checkedId: currentTarget.dataset.id
  20. })
  21. },
  22. activation() {
  23. this.setData({
  24. activationModal: true
  25. })
  26. },
  27. closeModal() {
  28. this.setData({
  29. activationModal: false
  30. })
  31. },
  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. duration: 3000
  59. })
  60. }
  61. let {
  62. timeStamp,
  63. nonceStr,
  64. signType,
  65. paySign
  66. } = res
  67. // package保留字
  68. wx.requestPayment({
  69. timeStamp,
  70. nonceStr,
  71. package: res.package,
  72. signType,
  73. paySign,
  74. success(res) {
  75. wx.showToast({
  76. title: "支付成功",
  77. duration: 2500
  78. })
  79. setTimeout(() => {
  80. this.setUserInfo()
  81. }, 1500)
  82. },
  83. fail(res) {
  84. wx.showToast({
  85. title: "支付失败",
  86. icon: "none",
  87. duration: 3000
  88. })
  89. }
  90. })
  91. },
  92. })