莫张周刘王 发表于 2024-7-26 01:31:28

vue3前端开发-小兔鲜项目-sku的实现

vue3前端开发-小兔鲜项目-sku的实现!这是一个会计学的特殊专业名词,可以理解为产品的型号,规格的货品计量单元。
它是一组数据的混合体。比如:尺寸,质料,品质,等等。组合在一起形成的一个混合数组对象。
下面是小兔鲜项目标官方提供的一个下载地址:
里面有我们需要的一些插件代码。包括本次项目测试内容用到的sku插件代码。
地址:百度网盘 请输入提取码
您可以去这个地址里下载百度网盘内的代码内容。里面有本次分享的插件sku内容了!
https://i-blog.csdnimg.cn/direct/2f555bcb2afc473e950cb032dac3e722.png 
它的位置,照旧在项目标根components下面的。
<script>
import { watchEffect } from 'vue'
import getPowerSet from './power-set'
const spliter = '★'
// 根据skus数据得到路径字典对象
const getPathMap = (skus) => {
const pathMap = {}
if (skus && skus.length > 0) {
    skus.forEach(sku => {
      // 1. 过滤出有库存有效的sku
      if (sku.inventory) {
      // 2. 得到sku属性值数组
      const specs = sku.specs.map(spec => spec.valueName)
      // 3. 得到sku属性值数组的子集
      const powerSet = getPowerSet(specs)
      // 4. 设置给路径字典对象
      powerSet.forEach(set => {
          const key = set.join(spliter)
          // 如果没有就先初始化一个空数组
          if (!pathMap) {
            pathMap = []
          }
          pathMap.push(sku.id)
      })
      }
    })
}
return pathMap
}

// 初始化禁用状态
function initDisabledStatus (specs, pathMap) {
if (specs && specs.length > 0) {
    specs.forEach(spec => {
      spec.values.forEach(val => {
      // 设置禁用状态
      val.disabled = !pathMap
      })
    })
}
}

// 得到当前选中规格集合
const getSelectedArr = (specs) => {
const selectedArr = []
specs.forEach((spec, index) => {
    const selectedVal = spec.values.find(val => val.selected)
    if (selectedVal) {
      selectedArr = selectedVal.name
    } else {
      selectedArr = undefined
    }
})
return selectedArr
}

// 更新按钮的禁用状态
const updateDisabledStatus = (specs, pathMap) => {
// 遍历每一种规格
specs.forEach((item, i) => {
    // 拿到当前选择的项目
    const selectedArr = getSelectedArr(specs)
    // 遍历每一个按钮
    item.values.forEach(val => {
      if (!val.selected) {
      selectedArr = val.name
      // 去掉undefined之后组合成key
      const key = selectedArr.filter(value => value).join(spliter)
      val.disabled = !pathMap
      }
    })
})
}


