/** * 赠品信息(evaluator 返回) */ interface GiftInfo { strategyId: string; strategyName: string | Record; giftCount: number; giftOptions: Array<{ product_id: number; product_variant_id: number; }>; } /** * 扩展的赠品信息(包含触发赠品的主商品ID) */ interface ExtendedGiftInfo extends GiftInfo { /** 触发此赠品的主商品 _id 列表 */ sourceProductIds: string[]; } /** * 赠品详情信息(放在 _extend._promotion.giftInfo) */ interface PromotionGiftDetail { strategyName: string | Record; giftCount: number; giftOptions: Array<{ product_id: number; product_variant_id: number; }>; } /** * 商品促销信息(整合后,放在 _extend._promotion) */ interface ProductPromotionInfo { /** 策略ID */ strategyId: string; /** 策略元数据 */ strategyMetadata?: any; /** 是否参与促销 */ inPromotion: boolean; /** 原始价格 */ originalPrice: number; /** 最终价格 */ finalPrice: number; /** 赠品信息(仅"买X送Y"促销的第一个商品有) */ giftInfo?: PromotionGiftDetail; } /** * 赠品商品上的标记信息(放在 _giftInfo) */ interface GiftProductInfo { /** 关联策略ID */ strategyId: string; /** 策略名称 */ strategyName: string | Record; /** 触发此赠品的主商品 _id 列表 */ sourceProductIds: string[]; } /** * 赠品减少操作项 */ interface GiftReduceItem { /** 赠品的 _id */ _id: string; /** 当前数量 */ currentQuantity: number; /** 目标数量(减少后的数量,不会是0,0的情况在toRemove处理) */ newQuantity: number; } /** * 赠品操作类型 */ interface GiftAction { /** 需要添加的赠品 */ toAdd: Array<{ strategyId: string; strategyName: string | Record; giftCount: number; giftOptions: Array<{ product_id: number; product_variant_id: number; }>; /** 触发此赠品的主商品 _id 列表 */ sourceProductIds: string[]; }>; /** 需要减少数量的赠品(数量减少但不删除) */ toReduce: Array<{ strategyId: string; /** 具体要操作的赠品列表 */ items: GiftReduceItem[]; }>; /** 需要完全删除的赠品 _id 列表 */ toRemove: string[]; } /** * 促销计算结果 */ /** * 未满足促销条件的信息 */ interface UnfulfilledPromotion { /** 策略ID */ strategyId: string; /** 活动名称 */ strategyName: string | Record; /** 促销类型(X_ITEMS_FOR_Y_PRICE / BUY_X_GET_Y_FREE) */ actionType: string; /** 还需要买几件 */ needQuantity: number; /** 当前已有几件 */ currentQuantity: number; /** 促销需要的总件数 */ requiredQuantity: number; /** 可以参与此促销的商品列表 */ eligibleProducts: Array<{ product_id: number; product_variant_id: number; }>; /** 策略元数据 */ strategyMetadata?: any; /** 展示配置 */ display?: { text: string | Record; type: string; }; } interface PromotionResult { /** 处理后的商品列表(不含赠品) */ products: any[]; /** 总优惠金额 */ totalDiscount: number; /** 扩展的赠品信息(包含 sourceProductIds) */ gifts: ExtendedGiftInfo[]; /** 赠品操作(需要添加/减少/删除的赠品) */ giftActions: GiftAction; /** 未满足促销条件的信息(用于展示"再买X件"提示) */ unfulfilledPromotions: UnfulfilledPromotion[]; } /** * 赠品选择回调参数 */ interface GiftSelectionCallbackParams { /** 策略ID */ strategyId: string; /** 策略名称 */ strategyName: string | Record; /** 可选赠品列表 */ giftOptions: Array<{ product_id: number; product_variant_id: number; }>; /** 可选数量 */ giftCount: number; /** 触发此赠品的主商品 _id 列表 */ sourceProductIds: string[]; } /** * usePromotion 配置选项 */ interface UsePromotionOptions { /** 需要选择赠品时的回调 */ onGiftSelection?: (params: GiftSelectionCallbackParams) => void; } declare const usePromotion: (state?: any, dispatch?: any, options?: UsePromotionOptions) => { run: (this: unknown) => any; formatProductList: (this: unknown, list: any[]) => any[]; processPromotion: (this: unknown, list: any[]) => PromotionResult; }; export default usePromotion; /** * 主商品的 _extend._promotion 结构示例 * * @example * ```typescript * // 1. X件Y元促销 - 已满足条件 * { * _extend: { * _promotion: { * strategyId: "strategy_1", * strategyMetadata: { ... }, * inPromotion: true, * originalPrice: 100, * finalPrice: 80, * } * } * } * * // 2. X件Y元促销 - 未满足条件(如买2件享优惠,当前只买了1件) * // UI 可以展示"再买1件即可享受2件80元"等提示 * { * _extend: { * _promotion: { * strategyId: "strategy_1", * strategyMetadata: { ... }, * inPromotion: false, // 未满足条件 * originalPrice: 100, * finalPrice: 100, // 价格不变 * } * } * } * * // 3. 买X送Y促销的第一个商品(有 giftInfo) * { * _extend: { * _promotion: { * strategyId: "strategy_2", * strategyMetadata: { ... }, * inPromotion: true, * originalPrice: 100, * finalPrice: 100, * giftInfo: { * strategyName: "买3送1", * giftCount: 1, * giftOptions: [{ product_id: 123, product_variant_id: 0 }] * } * } * } * } * * // 4. 买X送Y促销的其他商品(无 giftInfo) * { * _extend: { * _promotion: { * strategyId: "strategy_2", * strategyMetadata: { ... }, * inPromotion: true, * originalPrice: 100, * finalPrice: 100, * } * } * } * * // 5. 无适用促销的商品 * // 没有 _promotion 字段 * { * _extend: { * // 无 _promotion 字段 * } * } * ``` */ /** * onGiftSelection 回调中如何构建赠品商品的示例 * * @example * ```tsx * const { run, formatProductList, processPromotion } = usePromotion(state, dispatch, { * onGiftSelection: async ({ strategyId, strategyName, giftOptions, giftCount, sourceProductIds }) => { * // giftOptions 只包含 product_id 和 product_variant_id * // 需要自行获取完整商品信息 * * let selectedGiftOption = giftOptions[0]; // 默认选第一个 * * // 如果有多个赠品选项,弹出选择弹窗 * if (giftOptions.length > 1) { * selectedGiftOption = await showGiftSelectionModal(giftOptions); * } * * // 获取完整商品信息(根据 product_id 和 product_variant_id) * const productDetail = await fetchProductDetail( * selectedGiftOption.product_id, * selectedGiftOption.product_variant_id * ); * * // 构建赠品商品对象 * const giftProduct = { * _id: generateStringId(), // 唯一标识 * _isGift: true, // 标记为赠品 * _giftInfo: { // 赠品关联信息(注意:赠品用 _giftInfo,主商品用 _extend._promotion) * strategyId, // 策略ID * strategyName, // 策略名称 * sourceProductIds, // 触发此赠品的主商品 _id 列表 * } as GiftProductInfo, * _extend: { * price: 0, // 赠品价格为0 * main_product_selling_price: 0, * quantity: giftCount, // 赠品数量 * total: 0, * origin_total: 0, * product_name: productDetail.title, * other: { * product_id: selectedGiftOption.product_id, * product_variant_id: selectedGiftOption.product_variant_id, * }, * }, * // ... 其他商品基础信息从 productDetail 获取 * title: productDetail.title, * price: productDetail.price, // 原价(用于展示划线价) * // ... * }; * * // 添加到购物车 * dispatch({ * type: 'addService', * payload: { value: giftProduct }, * }); * }, * }); * ``` */ export type { GiftInfo, ExtendedGiftInfo, PromotionGiftDetail, ProductPromotionInfo, GiftProductInfo, GiftAction, GiftReduceItem, GiftSelectionCallbackParams, UsePromotionOptions, PromotionResult, UnfulfilledPromotion, };