mirror of
https://github.com/Ed1s0nZ/CyberStrikeAI.git
synced 2026-07-13 23:47:24 +02:00
Add files via upload
This commit is contained in:
@@ -8,6 +8,10 @@ let authScope = '';
|
||||
let authPromise = null;
|
||||
let authPromiseResolvers = [];
|
||||
let isAppInitialized = false;
|
||||
let robotBindingCountdownTimer = null;
|
||||
let robotBindingExpiresAt = 0;
|
||||
let robotBindingLifetimeMs = 5 * 60 * 1000;
|
||||
let activeRobotBindingCode = '';
|
||||
|
||||
function isTokenValid() {
|
||||
return !!authToken && authTokenExpiry instanceof Date && authTokenExpiry.getTime() > Date.now();
|
||||
@@ -698,6 +702,182 @@ document.addEventListener('languagechange', function () {
|
||||
renderUserMenuProfile();
|
||||
});
|
||||
|
||||
async function openRobotAccountBinding() {
|
||||
setUserMenuOpen(false);
|
||||
openAppModal('robot-account-binding-modal');
|
||||
if (robotBindingExpiresAt) updateRobotBindingCountdown();
|
||||
await loadRobotAccountBindings();
|
||||
}
|
||||
|
||||
function closeRobotAccountBinding() {
|
||||
closeAppModal('robot-account-binding-modal');
|
||||
}
|
||||
|
||||
async function generateRobotBindingCode() {
|
||||
const generateBtn = document.getElementById('robot-binding-generate-btn');
|
||||
if (generateBtn) {
|
||||
generateBtn.disabled = true;
|
||||
generateBtn.textContent = '正在生成…';
|
||||
}
|
||||
let response;
|
||||
let data;
|
||||
try {
|
||||
response = await apiFetch('/api/auth/robot-binding-code', { method: 'POST' });
|
||||
data = await response.json().catch(() => ({}));
|
||||
} catch (error) {
|
||||
if (typeof showNotification === 'function') showNotification('生成绑定码失败,请检查网络连接', 'error');
|
||||
resetRobotBindingGenerateButton();
|
||||
return;
|
||||
}
|
||||
if (!response.ok || !data.code) {
|
||||
if (typeof showNotification === 'function') showNotification(data.error || '生成绑定码失败', 'error');
|
||||
resetRobotBindingGenerateButton();
|
||||
return;
|
||||
}
|
||||
const codeEl = document.getElementById('robot-binding-code');
|
||||
const copyBtn = document.getElementById('robot-binding-copy-btn');
|
||||
const card = document.getElementById('robot-binding-code-card');
|
||||
const timer = document.getElementById('robot-binding-timer');
|
||||
const state = document.getElementById('robot-binding-code-state');
|
||||
activeRobotBindingCode = data.code;
|
||||
robotBindingLifetimeMs = Math.max(1000, Number(data.expires_in_seconds || 300) * 1000);
|
||||
// Use the server-provided duration instead of comparing wall clocks, so a
|
||||
// client machine with clock skew still gets an accurate countdown.
|
||||
robotBindingExpiresAt = Date.now() + robotBindingLifetimeMs;
|
||||
if (codeEl) codeEl.textContent = data.code;
|
||||
if (copyBtn) copyBtn.disabled = false;
|
||||
if (card) card.className = 'robot-binding-code-card is-active';
|
||||
if (timer) timer.hidden = false;
|
||||
if (state) state.textContent = '等待绑定';
|
||||
if (generateBtn) {
|
||||
generateBtn.disabled = false;
|
||||
generateBtn.textContent = '重新生成';
|
||||
}
|
||||
startRobotBindingCountdown();
|
||||
}
|
||||
|
||||
function resetRobotBindingGenerateButton() {
|
||||
const generateBtn = document.getElementById('robot-binding-generate-btn');
|
||||
if (generateBtn) {
|
||||
generateBtn.disabled = false;
|
||||
generateBtn.textContent = activeRobotBindingCode ? '重新生成' : '生成绑定码';
|
||||
}
|
||||
}
|
||||
|
||||
function startRobotBindingCountdown() {
|
||||
if (robotBindingCountdownTimer) clearInterval(robotBindingCountdownTimer);
|
||||
updateRobotBindingCountdown();
|
||||
robotBindingCountdownTimer = setInterval(updateRobotBindingCountdown, 250);
|
||||
}
|
||||
|
||||
function updateRobotBindingCountdown() {
|
||||
if (!robotBindingExpiresAt) return;
|
||||
const remaining = Math.max(0, robotBindingExpiresAt - Date.now());
|
||||
const seconds = Math.ceil(remaining / 1000);
|
||||
const minutesPart = String(Math.floor(seconds / 60)).padStart(2, '0');
|
||||
const secondsPart = String(seconds % 60).padStart(2, '0');
|
||||
const countdown = document.getElementById('robot-binding-countdown');
|
||||
const progress = document.getElementById('robot-binding-progress-bar');
|
||||
const expiry = document.getElementById('robot-binding-expiry');
|
||||
if (countdown) countdown.textContent = `${minutesPart}:${secondsPart}`;
|
||||
if (progress) progress.style.width = `${Math.max(0, Math.min(100, remaining / robotBindingLifetimeMs * 100))}%`;
|
||||
if (expiry && activeRobotBindingCode) expiry.textContent = `请发送:绑定 ${activeRobotBindingCode}`;
|
||||
if (remaining <= 0) expireRobotBindingCode();
|
||||
}
|
||||
|
||||
function expireRobotBindingCode() {
|
||||
if (robotBindingCountdownTimer) clearInterval(robotBindingCountdownTimer);
|
||||
robotBindingCountdownTimer = null;
|
||||
robotBindingExpiresAt = 0;
|
||||
activeRobotBindingCode = '';
|
||||
const card = document.getElementById('robot-binding-code-card');
|
||||
const codeEl = document.getElementById('robot-binding-code');
|
||||
const state = document.getElementById('robot-binding-code-state');
|
||||
const expiry = document.getElementById('robot-binding-expiry');
|
||||
const countdown = document.getElementById('robot-binding-countdown');
|
||||
const progress = document.getElementById('robot-binding-progress-bar');
|
||||
const copyBtn = document.getElementById('robot-binding-copy-btn');
|
||||
if (card) card.className = 'robot-binding-code-card is-expired';
|
||||
if (codeEl) codeEl.textContent = '已失效';
|
||||
if (state) state.textContent = '已过期';
|
||||
if (expiry) expiry.textContent = '绑定码已过期,请重新生成';
|
||||
if (countdown) countdown.textContent = '00:00';
|
||||
if (progress) progress.style.width = '0%';
|
||||
if (copyBtn) copyBtn.disabled = true;
|
||||
resetRobotBindingGenerateButton();
|
||||
const generateBtn = document.getElementById('robot-binding-generate-btn');
|
||||
if (generateBtn) generateBtn.textContent = '重新生成';
|
||||
loadRobotAccountBindings();
|
||||
}
|
||||
|
||||
async function copyRobotBindingCode() {
|
||||
if (!activeRobotBindingCode || !robotBindingExpiresAt || robotBindingExpiresAt <= Date.now()) return;
|
||||
const command = `绑定 ${activeRobotBindingCode}`;
|
||||
try {
|
||||
await navigator.clipboard.writeText(command);
|
||||
} catch (_) {
|
||||
const textarea = document.createElement('textarea');
|
||||
textarea.value = command;
|
||||
textarea.style.position = 'fixed';
|
||||
textarea.style.opacity = '0';
|
||||
document.body.appendChild(textarea);
|
||||
textarea.select();
|
||||
document.execCommand('copy');
|
||||
textarea.remove();
|
||||
}
|
||||
const copyBtn = document.getElementById('robot-binding-copy-btn');
|
||||
const label = copyBtn?.querySelector('span');
|
||||
if (label) label.textContent = '已复制';
|
||||
setTimeout(() => { if (label) label.textContent = '复制命令'; }, 1400);
|
||||
if (typeof showNotification === 'function') showNotification('绑定命令已复制', 'success');
|
||||
}
|
||||
|
||||
async function loadRobotAccountBindings() {
|
||||
const list = document.getElementById('robot-account-binding-list');
|
||||
if (!list) return;
|
||||
const response = await apiFetch('/api/auth/robot-bindings');
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (!response.ok) {
|
||||
list.textContent = data.error || '获取绑定失败';
|
||||
return;
|
||||
}
|
||||
const bindings = Array.isArray(data.bindings) ? data.bindings : [];
|
||||
if (!bindings.length) {
|
||||
list.innerHTML = `<div class="robot-binding-empty-state">
|
||||
<span class="robot-binding-empty-icon"><svg width="24" height="24" viewBox="0 0 24 24" fill="none"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71" stroke="currentColor" stroke-width="2"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71" stroke="currentColor" stroke-width="2"/></svg></span>
|
||||
<div><strong>暂无绑定账号</strong><p>生成绑定码并在机器人中发送,即可完成首次绑定。</p></div>
|
||||
</div>`;
|
||||
return;
|
||||
}
|
||||
const platformLabels = { wechat: '微信', wecom: '企业微信', dingtalk: '钉钉', lark: '飞书', telegram: 'Telegram', slack: 'Slack', discord: 'Discord', qq: 'QQ' };
|
||||
list.innerHTML = bindings.map(binding => `
|
||||
<div class="robot-binding-account-card">
|
||||
<span class="robot-binding-platform-icon">${escapeHtml((platformLabels[binding.platform] || binding.platform || '?').slice(0, 1).toUpperCase())}</span>
|
||||
<div class="robot-binding-account-main">
|
||||
<div class="robot-binding-account-name"><strong>${escapeHtml(platformLabels[binding.platform] || binding.platform || '-')}</strong><span>已连接</span></div>
|
||||
<small>账号标识 ${escapeHtml(binding.external_user_hint || '-')} · 更新于 ${escapeHtml(formatRobotBindingTime(binding.updated_at))}</small>
|
||||
</div>
|
||||
<button type="button" class="btn-secondary btn-small robot-binding-unbind-btn" onclick="deleteRobotAccountBinding('${escapeHtml(binding.id || '')}')">解除绑定</button>
|
||||
</div>`).join('');
|
||||
}
|
||||
|
||||
function formatRobotBindingTime(value) {
|
||||
const date = new Date(value || '');
|
||||
if (Number.isNaN(date.getTime())) return '未知时间';
|
||||
return date.toLocaleString([], { month: '2-digit', day: '2-digit', hour: '2-digit', minute: '2-digit' });
|
||||
}
|
||||
|
||||
async function deleteRobotAccountBinding(id) {
|
||||
if (!id || !window.confirm('确定解除该机器人账号绑定吗?')) return;
|
||||
const response = await apiFetch(`/api/auth/robot-bindings/${encodeURIComponent(id)}`, { method: 'DELETE' });
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => ({}));
|
||||
if (typeof showNotification === 'function') showNotification(data.error || '解绑失败', 'error');
|
||||
return;
|
||||
}
|
||||
await loadRobotAccountBindings();
|
||||
}
|
||||
|
||||
// 退出登录
|
||||
async function logout() {
|
||||
// 关闭下拉菜单
|
||||
@@ -727,6 +907,11 @@ async function logout() {
|
||||
|
||||
// 导出函数供HTML使用
|
||||
window.toggleUserMenu = toggleUserMenu;
|
||||
window.openRobotAccountBinding = openRobotAccountBinding;
|
||||
window.closeRobotAccountBinding = closeRobotAccountBinding;
|
||||
window.generateRobotBindingCode = generateRobotBindingCode;
|
||||
window.copyRobotBindingCode = copyRobotBindingCode;
|
||||
window.deleteRobotAccountBinding = deleteRobotAccountBinding;
|
||||
window.logout = logout;
|
||||
window.hasPermission = hasPermission;
|
||||
window.hasAnyPermission = hasAnyPermission;
|
||||
|
||||
@@ -7,6 +7,7 @@ let alwaysVisibleBuiltinToolNames = new Set();
|
||||
// key: 唯一工具标识符(toolKey),value: { enabled: boolean, is_external: boolean, external_mcp: string }
|
||||
let toolStateMap = new Map();
|
||||
let activeRobotEditor = '';
|
||||
let robotAuthDrafts = {};
|
||||
|
||||
function settingsT(key, fallback) {
|
||||
if (typeof window.t === 'function') {
|
||||
@@ -258,6 +259,9 @@ function refreshRobotManager() {
|
||||
}
|
||||
|
||||
function openRobotEditor(type) {
|
||||
if (activeRobotEditor && activeRobotEditor !== type) {
|
||||
robotAuthDrafts[activeRobotEditor] = readRobotAuthPolicyEditor();
|
||||
}
|
||||
activeRobotEditor = type;
|
||||
const empty = document.getElementById('robot-editor-empty');
|
||||
if (empty) empty.hidden = true;
|
||||
@@ -269,6 +273,53 @@ function openRobotEditor(type) {
|
||||
if (panel) {
|
||||
panel.scrollIntoView({ behavior: 'smooth', block: 'start' });
|
||||
}
|
||||
loadRobotAuthPolicyEditor(type);
|
||||
}
|
||||
|
||||
function loadRobotAuthPolicyEditor(type) {
|
||||
const panel = document.getElementById('robot-auth-policy-panel');
|
||||
if (!panel) return;
|
||||
panel.hidden = !type;
|
||||
const auth = robotAuthDrafts[type] || (currentConfig?.robots?.[type]?.auth) || {};
|
||||
const mode = auth.mode === 'service_account' ? 'service_account' : 'user_binding';
|
||||
const modeInput = document.getElementById('robot-auth-mode');
|
||||
const serviceUserInput = document.getElementById('robot-service-user-id');
|
||||
const allowlistInput = document.getElementById('robot-allowed-external-users');
|
||||
if (modeInput) modeInput.value = mode;
|
||||
if (serviceUserInput) serviceUserInput.value = auth.service_user_id || '';
|
||||
if (allowlistInput) allowlistInput.value = Array.isArray(auth.allowed_external_users) ? auth.allowed_external_users.join('\n') : '';
|
||||
onRobotAuthModeChange();
|
||||
}
|
||||
|
||||
function onRobotAuthModeChange() {
|
||||
const serviceFields = document.getElementById('robot-service-account-fields');
|
||||
if (serviceFields) serviceFields.hidden = document.getElementById('robot-auth-mode')?.value !== 'service_account';
|
||||
updateRobotServiceAccountWarning();
|
||||
}
|
||||
|
||||
function updateRobotServiceAccountWarning() {
|
||||
const warning = document.getElementById('robot-service-admin-warning');
|
||||
if (!warning) return;
|
||||
const isServiceMode = document.getElementById('robot-auth-mode')?.value === 'service_account';
|
||||
const userID = document.getElementById('robot-service-user-id')?.value.trim().toLowerCase() || '';
|
||||
warning.hidden = !(isServiceMode && userID === 'admin');
|
||||
}
|
||||
|
||||
function readRobotAuthPolicyEditor() {
|
||||
const mode = document.getElementById('robot-auth-mode')?.value === 'service_account' ? 'service_account' : 'user_binding';
|
||||
if (mode === 'user_binding') return { mode };
|
||||
const allowed = (document.getElementById('robot-allowed-external-users')?.value || '')
|
||||
.split(/[\n,,]/).map(value => value.trim()).filter(Boolean);
|
||||
return {
|
||||
mode,
|
||||
service_user_id: document.getElementById('robot-service-user-id')?.value.trim() || '',
|
||||
allowed_external_users: Array.from(new Set(allowed))
|
||||
};
|
||||
}
|
||||
|
||||
function robotAuthPayload(type, prevRobots) {
|
||||
if (type === activeRobotEditor) return readRobotAuthPolicyEditor();
|
||||
return robotAuthDrafts[type] || (prevRobots[type] && prevRobots[type].auth) || { mode: 'user_binding' };
|
||||
}
|
||||
|
||||
function openRobotCreateModal() {
|
||||
@@ -904,6 +955,7 @@ async function loadConfig(loadTools = true, options = {}) {
|
||||
syncC2NavFromConfig(currentConfig);
|
||||
|
||||
// 填充机器人配置
|
||||
robotAuthDrafts = {};
|
||||
const robots = currentConfig.robots || {};
|
||||
const wechat = robots.wechat || {};
|
||||
const wecom = robots.wecom || {};
|
||||
@@ -1894,6 +1946,7 @@ async function applySettings() {
|
||||
...(prevRobots.session && typeof prevRobots.session === 'object' ? { session: prevRobots.session } : {}),
|
||||
wechat: {
|
||||
enabled: document.getElementById('robot-wechat-enabled')?.checked === true,
|
||||
auth: robotAuthPayload('wechat', prevRobots),
|
||||
base_url: document.getElementById('robot-wechat-base-url')?.value.trim() || 'https://ilinkai.weixin.qq.com',
|
||||
bot_type: document.getElementById('robot-wechat-bot-type')?.value.trim() || '3',
|
||||
bot_agent: document.getElementById('robot-wechat-bot-agent')?.value.trim() || 'CyberStrikeAI/1.0',
|
||||
@@ -1906,6 +1959,7 @@ async function applySettings() {
|
||||
},
|
||||
wecom: {
|
||||
enabled: document.getElementById('robot-wecom-enabled')?.checked === true,
|
||||
auth: robotAuthPayload('wecom', prevRobots),
|
||||
token: document.getElementById('robot-wecom-token')?.value.trim() || '',
|
||||
encoding_aes_key: document.getElementById('robot-wecom-encoding-aes-key')?.value.trim() || '',
|
||||
corp_id: document.getElementById('robot-wecom-corp-id')?.value.trim() || '',
|
||||
@@ -1914,12 +1968,14 @@ async function applySettings() {
|
||||
},
|
||||
dingtalk: {
|
||||
enabled: document.getElementById('robot-dingtalk-enabled')?.checked === true,
|
||||
auth: robotAuthPayload('dingtalk', prevRobots),
|
||||
client_id: document.getElementById('robot-dingtalk-client-id')?.value.trim() || '',
|
||||
client_secret: document.getElementById('robot-dingtalk-client-secret')?.value.trim() || '',
|
||||
allow_conversation_id_fallback: !!(prevRobots.dingtalk && prevRobots.dingtalk.allow_conversation_id_fallback)
|
||||
},
|
||||
lark: {
|
||||
enabled: document.getElementById('robot-lark-enabled')?.checked === true,
|
||||
auth: robotAuthPayload('lark', prevRobots),
|
||||
app_id: document.getElementById('robot-lark-app-id')?.value.trim() || '',
|
||||
app_secret: document.getElementById('robot-lark-app-secret')?.value.trim() || '',
|
||||
verify_token: document.getElementById('robot-lark-verify-token')?.value.trim() || '',
|
||||
@@ -1927,6 +1983,7 @@ async function applySettings() {
|
||||
},
|
||||
telegram: {
|
||||
enabled: document.getElementById('robot-telegram-enabled')?.checked === true,
|
||||
auth: robotAuthPayload('telegram', prevRobots),
|
||||
bot_token: document.getElementById('robot-telegram-bot-token')?.value.trim() || '',
|
||||
bot_username: document.getElementById('robot-telegram-bot-username')?.value.trim() || '',
|
||||
allow_group_messages: document.getElementById('robot-telegram-allow-group')?.checked === true,
|
||||
@@ -1936,16 +1993,19 @@ async function applySettings() {
|
||||
},
|
||||
slack: {
|
||||
enabled: document.getElementById('robot-slack-enabled')?.checked === true,
|
||||
auth: robotAuthPayload('slack', prevRobots),
|
||||
bot_token: document.getElementById('robot-slack-bot-token')?.value.trim() || '',
|
||||
app_token: document.getElementById('robot-slack-app-token')?.value.trim() || ''
|
||||
},
|
||||
discord: {
|
||||
enabled: document.getElementById('robot-discord-enabled')?.checked === true,
|
||||
auth: robotAuthPayload('discord', prevRobots),
|
||||
bot_token: document.getElementById('robot-discord-bot-token')?.value.trim() || '',
|
||||
allow_guild_messages: document.getElementById('robot-discord-allow-guild')?.checked === true
|
||||
},
|
||||
qq: {
|
||||
enabled: document.getElementById('robot-qq-enabled')?.checked === true,
|
||||
auth: robotAuthPayload('qq', prevRobots),
|
||||
app_id: document.getElementById('robot-qq-app-id')?.value.trim() || '',
|
||||
client_secret: document.getElementById('robot-qq-client-secret')?.value.trim() || '',
|
||||
sandbox: document.getElementById('robot-qq-sandbox')?.checked === true
|
||||
|
||||
Reference in New Issue
Block a user