index.js 8.28 KB
const navStyle = 'background-color: transparent;'
let navBarHeight = 38
import homeApi from '~/api/module/home.js'
import cartApi from '~/api/module/cart.js'
import mineApi from '~/api/module/mine.js'
import commonApi from '~/api/module/common.js'
Page({
  data: {
    navStyle,
    tabs: ['商品详情', '商品参数'],
    curTabIndex: 0,
    service: {
      1001: '不支持无理由退货',
      1002: '支持7天无理由退货',
      1003: '支持7天无理由退货(拆封后不支持)',
      1004: '支持7天无理由退货(激活后不支持)',
      1005: '支持7天无理由退货(安装后不支持)',
      1006: '支持7天无理由退货(使用后不支持)',
    },
    skuInfo: '',
    payWayActions: [{ name: '金额支付', type: 1, disabled: false }, { name: '积分兑换', type: 2, disabled: false }],
    detailInfo: {},
    eventFlag: false,
    addressList: [],
    defaultAddress: {},
    bannerIndex: 0
  },
  appHide() {
    wx.offAppHide()
    wx.onAppHide(() => {
    })
  },
  previewImage(e) {
    let {
      current,
      item
    } = e.currentTarget.dataset
    let urls = []
    item.map(i => {
      urls.push(i.imgUrl)
    })
    wx.p.wxApi.previewImage({
      current,
      urls
    })
  },
  changeBanner(e) {
    this.setData({
      bannerIndex: e.detail.current
    })
  },
  navbarReady(e) {
    navBarHeight = e.detail.height
  },
  onPageScroll(e) {
    const { curTabIndex } = this.data
    if (curTabIndex == 0) {
      this.scrollY = e.scrollTop
    }
    this.setData({
      navStyle: e.scrollTop >= navBarHeight || curTabIndex ? '' : navStyle,
      opacity: curTabIndex == 1 ? 1 : e.scrollTop / 200,
    })
  },
  goodsActionReady(e) {
    this.setData({
      padding_bottom: e.detail
    })
  },
  goodsActionClick(e) {
    let { detailInfo } = this.data
    // if (e.detail == 'join' && detailInfo.goodsSource == 1) return this.toAddShopCar()
    this.showSkuModal(e.detail)
  },
  onLoad(options) {
    let userInfo = wx.p.storage.get('userInfo')
    this.setData({userInfo})
    wx.p.router.getRouterParams(options).then(params => {
      this.setData({pageParams: params}, () => {this.heartbeatFn({type: 'add'})})
      this.getDetail(params)
    }).catch(err => {
      console.error('参数获取失败:', err)
    })
    this.getPlatFormAddress()
  },
  onShow() {
    this.heartbeatFn({type: 'add'})
  },
  heartbeatFn(e) {
    let eventApi = e.type == 'add' ? 'dynamicRecordAdd' : 'dynamicRecordLeave'
    let { pageParams, userInfo, reviewId } = this.data
    if (!pageParams) return
    let params = e.type == 'add' ? {
      goodsId: pageParams?.id,
      goodsType: 1,
      customerMobile: userInfo?.phone,
      customerName: userInfo?.nickName,
      merchantId: userInfo?.merchantId,
      merchantName: userInfo?.merchantName,
    } : {id: reviewId}
    let { goodsId, customerMobile, merchantId } = params
    if (e.type == 'add' && (!goodsId || !customerMobile || !merchantId)) {
      wx.p.sentry.reportParamError(
        '商品详情进入埋点异常:goodsId, customerMobile, merchantId',
        params
      )
    }
    if (e.type != 'add' && !reviewId) {
      wx.p.sentry.reportParamError(
        '商品详情离开埋点异常:id',
        {id: reviewId}
      )
    }
    commonApi[eventApi](params).then(res => {
      this.setData({reviewId: res?.data})
    })
  },
  showSkuModal(type) {
    this.setData({
      showSku: true,
      clickType: type
    })
    wx.setPageStyle({
      style: {
        overflow: 'hidden'
      }
    })
  },
  // 获取默认的地址
  getPlatFormAddress() {
    wx.p.wxApi.showLoading()
    mineApi.getCustomerAddressList().then(res => {
      wx.hideLoading()
      let _isSetDefault = false, contactAddress = '', defaultAddress = {}
      if (res && res.length) {
        res.forEach(el => {
          if (el.isDefault == 1) {
            contactAddress = el.provinceName + el.cityName + el.countyName + el.address,
            defaultAddress = el
            _isSetDefault = true
          }
        })
        if (!_isSetDefault) {
          let _first = res[0] || {}
          contactAddress = _first.provinceName + _first.cityName + _first.countyName + _first.address,
          defaultAddress = _first
        }
      }
      this.setData({
        addressList: res || [],
        contactAddress,
        defaultAddress
      })
    })
  },
  // 显示地址选择弹窗
  toShowAddressModal() {
    this.setData({
      showAddress: true
    })
    wx.setPageStyle({
      style: {
        overflow: 'hidden'
      }
    })
  },
  closeAddress() {
    this.setData({
      showAddress: false
    })
    if (!this.data.showSku) {
      wx.setPageStyle({
        style: {
          overflow: 'unset'
        }
      })
    }
  },
  // 地址选择确定
  getAddress(e) {
    this.setData({
      contactAddress: e.detail.contactAddress,
      defaultAddress: e.detail.defaultAddress
    })
  },
  back() {
    wx.p.router.back()
  },
  changeTab(e) {
    if (this.data.opacity < 0.8) return
    this.setData({
      curTabIndex: e.currentTarget.dataset.index,
      opacity: e.currentTarget.dataset.index
    }, () => {
      setTimeout(() => {
        wx.pageScrollTo({
          scrollTop: this.scrollY
        })
      }, 200)
    })
  },
  // 获取详情
  getDetail(options) {
    if (!options.id) {
      wx.p.sentry.reportParamError(
        '商品详情异常:id',
        id
      )
    }
    homeApi.getGoodsDetailInfo({id: options.id, businessType: 1}).then(res => {
      if (res && res.specificationResponses?.length) {
        this.toGetSpecString(res, res.startQty)
      }
      if (res && res.platformGlGoodsSpecifications?.length) {
        let item = res.platformGlGoodsSpecifications[0] || {}
        res.goodsStock = item.stock
        res.specificationData = item.specificationData
      }
      res?.platformGoodsResources?.forEach(item => {
        if (item.resType == 1) item.imgUrl = item.content
      })
      if (res.goodsSource == 2) {
        res.imageResponses = res?.platformGoodsResources?.filter(val => val.bizType == 1)
        res.goodsDescList = res?.platformGoodsResources?.filter(val => val.bizType == 2)
      }
      this.setData({
        detailInfo: res || {},
        eventFlag: true,
        activeSku: res.specificationData,
        goodsStock: res.goodsStock || 0
      })
    })
  },
  // 处理云中鹤已选商品
  toGetSpecString(info, count) {
    let skuInfo = this.data, num = '', weight = ''
    num = `${count}${info.goodsUnit}`
    weight = `${info.grossWeight}${info.grossWeightUnit}`
    skuInfo = `${info.specificationResponses[0].specValue}` + `,${weight}` + `,${num}`
    this.setData({
      skuInfo
    })
  },
  getCount(e) {
    let { detailInfo } = this.data
    if (detailInfo?.goodsSource == 1) this.toGetSpecString(this.data.detailInfo, e.detail)
    this.count = e.detail
  },
  // 添加购物车
  toAddShopCar() {
    const { detailInfo, userInfo } = this.data
    const PARAMS = {
      goodsId: detailInfo.id,
      sellQty: this.count || detailInfo.startQty,
      goodsSource: detailInfo.goodsSource,
      scoreSource: userInfo?.channelSource,
      specificationData: detailInfo?.specificationData,
      merchantId: userInfo?.merchantId
    }
    if (!detailInfo.goodsStock) return wx.p.wxApi.showToast('暂无库存')
    let { goodsId, sellQty, goodsSource, scoreSource, merchantId, specificationData } = PARAMS
    if (!goodsId || !sellQty || !goodsSource || !scoreSource || !merchantId || (goodsSource == 2 && !specificationData)) {
      wx.p.sentry.reportParamError(
        '加入购物车异常:goodsId, sellQty, goodsSource, scoreSource, merchantId, specificationData',
        PARAMS
      )
    }
    cartApi.addToCart(PARAMS).then(res => {
      wx.p.wxApi.showToast('加入购物车成功')
      this.joinSuccess()
    })
  },
  joinSuccess() {
    // 获取最新购物车数据更新显示的数量
    const goodsAction = this.selectComponent('#goods-action')
    if (goodsAction) {
      goodsAction.getCartNum()
    }
  },
  // 同步选择的地址
  resetAddress(e) {
    this.setData(e.detail)
  },
  closeSku() {
    this.setData({
      showSku: false
    })
    wx.setPageStyle({
      style: {
        overflow: 'unset'
      }
    })
  },
  onHide() {
    this.heartbeatFn({type: 'level'})
  },
  onUnload: function () {
    this.heartbeatFn({type: 'level'})
  },
  onShareAppMessage: function () {
    
  }
})