- Add SSO QR status poller and assist platform HTTP client - Add assist-platform session store with 0600 secure permissions - Implement domains/assist-platform (api, services, types) for corporation search, detail, tickets, staff query and passwordless token generation - Implement cli/commands/assist-platform with aliases assist-center and ap - Support seamless --save --switch --elevate pipeline to import tenant and extract persistent OpenAPI credentials - Update embedded skill documentation, reference guide, and test suites
156 lines
4.3 KiB
TypeScript
156 lines
4.3 KiB
TypeScript
import type { KyInstance } from "ky";
|
|
import { BusinessError, NotFoundError } from "../../core/errors.js";
|
|
import { importFromLink, type ImportFromLinkOptions } from "../../core/auth/link-import.js";
|
|
import type { CorpConfig } from "../../core/config/types.js";
|
|
import {
|
|
getAssistTokenApi,
|
|
getCorporationDetailApi,
|
|
getStaffApi,
|
|
searchAssistTicketsApi,
|
|
searchCorporationsApi,
|
|
type SearchCorporationsApiParams,
|
|
type SearchCorporationsApiResponse,
|
|
} from "./api.js";
|
|
import type {
|
|
AssistPlatformCorporation,
|
|
AssistPlatformSessionResult,
|
|
AssistPlatformStaff,
|
|
AssistPlatformTicket,
|
|
CorporationDetail,
|
|
} from "./types.js";
|
|
|
|
export async function searchCorporations(
|
|
client: KyInstance,
|
|
params: SearchCorporationsApiParams = {}
|
|
): Promise<SearchCorporationsApiResponse> {
|
|
return await searchCorporationsApi(client, params);
|
|
}
|
|
|
|
export async function getCorporationDetail(
|
|
client: KyInstance,
|
|
corpId: string
|
|
): Promise<CorporationDetail> {
|
|
return await getCorporationDetailApi(client, corpId);
|
|
}
|
|
|
|
export async function listAssistTickets(
|
|
client: KyInstance,
|
|
corpId: string
|
|
): Promise<AssistPlatformTicket[]> {
|
|
return await searchAssistTicketsApi(client, corpId);
|
|
}
|
|
|
|
export async function findStaff(
|
|
client: KyInstance,
|
|
corpId: string,
|
|
name: string
|
|
): Promise<AssistPlatformStaff[]> {
|
|
return await getStaffApi(client, corpId, name);
|
|
}
|
|
|
|
export interface GenerateAssistSessionParams {
|
|
corpId: string;
|
|
ticketId?: string;
|
|
isAdmin?: boolean;
|
|
staffName?: string;
|
|
staffId?: string;
|
|
}
|
|
|
|
/**
|
|
* 智能解析并换取协助平台的免密登录直通链接
|
|
*/
|
|
export async function generateAssistSession(
|
|
client: KyInstance,
|
|
params: GenerateAssistSessionParams
|
|
): Promise<AssistPlatformSessionResult> {
|
|
const corpId = params.corpId.trim();
|
|
let ticketId = params.ticketId?.trim();
|
|
|
|
// 若未手动指定授权码 ticketId,自动检索该企业名下首个有效状态工单
|
|
if (!ticketId) {
|
|
const tickets = await searchAssistTicketsApi(client, corpId);
|
|
if (tickets.length === 0) {
|
|
throw new NotFoundError(`企业 [${corpId}] 协助中心未查询到任何协助工单`);
|
|
}
|
|
|
|
const activeTicket =
|
|
tickets.find((t) => t.state === "已提供" || t.state === "待提供") ?? tickets[0];
|
|
|
|
if (!activeTicket || !activeTicket.ticketId) {
|
|
throw new NotFoundError(`企业 [${corpId}] 暂无可用状态的协助授权工单`);
|
|
}
|
|
ticketId = activeTicket.ticketId;
|
|
}
|
|
|
|
const isAdmin = params.isAdmin !== false;
|
|
let resolvedStaffId = params.staffId?.trim();
|
|
let resolvedStaffName = params.staffName?.trim();
|
|
|
|
// 若要求用户模式且指定了姓名,自动检索员工工号
|
|
if (!isAdmin && resolvedStaffName && !resolvedStaffId) {
|
|
const staffList = await getStaffApi(client, corpId, resolvedStaffName);
|
|
if (staffList.length === 0) {
|
|
throw new NotFoundError(
|
|
`在企业 [${corpId}] 中未找到名为 "${resolvedStaffName}" 的员工`
|
|
);
|
|
}
|
|
const targetStaff = staffList[0];
|
|
resolvedStaffId = targetStaff.id;
|
|
resolvedStaffName = targetStaff.name;
|
|
}
|
|
|
|
const tokenRes = await getAssistTokenApi(client, {
|
|
ticketId,
|
|
corporationId: corpId,
|
|
isAdmin,
|
|
staffId: resolvedStaffId,
|
|
});
|
|
|
|
return {
|
|
token: tokenRes.token,
|
|
loginUrl: tokenRes.loginUrl,
|
|
baseUrl: tokenRes.baseUrl,
|
|
ticketId,
|
|
corporationId: corpId,
|
|
isAdmin,
|
|
staffId: resolvedStaffId,
|
|
staffName: resolvedStaffName,
|
|
};
|
|
}
|
|
|
|
export interface ImportAssistSessionParams extends GenerateAssistSessionParams {
|
|
saveName?: string;
|
|
overwrite?: boolean;
|
|
enterprisesDir?: string;
|
|
ttl?: string | number;
|
|
elevate?: boolean;
|
|
}
|
|
|
|
/**
|
|
* 自动化闭环:换取协助免密链接,并通过 importFromLink 一键落盘持久化至本地企业配置
|
|
*/
|
|
export async function importAssistSessionToLocal(
|
|
client: KyInstance,
|
|
params: ImportAssistSessionParams
|
|
): Promise<{
|
|
sessionResult: AssistPlatformSessionResult;
|
|
corpConfig: CorpConfig;
|
|
}> {
|
|
const sessionResult = await generateAssistSession(client, params);
|
|
|
|
const importOptions: ImportFromLinkOptions = {
|
|
name: params.saveName,
|
|
overwrite: params.overwrite ?? true,
|
|
enterprisesDir: params.enterprisesDir,
|
|
ttl: params.ttl,
|
|
elevate: params.elevate ?? true,
|
|
};
|
|
|
|
const corpConfig = await importFromLink(sessionResult.loginUrl, importOptions);
|
|
|
|
return {
|
|
sessionResult,
|
|
corpConfig,
|
|
};
|
|
}
|