| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- import {
- ProductOption,
- VatiantImages,
- ResolvedVariant,
- ProductFlexibleVariant,
- ProductFlexibleVariantValue,
- ProductFlexibleVariantPriceIndices
- } from "@/components/catalog/type";
- /**
- * 辅助函数:判断一个完整的选项值ID组合是否对应一个可购买的变体(quantity > 0)
- * @param selectedIds {number[]} 选中的选项值ID的数组
- * @param variants {ResolvedVariant[]} 变体列表
- * @returns {boolean} 该选项组和对应的变体是否可用
- * */
- export function isCombinationAvailable(
- selectedIds: number[],
- variants: ResolvedVariant[]
- ): boolean {
- // some 每一项都带入参数函数执行,如果遇到一项带入函数执行结果是true,则返回true;如果所有项带入函数执行,结果都是false,则返回false
- return variants.some((variant) => {
- if (variant.quantity <= 0) return false;
- const values = variant.optionValues;
- const variantValueIds = values.map((v) => v.id);
- return (
- selectedIds.length === variantValueIds.length &&
- selectedIds.every((id) => variantValueIds.includes(id))
- );
- });
- }
- /**
- * 辅助函数:根据部分选中的映射,判断某个选项值是否可用
- * @param optionId {number} 选项组ID
- * @param valueId {number} 选项值ID
- * @param currentSelected {Record<number, number>} 当前已选中的映射
- * @param allOptions 产品选项列表
- * @param variants 产品变体列表
- * @returns {boolean} 该选项值是否可用
- * */
- export function isOptionValueAvailable(
- optionId: number,
- valueId: number,
- currentSelected: Record<number, number>, // 当前已选中的映射
- allOptions: ProductOption[],
- variants: ResolvedVariant[]
- ): boolean {
- const testSelected = { ...currentSelected, [optionId]: valueId };
- // 检查是否所有选项组都已选中(有些组可能尚未选择,但禁用判断时我们只关心已选中的组能否与当前测试值组成可用变体)
- // 获取所有选项值ID的列表
- const selectedValueIds = Object.values(testSelected);
- // 如果有任何组未选中,则无法构成完整变体,此时应视为可能可用(不禁用),因为用户还需要继续选择
- if (selectedValueIds.length < allOptions.length) {
- // 但需要检查:在已选中的这些组中,是否存在一个变体其对应的值包含这些已选中的值?
- // 即是否存在变体,其选项值集合包含当前测试选中的值(作为子集)
- return variants.some((variant) => {
- if (variant.quantity <= 0) return false;
- const values = variant.optionValues;
- const variantValueIds = values.map((v) => v.id);
- // 检查测试选中的值是否都是该变体选项值的子集
- return selectedValueIds.every((id) => variantValueIds.includes(id));
- });
- } else {
- // 所有组都已选中,直接检查完整组合是否可用
- return isCombinationAvailable(selectedValueIds, variants);
- }
- }
- /**
- * 根据传入的value id判断变体中是否存在可购买变体,如果有返回true,如果没有则返回false
- * @param valueId {number} 选项值ID
- * @param variants {ResolvedVariant[]} 变体列表
- * @returns {boolean} 该value id是否可用
- * */
- export function isValueAvailable( valueId: number, variants: ResolvedVariant[]): boolean {
- return variants.some((variant) => {
- if (variant.quantity <= 0) return false;
- const values = variant.optionValues;
- const variantValueIds = values.map((v) => v.id);
- return variantValueIds.includes(valueId);
- });
- }
- /**
- * 根据传入的value id组合,找到一个最近的可购买的变体
- * @param fixedOptionId {number} 调过校验的选项组id
- * @param fixedValueId {number} 调过校验的选项值id
- * @param valueIdMap {Record<number, number>} 选中的选项值映射
- * @param allOptions 产品的选项列表
- * @param variants 产品的变体列表
- * @returns {Record<number, number>} 返回一个最近可用变体的选项值映射
- * */
- export function findNearestAvailableVariant(
- fixedOptionId: number,
- fixedValueId: number,
- valueIdMap: Record<number, number>,
- allOptions: ProductOption[],
- variants: ResolvedVariant[] // 可购买的变体列表
- ): Record<number, number>{
- let res:Record<number, number> = {[fixedOptionId]: fixedValueId};
- for (const option of allOptions) {
- const optionId = option.id;
- if(fixedOptionId === optionId) continue;
- const valueId = valueIdMap[optionId];
- if(isOptionValueAvailable(optionId,valueId,res,allOptions,variants)) {
- res = {...res,[optionId]: valueId};
- } else {
- let found = false;
- for(let i = 0; i < option.values.length; i++) {
- if(isOptionValueAvailable(optionId,option.values[i].id,res,allOptions,variants)) {
- found = true;
- res = {...res,[optionId]: option.values[i].id};
- break;
- }
- }
- if (!found) {
- // 极端情况:找不到可用值,保留原值以维持完整状态
- res = { ...res, [optionId]: valueId };
- }
- }
- }
- return res;
- }
- // 递归回溯查找第一个可用组合
- export function getFirstAvailable(productOptions: ProductOption[],flexibleVariants: ResolvedVariant[]) {
- function findFirstAvailable(
- optionIndex: number,
- currentMap: Record<number, number>
- ): Record<number, number> | null {
- if (optionIndex >= productOptions.length) {
- // 所有组都已选,检查完整组合是否可用
- const valueIds = Object.values(currentMap);
- return isCombinationAvailable(valueIds, flexibleVariants) ? { ...currentMap } : null;
- }
- const option = productOptions[optionIndex];
- for (const value of option.values) {
- const nextMap = { ...currentMap, [option.id]: value.id };
- // 剪枝:检查当前部分组合是否可能导向可用变体
- const partialValueIds = Object.values(nextMap);
- const hasPotential = flexibleVariants.some((v) => {
- if (v.quantity <= 0) return false;
- const vIds = v.optionValues.map((ov) => ov.id);
- return partialValueIds.every((id) => vIds.includes(id));
- });
- if (!hasPotential) continue;
- const result = findFirstAvailable(optionIndex + 1, nextMap);
- if (result) return result;
- }
- return null;
- }
- return findFirstAvailable(0, {});
- }
- // 格式化接口返的变体数据
- export function formatFlexibleVariants(originData: ProductFlexibleVariant[] | undefined) {
- let res: ResolvedVariant[] = [];
- if(originData) {
- res = originData.map((edge: ProductFlexibleVariant ) => {
- let variantImages: VatiantImages[] = [],
- optionValues: ProductFlexibleVariantValue[] = [],
- priceIndices: ProductFlexibleVariantPriceIndices[] = [];
- if(typeof edge.variantImages === 'string') {
- variantImages = JSON.parse(edge.variantImages);
- } else if(edge.variantImages === null) {
- variantImages = [];
- }
- if(typeof edge.optionValues === 'string') {
- optionValues = JSON.parse(edge.optionValues);
- }
- if(typeof edge.priceIndices === 'string') {
- priceIndices = JSON.parse(edge.priceIndices);
- } else if(edge.priceIndices === null) {
- priceIndices = [];
- }
- return {...edge,optionValues,variantImages,priceIndices};
- });
- }
- return res;
- }
- // 获取可以购买的变体
- export function getAvailableVariants(flexibleVariants: ResolvedVariant[]) {
- return flexibleVariants.filter((variant) => variant.quantity > 0);
- }
|