PickerSelect.js 2.14 KB
// packageCrowdFunding/components/PickerSelect/PickerSelect.js
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    show: {
      type: Boolean,
      observer: function(n) {
        let { pickerObj, orderInfo } = this.data
        if (n) {
          pickerObj?.optionList?.map(i => {
            i.active = false
            if (i[pickerObj?.optionId] == orderInfo[pickerObj?.optionId]) {
              i.active = true
            }
            return i
          })
          this.setData({
            optionList: pickerObj?.optionList || []
          })
        }
      }
    },
    title: String,
    orderInfo: Object,
    pickerObj: Object
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeitem(e) {
      let { target } = e.currentTarget.dataset
      let { optionList, pickerObj } = this.data
      optionList.map(i => {
        i.active = false
        if (i[pickerObj?.optionId] == target[pickerObj?.optionId]) {
          i.active = true
        }
        return i
      })
      this.setData({optionList})
    },
    confirm() {
      let { optionList, orderInfo, pickerObj, title } = this.data
      let arr = optionList?.filter(v => v.active) || []
      if (!arr.length) {
        return wx.showToast({
          title: `请${title}`,
          icon: 'none'
        })
      }
      let thisPage = wx.$router.getPageInstance()
      orderInfo[pickerObj.optionId] = arr[0][pickerObj.optionId] || ''
      orderInfo[pickerObj.optionName] = arr[0][pickerObj.optionName] || ''
      if (pickerObj.flag) { // 商品带出金额
        orderInfo['activityPrice'] = `¥${this.priceFilter(arr[0]?.activityPrice / 100 || 0)}`
        orderInfo['crowdFundingPrice'] = `¥${this.priceFilter(arr[0]?.crowdFundingPrice / 100 || 0)}`
      }
      thisPage.setData({
        orderInfo,
        showPickerSelect: false
      })
    },

    priceFilter(num) {
      var pow = Math.pow(10, 2)
      return (Math.round(num * pow) / pow).toFixed(2)
    },
    cancel() {
      let thisPage = wx.$router.getPageInstance()
      thisPage.setData({
        showPickerSelect: false
      })
    }
  }
})