goods-detail.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import { VueLikePage } from '../../utils/page'
  2. import GoodsApi from '../../apis/goods'
  3. import Router from '../../utils/routes'
  4. import ImSend from '../../utils/imSend'
  5. import CenSus from '../../apis/census'
  6. import CompanyApi from '../../apis/company'
  7. import { saveCollectHistory, loadCollectHistory, removeCollect } from '../../utils/storage'
  8. VueLikePage([], {
  9. data: {
  10. detail: {}
  11. },
  12. methods: {
  13. async onLoad (options) {
  14. console.log(options)
  15. const { goods_id, company_id } = options
  16. this.goods_id = goods_id
  17. const goodsDetail = await this.getGoodsDetail()
  18. this.setData({
  19. detail: goodsDetail
  20. })
  21. // this.getGuide()
  22. this.getGoodsCollectStatus()
  23. const companyRes = await CompanyApi.getCompanyDetail(company_id)
  24. console.log(companyRes)
  25. this.company = companyRes.data
  26. },
  27. async getGoodsDetail () {
  28. let res = await GoodsApi.getShopGoodsDetail(this.goods_id)
  29. let goodsDetail = res.data
  30. goodsDetail.banner = goodsDetail.gallery.map(item => item.img_url)
  31. goodsDetail.info.goods_desc = goodsDetail.info.goods_desc && goodsDetail.info.goods_desc.replace(/(\<img)/gi, function ($0, $1) {
  32. return {
  33. "<img": '<img style="width:100%;height:auto;display:block;" '
  34. }[$1];
  35. })
  36. return goodsDetail
  37. },
  38. getGoodsCollectStatus () {
  39. const status = loadCollectHistory(this.goods_id)
  40. this.setData({
  41. collectStatus: Boolean(status) ? 1 : 0
  42. })
  43. },
  44. changeCollect () {
  45. const collectStatus = this.data.collectStatus
  46. let data = '', apiFn
  47. if (collectStatus === 1) {
  48. data = this.goods_id
  49. apiFn = removeCollect
  50. } else {
  51. apiFn = saveCollectHistory
  52. data = {
  53. img_url: this.data.detail.banner[0],
  54. title: this.data.detail.info.name,
  55. id: this.goods_id
  56. }
  57. }
  58. apiFn(data)
  59. wx.showToast({
  60. title: `${collectStatus === 1 ? '取消' : '收藏'}成功`,
  61. })
  62. this.getGoodsCollectStatus()
  63. },
  64. getGuide () {
  65. GoodsApi.getGuideByGoods(this.goods_id).then(res => {
  66. this.guide = res.data
  67. })
  68. },
  69. toChat () {
  70. // if (!this.guide) return
  71. if (!getApp().globalData.token) {
  72. Router.push('login')
  73. return
  74. }
  75. CenSus.addGoodsQueryNum({
  76. goodsId: this.goods_id,
  77. sceneNum: this.company.sceneNum
  78. })
  79. ImSend.sendMsg({content:'你好', msgType:'text', toId:this.company.guideId}).then(() => {
  80. Router.push({
  81. url: 'chat',
  82. query: {
  83. toId: this.company.guideId
  84. }
  85. })
  86. })
  87. }
  88. }
  89. })