OptionsComponent.vue 3.78 KB
<template>
  <view class="options-list">
    <template v-for="(i, index) in listData">
      <view :key="index" @click="choose(i)" :class="['item flex', { active: checkActive(i) }]">
        <view class="sprite-icon icon-tag-checked"></view>
        {{ i.label }}
      </view>
    </template>
  </view>
</template>

<script type="text/ecmascript-6">
import popupmixin from './popupmixin'
export default {
  mixins: [popupmixin],
  name: 'OptionsComponent',
  inject: ['userInfo'],
  data() {
    return {
    }
  },
  computed: {

  },
  mounted() {
  },
  methods: {
    async getList() {
      const hasCommonElement = (arrA, arrB) => {
        return arrA.some(item => arrB.includes(item))
      }
      const isSubset = (arrA, arrB) => {
        // 检查arrB的每个元素是否都在arrA中
        const setA = new Set(arrA)
        return arrB.every(item => setA.has(item))
      }
      let { params, data: { options }, STORE_BASE_SCENE_CODE, CAR_BRAND_ID } = this
      this.currentId = ''
      this.currentIds = []
      this.chooseList = []
      this.componentLoading(params)
      options = options.filter(item => {
        // 品牌过滤
        let filterBrand = true,
          filterScene = true,
          brandId = true,
          flag = true
        if (item.filterBrandId && item.filterBrandId.length) {
          filterBrand = !isSubset(item.filterBrandId, CAR_BRAND_ID)
        } else {
          brandId = item.brandId && item.brandId.length ? hasCommonElement(item.brandId, CAR_BRAND_ID) : true
        }
        filterScene = item.baseSceneCode&& item.baseSceneCode?.length ? hasCommonElement(item.baseSceneCode, STORE_BASE_SCENE_CODE) : true
        flag = filterBrand && brandId && filterScene
       
        if(item.matchEffect ) {
          flag = !flag
        }
        return flag
      })
      this.updateParams(params, options || [])
    },
    initData(list) {
      const { data, data: { key }, params: { multiple } } = this
      if (data[key]) {
        if (!multiple) {
          this.currentId = data[key]
          let item = list.find(i => i.value == this.currentId)
          this.$emit('choose', item)
        } else {
          let arr = data[key]
          if (typeof arr == 'string') arr = arr.split(',')
          this.chooseList = arr.map(i => list.find(e => e.value == i))
          this.currentIds = arr
          this.$emit('choose', this.chooseList)
        }
      }

      this.list = list
      this.$emit('onComplete')
    },
    choose(i) {
      let { params: { multiple }, currentIds } = this
      if (!multiple) {
        this.currentId = i.value
        this.$emit('choose', i)
      } else {
        let index = currentIds.indexOf(i.value)
        if (index == -1) {
          this.currentIds.push(i.value)
          this.chooseList.push(i)
        } else {
          this.currentIds.splice(index, 1)
          this.chooseList.splice(index, 1)
        }
        this.$emit('choose', this.chooseList)
      }
    },
    checkActive(i) {
      let { currentId, currentIds, data: { key }, data } = this
      if (currentIds && currentIds.length) return currentIds.includes(i.value)
      return currentId ? i.value == currentId : i.value == data[key]
    }
  }
}
</script>

<style scoped lang="scss">
.options-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: toRpx(32);
  margin: 0 toRpx(48);

  .item {
    justify-content: center;
    height: toRpx(72);
    border-radius: toRpx(8);
    border: toRpx(2) solid #dce0e6;
    box-sizing: border-box;
    position: relative;
    color: #333333;
    font-size: toRpx(32);

    .sprite-icon {
      display: none;
      position: absolute;
      top: 0;
      right: 0;
    }
  }

  .active {
    border-color: #2c66f7;
    background: #ecf1ff;
    color: #2c66f7;

    .sprite-icon {
      display: block;
    }
  }
}
</style>