index.js 5 KB
// components/selfSku/index.js
import { centsToyuan } from '~/utils/format'
import cartApi from '~/api/module/cart.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer(n) {
        if (n)
          this.setData({
            close: false
          })
      }
    },
    showName: String,
    skuData: {
      type: Object,
      observer(n) {
        this.setDefaultSku(n)
      }
    },
    clickType: String,
    defaultAds: String,
    defaultAddress: Object
  },

  /**
   * 组件的初始数据
   */
  data: {
    goodsNumber: 1
  },

  /**
   * 组件的方法列表
   */
  methods: {
    noScroll() {},
    setDefaultSku(skuData) {
      let { specificationList } = skuData
      let [specificationDetailList] = skuData.specificationDetailList
      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 = centsToyuan(specificationDetailList.retailPrice)
      this.setData({
        skuObj: skuData,
        goodsInfo: specificationDetailList
      })
    },
    choose(e) {
      let { ind, index } = e.currentTarget.dataset
      let {
        skuObj: { specificationList, id, specificationExtraList }
      } = 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({
        [`skuObj.specificationList[${index}]`]: specificationList[index]
      })
      shopmallApi
        .getGoodsSpecificationExtra({
          goodsId: id,
          specificationData
        })
        .then(data => {
          data.retailPrice = centsToyuan(data.retailPrice)
          this.setData({
            goodsInfo: data
          })
          console.log(this.data.goodsInfo)
        })
      this.setData({
        goodsNumber: 1
      })
      const thisPage = wx.p.router.getPageInstance()
      thisPage.setData({
        activeSku: specificationData
      })
    },
    changeCount(e) {
      let goodsNumber = e.detail
      this.setData({
        goodsNumber
      })
    },
    closeSku() {
      let thisPage = wx.p.router.getPageInstance()
      let { showName } = this.data
      this.setData({
        close: true
      }, () => {
        thisPage.setData({
          [showName]: false
        })
      })
    },
    intRemain(e) {
      this.setData({
        remain: e.detail.value
      })
    },
    toShowAddressModal() {
      let thisPage = wx.p.router.getPageInstance()
      thisPage.setData({
        showAddress: true
      })
    },
    toCreatedOrder() {
      let { clickType, goodsInfo, goodsNumber } = this.data
      let userInfo = wx.p.storage.get('userInfo')
      if (!clickType) {
        return this.cancel()
      }
      if (clickType == 'join') {
        console.log('加入购物车成功')
        const PARAMS = {
          goodsId: goodsInfo.id,
          sellQty: goodsNumber,
          goodsSkuCode: goodsInfo.goodsSkuCode,
          goodsSource: goodsInfo.goodsSource,
          scoreSource: userInfo?.channelSource
        }
        cartApi.addToCart(PARAMS).then(res => {
          this.cancel()
          wx.$api.showToast('加入购物车成功')
        })
        return
      }
      const { remain, goods, defaultAddress,staffShop } = this.data
      console.log(defaultAddress)
      if(!defaultAddress.receiveName){
        return wx.p.wxApi.showToast('请选择收货地址')
      }
      // 跳转到确认订单
      let query = {
        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,
        }],
      }
      wx.p.router.go('/packageIndex/pages/confirmOrder/index', query)
    },
    cancel() {
      this.setData({
        show: false
      })
      this.triggerEvent('cancelSku')
    },
  },
  
  lifetimes: {
    attached() {
    }
  }
})