@pisell/pisellos 2.1.60 → 2.1.61
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +0 -1
- package/dist/index.js +0 -1
- package/dist/modules/Cart/types.d.ts +0 -2
- package/dist/modules/Cart/utils/cartProduct.js +0 -11
- package/dist/modules/Customer/index.js +1 -1
- package/dist/modules/Discount/types.d.ts +0 -15
- package/dist/modules/Payment/index.d.ts +1 -2
- package/dist/modules/Payment/index.js +7 -10
- package/dist/modules/Payment/utils.js +0 -3
- package/dist/modules/Payment/walletpass.d.ts +0 -23
- package/dist/modules/Payment/walletpass.js +95 -191
- package/dist/modules/Product/index.d.ts +1 -1
- package/dist/modules/Rules/index.d.ts +3 -8
- package/dist/modules/Rules/index.js +28 -158
- package/dist/modules/Rules/types.d.ts +0 -1
- package/dist/modules/Summary/types.d.ts +0 -2
- package/dist/modules/Summary/utils.d.ts +0 -6
- package/dist/modules/Summary/utils.js +0 -21
- package/dist/plugins/window.d.ts +0 -2
- package/dist/solution/BookingByStep/index.d.ts +0 -1
- package/dist/solution/BookingTicket/index.d.ts +1 -1
- package/dist/solution/Checkout/index.js +0 -2
- package/dist/solution/ShopDiscount/index.d.ts +0 -1
- package/dist/solution/ShopDiscount/index.js +1 -11
- package/dist/solution/ShopDiscount/types.d.ts +0 -1
- package/dist/solution/ShopDiscount/utils.js +11 -26
- package/lib/index.d.ts +0 -1
- package/lib/index.js +1 -3
- package/lib/modules/Cart/types.d.ts +0 -2
- package/lib/modules/Cart/utils/cartProduct.js +0 -9
- package/lib/modules/Customer/index.js +1 -1
- package/lib/modules/Discount/types.d.ts +0 -15
- package/lib/modules/Payment/index.d.ts +1 -2
- package/lib/modules/Payment/index.js +0 -1
- package/lib/modules/Payment/utils.js +0 -3
- package/lib/modules/Payment/walletpass.d.ts +0 -23
- package/lib/modules/Payment/walletpass.js +17 -94
- package/lib/modules/Product/index.d.ts +1 -1
- package/lib/modules/Rules/index.d.ts +3 -8
- package/lib/modules/Rules/index.js +191 -373
- package/lib/modules/Rules/types.d.ts +0 -1
- package/lib/modules/Summary/types.d.ts +0 -2
- package/lib/modules/Summary/utils.d.ts +0 -6
- package/lib/modules/Summary/utils.js +0 -15
- package/lib/plugins/window.d.ts +0 -2
- package/lib/solution/BookingByStep/index.d.ts +0 -1
- package/lib/solution/BookingTicket/index.d.ts +1 -1
- package/lib/solution/BookingTicket/index.js +6 -0
- package/lib/solution/Checkout/index.js +0 -2
- package/lib/solution/ShopDiscount/index.d.ts +0 -1
- package/lib/solution/ShopDiscount/index.js +0 -6
- package/lib/solution/ShopDiscount/types.d.ts +0 -1
- package/lib/solution/ShopDiscount/utils.js +6 -10
- package/package.json +1 -1
- package/dist/model/index.d.ts +0 -1
- package/dist/model/index.js +0 -1
- package/dist/model/strategy/adapter/index.d.ts +0 -3
- package/dist/model/strategy/adapter/index.js +0 -4
- package/dist/model/strategy/adapter/type.d.ts +0 -28
- package/dist/model/strategy/adapter/type.js +0 -1
- package/dist/model/strategy/adapter/walletPass/evaluator.d.ts +0 -84
- package/dist/model/strategy/adapter/walletPass/evaluator.js +0 -496
- package/dist/model/strategy/adapter/walletPass/example.d.ts +0 -4
- package/dist/model/strategy/adapter/walletPass/example.js +0 -258
- package/dist/model/strategy/adapter/walletPass/index.d.ts +0 -32
- package/dist/model/strategy/adapter/walletPass/index.js +0 -182
- package/dist/model/strategy/adapter/walletPass/locales.d.ts +0 -1
- package/dist/model/strategy/adapter/walletPass/locales.js +0 -23
- package/dist/model/strategy/adapter/walletPass/type.d.ts +0 -163
- package/dist/model/strategy/adapter/walletPass/type.js +0 -1
- package/dist/model/strategy/adapter/walletPass/utils.d.ts +0 -50
- package/dist/model/strategy/adapter/walletPass/utils.js +0 -965
- package/dist/model/strategy/index.d.ts +0 -94
- package/dist/model/strategy/index.js +0 -551
- package/dist/model/strategy/strategy-example.d.ts +0 -5
- package/dist/model/strategy/strategy-example.js +0 -331
- package/dist/model/strategy/type.d.ts +0 -228
- package/dist/model/strategy/type.js +0 -94
- package/lib/model/index.d.ts +0 -1
- package/lib/model/index.js +0 -23
- package/lib/model/strategy/adapter/index.d.ts +0 -3
- package/lib/model/strategy/adapter/index.js +0 -45
- package/lib/model/strategy/adapter/type.d.ts +0 -28
- package/lib/model/strategy/adapter/type.js +0 -17
- package/lib/model/strategy/adapter/walletPass/evaluator.d.ts +0 -84
- package/lib/model/strategy/adapter/walletPass/evaluator.js +0 -420
- package/lib/model/strategy/adapter/walletPass/example.d.ts +0 -4
- package/lib/model/strategy/adapter/walletPass/example.js +0 -207
- package/lib/model/strategy/adapter/walletPass/index.d.ts +0 -32
- package/lib/model/strategy/adapter/walletPass/index.js +0 -142
- package/lib/model/strategy/adapter/walletPass/locales.d.ts +0 -1
- package/lib/model/strategy/adapter/walletPass/locales.js +0 -51
- package/lib/model/strategy/adapter/walletPass/type.d.ts +0 -163
- package/lib/model/strategy/adapter/walletPass/type.js +0 -17
- package/lib/model/strategy/adapter/walletPass/utils.d.ts +0 -50
- package/lib/model/strategy/adapter/walletPass/utils.js +0 -660
- package/lib/model/strategy/index.d.ts +0 -94
- package/lib/model/strategy/index.js +0 -413
- package/lib/model/strategy/strategy-example.d.ts +0 -5
- package/lib/model/strategy/strategy-example.js +0 -318
- package/lib/model/strategy/type.d.ts +0 -228
- package/lib/model/strategy/type.js +0 -44
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
import { StrategyConfig, RuntimeContext, EvaluationResult, EngineOptions, OperatorHandler } from './type';
|
|
2
|
-
/**
|
|
3
|
-
* 策略模型 - 核心实现
|
|
4
|
-
*
|
|
5
|
-
* 完全业务无关的通用策略引擎
|
|
6
|
-
* 支持条件评估、动作匹配、结果返回
|
|
7
|
-
*/
|
|
8
|
-
/**
|
|
9
|
-
* 策略引擎
|
|
10
|
-
*
|
|
11
|
-
* 核心职责:
|
|
12
|
-
* 1. 递归评估条件组
|
|
13
|
-
* 2. 收集满足条件的 actionIds
|
|
14
|
-
* 3. 根据 actionIds 获取 ActionEffect 对象
|
|
15
|
-
* 4. 按 priority 排序返回
|
|
16
|
-
*/
|
|
17
|
-
export declare class StrategyEngine {
|
|
18
|
-
private options;
|
|
19
|
-
private operatorHandlers;
|
|
20
|
-
constructor(options?: EngineOptions);
|
|
21
|
-
/**
|
|
22
|
-
* 评估策略
|
|
23
|
-
*/
|
|
24
|
-
evaluate(config: StrategyConfig, context: RuntimeContext): EvaluationResult;
|
|
25
|
-
/**
|
|
26
|
-
* 递归评估条件组
|
|
27
|
-
*
|
|
28
|
-
* @param group 条件组
|
|
29
|
-
* @param context 运行时上下文
|
|
30
|
-
* @returns 评估结果对象,包含条件是否满足和收集到的 actionIds
|
|
31
|
-
*/
|
|
32
|
-
private evaluateConditionGroup;
|
|
33
|
-
/**
|
|
34
|
-
* 评估条件组的逻辑运算
|
|
35
|
-
*/
|
|
36
|
-
private evaluateGroupLogic;
|
|
37
|
-
/**
|
|
38
|
-
* 评估单个规则
|
|
39
|
-
*/
|
|
40
|
-
private evaluateRule;
|
|
41
|
-
/**
|
|
42
|
-
* 评估代码模式条件
|
|
43
|
-
*/
|
|
44
|
-
private evaluateCodeCondition;
|
|
45
|
-
/**
|
|
46
|
-
* 评估运算符模式条件
|
|
47
|
-
*/
|
|
48
|
-
private evaluateOperatorCondition;
|
|
49
|
-
/**
|
|
50
|
-
* 获取字段值
|
|
51
|
-
*/
|
|
52
|
-
private getFieldValue;
|
|
53
|
-
/**
|
|
54
|
-
* 评估内置运算符
|
|
55
|
-
*/
|
|
56
|
-
private evaluateBuiltInOperator;
|
|
57
|
-
/**
|
|
58
|
-
* 根据 actionIds 获取 ActionEffect 对象
|
|
59
|
-
*/
|
|
60
|
-
private getActionsByIds;
|
|
61
|
-
/**
|
|
62
|
-
* 按 priority 排序(降序)
|
|
63
|
-
*/
|
|
64
|
-
private sortActionsByPriority;
|
|
65
|
-
/**
|
|
66
|
-
* 判断是否为条件组
|
|
67
|
-
*/
|
|
68
|
-
private isConditionGroup;
|
|
69
|
-
/**
|
|
70
|
-
* 验证配置
|
|
71
|
-
*/
|
|
72
|
-
private validateConfig;
|
|
73
|
-
/**
|
|
74
|
-
* 初始化内置运算符处理器
|
|
75
|
-
*/
|
|
76
|
-
private initializeBuiltInOperators;
|
|
77
|
-
/**
|
|
78
|
-
* 注册自定义运算符
|
|
79
|
-
*/
|
|
80
|
-
registerOperator(operator: string, handler: OperatorHandler): void;
|
|
81
|
-
/**
|
|
82
|
-
* 获取调试信息
|
|
83
|
-
*/
|
|
84
|
-
getDebugInfo(): Record<string, any>;
|
|
85
|
-
}
|
|
86
|
-
export * from './adapter';
|
|
87
|
-
/**
|
|
88
|
-
* 创建策略引擎实例
|
|
89
|
-
*/
|
|
90
|
-
export declare function createStrategyEngine(options?: EngineOptions): StrategyEngine;
|
|
91
|
-
/**
|
|
92
|
-
* 快速评估(使用默认引擎配置)
|
|
93
|
-
*/
|
|
94
|
-
export declare function evaluate(config: StrategyConfig, context: RuntimeContext): EvaluationResult;
|
|
@@ -1,551 +0,0 @@
|
|
|
1
|
-
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
-
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
3
|
-
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
4
|
-
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
5
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
6
|
-
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
7
|
-
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
8
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
9
|
-
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
|
10
|
-
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
|
|
11
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
12
|
-
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
13
|
-
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
14
|
-
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
15
|
-
import { SUCCESS_CODES, NOT_APPLICABLE_CODES, ERROR_CODES } from "./type";
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* 策略模型 - 核心实现
|
|
19
|
-
*
|
|
20
|
-
* 完全业务无关的通用策略引擎
|
|
21
|
-
* 支持条件评估、动作匹配、结果返回
|
|
22
|
-
*/
|
|
23
|
-
|
|
24
|
-
// ============================================
|
|
25
|
-
// 策略引擎实现
|
|
26
|
-
// ============================================
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* 策略引擎
|
|
30
|
-
*
|
|
31
|
-
* 核心职责:
|
|
32
|
-
* 1. 递归评估条件组
|
|
33
|
-
* 2. 收集满足条件的 actionIds
|
|
34
|
-
* 3. 根据 actionIds 获取 ActionEffect 对象
|
|
35
|
-
* 4. 按 priority 排序返回
|
|
36
|
-
*/
|
|
37
|
-
export var StrategyEngine = /*#__PURE__*/function () {
|
|
38
|
-
function StrategyEngine() {
|
|
39
|
-
var _options$debug, _options$enableTrace, _options$operatorHand, _options$errorHandler;
|
|
40
|
-
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
41
|
-
_classCallCheck(this, StrategyEngine);
|
|
42
|
-
_defineProperty(this, "options", void 0);
|
|
43
|
-
_defineProperty(this, "operatorHandlers", void 0);
|
|
44
|
-
this.options = {
|
|
45
|
-
debug: (_options$debug = options.debug) !== null && _options$debug !== void 0 ? _options$debug : false,
|
|
46
|
-
enableTrace: (_options$enableTrace = options.enableTrace) !== null && _options$enableTrace !== void 0 ? _options$enableTrace : false,
|
|
47
|
-
operatorHandlers: (_options$operatorHand = options.operatorHandlers) !== null && _options$operatorHand !== void 0 ? _options$operatorHand : {},
|
|
48
|
-
errorHandler: (_options$errorHandler = options.errorHandler) !== null && _options$errorHandler !== void 0 ? _options$errorHandler : function (error) {
|
|
49
|
-
return console.error(error);
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// 初始化内置运算符处理器
|
|
54
|
-
this.operatorHandlers = new Map(Object.entries(this.options.operatorHandlers));
|
|
55
|
-
this.initializeBuiltInOperators();
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* 评估策略
|
|
60
|
-
*/
|
|
61
|
-
_createClass(StrategyEngine, [{
|
|
62
|
-
key: "evaluate",
|
|
63
|
-
value: function evaluate(config, context) {
|
|
64
|
-
var startTime = Date.now();
|
|
65
|
-
var trace = {
|
|
66
|
-
steps: [],
|
|
67
|
-
duration: 0,
|
|
68
|
-
errors: []
|
|
69
|
-
};
|
|
70
|
-
try {
|
|
71
|
-
// 验证配置
|
|
72
|
-
this.validateConfig(config);
|
|
73
|
-
if (this.options.enableTrace) {
|
|
74
|
-
trace.steps.push({
|
|
75
|
-
step: 'validate_config',
|
|
76
|
-
status: 'success',
|
|
77
|
-
duration: Date.now() - startTime
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
// 递归评估条件组,收集 actionIds
|
|
82
|
-
var evaluateStart = Date.now();
|
|
83
|
-
var evaluationResult = this.evaluateConditionGroup(config.conditions, context);
|
|
84
|
-
if (this.options.enableTrace) {
|
|
85
|
-
trace.steps.push({
|
|
86
|
-
step: 'evaluate_conditions',
|
|
87
|
-
status: 'success',
|
|
88
|
-
duration: Date.now() - evaluateStart,
|
|
89
|
-
details: {
|
|
90
|
-
satisfied: evaluationResult.satisfied,
|
|
91
|
-
collectedActionIds: evaluationResult.actionIds
|
|
92
|
-
}
|
|
93
|
-
});
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
// 根据 actionIds 获取 ActionEffect 对象
|
|
97
|
-
var actionStart = Date.now();
|
|
98
|
-
var matchedActions = this.getActionsByIds(evaluationResult.actionIds, config.actions);
|
|
99
|
-
if (this.options.enableTrace) {
|
|
100
|
-
trace.steps.push({
|
|
101
|
-
step: 'get_actions',
|
|
102
|
-
status: 'success',
|
|
103
|
-
duration: Date.now() - actionStart,
|
|
104
|
-
details: {
|
|
105
|
-
matchedCount: matchedActions.length
|
|
106
|
-
}
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
// 按 priority 排序(降序)
|
|
111
|
-
var sortStart = Date.now();
|
|
112
|
-
var sortedActions = this.sortActionsByPriority(matchedActions);
|
|
113
|
-
if (this.options.enableTrace) {
|
|
114
|
-
trace.steps.push({
|
|
115
|
-
step: 'sort_actions',
|
|
116
|
-
status: 'success',
|
|
117
|
-
duration: Date.now() - sortStart
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
// 构建结果 - applicable 基于条件是否满足,而不是 actionIds 数量
|
|
122
|
-
var applicable = evaluationResult.satisfied;
|
|
123
|
-
var result = {
|
|
124
|
-
success: true,
|
|
125
|
-
applicable: applicable,
|
|
126
|
-
code: applicable ? SUCCESS_CODES.SUCCESS : NOT_APPLICABLE_CODES.CONDITION_NOT_MET,
|
|
127
|
-
message: applicable ? 'Strategy is applicable' : 'Conditions not met',
|
|
128
|
-
matched: {
|
|
129
|
-
conditions: applicable,
|
|
130
|
-
actionIds: evaluationResult.actionIds,
|
|
131
|
-
details: {}
|
|
132
|
-
},
|
|
133
|
-
matchedActions: sortedActions,
|
|
134
|
-
outputs: {},
|
|
135
|
-
config: config
|
|
136
|
-
};
|
|
137
|
-
trace.duration = Date.now() - startTime;
|
|
138
|
-
if (this.options.enableTrace) {
|
|
139
|
-
result.trace = trace;
|
|
140
|
-
}
|
|
141
|
-
return result;
|
|
142
|
-
} catch (error) {
|
|
143
|
-
var errorMessage = error instanceof Error ? error.message : 'Unknown error';
|
|
144
|
-
if (this.options.enableTrace) {
|
|
145
|
-
var _trace$errors;
|
|
146
|
-
(_trace$errors = trace.errors) === null || _trace$errors === void 0 || _trace$errors.push({
|
|
147
|
-
step: 'evaluation',
|
|
148
|
-
error: errorMessage,
|
|
149
|
-
timestamp: Date.now()
|
|
150
|
-
});
|
|
151
|
-
trace.duration = Date.now() - startTime;
|
|
152
|
-
}
|
|
153
|
-
this.options.errorHandler(error, context);
|
|
154
|
-
return {
|
|
155
|
-
success: false,
|
|
156
|
-
applicable: false,
|
|
157
|
-
code: ERROR_CODES.EVALUATION_ERROR,
|
|
158
|
-
message: errorMessage,
|
|
159
|
-
matched: {
|
|
160
|
-
conditions: false,
|
|
161
|
-
actionIds: [],
|
|
162
|
-
details: {}
|
|
163
|
-
},
|
|
164
|
-
matchedActions: [],
|
|
165
|
-
outputs: {},
|
|
166
|
-
trace: this.options.enableTrace ? trace : undefined,
|
|
167
|
-
config: config
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
/**
|
|
173
|
-
* 递归评估条件组
|
|
174
|
-
*
|
|
175
|
-
* @param group 条件组
|
|
176
|
-
* @param context 运行时上下文
|
|
177
|
-
* @returns 评估结果对象,包含条件是否满足和收集到的 actionIds
|
|
178
|
-
*/
|
|
179
|
-
}, {
|
|
180
|
-
key: "evaluateConditionGroup",
|
|
181
|
-
value: function evaluateConditionGroup(group, context) {
|
|
182
|
-
var collectedActionIds = [];
|
|
183
|
-
|
|
184
|
-
// 评估当前层的 operator 和 rules
|
|
185
|
-
var isCurrentLayerSatisfied = this.evaluateGroupLogic(group, context);
|
|
186
|
-
|
|
187
|
-
// 如果当前层条件满足
|
|
188
|
-
if (isCurrentLayerSatisfied) {
|
|
189
|
-
// 收集当前层的 actionIds
|
|
190
|
-
collectedActionIds.push.apply(collectedActionIds, _toConsumableArray(group.actionIds));
|
|
191
|
-
|
|
192
|
-
// 递归评估嵌套的 ConditionGroup,收集嵌套层的 actionIds
|
|
193
|
-
var _iterator = _createForOfIteratorHelper(group.rules),
|
|
194
|
-
_step;
|
|
195
|
-
try {
|
|
196
|
-
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
197
|
-
var rule = _step.value;
|
|
198
|
-
if (this.isConditionGroup(rule)) {
|
|
199
|
-
var nestedResult = this.evaluateConditionGroup(rule, context);
|
|
200
|
-
collectedActionIds.push.apply(collectedActionIds, _toConsumableArray(nestedResult.actionIds));
|
|
201
|
-
}
|
|
202
|
-
}
|
|
203
|
-
} catch (err) {
|
|
204
|
-
_iterator.e(err);
|
|
205
|
-
} finally {
|
|
206
|
-
_iterator.f();
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
return {
|
|
210
|
-
satisfied: isCurrentLayerSatisfied,
|
|
211
|
-
actionIds: collectedActionIds
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* 评估条件组的逻辑运算
|
|
217
|
-
*/
|
|
218
|
-
}, {
|
|
219
|
-
key: "evaluateGroupLogic",
|
|
220
|
-
value: function evaluateGroupLogic(group, context) {
|
|
221
|
-
var _this = this;
|
|
222
|
-
var operator = group.operator,
|
|
223
|
-
rules = group.rules;
|
|
224
|
-
switch (operator) {
|
|
225
|
-
case 'and':
|
|
226
|
-
// 所有规则都必须满足
|
|
227
|
-
return rules.every(function (rule) {
|
|
228
|
-
return _this.evaluateRule(rule, context);
|
|
229
|
-
});
|
|
230
|
-
case 'or':
|
|
231
|
-
// 任一规则满足即可
|
|
232
|
-
return rules.some(function (rule) {
|
|
233
|
-
return _this.evaluateRule(rule, context);
|
|
234
|
-
});
|
|
235
|
-
case 'not':
|
|
236
|
-
// 对第一个规则取反
|
|
237
|
-
if (rules.length === 0) return false;
|
|
238
|
-
return !this.evaluateRule(rules[0], context);
|
|
239
|
-
default:
|
|
240
|
-
throw new Error("Unknown operator: ".concat(operator));
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
|
|
244
|
-
/**
|
|
245
|
-
* 评估单个规则
|
|
246
|
-
*/
|
|
247
|
-
}, {
|
|
248
|
-
key: "evaluateRule",
|
|
249
|
-
value: function evaluateRule(rule, context) {
|
|
250
|
-
// 如果是嵌套的条件组,递归评估
|
|
251
|
-
if (this.isConditionGroup(rule)) {
|
|
252
|
-
return this.evaluateGroupLogic(rule, context);
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
// 原子条件规则
|
|
256
|
-
var conditionRule = rule;
|
|
257
|
-
|
|
258
|
-
// Code 模式:使用 eval 执行
|
|
259
|
-
if (conditionRule.type === 'code' && conditionRule.code) {
|
|
260
|
-
return this.evaluateCodeCondition(conditionRule.code, context);
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// Operator 模式:标准运算符判断
|
|
264
|
-
return this.evaluateOperatorCondition(conditionRule, context);
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* 评估代码模式条件
|
|
269
|
-
*/
|
|
270
|
-
}, {
|
|
271
|
-
key: "evaluateCodeCondition",
|
|
272
|
-
value: function evaluateCodeCondition(code, context) {
|
|
273
|
-
try {
|
|
274
|
-
// 将 context 解构到作用域中,方便代码直接使用
|
|
275
|
-
var entities = context.entities,
|
|
276
|
-
attributes = context.attributes,
|
|
277
|
-
metadata = context.metadata;
|
|
278
|
-
|
|
279
|
-
// 使用 Function 构造器代替 eval,更安全
|
|
280
|
-
var evalFunc = new Function('entities', 'attributes', 'metadata', "return (".concat(code, ")"));
|
|
281
|
-
var result = evalFunc(entities, attributes, metadata);
|
|
282
|
-
return Boolean(result);
|
|
283
|
-
} catch (error) {
|
|
284
|
-
if (this.options.debug) {
|
|
285
|
-
console.error('Code evaluation error:', error);
|
|
286
|
-
}
|
|
287
|
-
return false;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
/**
|
|
292
|
-
* 评估运算符模式条件
|
|
293
|
-
*/
|
|
294
|
-
}, {
|
|
295
|
-
key: "evaluateOperatorCondition",
|
|
296
|
-
value: function evaluateOperatorCondition(rule, context) {
|
|
297
|
-
var field = rule.field,
|
|
298
|
-
operator = rule.operator,
|
|
299
|
-
value = rule.value;
|
|
300
|
-
if (!field || !operator) {
|
|
301
|
-
return false;
|
|
302
|
-
}
|
|
303
|
-
|
|
304
|
-
// 从 context.attributes 中获取字段值
|
|
305
|
-
var fieldValue = this.getFieldValue(field, context);
|
|
306
|
-
|
|
307
|
-
// 使用运算符处理器
|
|
308
|
-
var handler = this.operatorHandlers.get(operator);
|
|
309
|
-
if (handler) {
|
|
310
|
-
return handler(fieldValue, value, rule);
|
|
311
|
-
}
|
|
312
|
-
|
|
313
|
-
// 回退到内置运算符
|
|
314
|
-
return this.evaluateBuiltInOperator(fieldValue, operator, value);
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
/**
|
|
318
|
-
* 获取字段值
|
|
319
|
-
*/
|
|
320
|
-
}, {
|
|
321
|
-
key: "getFieldValue",
|
|
322
|
-
value: function getFieldValue(field, context) {
|
|
323
|
-
// 支持点号路径,如 "order.total"
|
|
324
|
-
var path = field.split('.');
|
|
325
|
-
var value = context.attributes;
|
|
326
|
-
var _iterator2 = _createForOfIteratorHelper(path),
|
|
327
|
-
_step2;
|
|
328
|
-
try {
|
|
329
|
-
for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
|
|
330
|
-
var key = _step2.value;
|
|
331
|
-
if (value && _typeof(value) === 'object' && key in value) {
|
|
332
|
-
value = value[key];
|
|
333
|
-
} else {
|
|
334
|
-
return undefined;
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
} catch (err) {
|
|
338
|
-
_iterator2.e(err);
|
|
339
|
-
} finally {
|
|
340
|
-
_iterator2.f();
|
|
341
|
-
}
|
|
342
|
-
return value;
|
|
343
|
-
}
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
* 评估内置运算符
|
|
347
|
-
*/
|
|
348
|
-
}, {
|
|
349
|
-
key: "evaluateBuiltInOperator",
|
|
350
|
-
value: function evaluateBuiltInOperator(fieldValue, operator, compareValue) {
|
|
351
|
-
switch (operator) {
|
|
352
|
-
// 比较运算符
|
|
353
|
-
case '=':
|
|
354
|
-
case '==':
|
|
355
|
-
return fieldValue == compareValue;
|
|
356
|
-
case '!=':
|
|
357
|
-
return fieldValue != compareValue;
|
|
358
|
-
case '>':
|
|
359
|
-
return fieldValue > compareValue;
|
|
360
|
-
case '>=':
|
|
361
|
-
return fieldValue >= compareValue;
|
|
362
|
-
case '<':
|
|
363
|
-
return fieldValue < compareValue;
|
|
364
|
-
case '<=':
|
|
365
|
-
return fieldValue <= compareValue;
|
|
366
|
-
|
|
367
|
-
// 集合运算符
|
|
368
|
-
case 'in':
|
|
369
|
-
return Array.isArray(compareValue) && compareValue.includes(fieldValue);
|
|
370
|
-
case 'not_in':
|
|
371
|
-
return Array.isArray(compareValue) && !compareValue.includes(fieldValue);
|
|
372
|
-
case 'contains':
|
|
373
|
-
if (Array.isArray(fieldValue)) {
|
|
374
|
-
return fieldValue.includes(compareValue);
|
|
375
|
-
}
|
|
376
|
-
if (typeof fieldValue === 'string') {
|
|
377
|
-
return fieldValue.includes(compareValue);
|
|
378
|
-
}
|
|
379
|
-
return false;
|
|
380
|
-
case 'not_contains':
|
|
381
|
-
if (Array.isArray(fieldValue)) {
|
|
382
|
-
return !fieldValue.includes(compareValue);
|
|
383
|
-
}
|
|
384
|
-
if (typeof fieldValue === 'string') {
|
|
385
|
-
return !fieldValue.includes(compareValue);
|
|
386
|
-
}
|
|
387
|
-
return true;
|
|
388
|
-
|
|
389
|
-
// 字符串运算符
|
|
390
|
-
case 'starts_with':
|
|
391
|
-
return typeof fieldValue === 'string' && fieldValue.startsWith(compareValue);
|
|
392
|
-
case 'ends_with':
|
|
393
|
-
return typeof fieldValue === 'string' && fieldValue.endsWith(compareValue);
|
|
394
|
-
case 'regex':
|
|
395
|
-
return typeof fieldValue === 'string' && new RegExp(compareValue).test(fieldValue);
|
|
396
|
-
|
|
397
|
-
// 范围运算符
|
|
398
|
-
case 'between':
|
|
399
|
-
if (!Array.isArray(compareValue) || compareValue.length !== 2) return false;
|
|
400
|
-
return fieldValue >= compareValue[0] && fieldValue <= compareValue[1];
|
|
401
|
-
|
|
402
|
-
// 逻辑运算符
|
|
403
|
-
case 'is_null':
|
|
404
|
-
return fieldValue === null || fieldValue === undefined;
|
|
405
|
-
case 'is_not_null':
|
|
406
|
-
return fieldValue !== null && fieldValue !== undefined;
|
|
407
|
-
case 'is_empty':
|
|
408
|
-
if (Array.isArray(fieldValue)) return fieldValue.length === 0;
|
|
409
|
-
if (typeof fieldValue === 'string') return fieldValue.length === 0;
|
|
410
|
-
if (_typeof(fieldValue) === 'object') return Object.keys(fieldValue).length === 0;
|
|
411
|
-
return !fieldValue;
|
|
412
|
-
case 'is_not_empty':
|
|
413
|
-
if (Array.isArray(fieldValue)) return fieldValue.length > 0;
|
|
414
|
-
if (typeof fieldValue === 'string') return fieldValue.length > 0;
|
|
415
|
-
if (_typeof(fieldValue) === 'object') return Object.keys(fieldValue).length > 0;
|
|
416
|
-
return Boolean(fieldValue);
|
|
417
|
-
default:
|
|
418
|
-
throw new Error("Unsupported operator: ".concat(operator));
|
|
419
|
-
}
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/**
|
|
423
|
-
* 根据 actionIds 获取 ActionEffect 对象
|
|
424
|
-
*/
|
|
425
|
-
}, {
|
|
426
|
-
key: "getActionsByIds",
|
|
427
|
-
value: function getActionsByIds(actionIds, actions) {
|
|
428
|
-
var matchedActions = [];
|
|
429
|
-
var actionMap = new Map(actions.map(function (action) {
|
|
430
|
-
return [action.id, action];
|
|
431
|
-
}));
|
|
432
|
-
var _iterator3 = _createForOfIteratorHelper(actionIds),
|
|
433
|
-
_step3;
|
|
434
|
-
try {
|
|
435
|
-
for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
|
|
436
|
-
var id = _step3.value;
|
|
437
|
-
var action = actionMap.get(id);
|
|
438
|
-
if (action) {
|
|
439
|
-
matchedActions.push(action);
|
|
440
|
-
} else {
|
|
441
|
-
// 找不到的 ID 记录警告
|
|
442
|
-
if (this.options.debug) {
|
|
443
|
-
console.warn("Action with id \"".concat(id, "\" not found in actions array"));
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
}
|
|
447
|
-
} catch (err) {
|
|
448
|
-
_iterator3.e(err);
|
|
449
|
-
} finally {
|
|
450
|
-
_iterator3.f();
|
|
451
|
-
}
|
|
452
|
-
return matchedActions;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* 按 priority 排序(降序)
|
|
457
|
-
*/
|
|
458
|
-
}, {
|
|
459
|
-
key: "sortActionsByPriority",
|
|
460
|
-
value: function sortActionsByPriority(actions) {
|
|
461
|
-
return _toConsumableArray(actions).sort(function (a, b) {
|
|
462
|
-
var _a$priority, _b$priority;
|
|
463
|
-
var priorityA = (_a$priority = a.priority) !== null && _a$priority !== void 0 ? _a$priority : 0;
|
|
464
|
-
var priorityB = (_b$priority = b.priority) !== null && _b$priority !== void 0 ? _b$priority : 0;
|
|
465
|
-
return priorityB - priorityA; // 降序
|
|
466
|
-
});
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
/**
|
|
470
|
-
* 判断是否为条件组
|
|
471
|
-
*/
|
|
472
|
-
}, {
|
|
473
|
-
key: "isConditionGroup",
|
|
474
|
-
value: function isConditionGroup(rule) {
|
|
475
|
-
return 'operator' in rule && 'rules' in rule && Array.isArray(rule.rules);
|
|
476
|
-
}
|
|
477
|
-
|
|
478
|
-
/**
|
|
479
|
-
* 验证配置
|
|
480
|
-
*/
|
|
481
|
-
}, {
|
|
482
|
-
key: "validateConfig",
|
|
483
|
-
value: function validateConfig(config) {
|
|
484
|
-
if (!config) {
|
|
485
|
-
throw new Error('Strategy config is required');
|
|
486
|
-
}
|
|
487
|
-
if (!config.metadata || !config.metadata.id) {
|
|
488
|
-
throw new Error('Strategy metadata.id is required');
|
|
489
|
-
}
|
|
490
|
-
if (!config.conditions) {
|
|
491
|
-
throw new Error('Strategy conditions is required');
|
|
492
|
-
}
|
|
493
|
-
if (!Array.isArray(config.actions)) {
|
|
494
|
-
throw new Error('Strategy actions must be an array');
|
|
495
|
-
}
|
|
496
|
-
}
|
|
497
|
-
|
|
498
|
-
/**
|
|
499
|
-
* 初始化内置运算符处理器
|
|
500
|
-
*/
|
|
501
|
-
}, {
|
|
502
|
-
key: "initializeBuiltInOperators",
|
|
503
|
-
value: function initializeBuiltInOperators() {
|
|
504
|
-
// 内置运算符已经在 evaluateBuiltInOperator 中实现
|
|
505
|
-
// 这里可以添加自定义的运算符处理器
|
|
506
|
-
}
|
|
507
|
-
|
|
508
|
-
/**
|
|
509
|
-
* 注册自定义运算符
|
|
510
|
-
*/
|
|
511
|
-
}, {
|
|
512
|
-
key: "registerOperator",
|
|
513
|
-
value: function registerOperator(operator, handler) {
|
|
514
|
-
this.operatorHandlers.set(operator, handler);
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* 获取调试信息
|
|
519
|
-
*/
|
|
520
|
-
}, {
|
|
521
|
-
key: "getDebugInfo",
|
|
522
|
-
value: function getDebugInfo() {
|
|
523
|
-
return {
|
|
524
|
-
debug: this.options.debug,
|
|
525
|
-
enableTrace: this.options.enableTrace,
|
|
526
|
-
registeredOperators: Array.from(this.operatorHandlers.keys())
|
|
527
|
-
};
|
|
528
|
-
}
|
|
529
|
-
}]);
|
|
530
|
-
return StrategyEngine;
|
|
531
|
-
}();
|
|
532
|
-
export * from "./adapter";
|
|
533
|
-
|
|
534
|
-
// ============================================
|
|
535
|
-
// 导出工具函数
|
|
536
|
-
// ============================================
|
|
537
|
-
|
|
538
|
-
/**
|
|
539
|
-
* 创建策略引擎实例
|
|
540
|
-
*/
|
|
541
|
-
export function createStrategyEngine(options) {
|
|
542
|
-
return new StrategyEngine(options);
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
/**
|
|
546
|
-
* 快速评估(使用默认引擎配置)
|
|
547
|
-
*/
|
|
548
|
-
export function evaluate(config, context) {
|
|
549
|
-
var engine = new StrategyEngine();
|
|
550
|
-
return engine.evaluate(config, context);
|
|
551
|
-
}
|