index.js 4.39 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.setDefaultSku(n)
      }
    },
    defaultAddress: Object,
    defaultAds: String,
    staffShop:Boolean,
    canPay:Boolean
  },
  data: {
    currentIndex: 0,
    goodsNumber: 1,
    showJfModal:false,
    jfCount:0
  },

  /**
   * 组件的方法列表
   */
  methods: {
    setDefaultSku(skuData) {
      let { specificationList } = skuData
      let [specificationDetailList] = skuData.specificationDetailList
      console.log(specificationDetailList, specificationList)
      let defaultGoods = specificationDetailList.specificationData.split('&')
      specificationList.map(e => {
        defaultGoods.map(i => {
          let [key, value] = i.split('-')
          if (key == e.specificationName) {
            e.specificationValueList.map(c => {
              if (c.specificationValue == value) c.active = true
            })
          }
        })
      })
      specificationDetailList.retailPrice = specificationDetailList.retailPrice
      this.setData({
        goods: skuData
      })
    },
    computeJf(num) {
      let {goods,jfCount,staffShop} = this.data
      if(!staffShop)return
      jfCount = NP.times(goods.redeemPoints,num)
      this.setData({
        jfCount
      })
    },
    cancel() {
      this.setData({
        show: false
      })
      this.triggerEvent('cancelSku')
    },
    changeCount(e) {
      this.triggerEvent('getCount', e.detail)
      this.computeJf(e.detail)
      this.setData({
        goodsNumber: e.detail
      })
    },
    toShowAddressModal() {
      this.setData({
        showAddress: true
      })
    },
    getAddress(e) {
      this.setData({
        defaultAds: e.detail.contactAddress,
        defaultAddress: e.detail.defaultAddress
      })
      this.triggerEvent('sameAddress', e.detail)
    },
    intRemain(e) {
      this.setData({
        remain: e.detail.value
      })
    },
    choose(e) {
      let { ind, index } = e.currentTarget.dataset
      let {
        goods: { specificationList, specificationDetailList }
      } = this.data
      let activeList = []
      if (specificationList[index].specificationValueList[ind].active) return
      specificationList[index].specificationValueList.map(i => (i.active = false))
      specificationList[index].specificationValueList[ind].active = true
      specificationList.map(i => {
        i.specificationValueList.map(c => {
          if (c.active) activeList.push(`${i.specificationName}-${c.specificationValue}`)
        })
      })
      let specificationData = activeList.join('&')
      this.setData({
        [`goods.specificationList[${index}]`]: specificationList[index]
      })
      this.setData({
        goodsNumber: 1
      })
      const thisPage = wx.p.router.getPageInstance()
      thisPage.setData({
        activeSku: specificationData
      })
    },
    toCreatedOrder() {
      const userInfo = wx.p.storage.get('userInfo')
      const { remain, goods, goodsNumber, defaultAddress,staffShop, clickType } = this.data
      if(!defaultAddress.receiveName){
        return wx.$api.showToast('请选择收货地址')
      }
      const params = {
        receiveName: defaultAddress.receiveName,
        receiveMobile: defaultAddress.receiveMobile,
        provinceId: defaultAddress.provinceId,
        provinceName: defaultAddress.provinceName,
        cityId: defaultAddress.cityId,
        cityName: defaultAddress.cityName,
        countyId: defaultAddress.countyId,
        countyName: defaultAddress.countyName,
        address: defaultAddress.address,
        customerOpenId:staffShop?userStorage.weChatOpenId:userInfo.openId,
        buyerMsg: remain,
        orderGoodsList: [{
          goodsSkuCode: goods.goodsSkuCode,
          goodsSkuName: goods.goodsSkuName,
          sellQty: goodsNumber,
        }],
      }
      if (clickType == 'join') {
        // 添加购物车
        cartApi.addToCart().then(res => {
          let { code, message } = res
          if (code == 0) {
            this.closeSku()
            wx.p.wxApi.showToast('加入购物车成功')
          } else {
            wx.p.wxApi.showToast(message)
          }
        })
      } else {
        wx.p.router.go('/pageIndex/pages/confirmOrder/index')
      }
    }
  }
})