/* vps-manager 前端应用(Vue 3 组件化) */ const { createApp, reactive, ref, computed, onMounted, watch } = Vue; const Api = window.VpsApi; const Fmt = window.VpsFmt; /* ---------------- 全局状态 ---------------- */ const store = reactive({ view: 'dashboard', assets: [], providers: [], overview: {}, expiring: [], serverMetrics: {}, search: '', filterType: '', filterStatus: '', dark: localStorage.getItem('vps_dark') === '1', loading: false, error: '', assetModal: { show: false, editing: null, form: null }, providerModal: { show: false, editing: null, form: null }, }); function applyDark() { document.documentElement.classList.toggle('dark', store.dark); localStorage.setItem('vps_dark', store.dark ? '1' : '0'); } /* ---------------- 表单工厂 ---------------- */ function emptyAssetForm() { return { name: '', asset_type: 'vps', provider: '', provider_id: null, renewal_cycle: 'monthly', account: '', expiry_date: '', auto_renew: false, cost: 0, currency: 'USD', status: 'active', is_archived: false, remark: '', vps_detail: { ip_address: '', tailscale_ip: '', region: '', os: '', cpu_cores: 1, memory_gb: 1, disk_gb: 20, bandwidth_gb: null, ssh_port: 22, panel_url: '', ssh_user: '', login_method: 'key', ssh_key: '', password: '', purpose: '' }, domain_detail: { domain_name: '', registrar: '', dns_provider: '', cloudflare_account: '', is_using: true, redirect_target: '', bind_asset_id: null }, ai_detail: { provider: '', api_key: '', plan: '', balance: null, currency: 'USD', monthly_usage: null, monthly_limit: null }, }; } function buildAssetPayload(f) { const p = { name: f.name, asset_type: f.asset_type, provider: f.provider, provider_id: f.provider_id || null, renewal_cycle: f.renewal_cycle || null, account: f.account || null, expiry_date: f.expiry_date || null, auto_renew: !!f.auto_renew, cost: Number(f.cost) || 0, currency: f.currency, status: f.status, is_archived: !!f.is_archived, remark: f.remark || null, }; if (f.asset_type === 'vps') { const v = f.vps_detail; p.vps_detail = { ip_address: v.ip_address, tailscale_ip: v.tailscale_ip || null, region: v.region || null, os: v.os || null, cpu_cores: Number(v.cpu_cores) || 1, memory_gb: Number(v.memory_gb) || 1, disk_gb: Number(v.disk_gb) || 20, bandwidth_gb: v.bandwidth_gb ? Number(v.bandwidth_gb) : null, ssh_port: Number(v.ssh_port) || 22, panel_url: v.panel_url || null, ssh_user: v.ssh_user || null, login_method: v.login_method || 'key', ssh_key: v.ssh_key || null, password: v.password || null, purpose: v.purpose || null }; } else if (f.asset_type === 'domain') { const d = f.domain_detail; p.domain_detail = { domain_name: d.domain_name, registrar: d.registrar || null, dns_provider: d.dns_provider || null, cloudflare_account: d.cloudflare_account || null, is_using: !!d.is_using, redirect_target: d.redirect_target || null, bind_asset_id: d.bind_asset_id || null }; } else if (f.asset_type === 'ai_agent') { const a = f.ai_detail; p.ai_detail = { provider: a.provider, api_key: a.api_key || null, plan: a.plan || null, balance: a.balance ? Number(a.balance) : null, currency: a.currency || 'USD', monthly_usage: a.monthly_usage ? Number(a.monthly_usage) : null, monthly_limit: a.monthly_limit ? Number(a.monthly_limit) : null }; } return p; } /* ---------------- 数据加载 ---------------- */ async function loadAssets() { const params = new URLSearchParams(); if (store.filterType) params.set('asset_type', store.filterType); if (store.filterStatus) params.set('status', store.filterStatus); if (store.search) params.set('q', store.search); params.set('sort', 'expiry_date'); params.set('order', 'asc'); store.assets = await Api.get('/assets?' + params.toString()); } async function loadProviders() { store.providers = await Api.get('/providers'); } async function loadOverview() { store.overview = await Api.get('/stats/overview'); } async function loadExpiring() { store.expiring = await Api.get('/stats/expiring?days=30'); } async function loadAll() { store.loading = true; store.error = ''; try { await Promise.all([loadAssets(), loadProviders(), loadOverview(), loadExpiring()]); } catch (e) { store.error = e.message; } finally { store.loading = false; } } /* ---------------- 资产操作 ---------------- */ function openAssetCreate(presetType) { const form = emptyAssetForm(); if (presetType) form.asset_type = presetType; store.assetModal = { show: true, editing: null, form }; } function openAssetEdit(a) { const form = emptyAssetForm(); ['name', 'asset_type', 'provider', 'provider_id', 'renewal_cycle', 'account', 'expiry_date', 'auto_renew', 'cost', 'currency', 'status', 'is_archived', 'remark'].forEach(k => { form[k] = a[k]; }); if (a.vps_detail) Object.assign(form.vps_detail, a.vps_detail); if (a.domain_detail) Object.assign(form.domain_detail, a.domain_detail); if (a.ai_detail) Object.assign(form.ai_detail, a.ai_detail); form.vps_detail.ssh_key = ''; form.vps_detail.password = ''; store.assetModal = { show: true, editing: a.id, form }; } async function saveAsset() { store.error = ''; const f = store.assetModal.form; try { const payload = buildAssetPayload(f); if (store.assetModal.editing) await Api.put('/assets/' + store.assetModal.editing, payload); else await Api.post('/assets', payload); store.assetModal.show = false; await loadAll(); } catch (e) { store.error = e.message; } } async function deleteAsset(a) { if (!confirm('确认删除资产「' + a.name + '」?此操作不可恢复。')) return; store.error = ''; try { await Api.del('/assets/' + a.id); await loadAll(); } catch (e) { store.error = e.message; } } /* ---------------- 平台操作 ---------------- */ function openProviderCreate() { store.providerModal = { show: true, editing: null, form: { slug: '', name: '', name_en: '', category: 'vps', website: '', console_url: '', sdk_type: '', enabled: true, remark: '', api_config: '' } }; } function openProviderEdit(p) { store.providerModal = { show: true, editing: p.id, form: { slug: p.slug, name: p.name, name_en: p.name_en || '', category: p.category, website: p.website || '', console_url: p.console_url || '', sdk_type: p.sdk_type || '', enabled: p.enabled, remark: p.remark || '', api_config: '' } }; } async function saveProvider() { store.error = ''; const f = store.providerModal.form; const payload = { slug: f.slug, name: f.name, name_en: f.name_en || null, category: f.category, website: f.website || null, console_url: f.console_url || null, sdk_type: f.sdk_type || null, enabled: !!f.enabled, remark: f.remark || null, api_config: f.api_config || null }; try { if (store.providerModal.editing) await Api.put('/providers/' + store.providerModal.editing, payload); else await Api.post('/providers', payload); store.providerModal.show = false; await loadProviders(); } catch (e) { store.error = e.message; } } async function seedProviders() { store.error = ''; try { const r = await Api.post('/providers/seed', {}); await loadProviders(); alert('已初始化预设平台,新增 ' + r.added + ' 个'); } catch (e) { store.error = e.message; } } async function deleteProvider(p) { if (!confirm('确认删除平台「' + p.name + '」?')) return; try { await Api.del('/providers/' + p.id); await loadProviders(); } catch (e) { store.error = e.message; } } /* ---------------- 服务器监控 ---------------- */ async function viewServer(a) { store.view = 'servers'; try { const [metrics, info, checks] = await Promise.all([ Api.get('/monitor/' + a.id + '/metrics'), Api.get('/monitor/' + a.id + '/info'), Api.get('/monitor/' + a.id + '/security'), ]); store.serverMetrics = { asset: a, metrics, info, checks }; } catch (e) { store.serverMetrics = { asset: a, metrics: [], info: null, checks: [] }; } } /* ---------------- 设置 ---------------- */ const settings = reactive({ apiKey: Api.getApiKey(), saved: false }); function saveSettings() { Api.setApiKey(settings.apiKey); settings.saved = true; setTimeout(() => settings.saved = false, 2000); } /* ================= 视图组件 ================= */ const DashboardView = { template: `
| 名称 | 类型 | 平台 | 到期 | 费用 | 状态 | 操作 |
|---|---|---|---|---|---|---|
| 暂无资产 | ||||||
| {{ a.name }} | {{ Fmt.TYPE_LABELS[a.asset_type] }} | {{ a.provider_name || a.provider }} | {{ a.expiry_date || '—' }}({{ a.days_to_expiry }}天) | {{ a.cost }} {{ a.currency }}/{{ Fmt.CYCLE_LABELS[a.renewal_cycle] || a.renewal_cycle }} | {{ Fmt.STATUS_LABELS[a.status] }} | |
| 域名 | 注册商 | DNS | 到期 | 使用中 | 操作 |
|---|---|---|---|---|---|
| 暂无域名资产 | |||||
| {{ a.domain_detail.domain_name }} | {{ a.domain_detail.registrar || '—' }} | {{ a.domain_detail.dns_provider || '—' }} | {{ a.expiry_date || '—' }}({{ a.days_to_expiry }}天) | {{ a.domain_detail.is_using ? '使用' : '闲置' }} | |
若服务端 .env 配置了 API_KEY,请在此填写相同的值,前端写操作会自动携带。