goodsDetail.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. const { getShopDetail } = require('../../api/common')
  2. const { getProductDetail, postProductCollect } = require('./api/index')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. searchForm: {
  9. product_id: '',
  10. type: -1
  11. },
  12. nav: [
  13. {
  14. name: '描述',
  15. value: '1'
  16. },
  17. {
  18. name: '供应商',
  19. value: '2'
  20. }
  21. ],
  22. active: '1',
  23. objProductDetail: {
  24. product_rotation_img_list: [],
  25. product_detail_img_list: []
  26. },
  27. shopDetail: { shop_address: {} },
  28. booLock: false
  29. },
  30. /**
  31. * 生命周期函数--监听页面加载
  32. */
  33. onLoad(options) {
  34. const { product_id } = options
  35. this.setData({
  36. 'searchForm.product_id': product_id
  37. }, () => {
  38. this.fetchProductDetail()
  39. })
  40. },
  41. /**
  42. * 生命周期函数--监听页面初次渲染完成
  43. */
  44. onReady() {
  45. },
  46. /**
  47. * 生命周期函数--监听页面显示
  48. */
  49. onShow() {
  50. },
  51. /**
  52. * 生命周期函数--监听页面隐藏
  53. */
  54. onHide() {
  55. },
  56. /**
  57. * 生命周期函数--监听页面卸载
  58. */
  59. onUnload() {
  60. },
  61. /**
  62. * 页面相关事件处理函数--监听用户下拉动作
  63. */
  64. onPullDownRefresh() {
  65. },
  66. /**
  67. * 页面上拉触底事件的处理函数
  68. */
  69. onReachBottom() {
  70. },
  71. /**
  72. * 用户点击右上角分享
  73. */
  74. onShareAppMessage() {
  75. },
  76. async fetchProductDetail() {
  77. try {
  78. const { status, data, msg } = await getProductDetail(this.data.searchForm.product_id)
  79. if (status) {
  80. const { shop_id, product_rotation_img_list, product_detail_img_list, collect_status } = data
  81. this.setData({
  82. objProductDetail: {
  83. ...data,
  84. product_rotation_img_list: product_rotation_img_list ? JSON.parse(product_rotation_img_list) : [],
  85. product_detail_img_list: product_detail_img_list ? JSON.parse(product_detail_img_list) : []
  86. },
  87. 'searchForm.shop_id': shop_id,
  88. 'searchForm.type': collect_status === 0 ? 1 : 2 // 是否收藏(0否1是)
  89. }, () => {
  90. this.fetchShopDetail()
  91. })
  92. } else {
  93. wx.showToast({
  94. title: msg,
  95. icon: 'none'
  96. })
  97. }
  98. } catch (err) {}
  99. },
  100. async fetchShopDetail() {
  101. try {
  102. const { status, data, msg } = await getShopDetail(this.data.searchForm.shop_id)
  103. if (status) {
  104. this.setData({
  105. shopDetail: {
  106. ...data,
  107. shop_address: data.shop_address ? JSON.parse(data.shop_address) : {}
  108. }
  109. })
  110. } else {
  111. wx.showToast({
  112. title: msg,
  113. icon: 'none'
  114. })
  115. }
  116. } catch (err) {}
  117. },
  118. async productCollect() {
  119. const { type, product_id } = this.data.searchForm
  120. this.setData({
  121. booLock: true
  122. })
  123. try {
  124. const { status, msg } = await postProductCollect(product_id, type)
  125. if (status) {
  126. this.setData({
  127. 'searchForm.type': type === 1 ? 2 : 1
  128. })
  129. } else {
  130. wx.showToast({
  131. title: msg,
  132. icon: 'none'
  133. })
  134. }
  135. } catch (err) {}
  136. this.setData({
  137. booLock: false
  138. })
  139. },
  140. handleNav(e) {
  141. const { value } = e.detail
  142. this.setData({
  143. active: value
  144. })
  145. },
  146. handlePreviewImage(e) {
  147. const { imgs, index } = e.currentTarget.dataset
  148. if (Array.isArray(imgs)) {
  149. wx.previewImage({
  150. urls: imgs,
  151. current: imgs[index]
  152. })
  153. }
  154. }
  155. })