@tbox.cn/app-module-mall-core 0.3.0 → 0.3.1

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.
@@ -157,10 +157,12 @@ function createScopeAssembly(raw, ctx) {
157
157
  }
158
158
  };
159
159
  }
160
+ function resolveMallMiniAppId(controlPlane, rootMiniAppId, mallId) {
161
+ return rootMiniAppId ?? controlPlane.malls.find((m) => m.mallId === mallId)?.miniAppId;
162
+ }
160
163
  function buildDefaultMallContext(controlPlane, rootMiniAppId) {
161
164
  if (!controlPlane.defaultMallId) return void 0;
162
- const mall = controlPlane.malls.find((m) => m.mallId === controlPlane.defaultMallId);
163
- const miniAppId = rootMiniAppId ?? mall?.miniAppId;
165
+ const miniAppId = resolveMallMiniAppId(controlPlane, rootMiniAppId, controlPlane.defaultMallId);
164
166
  if (!miniAppId) {
165
167
  deploymentLogger.warn(
166
168
  `defaultMallId=${controlPlane.defaultMallId} \u65E0 miniAppId \u771F\u6E90\uFF08root config \u4E0E\u53C2\u8003\u8868\u53CC\u7F3A\uFF09\u2014\u2014\u6309 mallId-only \u6CE8\u5165\u9ED8\u8BA4\u5546\u5708`
@@ -230,6 +232,7 @@ import { createLogger as createLogger4, getRequestContext } from "@tbox.cn/app-a
230
232
  import {
231
233
  MEMBER_SNAPSHOT_SERVICE,
232
234
  PARKING_SNAPSHOT_SERVICE,
235
+ mallScopeOrEmpty,
233
236
  withMallScopeFromQuery
234
237
  } from "@tbox.cn/app-contracts-mall";
235
238
 
@@ -378,13 +381,12 @@ function createMallHomeRouter(ctx) {
378
381
  res.json({ member: null, parking: { status: "unavailable" } });
379
382
  return;
380
383
  }
381
- const mallMiniAppId = resolveMallScopeAssembly(ctx)?.rootMiniAppId ?? resolveMallScopeAssembly(ctx)?.controlPlane.malls.find((m) => m.mallId === mallId)?.miniAppId;
384
+ const assembly = resolveMallScopeAssembly(ctx);
385
+ const mallMiniAppId = assembly ? resolveMallMiniAppId(assembly.controlPlane, assembly.rootMiniAppId, mallId) : void 0;
382
386
  if (!mallMiniAppId) {
383
387
  serverLogger.warn(
384
- `[MALL] linked-panel mallId=${mallId} \u65E0 miniAppId \u771F\u6E90\uFF08root config \u4E0E malls \u8868\u53CC\u7F3A\uFF09\u2014\u2014\u4E1A\u52A1\u69FD\u9A8C\u7B7E\u5C06\u5931\u8D25\uFF08\u62D2\u7EDD\u6CE8\u5165\u5047\u503C\uFF09`
388
+ `[MALL] linked-panel mallId=${mallId} \u65E0 miniAppId \u771F\u6E90\uFF08root config \u4E0E malls \u8868\u53CC\u7F3A\uFF09\u2014\u2014\u6309 mallId-only \u964D\u7EA7\u7EE7\u7EED`
385
389
  );
386
- res.json({ member: null, parking: { status: "error" } });
387
- return;
388
390
  }
389
391
  const reqCtxWithMall = withMallScopeFromQuery(authCtx, req.query, { miniAppId: mallMiniAppId });
390
392
  const [memberResult, parkingResult] = await Promise.allSettled([
@@ -427,7 +429,7 @@ function createMallHomeRouter(ctx) {
427
429
  });
428
430
  if (!service) return { status: "error" };
429
431
  const query = {
430
- scope: { mallId, miniAppId: mallMiniAppId },
432
+ scope: mallScopeOrEmpty(reqCtxWithMall),
431
433
  requestContext: reqCtxWithMall
432
434
  };
433
435
  return loadLinkedPanelParking(service, query, async () => {
@@ -1340,7 +1340,6 @@ function mapDirectEnrollReceipt(status) {
1340
1340
  if (status === 200) return { loginStatus: "success", message: "\u767B\u5F55\u6210\u529F" };
1341
1341
  if (status === 401) return { loginStatus: "fail", message: "\u8BF7\u5148\u767B\u5F55" };
1342
1342
  if (status === 404) return { loginStatus: "fail", message: "\u5F53\u524D\u73AF\u5883\u672A\u63D0\u4F9B\u5165\u4F1A\u670D\u52A1\uFF0C\u8BF7\u5728\u652F\u4ED8\u5B9D\u5C0F\u7A0B\u5E8F\u5185\u6253\u5F00" };
1343
- if (status === 503) return { loginStatus: "fail", message: "\u6F14\u793A\u5165\u4F1A\u4EC5\u9650\u975E\u751F\u4EA7\u73AF\u5883" };
1344
1343
  return { loginStatus: "fail", message: "\u767B\u5F55\u72B6\u6001\u540C\u6B65\u5931\u8D25\uFF0C\u8BF7\u91CD\u8BD5" };
1345
1344
  }
1346
1345
  async function directLoginGate() {
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  clientModule,
3
3
  client_default
4
- } from "../chunk-VA2ROMX7.js";
4
+ } from "../chunk-HMQKNJEO.js";
5
5
  export {
6
6
  clientModule,
7
7
  client_default as default
package/dist/index.js CHANGED
@@ -9,10 +9,10 @@ import {
9
9
  resolveMallInfo,
10
10
  resolveMallScopeAssembly,
11
11
  serverModule
12
- } from "./chunk-LJJTKND6.js";
12
+ } from "./chunk-B7ZAK6VM.js";
13
13
  import {
14
14
  clientModule
15
- } from "./chunk-VA2ROMX7.js";
15
+ } from "./chunk-HMQKNJEO.js";
16
16
  export {
17
17
  bindMallInfoContext,
18
18
  buildDefaultMallContext,
@@ -39,9 +39,8 @@ declare function createScopeAssembly(raw: IntegrationsRaw | undefined, ctx: Serv
39
39
  /**
40
40
  * 016:默认商圈 HELLO context(无 URL 参数访问时的服务端兜底)。
41
41
  * v4 mallId-only 注入(D8/D9):miniAppId 是尽力回填的展示值,非会话边界——
42
- * 读源统一 root config.miniAppId 优先、回落参考表 attributes.miniAppId;双缺不再
43
- * 阻断注入(此前 warn-refuse 会造成默认商圈静默失效),按 mallId-only 注入。
44
- * 缺省 defaultMallId → undefined(不兜底)。
42
+ * 读源统一经 resolveMallMiniAppId 单点;双缺不再阻断注入(此前 warn-refuse 会造成
43
+ * 默认商圈静默失效),按 mallId-only 注入。缺省 defaultMallId → undefined(不兜底)。
45
44
  */
46
45
  declare function buildDefaultMallContext(controlPlane: MallDeploymentControlPlane, rootMiniAppId?: string): Record<string, unknown> | undefined;
47
46
 
@@ -10,7 +10,7 @@ import {
10
10
  resolveMallScopeAssembly,
11
11
  serverModule,
12
12
  server_default
13
- } from "../chunk-LJJTKND6.js";
13
+ } from "../chunk-B7ZAK6VM.js";
14
14
  export {
15
15
  bindMallInfoContext,
16
16
  buildDefaultMallContext,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tbox.cn/app-module-mall-core",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "type": "module",
5
5
  "description": "商圈基础模块:scope 域装配注册 + mall-info 工具链 + mall-directory 服务 + 首页主屏(home 槽 / 联单面板 / 会员停车快览)+ 首页预览卡。",
6
6
  "exports": {
@@ -28,7 +28,6 @@ export function mapDirectEnrollReceipt(status: number): LoginGateReceipt {
28
28
  if (status === 200) return { loginStatus: 'success', message: '登录成功' };
29
29
  if (status === 401) return { loginStatus: 'fail', message: '请先登录' };
30
30
  if (status === 404) return { loginStatus: 'fail', message: '当前环境未提供入会服务,请在支付宝小程序内打开' };
31
- if (status === 503) return { loginStatus: 'fail', message: '演示入会仅限非生产环境' };
32
31
  return { loginStatus: 'fail', message: '登录状态同步失败,请重试' };
33
32
  }
34
33
 
@@ -136,20 +136,32 @@ export function createScopeAssembly(
136
136
  } satisfies MallScopeAssembly;
137
137
  }
138
138
 
139
+ /**
140
+ * mall miniAppId 读源单点:root config.miniAppId 优先 → 参考表按 mallId 查;双缺 → undefined。
141
+ * 软依赖契约(v4 D9):miniAppId 是尽力回填的展示/定位值,非会话边界——双缺由调用方按
142
+ * mallId-only 降级(消费点自补语义,见 contracts-mall createMallContextValidator 注释)。
143
+ * 严禁任何假值注入(09-01 事故防线:回退假字面量致真实档业务槽验签 appId 恒假 → 网关 401)。
144
+ */
145
+ export function resolveMallMiniAppId(
146
+ controlPlane: MallDeploymentControlPlane,
147
+ rootMiniAppId: string | undefined,
148
+ mallId: string,
149
+ ): string | undefined {
150
+ return rootMiniAppId ?? controlPlane.malls.find((m) => m.mallId === mallId)?.miniAppId;
151
+ }
152
+
139
153
  /**
140
154
  * 016:默认商圈 HELLO context(无 URL 参数访问时的服务端兜底)。
141
155
  * v4 mallId-only 注入(D8/D9):miniAppId 是尽力回填的展示值,非会话边界——
142
- * 读源统一 root config.miniAppId 优先、回落参考表 attributes.miniAppId;双缺不再
143
- * 阻断注入(此前 warn-refuse 会造成默认商圈静默失效),按 mallId-only 注入。
144
- * 缺省 defaultMallId → undefined(不兜底)。
156
+ * 读源统一经 resolveMallMiniAppId 单点;双缺不再阻断注入(此前 warn-refuse 会造成
157
+ * 默认商圈静默失效),按 mallId-only 注入。缺省 defaultMallId → undefined(不兜底)。
145
158
  */
146
159
  export function buildDefaultMallContext(
147
160
  controlPlane: MallDeploymentControlPlane,
148
161
  rootMiniAppId?: string,
149
162
  ): Record<string, unknown> | undefined {
150
163
  if (!controlPlane.defaultMallId) return undefined;
151
- const mall = controlPlane.malls.find((m) => m.mallId === controlPlane.defaultMallId);
152
- const miniAppId = rootMiniAppId ?? mall?.miniAppId;
164
+ const miniAppId = resolveMallMiniAppId(controlPlane, rootMiniAppId, controlPlane.defaultMallId);
153
165
  if (!miniAppId) {
154
166
  deploymentLogger.warn(
155
167
  `defaultMallId=${controlPlane.defaultMallId} 无 miniAppId 真源(root config 与参考表双缺)——按 mallId-only 注入默认商圈`,
@@ -17,13 +17,14 @@ import type { PromotionActivityDetail, PromotionQueryContext } from '@tbox.cn/ap
17
17
  import {
18
18
  MEMBER_SNAPSHOT_SERVICE,
19
19
  PARKING_SNAPSHOT_SERVICE,
20
+ mallScopeOrEmpty,
20
21
  withMallScopeFromQuery,
21
22
  } from '@tbox.cn/app-contracts-mall';
22
23
  import type {
23
24
  MemberSnapshotFactory,
24
25
  ParkingSnapshotFactory,
25
26
  } from '@tbox.cn/app-contracts-mall';
26
- import { resolveMallScopeAssembly } from './assembly.js';
27
+ import { resolveMallMiniAppId, resolveMallScopeAssembly } from './assembly.js';
27
28
  import { loadBumpinPorts } from './module-ports.js';
28
29
  import {
29
30
  buildLinkedPanelParking,
@@ -79,18 +80,19 @@ router.get('/linked-panel', async (req, res) => {
79
80
 
80
81
  // mallId 注入经 TIER1 唯一写入口(query 契约键 + opts.miniAppId 兜底),使
81
82
  // resolveMember*Port/resolveParkingQueryPort 能正常取到 mall scope
82
- // miniAppId 读源统一(对齐 WS enhanceContext):root config.miniAppId 优先 → malls[] 按 mallId 查;
83
- // 双缺(root 无 miniAppId 且动态 mall 无表可查)→ fail-fast 显性暴露——绝不回退假值
84
- // (曾回退字面量 'linked-panel' 致业务槽验签 appId 恒假 → 网关 401)
85
- const mallMiniAppId =
86
- resolveMallScopeAssembly(ctx)?.rootMiniAppId ??
87
- resolveMallScopeAssembly(ctx)?.controlPlane.malls.find((m) => m.mallId === mallId)?.miniAppId;
83
+ // miniAppId 读源单点(对齐 WS enhanceContext)= resolveMallMiniAppId(root config 优先 →
84
+ // 参考表按 mallId 查);双缺按 mallId-only 降级继续(v4 D9:miniAppId 是尽力回填的展示值,
85
+ // 非会话边界——与 HELLO defaultMallContext 同语义)。绝不注入假值(09-01 事故防线保留):
86
+ // 真实档加签读源 scope ?? config(如 wanda-headers),scope 缺席回落部署 config,无假值消费面。
87
+ // (曾回退字面量 'linked-panel' 属假值注入——与本降级有本质区别,仍被禁止)
88
+ const assembly = resolveMallScopeAssembly(ctx);
89
+ const mallMiniAppId = assembly
90
+ ? resolveMallMiniAppId(assembly.controlPlane, assembly.rootMiniAppId, mallId)
91
+ : undefined;
88
92
  if (!mallMiniAppId) {
89
93
  serverLogger.warn(
90
- `[MALL] linked-panel mallId=${mallId} 无 miniAppId 真源(root config 与 malls 表双缺)——业务槽验签将失败(拒绝注入假值)`,
94
+ `[MALL] linked-panel mallId=${mallId} 无 miniAppId 真源(root config 与 malls 表双缺)——按 mallId-only 降级继续`,
91
95
  );
92
- res.json({ member: null, parking: { status: 'error' } satisfies LinkedPanelParking });
93
- return;
94
96
  }
95
97
  const reqCtxWithMall = withMallScopeFromQuery(authCtx, req.query, { miniAppId: mallMiniAppId });
96
98
 
@@ -136,9 +138,11 @@ router.get('/linked-panel', async (req, res) => {
136
138
  });
137
139
  if (!service) return { status: 'error' } satisfies LinkedPanelParking;
138
140
 
139
- // listVehicles 需完整 ParkingQueryContext(scope + requestContext;userId 经 requestContext 传递)
141
+ // listVehicles 需完整 ParkingQueryContext(scope + requestContext;userId 经 requestContext 传递)。
142
+ // scope 回读 attributes(mallScopeOrEmpty)——与写入单源(withMallScopeFromQuery)恒一致,
143
+ // 消灭手拼 scope 与 session scope 的来源分叉(query.miniAppId 透传优先语义)
140
144
  const query = {
141
- scope: { mallId, miniAppId: mallMiniAppId },
145
+ scope: mallScopeOrEmpty(reqCtxWithMall),
142
146
  requestContext: reqCtxWithMall,
143
147
  };
144
148
  // 凭证不足以查车时由 provider 的 requiresMembership 能力口收口(未声明该口即照常查询)
@@ -0,0 +1,116 @@
1
+ // @vitest-environment node
2
+ /**
3
+ * linked-panel miniAppId 软依赖降级(v4 D9 语义收口):
4
+ * miniAppId 读源双缺(root config 与参考表双缺)→ warn 留痕 + mallId-only 降级继续,
5
+ * 会员/停车照常投影——mock/演示档首页双卡不再因配置缺元数据全灭
6
+ * (回归背景:2026-09-18 N-01 同症 + 7e01d1e4 defaultInstanceId=1000001 无表形态复发)。
7
+ * 防线不变式:绝不注入假值(降级 = scope 不带键,真实档加签回落部署 config)。
8
+ */
9
+ import express from 'express';
10
+ import type { Server } from 'node:http';
11
+ import { afterAll, beforeAll, describe, expect, it } from 'vitest';
12
+ import { setRequestContext, type ServerContext } from '@tbox.cn/app-agent-sdk-server';
13
+ import type { RequestContext } from '@tbox.cn/app-contracts';
14
+ import {
15
+ MEMBER_SNAPSHOT_SERVICE,
16
+ PARKING_SNAPSHOT_SERVICE,
17
+ } from '@tbox.cn/app-contracts-mall';
18
+ import { createMallHomeRouter } from '../src/server/routes.js';
19
+
20
+ const observedAt = '2026-09-21T00:00:00.000Z';
21
+
22
+ const memberPort = {
23
+ getAccount: async () => ({ authState: 'authenticated', state: 'not_enrolled', observedAt }),
24
+ queryPoints: async () => ({ userId: 'u1', balance: 0, membershipLabel: '示例会员卡' }),
25
+ enrollUrl: 'alipays://platformapi/startapp?appId=demo&page=v/index/index',
26
+ };
27
+
28
+ const parkingService = {
29
+ listVehicles: async () => ({ bound: [{ plateNo: '京A12345' }] }),
30
+ };
31
+
32
+ function makeCtx(assembly: unknown): ServerContext {
33
+ return {
34
+ domainAssemblies: { resolve: () => assembly },
35
+ resolveService: (service: string) =>
36
+ service === MEMBER_SNAPSHOT_SERVICE
37
+ ? async () => memberPort
38
+ : service === PARKING_SNAPSHOT_SERVICE
39
+ ? async () => parkingService
40
+ : undefined,
41
+ } as unknown as ServerContext;
42
+ }
43
+
44
+ /** 双缺形态:root 无 miniAppId + 参考表空(7e01d1e4 后 mock 档真实形态) */
45
+ const assemblyNoMiniAppId = {
46
+ rootMiniAppId: undefined,
47
+ controlPlane: { malls: [], defaultMallId: 'mall-1' },
48
+ };
49
+
50
+ /** 参考表命中形态:miniAppId 由表供给(回归保护——读源路径不变) */
51
+ const assemblyWithTableHit = {
52
+ rootMiniAppId: undefined,
53
+ controlPlane: {
54
+ malls: [{ mallId: 'mall-1', name: '星河 Mock 小镇', miniAppId: 'tbl-app' }],
55
+ defaultMallId: 'mall-1',
56
+ },
57
+ };
58
+
59
+ const authCtx = { identity: { userId: 'u1', source: 'guest' }, attributes: {} } as unknown as RequestContext;
60
+
61
+ let server: Server;
62
+ let base: string;
63
+
64
+ beforeAll(async () => {
65
+ const app = express();
66
+ app.use('/api/m/mall-core', (req, _res, next) => {
67
+ setRequestContext(req, authCtx);
68
+ next();
69
+ });
70
+ app.use('/api/m/mall-core', createMallHomeRouter(makeCtx(assemblyNoMiniAppId)));
71
+ server = app.listen(0);
72
+ await new Promise<void>((resolve) => server.once('listening', () => resolve()));
73
+ const address = server.address();
74
+ if (!address || typeof address === 'string') throw new Error(`意外地址形态: ${String(address)}`);
75
+ base = `http://127.0.0.1:${address.port}`;
76
+ });
77
+
78
+ afterAll(() => {
79
+ server.close();
80
+ });
81
+
82
+ async function linkedPanel(): Promise<{ status: number; body: { member: unknown; parking: { status: string } } }> {
83
+ const res = await fetch(`${base}/api/m/mall-core/linked-panel?mallId=mall-1`);
84
+ return { status: res.status, body: (await res.json()) as { member: unknown; parking: { status: string } } };
85
+ }
86
+
87
+ describe('linked-panel miniAppId 软依赖降级', () => {
88
+ it('双缺(root 与参考表双缺)→ warn 降级继续:member/parking 照常投影(不再 member:null + parking:error)', async () => {
89
+ const { status, body } = await linkedPanel();
90
+ expect(status).toBe(200);
91
+ expect(body.member).toMatchObject({ authState: 'authenticated', state: 'not_enrolled' });
92
+ expect(body.parking.status).toBe('ready');
93
+ });
94
+
95
+ it('参考表命中(miniAppId 在场)→ 读源路径行为不变', async () => {
96
+ // 单服务器挂双路由不可行(ctx 定型于创建期)——重建独立实例验证命中分支
97
+ const app = express();
98
+ app.use('/api/m/mall-core', (req, _res, next) => {
99
+ setRequestContext(req, authCtx);
100
+ next();
101
+ });
102
+ app.use('/api/m/mall-core', createMallHomeRouter(makeCtx(assemblyWithTableHit)));
103
+ const srv = app.listen(0);
104
+ try {
105
+ const address = srv.address();
106
+ if (!address || typeof address === 'string') throw new Error(`意外地址形态: ${String(address)}`);
107
+ const res = await fetch(`http://127.0.0.1:${address.port}/api/m/mall-core/linked-panel?mallId=mall-1`);
108
+ const body = (await res.json()) as { member: unknown; parking: { status: string } };
109
+ expect(res.status).toBe(200);
110
+ expect(body.member).toMatchObject({ authState: 'authenticated', state: 'not_enrolled' });
111
+ expect(body.parking.status).toBe('ready');
112
+ } finally {
113
+ srv.close();
114
+ }
115
+ });
116
+ });
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * mock 演示直连入会门禁测试(非壳 H5 直调 provider-mock demo enroll):
3
- * - 映射纯函数五态表驱动;
3
+ * - 映射纯函数四态表驱动(200/401/404 + 未知态兜底;503 态已随服务端 NODE_ENV 闸门删除移除);
4
4
  * - 直连实现:真实 httpPost 链(stubGlobal fetch)断言 URL/method/超时信号 + 各态映射;
5
5
  * - 门禁集成:directLoginGate 注入 createMemberGate → success 收场全链(冷登/判定/重放)。
6
6
  */
@@ -36,7 +36,6 @@ describe('mapDirectEnrollReceipt(HTTP 状态 → 门控回执纯映射)', ()
36
36
  [200, { loginStatus: 'success', message: '登录成功' }],
37
37
  [401, { loginStatus: 'fail', message: '请先登录' }],
38
38
  [404, { loginStatus: 'fail', message: '当前环境未提供入会服务,请在支付宝小程序内打开' }],
39
- [503, { loginStatus: 'fail', message: '演示入会仅限非生产环境' }],
40
39
  [500, { loginStatus: 'fail', message: '登录状态同步失败,请重试' }],
41
40
  ];
42
41
  it.each(cases)('status=%i → %j', (status, expected) => {