fix(settings): add DingTalk spaces and render the real login QR

Address two gaps in the first DingTalk-session UI:
- add an explicit admin-only '+ 新增钉钉知识库' action on the data-source
  panel; it accepts an alidocs space URL, resolves the live name/root through
  the authenticated sidecar, writes the inventory root atomically, upserts a
  deterministic source connection, and enqueues its first catalog refresh
- read DingTalk's real QR canvas pixels rather than taking a DOM screenshot;
  select by dark-pixel content, composite onto a white quiet zone, and render
  at 320x320 so the dialog contains a large complete code, not a page thumbnail

The live refresh path resolves registered roots from the connection locator,
while existing name-based DingTalk sources remain compatible.
This commit is contained in:
姚章浩
2026-09-14 22:53:01 +08:00
parent 173b44abca
commit e60b10256a
15 changed files with 560 additions and 79 deletions
+35
View File
@@ -41,6 +41,41 @@ export async function cancelDingtalkSession(id: string): Promise<void> {
await fetch(`/api/v1/dingtalk-session/${encodeURIComponent(id)}`, { method: 'DELETE', headers: { Accept: 'application/json' } }).catch(() => undefined)
}
export interface RegisteredDingtalkSpace {
connection_id: string
name: string
}
// Register a DingTalk knowledge-base space (URL or bare space id) as a new
// source connection. Errors carry the sidecar's machine codes so the UI can
// explain them: login_required, space_not_found, name_conflict.
export async function registerDingtalkSpace(url: string): Promise<RegisteredDingtalkSpace> {
const response = await fetch('/api/v1/dingtalk-spaces', {
method: 'POST',
headers: { Accept: 'application/json', 'Content-Type': 'application/json', 'X-Governance-Action': 'dingtalk-space', 'Idempotency-Key': newOperationID() },
body: JSON.stringify({ url }),
})
if (!response.ok) {
let code = ''
try {
const parsed = await response.json() as unknown
if (isRecord(parsed) && typeof parsed.error === 'string') code = parsed.error
} catch { /* status-only */ }
const friendly: Record<string, string> = {
login_required: '钉钉扫码会话无效或已过期,请先在钉钉来源卡片上「扫码刷新会话」',
space_not_found: '未找到该知识库空间:请确认空间链接正确且当前账号有阅读权限',
name_conflict: '该空间与已登记的来源同名',
invalid_space_url: '链接不是有效的钉钉知识库空间地址(应形如 alidocs.dingtalk.com/i/spaces/…)',
}
throw new Error(friendly[code] || `登记钉钉知识库失败(HTTP ${response.status}${code ? ':' + code : ''})`)
}
const value = await response.json() as unknown
if (!isRecord(value) || !isRecord(value.data)) throw new Error('invalid dingtalk space response')
const data = value.data
if (typeof data.connection_id !== 'string' || typeof data.name !== 'string') throw new Error('invalid dingtalk space response')
return { connection_id: data.connection_id, name: data.name }
}
function parseSession(value: unknown): DingtalkSessionView {
if (!isRecord(value) || typeof value.id !== 'string' || typeof value.state !== 'string' || !sessionStates.has(value.state)) {
throw new Error('invalid dingtalk session response')