index.js 5.07 KB
import NP from "number-precision";
import cartApi from '~/api/module/cart.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: Boolean,
    address: Array,
    type: String,
    goods: {
      type: Object,
      observer: function(n) {
        this.setData({
          detailInfo: JSON.parse(JSON.stringify(n))
        }, () => {
          this.setDefaultSku(n)
        })
      }
    },
    defaultAddress: Object,
    defaultAds: String,
    canPay:Boolean,
    clickType: String
  },
  data: {
    currentIndex: 0,
    goodsNumber: 1,
    showJfModal: false,
    detailInfo: {}
  },

  /**
   * 组件的方法列表
   */
  methods: {
    cancel() {
      this.setData({
        show: false
      })
      this.triggerEvent('cancelSku')
    },
    setDefaultSku(detailInfo) {
      let specificationDetailList = detailInfo.platformGlGoodsSpecifications && detailInfo.platformGlGoodsSpecifications[0]
      if (detailInfo?.specificationData) {
        specificationDetailList = detailInfo?.platformGlGoodsSpecifications?.find(val => val.specificationData == detailInfo.specificationData)
      }
      let defaultGoods = specificationDetailList?.specificationData.split('&')
      detailInfo.platformGlGoodsSpecificationList?.map(e => {
        defaultGoods?.map(i => {
          let [key, value] = i.split('-')
          if (key == e.specificationName) {
            e.platformSpecificationValueList.map(c => {
              if (c.specificationValue == value) c.active = true
            })
          }
        })
      })
      this.setData({
        detailInfo,
        ['detailInfo.goodsStock']: specificationDetailList?.stock || detailInfo.goodsStock, // 多规格商品库存在每个规格组中
        goodsNumber: detailInfo?.sellQty || 1
      })
    },
    // 选择规格
    choose(e) {
      let { index, ind } = e.currentTarget.dataset
      let { detailInfo } = this.data
      let { platformGlGoodsSpecificationList, platformGlGoodsSpecifications } = detailInfo
      let activeList = []
      if (platformGlGoodsSpecificationList[index].platformSpecificationValueList[ind].active) return
      platformGlGoodsSpecificationList[index].platformSpecificationValueList.map(i => (i.active = false))
      platformGlGoodsSpecificationList[index].platformSpecificationValueList[ind].active = true
      platformGlGoodsSpecificationList.map(i => {
        i.platformSpecificationValueList.map(c => {
          if (c.active) activeList.push(`${i.specificationName}-${c.specificationValue}`)
        })
      })
      let specificationData = activeList.join('&')
      let goodsStock
      platformGlGoodsSpecifications?.map(val => {
        if (val.specificationData == specificationData) {
          goodsStock = val.stock
        }
      })
      this.setData({
        detailInfo,
        ['detailInfo.goodsStock']: goodsStock,
        ['detailInfo.specificationData']: specificationData
      })
      const thisPage = wx.p.router.getPageInstance()
      thisPage.setData({
        activeSku: specificationData,
        goodsStock
      })
    },
    changeCount(e) {
      this.triggerEvent('getCount', e.detail)
      this.setData({
        goodsNumber: e.detail
      })
    },
    toShowAddressModal() {
      let thisPage = wx.p.router.getPageInstance()
      thisPage.setData({
        showAddress: true
      })
    },
    intRemain(e) {
      this.setData({
        remain: e.detail.value
      })
    },
    toCreatedOrder() {
      let { clickType, remain, detailInfo, defaultAddress, goodsNumber } = this.data
      const userInfo = wx.p.storage.get('userInfo')
      if (!['pay', 'join', 'change'].includes(clickType)) {
        return this.cancel()
      }
      if (clickType == 'change') {
        this.cancel()
        return this.triggerEvent('changeSku', {specificationData: detailInfo.specificationData, sellQty: goodsNumber || detailInfo.startQty})
      }
      console.log(detailInfo)
      if (!detailInfo.goodsStock) return
      detailInfo.sellQty = goodsNumber || detailInfo.sellQty
      detailInfo?.platformGlGoodsSpecifications?.forEach(val => {
        if (val.active) detailInfo.specificationData = val.specificationData
      })
      if (clickType == 'join') {
        // 加入购物车
        const PARAMS = {
          goodsId: detailInfo.id,
          sellQty: goodsNumber || detailInfo.startQty,
          goodsSource: detailInfo.goodsSource,
          scoreSource: userInfo?.channelSource,
          specificationData: detailInfo?.specificationData,
          merchantId: userInfo?.merchantId
        }
        wx.p.wxApi.showLoading()
        cartApi.addToCart(PARAMS).then(res => {
          wx.hideLoading()
          wx.p.wxApi.showToast('加入购物车成功')
          this.cancel()
          this.triggerEvent('joinSuccess')
        })
        return
      }
      if(!defaultAddress.receiveName){
        return wx.p.wxApi.showToast('请选择收货地址')
      }
      // 跳转到确认订单
      let query = {
        defaultAddress,
        buyerMsg: remain,
        goodsList: [{...detailInfo}],
      }
      wx.p.router.go('/packageIndex/pages/confirmOrder/index', query)
    }
  }
})