goodsDetail.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. bindCallBack() {
  77. this.fetchProductDetail()
  78. },
  79. async fetchProductDetail() {
  80. try {
  81. const { status, data, msg } = await getProductDetail(this.data.searchForm.product_id)
  82. if (status) {
  83. const { shop_id, product_rotation_img_list, product_detail_img_list, collect_status } = data
  84. this.setData({
  85. objProductDetail: {
  86. ...data,
  87. product_rotation_img_list: product_rotation_img_list ? JSON.parse(product_rotation_img_list) : [],
  88. product_detail_img_list: product_detail_img_list ? JSON.parse(product_detail_img_list) : []
  89. },
  90. 'searchForm.shop_id': shop_id,
  91. 'searchForm.type': collect_status === 0 ? 1 : 2 // 是否收藏(0否1是)
  92. }, () => {
  93. this.fetchShopDetail()
  94. })
  95. } else {
  96. wx.showToast({
  97. title: msg,
  98. icon: 'none'
  99. })
  100. }
  101. } catch (err) {}
  102. },
  103. async fetchShopDetail() {
  104. try {
  105. const { status, data, msg } = await getShopDetail(this.data.searchForm.shop_id)
  106. if (status) {
  107. this.setData({
  108. shopDetail: {
  109. ...data,
  110. shop_address: data.shop_address ? JSON.parse(data.shop_address) : {}
  111. }
  112. })
  113. } else {
  114. wx.showToast({
  115. title: msg,
  116. icon: 'none'
  117. })
  118. }
  119. } catch (err) {}
  120. },
  121. async productCollect() {
  122. const { type, product_id } = this.data.searchForm
  123. this.setData({
  124. booLock: true
  125. })
  126. try {
  127. const { status, msg } = await postProductCollect(product_id, type)
  128. if (status) {
  129. this.setData({
  130. 'searchForm.type': type === 1 ? 2 : 1
  131. })
  132. } else {
  133. wx.showToast({
  134. title: msg,
  135. icon: 'none'
  136. })
  137. }
  138. } catch (err) {}
  139. this.setData({
  140. booLock: false
  141. })
  142. },
  143. handleNav(e) {
  144. const { value } = e.detail
  145. this.setData({
  146. active: value
  147. })
  148. },
  149. handlePreviewImage(e) {
  150. const { imgs, index } = e.currentTarget.dataset
  151. if (Array.isArray(imgs)) {
  152. wx.previewImage({
  153. urls: imgs,
  154. current: imgs[index]
  155. })
  156. }
  157. }
  158. })