@teamdream/sems-mcp 1.1.3 → 1.3.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.
@@ -1,86 +0,0 @@
1
- /**
2
- * 当前登录用户的档案缓存,供写工具自动补全「SEMS 必填但 AI 无从得知」的字段。
3
- *
4
- * 起因:采购单保存时 `validData()` 强制要求 `deptId` / `handlerId` / `handlerName` /
5
- * `contactName` / `contactTel` 等 7 项,缺一项就整单失败并只返回一个字段名。
6
- * 让模型去猜部门 ID 是不可能的,猜错更糟——所以这些值必须从当前登录身份取。
7
- *
8
- * 🔴 与 `permission-guard` 一样,这里只取白名单字段:
9
- * `/admin/user/info` 的响应里带着没有 `@JsonIgnore` 的 `password` 哈希。
10
- */
11
- import { log } from "./logger.js";
12
- /** 与权限缓存对齐的 60 秒。身份信息比权限变得还慢,这个时长足够。 */
13
- const TTL_MS = 60_000;
14
- function asRecord(value) {
15
- return typeof value === "object" && value !== null && !Array.isArray(value)
16
- ? value
17
- : {};
18
- }
19
- /** 只收字符串(后端为防精度丢失就是这么下发的),数字仅在长度安全时兜底。 */
20
- function id(value) {
21
- if (typeof value === "string") {
22
- const v = value.trim();
23
- return v === "" ? undefined : v;
24
- }
25
- if (typeof value === "number" && Number.isSafeInteger(value))
26
- return String(value);
27
- return undefined;
28
- }
29
- function str(value) {
30
- return typeof value === "string" ? value.trim() : "";
31
- }
32
- export class UserProfileCache {
33
- client;
34
- #cache = null;
35
- #expiresAt = 0;
36
- /** 并发去重:一个写工具里可能同时要档案和权限,避免打两次同一接口。 */
37
- #inflight = null;
38
- constructor(client) {
39
- this.client = client;
40
- }
41
- /** 取档案;失败时抛错(写操作不该拿猜测的值硬着头皮提交)。 */
42
- async get() {
43
- if (this.#cache && Date.now() < this.#expiresAt)
44
- return this.#cache;
45
- if (this.#inflight)
46
- return this.#inflight;
47
- this.#inflight = this.#load().finally(() => {
48
- this.#inflight = null;
49
- });
50
- return this.#inflight;
51
- }
52
- /** 只看缓存。给「能取到就补全、取不到就留空让用户填」的场景用。 */
53
- peek() {
54
- return this.#cache;
55
- }
56
- async #load() {
57
- const payload = await this.client.call({ method: "GET", path: "/admin/user/info" });
58
- const data = asRecord(asRecord(payload.data)["data"]);
59
- const sysUser = asRecord(data["sysUser"]);
60
- const userVO = asRecord(data["userVO"]);
61
- // 真实 SEMS 两个都给;桩可能只给一个。字段名一致,取有内容的那个。
62
- const identity = Object.keys(sysUser).length > 0 ? sysUser : userVO;
63
- // 身份信息一律以 userVO 为准(sysUser 里 deptId 等可能为空),
64
- // 但 name 在两个结构里都有且语义相同。
65
- const profile = {
66
- userId: id(userVO["userId"]) ?? id(identity["userId"]),
67
- username: str(userVO["username"]) || str(identity["username"]),
68
- name: str(userVO["name"]) || str(identity["name"]) || str(identity["nickname"]),
69
- phone: str(userVO["phone"]) || str(identity["phone"]),
70
- deptId: id(userVO["deptId"]),
71
- deptName: str(userVO["deptName"]),
72
- tenantId: id(userVO["tenantId"]),
73
- };
74
- this.#cache = profile;
75
- this.#expiresAt = Date.now() + TTL_MS;
76
- log.debug("用户档案已缓存", {
77
- hasUserId: profile.userId !== undefined,
78
- hasDept: profile.deptId !== undefined,
79
- });
80
- return profile;
81
- }
82
- invalidate() {
83
- this.#cache = null;
84
- this.#expiresAt = 0;
85
- }
86
- }