拓恒河湖长制全民护河平台WEB端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.8KB

  1. // package_A/pages/goods-detail/index.js
  2. import {storeInfo, exchange} from '../../../api/goods.js'
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. // 商品详情信息
  9. detail: {},
  10. // 轮播图列表
  11. coverList: [],
  12. // 详情图片列表
  13. detailImages: [],
  14. // 商铺信息
  15. storeDetail: {},
  16. // 商品数量
  17. numbers: 1
  18. },
  19. /**
  20. * 生命周期函数--监听页面加载
  21. */
  22. onLoad(options) {
  23. if(options.data) {
  24. let detail = this.data.detail
  25. let coverList = []
  26. let detailImages = []
  27. detail = JSON.parse(decodeURIComponent(options.data))
  28. this.getStoreInfo({merchantId: detail.merchantId})
  29. if(detail.goodsImage) {
  30. coverList = detail.goodsImage.split(',')
  31. }
  32. if(detail.goodsDetailImage) {
  33. detailImages = detail.goodsDetailImage.split(',')
  34. }
  35. this.setData({detail, coverList, detailImages})
  36. }
  37. },
  38. // 获取商铺信息
  39. async getStoreInfo(params) {
  40. const res = await storeInfo(params)
  41. if(res.code === 0) {
  42. let storeDetail = this.data.storeDetail
  43. storeDetail = res.data
  44. this.setData({
  45. storeDetail
  46. })
  47. }
  48. },
  49. /**
  50. * 生命周期函数--监听页面显示
  51. */
  52. onShow() {
  53. },
  54. // 加数量
  55. addNum() {
  56. let numbers = this.data.numbers
  57. this.setData({
  58. numbers: numbers + 1
  59. })
  60. },
  61. // 减数量
  62. reduceNum() {
  63. let numbers = this.data.numbers
  64. if(numbers !== 1) {
  65. this.setData({
  66. numbers: numbers - 1
  67. })
  68. } else {
  69. wx.showToast({
  70. icon: "none",
  71. title: '请至少选择一件商品!',
  72. })
  73. }
  74. },
  75. // 兑换
  76. exchangeGoods() {
  77. let params = {}
  78. params.goodsId = this.data.detail.id
  79. params.goodsCount = this.data.numbers
  80. params.openid = wx.getStorageSync('openid')
  81. exchange(params).then(res=> {
  82. if(res.code === 0) {
  83. // 成功跳转页面
  84. wx.navigateTo({
  85. url: '/package_A/pages/exchange-success/index?data=' + encodeURIComponent(JSON.stringify(this.data.storeDetail))
  86. })
  87. } else {
  88. wx.showToast({
  89. icon: 'error',
  90. title: res.msg,
  91. duration: 2000
  92. })
  93. }
  94. }).catch((e)=> {
  95. wx.showToast({
  96. icon: 'error',
  97. title: e.msg,
  98. duration: 2000
  99. })
  100. })
  101. }
  102. })