Files
hose-use/src/core/http/assist-platform-client.ts
T
gitea 95af2177e5 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 项测试通过
2026-11-02 14:58:20 +08:00

63 lines
2.2 KiB
TypeScript

import ky, { type KyInstance } from "ky";
import { AuthenticationError } from "../errors.js";
export const DEFAULT_ASSIST_PLATFORM_API_URL = "https://unity.ekuaibao.com/api";
export interface AssistPlatformClientOptions {
token: string;
baseUrl?: string;
hoseAccessToken?: string;
timeout?: number;
}
export function createAssistPlatformClient(options: AssistPlatformClientOptions): KyInstance {
if (!options.token || !options.token.trim()) {
throw new AuthenticationError("缺少协助平台凭证 Token,请先登录协助平台");
}
const baseUrl = options.baseUrl || DEFAULT_ASSIST_PLATFORM_API_URL;
const cookieParts: string[] = [];
if (options.hoseAccessToken) {
cookieParts.push(`hose-access-token=${options.hoseAccessToken}`);
}
return ky.create({
prefix: baseUrl,
timeout: options.timeout ?? 30000,
headers: {
accept: "application/json",
"content-type": "application/json;charset=UTF-8",
token: options.token.trim(),
referer: "https://unity.ekuaibao.com/platform-charge/index.html",
...(cookieParts.length > 0 ? { cookie: cookieParts.join("; ") } : {}),
},
hooks: {
afterResponse: [
async ({ response }) => {
if (response.status === 401 || response.status === 403) {
throw new AuthenticationError("协助平台凭证已过期或无效,请重新登录协助平台");
}
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
try {
const clone = response.clone();
const json = (await clone.json()) as any;
if (
json?.errorCode === 403 ||
(typeof json?.errorMessage === "string" && json.errorMessage.includes("token过期"))
) {
throw new AuthenticationError(
"协助平台凭证已过期或无效,请运行: hose-use assist-platform login 重新扫码登录"
);
}
} catch (err) {
if (err instanceof AuthenticationError) throw err;
}
}
return response;
},
],
},
});
}