export default {
name: 'XtxGoodSku',
props: {
    // specs:所有的规格信息skus:所有的sku组合
    goods: {
      type: Object,
      default: () => ({ specs: [], skus: [] })
    }
},
emits: ['change'],
setup (props, { emit }) {
    let pathMap = {}
    watchEffect(() => {
      // 得到所有字典集合
      pathMap = getPathMap(props.goods.skus)
      // 组件初始化的时候更新禁用状态
      initDisabledStatus(props.goods.specs, pathMap)
    })

    const clickSpecs = (item, val) => {
      if (val.disabled) return false
      // 选中与取消选中逻辑
      if (val.selected) {
      val.selected = false
      } else {
      item.values.forEach(bv => { bv.selected = false })
      val.selected = true
      }
      // 点击之后再次更新选中状态
      updateDisabledStatus(props.goods.specs, pathMap)
      // 把选择的sku信息传出去给父组件
      // 触发change事件将sku数据传递出去
      const selectedArr = getSelectedArr(props.goods.specs).filter(value => value)
      // 如果选中得规格数量和传入得规格总数相等则传出完整信息(都选择了)
      // 否则传出空对象
      if (selectedArr.length === props.goods.specs.length) {
      // 从路径字典中得到skuId
      const skuId = pathMap
      const sku = props.goods.skus.find(sku => sku.id === skuId)
      // 传递数据给父组件
      emit('change', {
          skuId: sku.id,
          price: sku.price,
          oldPrice: sku.oldPrice,
          inventory: sku.inventory,
          specsText: sku.specs.reduce((p, n) => `${p} ${n.name}:${n.valueName}`, '').trim()
      })
      } else {
      emit('change', {})
      }
    }
    return { clickSpecs }
}
}
</script>
<template>
<div class="goods-sku">
    <dl v-for="item in goods.specs" :key="item.id">
      <dt>{{ item.name }}</dt>
      <dd>
      <template v-for="val in item.values" :key="val.name">
          <img :class="{ selected: val.selected, disabled: val.disabled }" @click="clickSpecs(item, val)"
            v-if="val.picture" :src="val.picture" />
          <span :class="{ selected: val.selected, disabled: val.disabled }" @click="clickSpecs(item, val)" v-else>{{
            val.name
          }}</span>
      </template>
      </dd>
    </dl>
</div>
</template>


<style scoped lang="scss">
@mixin sku-state-mixin {
border: 1px solid #e4e4e4;
margin-right: 10px;
cursor: pointer;

&.selected {
    border-color: $xtxColor;
}

&.disabled {
    opacity: 0.6;
    border-style: dashed;
    cursor: not-allowed;
}
}

.goods-sku {
padding-left: 10px;
padding-top: 20px;

dl {
    display: flex;
    padding-bottom: 20px;
    align-items: center;

    dt {
      width: 50px;
      color: #999;
    }

    dd {
      flex: 1;
      color: #666;

      >img {
      width: 50px;
      height: 50px;
      margin-bottom: 4px;
      @include sku-state-mixin;
      }

      >span {
      display: inline-block;
      height: 30px;
      line-height: 28px;
      padding: 0 20px;
      margin-bottom: 4px;
      @include sku-state-mixin;
      }
    }
}
}
</style> 这个就是index.vue的内容了。

export default function bwPowerSet (originalSet) {
const subSets = []

// We will have 2^n possible combinations (where n is a length of original set).
// It is because for every element of original set we will decide whether to include
// it or not (2 options for each set element).
const numberOfCombinations = 2 ** originalSet.length

// Each number in binary representation in a range from 0 to 2^n does exactly what we need:
// it shows by its bits (0 or 1) whether to include related element from the set or not.
// For example, for the set {1, 2, 3} the binary number of 0b010 would mean that we need to
// include only "2" to the current set.
for (let combinationIndex = 0; combinationIndex < numberOfCombinations; combinationIndex += 1) {
    const subSet = []

    for (let setElementIndex = 0; setElementIndex < originalSet.length; setElementIndex += 1) {
      // Decide whether we need to include current element into the subset or not.
      if (combinationIndex & (1 << setElementIndex)) {
      subSet.push(originalSet)
      }
    }

    // Add current subset to the list of all subsets.
    subSets.push(subSet)
}

return subSets
} 这个是power-set.js的内容了。
在组件内调用一下它,注意要给它通报对象的。
它还会给父组件回传一个对象sku
https://i-blog.csdnimg.cn/direct/e9d7a22a317d481cb6d613aef3b17be2.png
如图,父组件内调用了这个子组件(sku)
子组件会界说了一些方法,会回传给父组件一个sku对象。
https://i-blog.csdnimg.cn/direct/19623be31b864cbca6219f362b7befbc.png
如图,写的很清晰。通报数据给父组件。
因为,我们的父组件,将来要写一个参加购物车的按钮 操作。需要用到这个sku(信息)

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: vue3前端开发-小兔鲜项目-sku的实现