buy.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. })
  43. }
  44. })
  45. this.setData({
  46. goodsList: response
  47. })
  48. }).catch(e => {
  49. wx.hideLoading()
  50. wx.showModal({
  51. content: e,
  52. confirmColor: '#333',
  53. showCancel: false
  54. })
  55. })
  56. },
  57. handlePayType (e) {
  58. var index = Number(e.currentTarget.dataset.index)
  59. var goodsInfo = e.currentTarget.dataset.goodsinfo
  60. console.log(goodsInfo, 'goodsInfo');
  61. this.setData({
  62. activeIndex: index,
  63. goodsId: goodsInfo.goodsId,
  64. orderAmount: goodsInfo.salePrice,
  65. num: goodsInfo.donateNumber + goodsInfo.useNumber
  66. })
  67. },
  68. // 调用微信支付
  69. handleWXPay () {
  70. var data = {
  71. goodsId: this.data.goodsId,
  72. orderAmount: this.data.orderAmount,
  73. num: this.data.num
  74. }
  75. wx.showLoading({
  76. title: '加载中...',
  77. mask: true
  78. })
  79. submitOrder(data).then(res => {
  80. wx.hideLoading()
  81. // 拉起微信支付
  82. wx.requestPayment({
  83. timeStamp: res.data.timeStamp,
  84. nonceStr: res.data.nonceStr,
  85. package: res.data.packageStr,
  86. signType: res.data.signType,
  87. paySign: res.data.paySign,
  88. success (res) {
  89. wx.showToast({
  90. title: '支付成功',
  91. icon: 'success'
  92. })
  93. },
  94. fail (res) {
  95. console.log('weixin pay fail', res);
  96. }
  97. })
  98. }).catch(e => {
  99. wx.hideLoading()
  100. wx.showModal({
  101. content: e,
  102. confirmColor: '#333',
  103. showCancel: false
  104. })
  105. })
  106. },
  107. handleAppointment () {
  108. wx.navigateTo({
  109. url: '/pages/appointment/appointment',
  110. })
  111. }
  112. })