import { useState } from "react"; import { createRoot } from "react-dom/client"; import "../styles.css"; import { api, type LarkBotKeyOption, type OpenPlatformKeyRequest, type OpenPlatformLarkBot } from "../lib/api"; import OpenPlatformRequestModal from "../components/OpenPlatformRequestModal"; import Toaster from "../components/Toaster"; import DialogHost from "../components/DialogHost"; /** * 本地演示:头像菜单 →「开放平台 API」弹窗,含「飞书机器人」登记区。不连任何接口 —— * 弹窗用到的 api 方法全换成内存里的假数据,登记 / 申请 / 停用 / 删除都只改这份数据。 * * 打开方式:control-web 下 `API_TARGET=http://127.0.0.1:9 bun x vite --port 8092`,访问 * /demo-openapi.html(API_TARGET 指向死端口,保证不会代理到任何真实服务)。 * `?empty=1` 看还没申请过任何 Key 的样子。 */ const now = Date.now(); const empty = new URLSearchParams(location.search).has("empty"); let requests: OpenPlatformKeyRequest[] = empty ? [] : [ { id: "opr_1", label: "Spoton", source: "spoton", reason: "话题群里 @ 机器人报 bug", requestedBy: "me@bytedance.com", requestedAt: now - 5 * 86_400_000, status: "approved", reviewedBy: "admin@bytedance.com", reviewedAt: now - 4 * 86_400_000, rejectReason: null, keyId: "opk_1", pickedUpAt: now - 4 * 86_400_000, hasSecretToPickup: false, larkBots: [{ id: "lkb_1a2b3c4d5e6f", name: "Spoton 助手", appId: "cli_a1b2c3d4e5f6a7b8" }], }, { id: "opr_2", label: "Spoton 海外", source: "spoton-sg", reason: null, requestedBy: "me@bytedance.com", requestedAt: now - 3_600_000, status: "pending", reviewedBy: null, reviewedAt: null, rejectReason: null, keyId: null, pickedUpAt: null, hasSecretToPickup: false, larkBots: [], }, ]; const keys = (): LarkBotKeyOption[] => empty ? [] : [ { id: "opk_1", label: "Spoton", pending: false }, ...requests.filter((r) => r.status === "pending").map((r) => ({ id: `req:${r.id}`, label: r.label, pending: true })), ]; let bots: OpenPlatformLarkBot[] = empty ? [] : [ { id: "lkb_1a2b3c4d5e6f", keyId: "opk_1", appId: "cli_a1b2c3d4e5f6a7b8", domain: "feishu", name: "Spoton 助手", avatarUrl: null, createdBy: "me@bytedance.com", createdAt: now - 4 * 86_400_000, disabledAt: null, disabledReason: null, consecutiveFailures: 0, lastSuccessAt: now - 20 * 60_000, lastFailureAt: null, lastError: null, }, { id: "lkb_9f8e7d6c5b4a", keyId: "opk_1", appId: "cli_b9c8d7e6f5a4b3c2", domain: "lark", name: "Spoton Global", avatarUrl: null, createdBy: "me@bytedance.com", createdAt: now - 86_400_000, disabledAt: now - 3_600_000, disabledReason: "连续 10 次发送失败,自动停用", consecutiveFailures: 10, lastSuccessAt: null, lastFailureAt: now - 3_600_000, lastError: "机器人不在被回复消息所在的群里,请把机器人拉进群", }, ]; const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); const verify = async (appSecret: string) => { await sleep(600); if (appSecret.length < 16) throw new Error("App ID / App Secret 校验失败(飞书返回 10014:app secret invalid)"); }; const newBot = (keyId: string, appId: string, domain: string): OpenPlatformLarkBot => ({ id: `lkb_${Math.random().toString(16).slice(2, 14)}`, keyId, appId, domain, name: `新机器人 ${appId.slice(-4)}`, avatarUrl: null, createdBy: "me@bytedance.com", createdAt: Date.now(), disabledAt: null, disabledReason: null, consecutiveFailures: 0, lastSuccessAt: null, lastFailureAt: null, lastError: null, }); Object.assign(api, { myOpenPlatformKeyRequests: async () => ({ requests }), requestOpenPlatformKey: async (label: string, source: string, reason: string | null, larkBot: { appId: string; appSecret: string; domain: string } | null) => { if (larkBot) await verify(larkBot.appSecret); const r: OpenPlatformKeyRequest = { id: `opr_${requests.length + 1}`, label, source, reason, requestedBy: "me@bytedance.com", requestedAt: Date.now(), status: "pending", reviewedBy: null, reviewedAt: null, rejectReason: null, keyId: null, pickedUpAt: null, hasSecretToPickup: false, larkBots: [], }; if (larkBot) { const b = newBot(`req:${r.id}`, larkBot.appId, larkBot.domain); bots = [b, ...bots]; r.larkBots = [{ id: b.id, name: b.name, appId: b.appId }]; } requests = [r, ...requests]; return { request: r }; }, pickupOpenPlatformKeyRequest: async () => ({ secret: "sk-sl-demo" }), myLarkBots: async () => ({ keys: keys(), bots }), createLarkBot: async (input: { keyId: string; appId: string; appSecret: string; domain: string }) => { await verify(input.appSecret); const b = newBot(input.keyId, input.appId, input.domain); bots = [b, ...bots]; return b; }, setLarkBotEnabled: async (id: string, enabled: boolean) => { bots = bots.map((b) => b.id !== id ? b : { ...b, disabledAt: enabled ? null : Date.now(), disabledReason: enabled ? null : "手动停用(me@bytedance.com)", consecutiveFailures: enabled ? 0 : b.consecutiveFailures }); return bots.find((b) => b.id === id)!; }, updateLarkBotSecret: async (id: string, secret: string) => { await verify(secret); bots = bots.map((b) => b.id !== id ? b : { ...b, disabledAt: null, disabledReason: null, consecutiveFailures: 0, lastError: null }); return bots.find((b) => b.id === id)!; }, deleteLarkBot: async (id: string) => { bots = bots.filter((b) => b.id !== id); return { ok: true }; }, }); function Demo() { const [open, setOpen] = useState(true); return (