index.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // pages/commodity/index.js
  2. Page({
  3. /**
  4. * 页面的初始数据
  5. */
  6. data: {
  7. checkedId: '1'
  8. },
  9. onLoad(options) {
  10. },
  11. onShow() {
  12. },
  13. checked({
  14. currentTarget
  15. }) {
  16. console.log(currentTarget);
  17. this.setData({
  18. checkedId: currentTarget.dataset.id
  19. })
  20. },
  21. async toBuy({
  22. currentTarget
  23. }) {
  24. let productId = currentTarget.dataset.type
  25. wx.showLoading({
  26. title: '提交中',
  27. mask: true
  28. })
  29. let res = ''
  30. if (productId == '1001') {
  31. res = await buyVip({
  32. productId
  33. }).finally(() => {
  34. wx.hideLoading()
  35. })
  36. } else if (productId == '1010') {
  37. res = await buyNum({
  38. productId
  39. }).finally(() => {
  40. wx.hideLoading()
  41. })
  42. } else {
  43. wx.hideLoading()
  44. wx.showToast({
  45. title: "支付失败,请重试",
  46. icon: "none",
  47. duration: 3000
  48. })
  49. }
  50. let {
  51. timeStamp,
  52. nonceStr,
  53. signType,
  54. paySign
  55. } = res
  56. // package保留字
  57. wx.requestPayment({
  58. timeStamp,
  59. nonceStr,
  60. package: res.package,
  61. signType,
  62. paySign,
  63. success(res) {
  64. wx.showToast({
  65. title: "支付成功",
  66. duration: 2500
  67. })
  68. setTimeout(() => {
  69. this.setUserInfo()
  70. }, 1500)
  71. },
  72. fail(res) {
  73. wx.showToast({
  74. title: "支付失败",
  75. icon: "none",
  76. duration: 3000
  77. })
  78. }
  79. })
  80. },
  81. })