feat(assist-platform): 固化钉钉扫码登录闭环并优化多集群与网关错误解析
- 支持一行命令自动发起钉钉交互式扫码登录 (hose-use assist-platform login [--qr]) - 集成 qrcode-terminal 在终端直接输出紧凑 ANSI 文本二维码 - 严格对齐 HoseCloud SSO 钉钉时间戳的 60 秒安全防重放倒计时 - 自动从重定向参数或 Token 换取 Unity 运营中心会话凭证并安全落盘 - 在 core/http/assist-platform-client 增加响应体 errorCode: 403 拦截,杜绝凭证过期被吞成空数据的静默失败 - 修复 assist-platform enter 多集群租户 (wx2/shanghai 等) baseUrl 硬编码为 app 的验票失败问题 - 修复 workflow nodes grant 网关回包双层嵌套 saved/skipped 数据解析问题 - 补全 core/http/sso-client 单元测试并确保全部 957 项测试通过
This commit is contained in:
1 parent
eb9b979943
commit
95af2177e5
10 files changed
+555
-29
No files matched your search
@@ -833,7 +833,7 @@ export const COMMAND_METADATA: Record<string, CliCommandMetadata> = {
|
||||
},
|
||||
flags: {
|
||||
...flagList("token t user name u hose-token jwt ticket ticket-id auth-code staff staff-id staffId alias corp-name count limit n start offset state status corpid id keyword query q message msg reason matter hours hour duration days day to recipient target remarks comment corp", "value"),
|
||||
...flagList("all a logout admin save import persist switch current overwrite candidates list-candidates", "boolean")
|
||||
...flagList("all a logout admin save import persist switch current overwrite candidates list-candidates qr", "boolean")
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -4,7 +4,10 @@ import {
|
||||
clearAssistPlatformSession,
|
||||
} from "../../../core/auth/assist-platform-store.js";
|
||||
import { createAssistPlatformClient } from "../../../core/http/assist-platform-client.js";
|
||||
import { searchCorporations } from "../../../domains/assist-platform/services.js";
|
||||
import {
|
||||
searchCorporations,
|
||||
loginAssistPlatformByQrCode,
|
||||
} from "../../../domains/assist-platform/services.js";
|
||||
import { booleanOption, stringOption } from "../../parser.js";
|
||||
import { printJson, printKeyValue } from "../../printer.js";
|
||||
import type { CliParsedArgs, CliResult, CliRuntime } from "../../types.js";
|
||||
@@ -66,35 +69,150 @@ export async function handleLogin(
|
||||
};
|
||||
}
|
||||
|
||||
// 查看当前协助平台会话状态
|
||||
const checkStatusOnly = booleanOption(parsed.options, ["status"]);
|
||||
const forceQr = booleanOption(parsed.options, ["qr", "force"]);
|
||||
const isInteractive = Boolean(
|
||||
process.stdin.isTTY && !process.env.CI && process.env.NODE_ENV !== "test"
|
||||
);
|
||||
const existing = loadAssistPlatformSession();
|
||||
if (!existing || !existing.token) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未检测到协助平台登录凭证。\n用法:\n hose-use assist-platform login --token <TOKEN> [--user <姓名>]\n hose-use assist-platform login --logout (清理凭证)\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
// 如果仅检查状态 (--status)
|
||||
if (checkStatusOnly) {
|
||||
if (!existing || !existing.token) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[ERROR] 未检测到协助平台登录凭证。\n请运行: hose-use assist-platform login 进行扫码登录\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
const testClient = createAssistPlatformClient({
|
||||
token: existing.token,
|
||||
baseUrl: existing.baseUrl,
|
||||
hoseAccessToken: existing.hoseAccessToken,
|
||||
});
|
||||
|
||||
try {
|
||||
await searchCorporations(testClient, { limit: { start: 0, count: 1 } });
|
||||
} catch {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[ERROR] 协助平台凭证已过期或无效。\n请运行: hose-use assist-platform login 进行扫码登录\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({ valid: true, ...existing }),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const pairs: Record<string, string> = {
|
||||
协助平台状态: "🟢 凭证有效 (已登录)",
|
||||
操作员工姓名: existing.userName || "运营实施人员",
|
||||
基础服务地址: existing.baseUrl || "https://unity.ekuaibao.com/api",
|
||||
当前会话凭证: existing.token.replace(/(.{4}).*(.{4})/, "$1****$2"),
|
||||
最后更新时间: new Date(existing.updatedAt).toLocaleString("zh-CN", { hour12: false }),
|
||||
};
|
||||
|
||||
return {
|
||||
stdout: printJson(existing),
|
||||
stdout: `====== 合思协助平台当前会话 ======\n${printKeyValue(pairs)}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const pairs: Record<string, string> = {
|
||||
协助平台状态: "🟢 已登录",
|
||||
操作员工姓名: existing.userName || "运营实施人员",
|
||||
基础服务地址: existing.baseUrl || "https://unity.ekuaibao.com/api",
|
||||
当前会话凭证: existing.token.replace(/(.{4}).*(.{4})/, "$1****$2"),
|
||||
最后更新时间: new Date(existing.updatedAt).toLocaleString("zh-CN", { hour12: false }),
|
||||
};
|
||||
// 如果已有凭证且未强制发起扫码,先探测有效性
|
||||
if (existing?.token && !forceQr) {
|
||||
let isValid = false;
|
||||
const testClient = createAssistPlatformClient({
|
||||
token: existing.token,
|
||||
baseUrl: existing.baseUrl,
|
||||
hoseAccessToken: existing.hoseAccessToken,
|
||||
});
|
||||
|
||||
return {
|
||||
stdout: `====== 合思协助平台当前会话 ======\n${printKeyValue(pairs)}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
try {
|
||||
await searchCorporations(testClient, { limit: { start: 0, count: 1 } });
|
||||
isValid = true;
|
||||
} catch {
|
||||
isValid = false;
|
||||
}
|
||||
|
||||
if (isValid) {
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson(existing),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
const pairs: Record<string, string> = {
|
||||
协助平台状态: "🟢 已登录 (凭证有效)",
|
||||
操作员工姓名: existing.userName || "运营实施人员",
|
||||
基础服务地址: existing.baseUrl || "https://unity.ekuaibao.com/api",
|
||||
当前会话凭证: existing.token.replace(/(.{4}).*(.{4})/, "$1****$2"),
|
||||
最后更新时间: new Date(existing.updatedAt).toLocaleString("zh-CN", { hour12: false }),
|
||||
};
|
||||
|
||||
return {
|
||||
stdout: `====== 合思协助平台当前会话 ======\n${printKeyValue(pairs)}\n\n💡 提示: 当前凭证处于有效状态。如需重新扫码登录,请运行: hose-use assist-platform login --qr\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 凭证不存在或已失效:非交互/测试模式下给出指引,交互模式或显式 --qr 下直接自动拉起扫码登录
|
||||
if (!isInteractive && !forceQr) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `未检测到协助平台登录凭证。\n用法:\n hose-use assist-platform login [--qr]\n hose-use assist-platform login --token <TOKEN> [--user <姓名>]\n hose-use assist-platform login --logout (清理凭证)\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
|
||||
// ── 自动发起钉钉交互式扫码登录流程 ──────────────────────────
|
||||
try {
|
||||
const loginRes = await loginAssistPlatformByQrCode({
|
||||
timeoutMs: 60000,
|
||||
onQrCode: (qrText, dingUrl) => {
|
||||
if (!runtime.jsonMode) {
|
||||
process.stdout.write(`\n====== 合思协助平台钉钉扫码登录 ======\n`);
|
||||
process.stdout.write(`请使用钉钉 App 扫描下方二维码确认登录(有效期 60 秒):\n\n`);
|
||||
process.stdout.write(`${qrText}\n\n`);
|
||||
process.stdout.write(`🔗 快捷授权链接: ${dingUrl}\n\n`);
|
||||
process.stdout.write(`等待钉钉扫码并确认中... (最长等待 60 秒)\n`);
|
||||
}
|
||||
},
|
||||
});
|
||||
|
||||
if (runtime.jsonMode) {
|
||||
return {
|
||||
stdout: printJson({
|
||||
success: true,
|
||||
token: loginRes.token,
|
||||
userName: loginRes.userName,
|
||||
baseUrl: loginRes.baseUrl,
|
||||
}),
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
stdout: `\n[SUCCESS] 协助平台凭证已自动更新成功!\n操作人员: ${loginRes.userName}\n会话 Token: ${loginRes.token}\n`,
|
||||
stderr: "",
|
||||
exitCode: 0,
|
||||
};
|
||||
} catch (err) {
|
||||
return {
|
||||
stdout: "",
|
||||
stderr: `[ERROR] 协助平台扫码登录失败: ${err instanceof Error ? err.message : String(err)}\n`,
|
||||
exitCode: 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user