支持自部署需求管理、交付流程与飞书集成

This commit is contained in:
xyt committed 2026-11-25 12:40:33 +08:00
1 parent 2149aeb6ed
commit 51a2e548f2
152 files changed
+8652 -6394

No files matched your search

+56
View File
@@ -0,0 +1,56 @@
import { createHash } from 'node:crypto';
import { appPath } from '../base-path.ts';
import { applicationOrigin } from './database.ts';
export function feishuLoginConfigured(): boolean {
return Boolean(process.env.FEISHU_APP_ID?.trim() && process.env.FEISHU_APP_SECRET?.trim());
}
export function feishuCallbackUrl(): string {
return applicationOrigin() + appPath('/api/auth/feishu/callback');
}
export function feishuLoginConfig() {
if (!feishuLoginConfigured()) throw new Error('飞书登录尚未配置 App ID 和 App Secret。');
return {
appId: process.env.FEISHU_APP_ID!.trim(), secret: process.env.FEISHU_APP_SECRET!.trim(),
callbackUrl: feishuCallbackUrl(), tenantKey: process.env.FEISHU_TENANT_KEY?.trim() || '',
};
}
export function feishuBotConfig() {
const webhook = process.env.FEISHU_BOT_WEBHOOK?.trim();
if (!webhook) return null;
let url: URL;
try { url = new URL(webhook); } catch { throw new Error('飞书群机器人 Webhook 地址无效。'); }
if (url.protocol !== 'https:' || url.hostname !== 'open.feishu.cn' || url.port || url.username || url.password || url.search || url.hash || !/^\/open-apis\/bot\/v2\/hook\/[a-zA-Z0-9_-]+$/.test(url.pathname)) {
throw new Error('飞书群机器人地址必须是 https://open.feishu.cn/open-apis/bot/v2/hook/ 开头的完整 Webhook。');
}
return { webhook: url.href, secret: process.env.FEISHU_BOT_SECRET?.trim() || '', targetKey: createHash('sha256').update(url.href).digest('hex') };
}
/** Never expose upstream error text: providers may echo credentials or user information. */
export async function feishuJson(url: string, init: RequestInit): Promise<Record<string, unknown>> {
const stage = ['https://accounts.feishu.cn/oauth/v3/token', 'https://open.feishu.cn/open-apis/authen/v2/oauth/token'].includes(url) ? 'oauth-token'
: url === 'https://open.feishu.cn/open-apis/authen/v1/user_info' ? 'user-info' : 'bot';
let response: Response;
try { response = await fetch(url, { ...init, redirect: 'error', cache: 'no-store', signal: AbortSignal.timeout(8000) }); }
catch (error) {
console.error('Feishu API failure', { stage, reason: error instanceof Error && error.name === 'TimeoutError' ? 'timeout' : 'network' });
throw new Error('飞书服务连接失败或超时。');
}
let data: unknown;
try { data = await response.json(); }
catch {
console.error('Feishu API failure', { stage, httpStatus: response.status, reason: 'invalid-json' });
throw new Error('飞书服务返回格式无效。');
}
if (!data || typeof data !== 'object' || Array.isArray(data)) {
console.error('Feishu API failure', { stage, httpStatus: response.status, reason: 'invalid-object' });
throw new Error('飞书服务返回格式无效。');
}
const record = data as Record<string, unknown>;
const code = record.code ?? record.StatusCode;
if (!response.ok || (typeof code === 'number' && code !== 0)) {
console.error('Feishu API failure', { stage, httpStatus: response.status, providerCode: typeof code === 'number' ? code : null });
}
if (!response.ok) throw new Error(`飞书服务返回 HTTP ${response.status}。`);
return data as Record<string, unknown>;
}