@tbox.cn/app-provider-mock 0.2.0 → 0.4.0
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/README.md +38 -0
- package/dist/server/index.d.ts +38 -64
- package/dist/server/index.js +269 -136
- package/package.json +6 -5
- package/schemas/mall-info.config.json +11 -0
- package/schemas/member.config.json +13 -0
- package/schemas/parking.config.json +19 -0
- package/schemas/promotion.config.json +13 -0
- package/schemas/shopping.config.json +12 -0
- package/src/server/config.ts +26 -0
- package/src/server/index.ts +39 -16
- package/src/server/mall-info.ts +27 -12
- package/src/server/member.ts +45 -17
- package/src/server/mock-enroll-route.ts +11 -10
- package/src/server/parking.ts +78 -49
- package/src/server/promotion.ts +77 -56
- package/src/server/shopping.ts +41 -3
- package/tbox.module.json +36 -17
- package/tests/config-schema.test.ts +92 -0
- package/tests/integrations-e2e.test.ts +164 -30
- package/tests/mall-info.test.ts +11 -2
- package/tests/mock-impl.test.ts +121 -18
package/dist/server/index.js
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
1
|
// src/server/index.ts
|
|
2
|
+
import { randomBytes } from "crypto";
|
|
2
3
|
import { readLoginMallId } from "@tbox.cn/app-contracts-mall";
|
|
3
4
|
|
|
5
|
+
// src/server/config.ts
|
|
6
|
+
function resolveConfig(raw, defaults, intKeys = []) {
|
|
7
|
+
const src = raw && typeof raw === "object" ? raw : {};
|
|
8
|
+
const out = {};
|
|
9
|
+
for (const [key, fallback] of Object.entries(defaults)) {
|
|
10
|
+
const value = src[key];
|
|
11
|
+
const ok = typeof value === typeof fallback && (typeof value !== "number" || Number.isFinite(value) && (!intKeys.includes(key) || Number.isInteger(value))) && (typeof value !== "string" || value !== "");
|
|
12
|
+
out[key] = ok ? value : fallback;
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
|
|
4
17
|
// src/server/member.ts
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
18
|
+
var MEMBER_CFG_DEFAULTS = {
|
|
19
|
+
memberName: "\u793A\u4F8B\u4F1A\u5458",
|
|
20
|
+
cardName: "\u793A\u4F8B\u4F1A\u5458\u5361",
|
|
21
|
+
levelName: "",
|
|
22
|
+
// '' 哨兵 = 不展示等级区(MemberCardSnapshot.levelName 可选位)
|
|
23
|
+
initialPoints: 300,
|
|
24
|
+
enrollUrl: "alipays://platformapi/startapp?appId=demo&page=v/index/index",
|
|
25
|
+
loginUrl: "alipays://platformapi/startapp?appId=demo&page=v/login/index"
|
|
26
|
+
};
|
|
27
|
+
var MEMBER_CFG_INT_KEYS = ["initialPoints"];
|
|
8
28
|
var mockMemberState = {
|
|
9
29
|
enrolled: /* @__PURE__ */ new Set(),
|
|
10
30
|
balances: /* @__PURE__ */ new Map(),
|
|
@@ -15,7 +35,6 @@ var MOCK_SHELL_USER_ID = "demo-user";
|
|
|
15
35
|
function enrollMockMember(userId) {
|
|
16
36
|
mockMemberState.enrolled.add(userId);
|
|
17
37
|
mockMemberState.loggedOut.delete(userId);
|
|
18
|
-
if (!mockMemberState.balances.has(userId)) mockMemberState.balances.set(userId, 300);
|
|
19
38
|
}
|
|
20
39
|
function isMockMemberEnrolled(userId) {
|
|
21
40
|
return mockMemberState.enrolled.has(userId);
|
|
@@ -40,11 +59,21 @@ var MockMemberServiceImpl = class {
|
|
|
40
59
|
* 透传素材,mock 出厂面恒配 demo 值)。demo 值厂商中立(appId=demo),点击在壳内
|
|
41
60
|
* navigateTo 失败即无害降级。
|
|
42
61
|
*/
|
|
43
|
-
enrollUrl
|
|
62
|
+
enrollUrl;
|
|
44
63
|
/** 登录页深链(同上,可选透传素材;供未登录引导卡跳转)。 */
|
|
45
|
-
loginUrl
|
|
64
|
+
loginUrl;
|
|
65
|
+
/** 配置快照(fail-soft,构造期一次解析;键集 ≡ MEMBER_CFG_DEFAULTS) */
|
|
66
|
+
cfg;
|
|
67
|
+
/** 积分流水(金额随 initialPoints 派生——与余额同源) */
|
|
68
|
+
usage;
|
|
46
69
|
/** 默认访客(无种子):demo-user 初始 not_enrolled,经入会闭环翻为 active */
|
|
47
|
-
constructor(initial = {}) {
|
|
70
|
+
constructor(initial = {}, config = {}) {
|
|
71
|
+
this.cfg = resolveConfig(config, MEMBER_CFG_DEFAULTS, MEMBER_CFG_INT_KEYS);
|
|
72
|
+
this.enrollUrl = this.cfg.enrollUrl;
|
|
73
|
+
this.loginUrl = this.cfg.loginUrl;
|
|
74
|
+
this.usage = [
|
|
75
|
+
{ usageRecordRef: "demo:usage:1", title: "\u5F00\u5361\u793C", amount: `+${String(this.cfg.initialPoints)}`, usedAt: (/* @__PURE__ */ new Date()).toISOString(), description: "mock \u6F14\u793A\u6D41\u6C34" }
|
|
76
|
+
];
|
|
48
77
|
for (const [userId, balance] of Object.entries(initial)) {
|
|
49
78
|
mockMemberState.enrolled.add(userId);
|
|
50
79
|
mockMemberState.balances.set(userId, balance);
|
|
@@ -60,44 +89,51 @@ var MockMemberServiceImpl = class {
|
|
|
60
89
|
};
|
|
61
90
|
}
|
|
62
91
|
async getProfile(_userId) {
|
|
63
|
-
return { labels: [{ labelRef: "demo:label:1", name:
|
|
92
|
+
return { labels: [{ labelRef: "demo:label:1", name: this.cfg.memberName }], observedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
64
93
|
}
|
|
65
94
|
async getCard(userId) {
|
|
66
95
|
if (!isMockMemberActive(userId)) return null;
|
|
67
96
|
return {
|
|
68
97
|
cardRef: `demo:card:${userId}`,
|
|
69
|
-
cardDisplay:
|
|
98
|
+
cardDisplay: this.cfg.cardName,
|
|
70
99
|
status: "active",
|
|
100
|
+
...this.cfg.levelName !== "" ? { levelName: this.cfg.levelName } : {},
|
|
71
101
|
observedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
72
102
|
};
|
|
73
103
|
}
|
|
74
104
|
async queryPoints(userId) {
|
|
75
|
-
return { userId, balance:
|
|
105
|
+
return { userId, balance: this.currentBalanceOf(userId), membershipLabel: this.cfg.cardName };
|
|
76
106
|
}
|
|
77
|
-
async getBenefits(
|
|
78
|
-
return { availablePoints:
|
|
107
|
+
async getBenefits(userId) {
|
|
108
|
+
return { availablePoints: this.currentBalanceOf(userId), levelName: "\u793A\u4F8B\u7B49\u7EA7", observedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
79
109
|
}
|
|
80
110
|
async listPointUsage(_userId, page, pageSize) {
|
|
81
111
|
const start = (page - 1) * pageSize;
|
|
82
|
-
return { items:
|
|
112
|
+
return { items: this.usage.slice(start, start + pageSize), total: this.usage.length };
|
|
83
113
|
}
|
|
84
114
|
async queryDiscount() {
|
|
85
115
|
return { level: "silver", discount: 0.95 };
|
|
86
116
|
}
|
|
87
117
|
async deductPoints(userId, amount, _key) {
|
|
88
|
-
const balance =
|
|
118
|
+
const balance = this.currentBalanceOf(userId);
|
|
89
119
|
if (balance < amount) return false;
|
|
90
120
|
mockMemberState.balances.set(userId, balance - amount);
|
|
91
121
|
return true;
|
|
92
122
|
}
|
|
93
123
|
async refundPoints(userId, amount, _key) {
|
|
94
|
-
mockMemberState.balances.set(userId,
|
|
124
|
+
mockMemberState.balances.set(userId, this.currentBalanceOf(userId) + amount);
|
|
95
125
|
return true;
|
|
96
126
|
}
|
|
97
127
|
async awardPoints(userId, amount) {
|
|
98
|
-
mockMemberState.balances.set(userId,
|
|
128
|
+
mockMemberState.balances.set(userId, this.currentBalanceOf(userId) + amount);
|
|
99
129
|
return true;
|
|
100
130
|
}
|
|
131
|
+
/** 余额单源:显式增减覆盖值 ??(已入会 ? 配置初值 : 0)——入会不写种子(惰性读) */
|
|
132
|
+
currentBalanceOf(userId) {
|
|
133
|
+
const override = mockMemberState.balances.get(userId);
|
|
134
|
+
if (override !== void 0) return override;
|
|
135
|
+
return isMockMemberEnrolled(userId) ? this.cfg.initialPoints : 0;
|
|
136
|
+
}
|
|
101
137
|
async enroll(input) {
|
|
102
138
|
enrollMockMember(input.userId);
|
|
103
139
|
return { status: "active", memberRef: `demo:member:${input.userId}` };
|
|
@@ -121,46 +157,76 @@ var MockMemberServiceImpl = class {
|
|
|
121
157
|
|
|
122
158
|
// src/server/parking.ts
|
|
123
159
|
var now = () => (/* @__PURE__ */ new Date()).toISOString();
|
|
124
|
-
var
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
revision: 1,
|
|
140
|
-
paymentState: "UNPAID"
|
|
141
|
-
};
|
|
142
|
-
}
|
|
160
|
+
var PARKING_CFG_DEFAULTS = {
|
|
161
|
+
parkName: "\u793A\u4F8B\u505C\u8F66\u573A",
|
|
162
|
+
feeName: "\u505C\u8F66\u8D39\uFF083 \u5C0F\u65F6\uFF09",
|
|
163
|
+
feeCents: 1500,
|
|
164
|
+
remainingSpaces: 128,
|
|
165
|
+
firstHourRate: "\u514D\u8D39",
|
|
166
|
+
memberBenefitEnabled: true,
|
|
167
|
+
memberBenefitDiscountCents: 75,
|
|
168
|
+
pointsBenefitEnabled: true,
|
|
169
|
+
invoiceEnabled: true,
|
|
170
|
+
vehicleFloor: "B1",
|
|
171
|
+
vehicleZone: "A \u533A",
|
|
172
|
+
vehicleSpaceNo: "A-128"
|
|
173
|
+
};
|
|
174
|
+
var PARKING_CFG_INT_KEYS = ["feeCents", "remainingSpaces", "memberBenefitDiscountCents"];
|
|
143
175
|
var MockParkingServiceImpl = class {
|
|
144
176
|
vehicles = /* @__PURE__ */ new Map();
|
|
145
|
-
records
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
amountCents: 1500,
|
|
150
|
-
paidAt: now(),
|
|
151
|
-
status: "PAID",
|
|
152
|
-
parkName: "\u793A\u4F8B\u505C\u8F66\u573A",
|
|
153
|
-
orderCompleted: true
|
|
154
|
-
}
|
|
155
|
-
];
|
|
156
|
-
constructor(mallId = "mall-demo") {
|
|
177
|
+
records;
|
|
178
|
+
/** 配置快照(fail-soft,构造期一次解析;键集 ≡ PARKING_CFG_DEFAULTS) */
|
|
179
|
+
cfg;
|
|
180
|
+
constructor(mallId = "mall-demo", config = {}) {
|
|
157
181
|
this.mallId = mallId;
|
|
158
182
|
void this.mallId;
|
|
183
|
+
this.cfg = resolveConfig(config, PARKING_CFG_DEFAULTS, PARKING_CFG_INT_KEYS);
|
|
184
|
+
this.records = [
|
|
185
|
+
{
|
|
186
|
+
recordRef: "demo:record:1",
|
|
187
|
+
plateNo: "demo-plate",
|
|
188
|
+
amountCents: this.cfg.feeCents,
|
|
189
|
+
paidAt: now(),
|
|
190
|
+
status: "PAID",
|
|
191
|
+
parkName: this.cfg.parkName,
|
|
192
|
+
orderCompleted: true
|
|
193
|
+
}
|
|
194
|
+
];
|
|
159
195
|
}
|
|
160
196
|
mallId;
|
|
197
|
+
/**
|
|
198
|
+
* kind 必须下发:客户端按它分流(discount = 自动生效、不进勾选区;points = 积分入口),
|
|
199
|
+
* 服务端 projectPaymentOptions 也只认 kind==='points'。缺省时整套分流静默失效——
|
|
200
|
+
* 等级权益会退化成一个可勾选项,积分入口则从不出现。
|
|
201
|
+
*/
|
|
202
|
+
benefits() {
|
|
203
|
+
const benefits = [];
|
|
204
|
+
if (this.cfg.memberBenefitEnabled) {
|
|
205
|
+
benefits.push({ benefitRef: "demo:benefit:member-discount", title: "\u4F1A\u5458 95 \u6298", description: "mock \u6F14\u793A\u6743\u76CA", kind: "discount", autoSelected: true, estimatedDiscountCents: this.cfg.memberBenefitDiscountCents });
|
|
206
|
+
}
|
|
207
|
+
if (this.cfg.pointsBenefitEnabled) {
|
|
208
|
+
benefits.push({ benefitRef: "demo:benefit:points-offset", title: "\u79EF\u5206\u62B5 5 \u5143", description: "mock \u6F14\u793A\u6743\u76CA", kind: "points", autoSelected: false, estimatedDiscountCents: 500, requiredPoints: 500, previewPayableCents: 1e3 });
|
|
209
|
+
}
|
|
210
|
+
return benefits;
|
|
211
|
+
}
|
|
212
|
+
quote(q, plateNo) {
|
|
213
|
+
return {
|
|
214
|
+
quoteRef: `demo:quote:${plateNo}`,
|
|
215
|
+
plateNo,
|
|
216
|
+
mallId: q.scope.mallId ?? "demo",
|
|
217
|
+
entryAt: now(),
|
|
218
|
+
feeItems: [{ name: this.cfg.feeName, amountCents: this.cfg.feeCents }],
|
|
219
|
+
totalCents: this.cfg.feeCents,
|
|
220
|
+
discountCents: 0,
|
|
221
|
+
payableCents: this.cfg.feeCents,
|
|
222
|
+
appliedBenefits: [],
|
|
223
|
+
revision: 1,
|
|
224
|
+
paymentState: "UNPAID"
|
|
225
|
+
};
|
|
226
|
+
}
|
|
161
227
|
async queryFee(q, input) {
|
|
162
228
|
const plateNo = input.plateNo ?? (input.vehicleRef ? this.vehicles.get(input.vehicleRef)?.plateNo : void 0) ?? "demo-plate";
|
|
163
|
-
const quote =
|
|
229
|
+
const quote = this.quote(q, plateNo);
|
|
164
230
|
return quote ? [quote] : [];
|
|
165
231
|
}
|
|
166
232
|
async listVehicles() {
|
|
@@ -178,7 +244,7 @@ var MockParkingServiceImpl = class {
|
|
|
178
244
|
return record;
|
|
179
245
|
}
|
|
180
246
|
async getInvoiceSettings() {
|
|
181
|
-
return { parkingInvoiceEnabled:
|
|
247
|
+
return { parkingInvoiceEnabled: this.cfg.invoiceEnabled };
|
|
182
248
|
}
|
|
183
249
|
async listInvoiceableOrders(_q, page, pageSize) {
|
|
184
250
|
const all = this.records.filter((r) => r.orderCompleted === true);
|
|
@@ -188,26 +254,26 @@ var MockParkingServiceImpl = class {
|
|
|
188
254
|
async queryParkingInfo(_q, kind) {
|
|
189
255
|
return {
|
|
190
256
|
kind,
|
|
191
|
-
title:
|
|
257
|
+
title: this.cfg.parkName,
|
|
192
258
|
entries: [
|
|
193
|
-
{ label: "\u5269\u4F59\u8F66\u4F4D", value:
|
|
194
|
-
{ label: "\u9996\u5C0F\u65F6\u8D39\u7387", value:
|
|
195
|
-
{ label: "\u4F1A\u5458\u6743\u76CA", value: "\u4F1A\u5458 95 \u6298" }
|
|
259
|
+
{ label: "\u5269\u4F59\u8F66\u4F4D", value: String(this.cfg.remainingSpaces) },
|
|
260
|
+
{ label: "\u9996\u5C0F\u65F6\u8D39\u7387", value: this.cfg.firstHourRate },
|
|
261
|
+
{ label: "\u4F1A\u5458\u6743\u76CA", value: this.cfg.memberBenefitEnabled ? "\u4F1A\u5458 95 \u6298" : "\u2014" }
|
|
196
262
|
]
|
|
197
263
|
};
|
|
198
264
|
}
|
|
199
265
|
async queryApplicableBenefits(_q, _plateNo) {
|
|
200
|
-
return
|
|
266
|
+
return this.benefits();
|
|
201
267
|
}
|
|
202
268
|
async locateVehicle(_q, _plateNo) {
|
|
203
|
-
return { floorName:
|
|
269
|
+
return { floorName: this.cfg.vehicleFloor, zoneName: this.cfg.vehicleZone, spaceNo: this.cfg.vehicleSpaceNo, walkingRoute: "\u7535\u68AF\u5DE6\u8F6C 50 \u7C73" };
|
|
204
270
|
}
|
|
205
271
|
// ===== 支付三步链(mock:reconcile 恒 PAID 收口) =====
|
|
206
272
|
async createOrder(_q, quoteRef) {
|
|
207
273
|
return { payOrderId: `demo:pay:${quoteRef}`, paymentRef: `demo:payment:${quoteRef}` };
|
|
208
274
|
}
|
|
209
275
|
async getOrderInfo(_q, _paymentRef) {
|
|
210
|
-
return { orderInfo: "demo-order-info", channel: "50", payableFeeCents:
|
|
276
|
+
return { orderInfo: "demo-order-info", channel: "50", payableFeeCents: this.cfg.feeCents };
|
|
211
277
|
}
|
|
212
278
|
async getPaymentSign(_q, _paymentRef) {
|
|
213
279
|
return { orderInfo: "demo-order-info" };
|
|
@@ -218,8 +284,8 @@ var MockParkingServiceImpl = class {
|
|
|
218
284
|
return "PAID";
|
|
219
285
|
}
|
|
220
286
|
async reprice(q, quoteRef, benefitRefs) {
|
|
221
|
-
const base =
|
|
222
|
-
const discount =
|
|
287
|
+
const base = this.quote(q, "demo-plate");
|
|
288
|
+
const discount = this.benefits().filter((b) => benefitRefs.includes(b.benefitRef)).reduce((sum, b) => sum + (b.estimatedDiscountCents ?? 0), 0);
|
|
223
289
|
return {
|
|
224
290
|
...base,
|
|
225
291
|
quoteRef: `${quoteRef}#r${Date.now()}`,
|
|
@@ -251,79 +317,97 @@ var MockParkingServiceImpl = class {
|
|
|
251
317
|
// src/server/promotion.ts
|
|
252
318
|
var now2 = () => (/* @__PURE__ */ new Date()).toISOString();
|
|
253
319
|
var inDays = (d) => new Date(Date.now() + d * 864e5).toISOString();
|
|
254
|
-
var
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
availability: { status: "AVAILABLE", reasonCodes: [], evaluatedAt: now2(), authoritative: false },
|
|
264
|
-
participation: { mode: "CLAIM_THEN_PURCHASE", steps: [{ title: "\u9886\u53D6", description: "\u70B9\u51FB\u9886\u53D6\u540E\u81EA\u52A8\u5B58\u5165\u5361\u5305" }] }
|
|
265
|
-
},
|
|
266
|
-
{
|
|
267
|
-
offerRef: "demo:groupon:2001",
|
|
268
|
-
offerType: "GROUPON",
|
|
269
|
-
title: "\u9910\u996E\u53CC\u4EBA\u5957\u9910 5 \u6298",
|
|
270
|
-
subtitle: "mock \u6F14\u793A\u56E2\u8D2D",
|
|
271
|
-
tags: ["\u9910\u996E"],
|
|
272
|
-
validity: { start: now2(), end: inDays(15) },
|
|
273
|
-
facets: [{ kind: "GROUPON", requiredParticipants: 2 }],
|
|
274
|
-
availability: { status: "AVAILABLE", reasonCodes: [], evaluatedAt: now2(), authoritative: false },
|
|
275
|
-
participation: { mode: "CLAIM_THEN_PURCHASE", steps: [{ title: "\u8D2D\u4E70", description: "\u6309\u56E2\u8D2D\u4EF7\u4E0B\u5355" }] }
|
|
276
|
-
}
|
|
277
|
-
];
|
|
278
|
-
var DEMO_ENTITLEMENTS = [
|
|
279
|
-
{
|
|
280
|
-
entitlementRef: "demo:entitlement:1",
|
|
281
|
-
offerRef: "demo:coupon:1001",
|
|
282
|
-
title: "\u6EE1 100 \u51CF 20 \u5143\u5238",
|
|
283
|
-
entitlementType: "COUPON",
|
|
284
|
-
status: "EFFECTIVE",
|
|
285
|
-
codeMasked: "DEMO-****-1001"
|
|
286
|
-
}
|
|
287
|
-
];
|
|
288
|
-
var DEMO_ACTIVITIES = [
|
|
289
|
-
{
|
|
290
|
-
activityRef: "demo:activity:3001",
|
|
291
|
-
title: "\u793A\u4F8B\u5546\u573A\u5468\u5E74\u5E86",
|
|
292
|
-
subtitle: "mock \u6F14\u793A\u6D3B\u52A8",
|
|
293
|
-
registrationRequired: false,
|
|
294
|
-
offerRefs: ["demo:coupon:1001"]
|
|
295
|
-
}
|
|
296
|
-
];
|
|
320
|
+
var PROMOTION_CFG_DEFAULTS = {
|
|
321
|
+
couponTitle: "\u6EE1 100 \u51CF 20 \u5143\u5238",
|
|
322
|
+
couponValueCents: 2e3,
|
|
323
|
+
couponThresholdCents: 1e4,
|
|
324
|
+
couponValidityDays: 30,
|
|
325
|
+
grouponEnabled: true,
|
|
326
|
+
activityTitle: "\u793A\u4F8B\u5546\u573A\u5468\u5E74\u5E86"
|
|
327
|
+
};
|
|
328
|
+
var PROMOTION_CFG_INT_KEYS = ["couponValueCents", "couponThresholdCents", "couponValidityDays"];
|
|
297
329
|
var MockPromotionServiceImpl = class {
|
|
330
|
+
/** 配置快照(fail-soft,构造期一次解析;键集 ≡ PROMOTION_CFG_DEFAULTS) */
|
|
331
|
+
cfg;
|
|
332
|
+
/** 供/资产/活动三表(构造期按 cfg 构建——请求级实例,成本可忽略) */
|
|
333
|
+
offers;
|
|
334
|
+
entitlements;
|
|
335
|
+
activities;
|
|
336
|
+
constructor(config = {}) {
|
|
337
|
+
this.cfg = resolveConfig(config, PROMOTION_CFG_DEFAULTS, PROMOTION_CFG_INT_KEYS);
|
|
338
|
+
const ts = now2();
|
|
339
|
+
const couponOffer = {
|
|
340
|
+
offerRef: "demo:coupon:1001",
|
|
341
|
+
offerType: "COUPON",
|
|
342
|
+
title: this.cfg.couponTitle,
|
|
343
|
+
subtitle: "\u5168\u573A\u901A\u7528\uFF08mock \u6F14\u793A\uFF09",
|
|
344
|
+
tags: ["\u901A\u7528"],
|
|
345
|
+
validity: { start: ts, end: inDays(this.cfg.couponValidityDays) },
|
|
346
|
+
facets: [{ kind: "COUPON", discountType: "AMOUNT", value: this.cfg.couponValueCents, thresholdCents: this.cfg.couponThresholdCents }],
|
|
347
|
+
availability: { status: "AVAILABLE", reasonCodes: [], evaluatedAt: ts, authoritative: false },
|
|
348
|
+
participation: { mode: "CLAIM_THEN_PURCHASE", steps: [{ title: "\u9886\u53D6", description: "\u70B9\u51FB\u9886\u53D6\u540E\u81EA\u52A8\u5B58\u5165\u5361\u5305" }] }
|
|
349
|
+
};
|
|
350
|
+
const grouponOffer = {
|
|
351
|
+
offerRef: "demo:groupon:2001",
|
|
352
|
+
offerType: "GROUPON",
|
|
353
|
+
title: "\u9910\u996E\u53CC\u4EBA\u5957\u9910 5 \u6298",
|
|
354
|
+
subtitle: "mock \u6F14\u793A\u56E2\u8D2D",
|
|
355
|
+
tags: ["\u9910\u996E"],
|
|
356
|
+
validity: { start: ts, end: inDays(15) },
|
|
357
|
+
facets: [{ kind: "GROUPON", requiredParticipants: 2 }],
|
|
358
|
+
availability: { status: "AVAILABLE", reasonCodes: [], evaluatedAt: ts, authoritative: false },
|
|
359
|
+
participation: { mode: "CLAIM_THEN_PURCHASE", steps: [{ title: "\u8D2D\u4E70", description: "\u6309\u56E2\u8D2D\u4EF7\u4E0B\u5355" }] }
|
|
360
|
+
};
|
|
361
|
+
this.offers = this.cfg.grouponEnabled ? [couponOffer, grouponOffer] : [couponOffer];
|
|
362
|
+
this.entitlements = [
|
|
363
|
+
{
|
|
364
|
+
entitlementRef: "demo:entitlement:1",
|
|
365
|
+
offerRef: "demo:coupon:1001",
|
|
366
|
+
title: this.cfg.couponTitle,
|
|
367
|
+
entitlementType: "COUPON",
|
|
368
|
+
status: "EFFECTIVE",
|
|
369
|
+
codeMasked: "DEMO-****-1001"
|
|
370
|
+
}
|
|
371
|
+
];
|
|
372
|
+
this.activities = [
|
|
373
|
+
{
|
|
374
|
+
activityRef: "demo:activity:3001",
|
|
375
|
+
title: this.cfg.activityTitle,
|
|
376
|
+
subtitle: "mock \u6F14\u793A\u6D3B\u52A8",
|
|
377
|
+
registrationRequired: false,
|
|
378
|
+
offerRefs: ["demo:coupon:1001"]
|
|
379
|
+
}
|
|
380
|
+
];
|
|
381
|
+
}
|
|
298
382
|
async searchOffers(_q, input) {
|
|
299
383
|
const keyword = input.keyword?.trim();
|
|
300
|
-
const items = keyword ?
|
|
384
|
+
const items = keyword ? this.offers.filter((o) => o.title.includes(keyword) || o.tags.some((t) => t.includes(keyword))) : this.offers;
|
|
301
385
|
return { items, hasMore: false };
|
|
302
386
|
}
|
|
303
387
|
async getOfferDetail(_q, input) {
|
|
304
|
-
if (input.offerRef) return
|
|
388
|
+
if (input.offerRef) return this.offers.find((o) => o.offerRef === input.offerRef) ?? null;
|
|
305
389
|
if (input.entitlementRef) {
|
|
306
|
-
const ent =
|
|
307
|
-
if (ent?.offerRef) return
|
|
390
|
+
const ent = this.entitlements.find((e) => e.entitlementRef === input.entitlementRef);
|
|
391
|
+
if (ent?.offerRef) return this.offers.find((o) => o.offerRef === ent.offerRef) ?? null;
|
|
308
392
|
return null;
|
|
309
393
|
}
|
|
310
|
-
if (input.keyword) return
|
|
394
|
+
if (input.keyword) return this.offers.find((o) => o.title.includes(input.keyword)) ?? null;
|
|
311
395
|
return null;
|
|
312
396
|
}
|
|
313
397
|
async recommendOffers() {
|
|
314
|
-
return
|
|
398
|
+
return this.offers;
|
|
315
399
|
}
|
|
316
400
|
async listEntitlements(_q, _input) {
|
|
317
|
-
return { items:
|
|
401
|
+
return { items: this.entitlements, hasMore: false };
|
|
318
402
|
}
|
|
319
403
|
async getEntitlementDetail(_q, entitlementRef) {
|
|
320
|
-
return
|
|
404
|
+
return this.entitlements.find((e) => e.entitlementRef === entitlementRef) ?? null;
|
|
321
405
|
}
|
|
322
406
|
async listActivities() {
|
|
323
|
-
return { items:
|
|
407
|
+
return { items: this.activities, hasMore: false };
|
|
324
408
|
}
|
|
325
409
|
async getActivityDetail(_q, activityRef) {
|
|
326
|
-
const activity =
|
|
410
|
+
const activity = this.activities.find((a) => a.activityRef === activityRef);
|
|
327
411
|
if (!activity) return null;
|
|
328
412
|
return { ...activity, sessions: [], description: "mock \u6F14\u793A\u6D3B\u52A8\u8BE6\u60C5" };
|
|
329
413
|
}
|
|
@@ -345,6 +429,16 @@ var MockPromotionServiceImpl = class {
|
|
|
345
429
|
};
|
|
346
430
|
|
|
347
431
|
// src/server/shopping.ts
|
|
432
|
+
var SHOPPING_CFG_DEFAULTS = {
|
|
433
|
+
extraShopName: "",
|
|
434
|
+
// '' = 不追加门店(空串 = 缺省/不生效语义)
|
|
435
|
+
extraShopCategory: "\u9910\u996E",
|
|
436
|
+
extraShopFloor: "3 \u697C",
|
|
437
|
+
extraShopUnit: "304",
|
|
438
|
+
extraShopOpen: true
|
|
439
|
+
};
|
|
440
|
+
var SHOPPING_CFG_INT_KEYS = [];
|
|
441
|
+
var SHOPPING_FLOORS = ["B1", "1 \u697C", "2 \u697C", "3 \u697C", "4 \u697C"];
|
|
348
442
|
var DEMO_BUILDINGS = [
|
|
349
443
|
{
|
|
350
444
|
buildingName: "A \u680B",
|
|
@@ -425,14 +519,35 @@ function toFloorRecord(floor) {
|
|
|
425
519
|
return { floorCode: floor.floorName, floorName: floor.floorName, businessCategories: cats, merchantCount: floor.shops.length };
|
|
426
520
|
}
|
|
427
521
|
var MockShoppingServiceImpl = class {
|
|
522
|
+
/** 配置快照(fail-soft,构造期一次解析;键集 ≡ SHOPPING_CFG_DEFAULTS) */
|
|
523
|
+
cfg;
|
|
428
524
|
buildings;
|
|
429
525
|
shops;
|
|
430
526
|
categories;
|
|
431
|
-
constructor(seed = DEMO_BUILDINGS) {
|
|
432
|
-
this.
|
|
433
|
-
|
|
527
|
+
constructor(seed = DEMO_BUILDINGS, config = {}) {
|
|
528
|
+
this.cfg = resolveConfig(config, SHOPPING_CFG_DEFAULTS, SHOPPING_CFG_INT_KEYS);
|
|
529
|
+
const withExtra = this.withExtraShop(seed);
|
|
530
|
+
this.buildings = withExtra;
|
|
531
|
+
this.shops = flattenShops(withExtra);
|
|
434
532
|
this.categories = aggregateCategories(this.shops);
|
|
435
533
|
}
|
|
534
|
+
/** extraShopName 非空 → 指定楼层追加演示门店(名称 trim 判空 + 楼层白名单回退——消费点守卫) */
|
|
535
|
+
withExtraShop(seed) {
|
|
536
|
+
const name = this.cfg.extraShopName.trim();
|
|
537
|
+
if (!name) return seed;
|
|
538
|
+
const floor = SHOPPING_FLOORS.includes(this.cfg.extraShopFloor) ? this.cfg.extraShopFloor : "3 \u697C";
|
|
539
|
+
const extra = {
|
|
540
|
+
merchantId: "demo-shop-1013",
|
|
541
|
+
name,
|
|
542
|
+
businessCategories: [this.cfg.extraShopCategory],
|
|
543
|
+
unitName: this.cfg.extraShopUnit,
|
|
544
|
+
status: this.cfg.extraShopOpen ? { code: "enabled", label: "\u8425\u4E1A\u4E2D" } : { code: "disabled", label: "\u4F11\u606F\u4E2D" }
|
|
545
|
+
};
|
|
546
|
+
return seed.map((b) => ({
|
|
547
|
+
...b,
|
|
548
|
+
floors: b.floors.map((f) => f.floorName === floor ? { ...f, shops: [...f.shops, extra] } : f)
|
|
549
|
+
}));
|
|
550
|
+
}
|
|
436
551
|
// ===== ShopDirectoryQueryPort =====
|
|
437
552
|
async searchShops(criteria) {
|
|
438
553
|
const kws = (criteria.keywords ?? (criteria.keyword?.trim() ? [criteria.keyword.trim()] : [])).map((k) => k.trim().toLowerCase()).filter(Boolean);
|
|
@@ -496,19 +611,30 @@ var MockShoppingServiceImpl = class {
|
|
|
496
611
|
// src/server/mall-info.ts
|
|
497
612
|
var MOCK_MALL_INFO_PROVIDER = "mock";
|
|
498
613
|
var MOCK_MALL_INFO_IMPL = "mock-mall-info@1";
|
|
614
|
+
var MALL_INFO_CFG_DEFAULTS = {
|
|
615
|
+
name: "",
|
|
616
|
+
// '' 哨兵 = 动态别名回退
|
|
617
|
+
address: "\u793A\u4F8B\u5546\u573A\u5730\u5740\uFF08demo\uFF09",
|
|
618
|
+
latitude: 30,
|
|
619
|
+
longitude: 120
|
|
620
|
+
};
|
|
621
|
+
var MALL_INFO_CFG_INT_KEYS = [];
|
|
499
622
|
var MALL_ID_RE = /^[\w-]{1,32}$/;
|
|
500
|
-
var mockMallInfoFactory = () =>
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
mallId
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
}
|
|
623
|
+
var mockMallInfoFactory = (input) => {
|
|
624
|
+
const cfg = resolveConfig(input.config, MALL_INFO_CFG_DEFAULTS, MALL_INFO_CFG_INT_KEYS);
|
|
625
|
+
return {
|
|
626
|
+
async getMallInfo(mallId) {
|
|
627
|
+
if (!MALL_ID_RE.test(mallId)) return void 0;
|
|
628
|
+
return {
|
|
629
|
+
mallId,
|
|
630
|
+
name: cfg.name !== "" ? cfg.name : `\u793A\u4F8B\u5546\u573A\uFF08${mallId}\uFF09`,
|
|
631
|
+
address: cfg.address,
|
|
632
|
+
latitude: cfg.latitude,
|
|
633
|
+
longitude: cfg.longitude
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
};
|
|
512
638
|
|
|
513
639
|
// src/server/mock-enroll-route.ts
|
|
514
640
|
import { Router } from "express";
|
|
@@ -521,7 +647,7 @@ function resolveMockEnroll(input) {
|
|
|
521
647
|
return { status: 401, body: { error: "unauthorized" } };
|
|
522
648
|
}
|
|
523
649
|
const body = input.body ?? {};
|
|
524
|
-
const userId = typeof body.userId === "string" && body.userId.trim() ? body.userId.trim() :
|
|
650
|
+
const userId = typeof body.userId === "string" && body.userId.trim() ? body.userId.trim() : input.identity.userId ?? `guest_${Date.now()}`;
|
|
525
651
|
enrollMockMember(userId);
|
|
526
652
|
return { status: 200, body: { state: "active", memberRef: `demo:member:${userId}` } };
|
|
527
653
|
}
|
|
@@ -533,7 +659,7 @@ function resolveMockLogout(input) {
|
|
|
533
659
|
return { status: 401, body: { error: "unauthorized" } };
|
|
534
660
|
}
|
|
535
661
|
const body = input.body ?? {};
|
|
536
|
-
const userId = typeof body.userId === "string" && body.userId.trim() ? body.userId.trim() :
|
|
662
|
+
const userId = typeof body.userId === "string" && body.userId.trim() ? body.userId.trim() : input.identity.userId ?? `guest_${Date.now()}`;
|
|
537
663
|
logoutMockMember(userId);
|
|
538
664
|
return { status: 200, body: { state: "logged_out" } };
|
|
539
665
|
}
|
|
@@ -558,33 +684,37 @@ var MOCK_MEMBER_IMPL = "mock-member@1";
|
|
|
558
684
|
var MOCK_PARKING_IMPL = "mock-parking@1";
|
|
559
685
|
var MOCK_PROMOTION_IMPL = "mock-promotion@1";
|
|
560
686
|
var MOCK_SHOPPING_IMPL = "mock-shopping@1";
|
|
561
|
-
var memberFactory = () => new MockMemberServiceImpl();
|
|
562
|
-
var parkingFactory = (input) => new MockParkingServiceImpl(input.instanceId || "mall-demo");
|
|
563
|
-
var promotionFactory = () => new MockPromotionServiceImpl();
|
|
687
|
+
var memberFactory = (input) => new MockMemberServiceImpl({}, input.config);
|
|
688
|
+
var parkingFactory = (input) => new MockParkingServiceImpl(input.instanceId || "mall-demo", input.config);
|
|
689
|
+
var promotionFactory = (input) => new MockPromotionServiceImpl(input.config);
|
|
564
690
|
var shoppingFactory = (input) => {
|
|
565
691
|
if (input.service !== "shopping-guide.catalog" && input.service !== "shopping-guide.shops" && input.service !== "shopping-guide.categories" && input.service !== "shopping-guide.directory") {
|
|
566
692
|
throw new Error(`mock guide \u5DE5\u5382\u4E0D\u4F9B\u7ED9\u69FD\uFF1A${input.service}`);
|
|
567
693
|
}
|
|
568
|
-
return new MockShoppingServiceImpl();
|
|
694
|
+
return new MockShoppingServiceImpl(void 0, input.config);
|
|
569
695
|
};
|
|
570
696
|
var mallInfoFactory = (input) => mockMallInfoFactory(input);
|
|
571
697
|
function createMockAlipayLoginExchange(integrations) {
|
|
572
698
|
return {
|
|
573
699
|
provider: "alipay",
|
|
700
|
+
/** 裸/demo 域默认开放 guest 登录(browser 轴 auto 档 → guest 签发) */
|
|
701
|
+
allowsGuestLogin() {
|
|
702
|
+
return true;
|
|
703
|
+
},
|
|
574
704
|
matches(context) {
|
|
575
705
|
const mallId = readLoginMallId(context);
|
|
576
706
|
if (!mallId) return true;
|
|
577
707
|
if (!integrations) return false;
|
|
578
708
|
return integrations.hasProviderBinding("member.account", { mallId }, MOCK_PROVIDER, MOCK_MEMBER_IMPL);
|
|
579
709
|
},
|
|
580
|
-
async exchangeByCode(
|
|
710
|
+
async exchangeByCode({ context, previousIdentity }) {
|
|
581
711
|
if (!readLoginMallId(context)) {
|
|
582
712
|
throw new Error("AUTH_NOT_CONFIGURED: \u672A\u9009\u62E9\u5546\u573A\uFF08\u767B\u5F55 context \u65E0 mallId\uFF09");
|
|
583
713
|
}
|
|
584
|
-
const userId =
|
|
714
|
+
const userId = previousIdentity?.source === "guest" ? previousIdentity.userId : `guest_${randomBytes(16).toString("hex")}`;
|
|
585
715
|
if (isMockMemberEnrolled(userId) && !isMockMemberLoggedOut(userId)) {
|
|
586
716
|
return {
|
|
587
|
-
identity: { userId, source: "
|
|
717
|
+
identity: { userId, source: "guest" },
|
|
588
718
|
credentials: {
|
|
589
719
|
token: `demo:token:${userId}`,
|
|
590
720
|
provider: "mock",
|
|
@@ -593,7 +723,7 @@ function createMockAlipayLoginExchange(integrations) {
|
|
|
593
723
|
};
|
|
594
724
|
}
|
|
595
725
|
return {
|
|
596
|
-
identity: { userId, source: "
|
|
726
|
+
identity: { userId, source: "guest" },
|
|
597
727
|
credentials: {
|
|
598
728
|
token: "",
|
|
599
729
|
provider: "mock",
|
|
@@ -610,6 +740,9 @@ var serverModule = {
|
|
|
610
740
|
},
|
|
611
741
|
cards,
|
|
612
742
|
register(ctx) {
|
|
743
|
+
ctx.integrations?.declareProviderInstances?.("mock", [
|
|
744
|
+
{ id: "mall-demo", name: "\u661F\u6CB3 Mock \u5C0F\u9547", attributes: { miniAppId: "mock-demo-app" } }
|
|
745
|
+
]);
|
|
613
746
|
ctx.integrations?.registerAdapter(MOCK_PROVIDER, MOCK_MEMBER_IMPL, memberFactory);
|
|
614
747
|
ctx.integrations?.registerAdapter(MOCK_PROVIDER, MOCK_PARKING_IMPL, parkingFactory);
|
|
615
748
|
ctx.integrations?.registerAdapter(MOCK_PROVIDER, MOCK_PROMOTION_IMPL, promotionFactory);
|
package/package.json
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tbox.cn/app-provider-mock",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"description": "商圈 Mock Provider 包(17 槽全 local:true:member×5 / parking×3 / promotion×4 / guide×4 + mall.info):模板缺省开箱演示——免传输/凭据/实例配置,裸跑即完整 mock
|
|
5
|
+
"description": "商圈 Mock Provider 包(17 槽全 local:true:member×5 / parking×3 / promotion×4 / guide×4 + mall.info):模板缺省开箱演示——免传输/凭据/实例配置,裸跑即完整 mock 演示;全槽 config schema(schemas/×5 域共享)+ 内存 config 消费(改配置重启即变)。",
|
|
6
6
|
"exports": {
|
|
7
7
|
".": {
|
|
8
8
|
"types": "./dist/index.d.ts",
|
|
@@ -14,9 +14,9 @@
|
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@tbox.cn/app-sdk": "0.
|
|
18
|
-
"@tbox.cn/app-contracts": "0.
|
|
19
|
-
"@tbox.cn/app-contracts-mall": "0.
|
|
17
|
+
"@tbox.cn/app-sdk": "0.20.0",
|
|
18
|
+
"@tbox.cn/app-contracts": "0.11.0",
|
|
19
|
+
"@tbox.cn/app-contracts-mall": "0.11.0"
|
|
20
20
|
},
|
|
21
21
|
"devDependencies": {
|
|
22
22
|
"@types/express": "4.17.25",
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
},
|
|
29
29
|
"files": [
|
|
30
30
|
"dist",
|
|
31
|
+
"schemas",
|
|
31
32
|
"src",
|
|
32
33
|
"tests",
|
|
33
34
|
"tbox.module.json",
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"title": "Mock 商场基础信息实例配置",
|
|
4
|
+
"type": "object",
|
|
5
|
+
"properties": {
|
|
6
|
+
"name": { "type": "string", "title": "商场名称", "description": "空串 = 动态别名「示例商场(mallId)」回退", "default": "" },
|
|
7
|
+
"address": { "type": "string", "title": "商场地址", "default": "示例商场地址(demo)" },
|
|
8
|
+
"latitude": { "type": "number", "minimum": -90, "maximum": 90, "title": "纬度(分式合法)", "default": 30 },
|
|
9
|
+
"longitude": { "type": "number", "minimum": -180, "maximum": 180, "title": "经度(分式合法)", "default": 120 }
|
|
10
|
+
}
|
|
11
|
+
}
|