buy.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // pages/buy/buy.js
  2. import { goodsList } from '../../api/index'
  3. import { submitOrder } from '../../api/pay'
  4. import itt from '../../utils/util'
  5. const app = getApp()
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. activeIndex: -1,
  12. goodsList: [],
  13. goodsId: '',
  14. orderAmount: '',
  15. num: '',
  16. appusername: '',
  17. appuserheadimg: ''
  18. },
  19. /**
  20. * 生命周期函数--监听页面显示
  21. */
  22. onShow() {
  23. this.getGoodsList()
  24. this.setData({
  25. appusername: app.globalData.userInfo.userName.length > 4 ? app.globalData.userInfo.userName.substring(0,4) : app.globalData.userInfo.userName,
  26. appuserheadimg: app.globalData.userInfo.headImg
  27. })
  28. },
  29. //获取套餐列表
  30. getGoodsList () {
  31. wx.showLoading({
  32. title: '加载中...',
  33. mask: true
  34. })
  35. goodsList({}).then(res => {
  36. wx.hideLoading()
  37. var response = res.data
  38. response.map((item, index) => {
  39. if (item.isRecommend) {
  40. this.setData({
  41. activeIndex: index,
  42. goodsId: item.goodsId,
  43. orderAmount: item.salePrice,
  44. num: item.donateNumber + item.useNumber
  45. })
  46. }
  47. })
  48. this.setData({
  49. goodsList: response
  50. })
  51. }).catch(e => {
  52. wx.hideLoading()
  53. wx.showModal({
  54. content: e,
  55. confirmColor: '#333',
  56. showCancel: false
  57. })
  58. })
  59. },
  60. handlePayType (e) {
  61. var index = Number(e.currentTarget.dataset.index)
  62. var goodsInfo = e.currentTarget.dataset.goodsinfo
  63. console.log(goodsInfo, 'goodsInfo');
  64. this.setData({
  65. activeIndex: index,
  66. goodsId: goodsInfo.goodsId,
  67. orderAmount: goodsInfo.salePrice,
  68. num: goodsInfo.donateNumber + goodsInfo.useNumber
  69. })
  70. },
  71. // 调用微信支付
  72. handleWXPay () {
  73. var data = {
  74. goodsId: this.data.goodsId,
  75. orderAmount: this.data.orderAmount,
  76. num: this.data.num
  77. }
  78. wx.showLoading({
  79. title: '加载中...',
  80. mask: true
  81. })
  82. submitOrder(data).then(res => {
  83. wx.hideLoading()
  84. // 拉起微信支付
  85. wx.requestPayment({
  86. timeStamp: res.data.timeStamp,
  87. nonceStr: res.data.nonceStr,
  88. package: res.data.packageStr,
  89. signType: res.data.signType,
  90. paySign: res.data.paySign,
  91. success (res) {
  92. wx.showToast({
  93. title: '支付成功',
  94. icon: 'success'
  95. })
  96. },
  97. fail (res) {
  98. console.log('weixin pay fail', res);
  99. }
  100. })
  101. }).catch(e => {
  102. wx.hideLoading()
  103. wx.showModal({
  104. content: e,
  105. confirmColor: '#333',
  106. showCancel: false
  107. })
  108. })
  109. },
  110. handleAppointment () {
  111. wx.navigateTo({
  112. url: '/pages/appointment/appointment',
  113. })
  114. }
  115. })