Files
ops-assistant/templates/ops_targets.html
2026-03-19 21:23:28 +08:00

146 lines
7.0 KiB
HTML

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>目标主机配置 - Ops-Assistant</title>
<style>
:root{--bg:#f5f7fb;--text:#222;--card:#fff;--border:#e5e7eb;--muted:#6b7280;--accent:#ee5a24;--accent-hover:#d63031;--header-bg:linear-gradient(135deg,#ff6b6b,#ee5a24);}
[data-theme="dark"]{--bg:#0f172a;--text:#e5e7eb;--card:#111827;--border:#1f2937;--muted:#9ca3af;--accent:#f97316;--accent-hover:#ea580c;--header-bg:linear-gradient(135deg,#7c2d12,#ea580c);}
*{margin:0;padding:0;box-sizing:border-box}
body{font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,sans-serif;background:var(--bg);color:var(--text);min-height:100vh}
.header{background:var(--header-bg);color:#fff;padding:18px 20px;position:sticky;top:0;z-index:100;box-shadow:0 2px 10px rgba(0,0,0,.15);display:flex;justify-content:space-between;align-items:center;gap:10px}
.header a,.header button{color:#fff;text-decoration:none;background:rgba(255,255,255,.2);padding:6px 10px;border-radius:8px;font-size:13px;border:none;cursor:pointer}
.header button{display:inline-flex;align-items:center;justify-content:center}
.header button svg{width:16px;height:16px;stroke:#fff;fill:none;stroke-width:2}
.wrap{max-width:980px;margin:0 auto;padding:14px}
.card{background:var(--card);border-radius:6px;padding:14px;border:1px solid var(--border);box-shadow:none;margin-bottom:10px}
.row{display:flex;gap:8px;align-items:center;flex-wrap:wrap;margin:8px 0}
.row label{width:120px;color:var(--muted);font-size:13px}
.row input{flex:1;min-width:160px;padding:8px;border:1px solid var(--border);border-radius:6px;font-size:13px;background:var(--card);color:var(--text)}
button{border:none;border-radius:6px;padding:8px 12px;cursor:pointer;color:#fff;background:var(--accent);font-size:13px}
button.secondary{background:#6b7280}
button.danger{background:#9b1c1c}
.table{width:100%;border-collapse:collapse;font-size:13px}
.table th,.table td{border-bottom:1px solid var(--border);padding:8px;text-align:left}
.badge{display:inline-block;padding:2px 8px;border-radius:999px;font-size:12px}
.on{background:#dcfce7;color:#166534}.off{background:#e5e7eb;color:#374151}
small{color:var(--muted)}
.theme-hidden{display:none !important;}
</style>
</head>
<body>
<div class="header">
<div>🎯 目标主机配置 · {{.version}}</div>
<div>
<button id="themeToggle" class="theme-hidden" title="切换主题" aria-label="切换主题">
<svg id="themeIcon" viewBox="0 0 24 24" aria-hidden="true">
<circle cx="12" cy="12" r="5"></circle>
<line x1="12" y1="2" x2="12" y2="4"></line>
<line x1="12" y1="20" x2="12" y2="22"></line>
<line x1="2" y1="12" x2="4" y2="12"></line>
<line x1="20" y1="12" x2="22" y2="12"></line>
<line x1="4.2" y1="4.2" x2="5.8" y2="5.8"></line>
<line x1="18.2" y1="18.2" x2="19.8" y2="19.8"></line>
<line x1="18.2" y1="5.8" x2="19.8" y2="4.2"></line>
<line x1="4.2" y1="19.8" x2="5.8" y2="18.2"></line>
</svg>
</button>
<a href="/">返回首页</a>
<a href="/logout">退出</a>
</div>
</div>
<div class="wrap">
<div class="card">
<h3>新增目标</h3>
<div class="row"><label>Name</label><input id="name" placeholder="如 hwsg"></div>
<div class="row"><label>Host</label><input id="host" placeholder="如 124.243.132.158"></div>
<div class="row"><label>Port</label><input id="port" placeholder="22"></div>
<div class="row"><label>User</label><input id="user" placeholder="root"></div>
<div class="row"><label>Enabled</label><input id="enabled" type="checkbox" checked></div>
<div class="row"><button onclick="create()">新增</button><small id="msg"></small></div>
</div>
<div class="card">
<h3>已有目标</h3>
<table class="table">
<thead><tr><th>Name</th><th>Host</th><th>Port</th><th>User</th><th>Enabled</th><th>操作</th></tr></thead>
<tbody id="tbody"></tbody>
</table>
</div>
</div>
<script>
async function api(url,opt={}){
const r=await fetch(url,opt);
const out=await r.json().catch(()=>({}));
if(!r.ok) throw new Error(out.message||('HTTP '+r.status));
return out?.data||{};
}
function esc(s){return String(s||'').replace(/[&<>"']/g,c=>({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;',"'":'&#39;'}[c]));}
async function load(){
const d=await api('/api/v1/admin/ops/targets');
const list=Array.isArray(d.targets)?d.targets:[];
const tbody=document.getElementById('tbody');
if(!list.length){tbody.innerHTML='<tr><td colspan="6"><small>暂无目标</small></td></tr>';return;}
tbody.innerHTML=list.map(t=>`<tr>
<td>${esc(t.name)}</td>
<td><input data-id="${t.id}" data-field="host" value="${esc(t.host)}"></td>
<td><input data-id="${t.id}" data-field="port" value="${esc(t.port)}"></td>
<td><input data-id="${t.id}" data-field="user" value="${esc(t.user)}"></td>
<td>${t.enabled?'<span class="badge on">ON</span>':'<span class="badge off">OFF</span>'}</td>
<td>
<button class="secondary" onclick="save(${t.id})">保存</button>
<button class="danger" onclick="toggle(${t.id},${t.enabled?0:1})">${t.enabled?'禁用':'启用'}</button>
</td>
</tr>`).join('');
}
async function create(){
const payload={
name:document.getElementById('name').value.trim(),
host:document.getElementById('host').value.trim(),
port:parseInt(document.getElementById('port').value||'22',10),
user:document.getElementById('user').value.trim(),
enabled:document.getElementById('enabled').checked
};
try{
await api('/api/v1/admin/ops/targets',{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(payload)});
document.getElementById('msg').textContent='已新增';
await load();
}catch(e){document.getElementById('msg').textContent='失败:'+esc(e.message||e);}
}
async function save(id){
const inputs=[...document.querySelectorAll(`input[data-id="${id}"]`)];
const payload={enabled:true};
inputs.forEach(i=>{payload[i.dataset.field]=i.value.trim();});
payload.port=parseInt(payload.port||'22',10);
try{
await api('/api/v1/admin/ops/targets/'+id,{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify(payload)});
await load();
}catch(e){alert('保存失败:'+(e.message||e));}
}
async function toggle(id, enable){
try{
await api('/api/v1/admin/ops/targets/'+id,{method:'PATCH',headers:{'Content-Type':'application/json'},body:JSON.stringify({enabled:!!enable})});
await load();
}catch(e){alert('失败:'+(e.message||e));}
}
function applyTheme(theme){
if(theme==='dark'){document.documentElement.setAttribute('data-theme','dark');}
else{document.documentElement.removeAttribute('data-theme');}
localStorage.setItem('theme',theme);
}
function toggleTheme(){
const cur=localStorage.getItem('theme')||'dark';
applyTheme(cur==='light'?'dark':'light');
}
(function(){
const saved=localStorage.getItem('theme')||'dark';
applyTheme(saved);
const btn=document.getElementById('themeToggle');
if(btn){btn.addEventListener('click',toggleTheme)}
load();
})();
</script>
</body>
</html>