/** * 合思开放平台接口 → 所需企业应用 的权限映射。 * * ── 为什么需要这张表 ──────────────────────────────────────────────── * * 工作流 HTTP 节点调用某些开放接口时,企业必须先开通对应的「应用商店」应用, * 否则运行时会直接返回: * * HTTP 403 未开通功能,无权访问 * * 这个 403 发生在流程跑到一半的时候,报错信息本身不含「缺哪个应用」, * 排查成本极高(真实案例:任子行「单据审批」未开通,链路跑到第 9 步才炸)。 * * 所以要在**保存/静态分析阶段**就根据接口路径反推所需应用,提前告警。 * * ── 判定边界(重要)──────────────────────────────────────────────── * * 只有 HTTP 节点、且 URL 里存在**静态可读的接口路径**时才判断。 * URL 若整体由上游节点变量拼接(如 `@{choose(...)}` 开头),运行前无人知道 * 会打到哪个接口,这类情况一律跳过,不做任何猜测性告警。 */ export interface OpenApiAppRequirement { /** 接口的人类可读名称 */ apiName: string; /** 静态路径匹配正则(作用在去掉域名、去掉变量段之后的路径上) */ pathPattern: RegExp; /** 需要校验的 HTTP 方法;省略表示不限方法 */ methods?: string[]; /** 所需应用名称 */ appName: string; /** 应用提供的权限代码,用于与已开通应用交叉核验 */ powerCode: string; /** 应用商店商品 ID,便于在告警里直接给出定位信息 */ productId?: string; /** 补充说明 */ hint?: string; } /** * 已知需要企业应用授权的开放接口。 * * 新增条目时请以线上真实工作流抓到的请求为准(URL + method + powerCode 三者一起核)。 */ export const OPENAPI_APP_REQUIREMENTS: OpenApiAppRequirement[] = [ { apiName: "单据审批(执行单据动作:同意 / 驳回 / 加签)", // POST /api/openapi/v1/backlog/data/{flowId} // 设计器里 flowId 通常写成 [@{choose(...)}],静态前缀到 backlog/data/ 为止 pathPattern: /^\/api\/openapi\/v1\/backlog\/data\//i, methods: ["POST", "PUT", "PATCH"], appName: "单据审批", powerCode: "TICKET_AUDIT_switch", productId: "o0hMBw000000g0", hint: "接口行为等效于在单据上手动点击「同意 / 驳回 / 加签」,会真实改变单据状态。", }, ]; /** * 从 HTTP 节点的 URL 中提取「静态可判断的路径前缀」。 * * 规则: * - 去掉协议与域名; * - 截断到第一个 `@{...}` 变量之前; * - 去掉 query 与 hash。 * * 若 URL 本身以变量开头(完全没有静态路径),返回 `null` —— 表示无法静态判断。 * * 示例: * ``` * https://wx2.ekuaibao.com/api/openapi/v1/backlog/data/[@{choose(...)}] * → /api/openapi/v1/backlog/data/[ * * @{choose(...)} * → null(纯变量拼接,放弃判断) * ``` */ export function extractStaticUrlPath(url: unknown): string | null { if (typeof url !== "string") return null; const trimmed = url.trim(); if (!trimmed) return null; let rest = trimmed; const schemeMatch = rest.match(/^https?:\/\/[^/]+/i); if (schemeMatch) { rest = rest.slice(schemeMatch[0].length); } else if (rest.startsWith("@{") || rest.startsWith("${")) { // 纯变量开头,没有可读的静态路径 return null; } const varIndex = rest.indexOf("@{"); const staticPart = varIndex === -1 ? rest : rest.slice(0, varIndex); if (!staticPart) return null; const path = staticPart.split("?")[0].split("#")[0].trim(); return path || null; } export interface MatchedAppRequirement { requirement: OpenApiAppRequirement; /** 提取到的静态路径,便于在诊断信息里展示判断依据 */ staticPath: string; } /** * 根据 URL 与 HTTP 方法匹配「需要应用授权」的接口规则。 * * 只有能提取出静态路径、且方法命中时才返回结果;否则返回 `null`(不判断、不告警)。 */ export function matchAppRequirement(url: unknown, method?: unknown): MatchedAppRequirement | null { const staticPath = extractStaticUrlPath(url); if (!staticPath) return null; const normalizedMethod = typeof method === "string" ? method.trim().toUpperCase() : ""; for (const requirement of OPENAPI_APP_REQUIREMENTS) { // 方法:节点没写 method 时不作为排除条件(路径已是足够强的信号); // 写了 method 但与规则不符才跳过。 if (requirement.methods && requirement.methods.length > 0 && normalizedMethod) { if (!requirement.methods.includes(normalizedMethod)) continue; } if (!requirement.pathPattern.test(staticPath)) continue; return { requirement, staticPath }; } return null; }