UNPKG

@pisell/pisellos

Version:

一个可扩展的前端模块化SDK框架,支持插件系统

319 lines (317 loc) 7.82 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/model/strategy/strategy-example.ts var strategy_example_exports = {}; __export(strategy_example_exports, { runExamples: () => runExamples }); module.exports = __toCommonJS(strategy_example_exports); var import_index = require("./index"); var voucherConfig = { metadata: { id: "VOUCHER_10", name: "10元代金券", type: "wallet_pass", calculationType: "single" }, conditions: { operator: "and", rules: [ { type: "operator", dimension: "scope_product", field: "productId", operator: "in", value: [60734, 60735] }, { type: "operator", dimension: "scope_channel", field: "channel", operator: "in", value: ["pos", "mini_program"] }, { type: "operator", dimension: "transaction", field: "applicableProductTotal", operator: ">=", value: 100 }, { type: "operator", dimension: "limit_usage", field: "perUserUsedCount", operator: "<", value: 1 } ], actionIds: ["deduct_10"] }, actions: [ { id: "deduct_10", type: "DEDUCT_AMOUNT", value: 10, valueType: "number", valueUnit: "元", target: "applicable_products", priority: 1, config: { allowCrossProduct: true, deductTaxAndFee: true } } ] }; var voucherContext = { entities: { order: { id: "ORDER_001", total: 150, items: [ { productId: 60734, quantity: 2, price: 75 } ] }, customer: { id: "USER_001", level: "vip" } }, attributes: { productId: 60734, channel: "pos", applicableProductTotal: 150, perUserUsedCount: 0 }, metadata: { timestamp: Date.now(), locale: "zh-CN" } }; var tieredDiscountConfig = { metadata: { id: "TIERED_DISCOUNT", name: "阶梯满减", type: "promotion", calculationType: "single" }, conditions: { operator: "or", rules: [ // 档位1:100-199 减10 { operator: "and", rules: [ { type: "operator", dimension: "transaction", field: "orderTotal", operator: ">=", value: 100 }, { type: "operator", dimension: "transaction", field: "orderTotal", operator: "<", value: 200 } ], actionIds: ["deduct_10"] }, // 档位2:200-299 减25 { operator: "and", rules: [ { type: "operator", dimension: "transaction", field: "orderTotal", operator: ">=", value: 200 }, { type: "operator", dimension: "transaction", field: "orderTotal", operator: "<", value: 300 } ], actionIds: ["deduct_25"] }, // 档位3:>= 300 减40 { operator: "and", rules: [ { type: "operator", dimension: "transaction", field: "orderTotal", operator: ">=", value: 300 } ], actionIds: ["deduct_40"] } ], actionIds: [] }, actions: [ { id: "deduct_10", type: "DEDUCT_AMOUNT", value: 10, target: "order", priority: 1 }, { id: "deduct_25", type: "DEDUCT_AMOUNT", value: 25, target: "order", priority: 2 }, { id: "deduct_40", type: "DEDUCT_AMOUNT", value: 40, target: "order", priority: 3 } ] }; var tieredContext = { entities: { order: { id: "ORDER_002", total: 250 } }, attributes: { orderTotal: 250 }, metadata: { timestamp: Date.now() } }; var codeConditionConfig = { metadata: { id: "CODE_CONDITION_EXAMPLE", name: "代码条件示例", type: "custom" }, conditions: { operator: "and", rules: [ // 使用 code 模式 { type: "code", code: 'attributes.orderTotal >= 100 && entities.customer.level === "vip"' }, // 混合使用 operator 模式 { type: "operator", field: "productCount", operator: ">=", value: 2 } ], actionIds: ["vip_discount"] }, actions: [ { id: "vip_discount", type: "DISCOUNT_RATE", value: 0.85, target: "order", priority: 1 } ] }; var codeContext = { entities: { customer: { id: "USER_002", level: "vip" }, order: { id: "ORDER_003", total: 150 } }, attributes: { orderTotal: 150, productCount: 3 }, metadata: { timestamp: Date.now() } }; function runExamples() { console.log("====== 策略模型使用示例 ======\n"); const engine = (0, import_index.createStrategyEngine)({ debug: true, enableTrace: true }); console.log("示例1: 10元代金券"); const result1 = engine.evaluate(voucherConfig, voucherContext); console.log("结果:", JSON.stringify(result1, null, 2)); console.log("\n"); console.log("示例2: 阶梯满减(订单金额250)"); const result2 = engine.evaluate(tieredDiscountConfig, tieredContext); console.log("结果:", JSON.stringify(result2, null, 2)); console.log("\n"); console.log("示例3: Code 模式条件"); const result3 = engine.evaluate(codeConditionConfig, codeContext); console.log("结果:", JSON.stringify(result3, null, 2)); console.log("\n"); console.log("示例4: 快速评估"); const result4 = (0, import_index.evaluate)(voucherConfig, voucherContext); console.log("适用:", result4.applicable); console.log("匹配动作数:", result4.matchedActions.length); console.log("\n"); console.log("示例5: 自定义运算符"); engine.registerOperator("divisible_by", (fieldValue, compareValue) => { return Number(fieldValue) % Number(compareValue) === 0; }); const customOperatorConfig = { metadata: { id: "CUSTOM_OP", name: "自定义运算符", type: "test" }, conditions: { operator: "and", rules: [ { type: "operator", field: "amount", operator: "divisible_by", value: 10 } ], actionIds: ["bonus"] }, actions: [{ id: "bonus", type: "ADD_POINTS", value: 100, target: "customer" }] }; const customContext = { entities: {}, attributes: { amount: 50 }, metadata: { timestamp: Date.now() } }; const result5 = engine.evaluate(customOperatorConfig, customContext); console.log("结果:", result5.applicable ? "适用" : "不适用"); console.log("匹配动作:", result5.matchedActions); console.log("\n引擎调试信息:", engine.getDebugInfo()); } if (require.main === module) { runExamples(); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { runExamples });