detail.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. let WxParse = require('../../lib/wxParse/wxParse')
  2. let util = require('../../utils/util')
  3. let api = require('../../config/api')
  4. Page({
  5. data: {
  6. id: 0, // 商品id
  7. productId: 0, // 规格id
  8. shareId: 0, // 分享id
  9. shareUrl: '', // 分享图片二维码
  10. show_spec: false, // 是否显示 商品规格弹出框
  11. swiper_index: 0, // 当前图片index
  12. goodsData: null,
  13. collectId: 0, // 收藏id
  14. },
  15. onLoad: function({ scene, obj }) {
  16. let id = 0
  17. let productId = 0
  18. let shareId = 0
  19. if (scene) { // 分享
  20. // 商品id,商品类型,商品活动id,分享用户id
  21. [id, shareId] = decodeURIComponent(scene).split(',')
  22. } else {
  23. const data = JSON.parse(obj)
  24. id = data.id
  25. productId = data.productId ? data.productId : 0
  26. }
  27. this.setData({ id, productId, shareId })
  28. if(wx.getStorageSync('token')) this.setBrowse(id)
  29. },
  30. onShow() {
  31. this.getGoodsDetail()
  32. },
  33. // 足迹
  34. setBrowse(goodsId) {
  35. util.showLoad('加载中')
  36. util.request(api.AddBrowse, { goodsId },'POST').then(res => {
  37. util.hideLoad()
  38. if (res.errno != 0) util.showErrorToast(res.errmsg)
  39. }).catch(() => {
  40. util.hideLoad()
  41. util.showErrorToast('网络连接失败')
  42. })
  43. },
  44. getGoodsDetail() {
  45. const { id, productId } = this.data
  46. util.showLoad('加载中')
  47. util.request(api.GoodsDetail, { id }).then(res => {
  48. util.hideLoad()
  49. if (res.errno === 0) {
  50. const { info, productList, collectId } = res.data
  51. const products = productList.map(p => {
  52. p.stock = p.number
  53. return p
  54. })
  55. WxParse.wxParse('goodsDetail', 'html', info.detail, this)
  56. const picUrl = info.picUrl.split(',')
  57. const pro_i = productId ? products.findIndex(p => p.id == productId) : 0
  58. const goodsData = {
  59. ...info,
  60. detail: undefined,
  61. products,
  62. curSpec: products[pro_i],
  63. picUrl
  64. }
  65. delete goodsData.detail
  66. this.setData({ goodsData, collectId: collectId || 0 })
  67. } else util.showErrorToast(res.errmsg)
  68. }).catch(() => {
  69. util.hideLoad()
  70. util.showErrorToast('网络连接失败')
  71. })
  72. },
  73. prevent() {
  74. return false
  75. },
  76. // 设置当前图片index
  77. setPicIndex({ detail }) {
  78. this.setData({ swiper_index: detail.current })
  79. },
  80. // 预览商品图片
  81. previewImage({ currentTarget }) {
  82. const { current, urls } = currentTarget.dataset
  83. wx.previewImage({ current, urls })
  84. },
  85. // 收藏取消收藏
  86. toggleCollect() {
  87. const { collectId, id } = this.data
  88. util.showLoad('加载中')
  89. if (collectId) { // 取消收藏
  90. util.request(api.CollectDelete, [collectId], 'POST').then(res => {
  91. util.hideLoad()
  92. if (res.errno === 0) {
  93. util.showToast('已取消收藏')
  94. this.setData({ collectId: 0 })
  95. } else util.showErrorToast(res.errmsg)
  96. }).catch(() => {
  97. util.hideLoad()
  98. util.showErrorToast('网络连接失败')
  99. })
  100. } else {
  101. util.request(api.CollectAdd, id, 'POST').then(res => {
  102. util.hideLoad()
  103. if (res.errno === 0) {
  104. util.showToast('已添加收藏')
  105. this.setData({ collectId: res.data.id })
  106. } else util.showErrorToast(res.errmsg)
  107. }).catch(() => {
  108. util.hideLoad()
  109. util.showErrorToast('网络连接失败')
  110. })
  111. }
  112. },
  113. // 显示规格弹出框
  114. showSpec() {
  115. this.setData({ show_spec: true })
  116. },
  117. // 隐藏规格弹出框
  118. hideSpec() {
  119. this.setData({ show_spec: false })
  120. },
  121. onShareAppMessage({ from }) {
  122. const { id, productId, goodsData } = this.data
  123. const obj = { id, productId }
  124. const promise = new Promise(resolve => {
  125. resolve({
  126. title: '富玖铭商户',
  127. path: `/packageGoods/detail/detail?obj=${JSON.stringify(obj)}`
  128. })
  129. })
  130. return from == "button" ? { title: goodsData.name, imageUrl: goodsData.picUrl[0] } : promise
  131. },
  132. onShareTimeline() {}
  133. })