Files
par/web/src/views/Sites.vue

218 lines
7.1 KiB
Vue

<template>
<div>
<h2>站点管理</h2>
<el-card style="margin-top: 20px">
<el-table :data="sites" v-loading="loading" stripe>
<el-table-column prop="siteId" label="站点ID" width="120" />
<el-table-column prop="name" label="名称" width="150" />
<el-table-column prop="baseUrl" label="基础URL" />
<el-table-column prop="siteType" label="类型" width="100" />
<el-table-column prop="status" label="状态" width="100">
<template #default="scope">
<el-tag :type="scope.row.status === 'active' ? 'success' : 'info'">
{{ scope.row.status }}
</el-tag>
</template>
</el-table-column>
<el-table-column prop="schemaVersion" label="Schema版本" width="120" />
<el-table-column label="操作" width="200">
<template #default="scope">
<el-button size="small" @click="handleEdit(scope.row)">编辑</el-button>
<el-button size="small" @click="handleViewConfig(scope.row)">配置</el-button>
</template>
</el-table-column>
</el-table>
</el-card>
<!-- 编辑对话框 -->
<el-dialog v-model="editVisible" title="编辑站点" width="500px" :close-on-click-modal="false">
<el-form :model="editForm" label-width="80px" @submit.prevent="handleSave">
<el-form-item label="站点ID">
<el-input v-model="editForm.siteId" disabled />
</el-form-item>
<el-form-item label="名称">
<el-input v-model="editForm.name" placeholder="站点名称" />
</el-form-item>
<el-form-item label="基础URL">
<el-input v-model="editForm.baseUrl" placeholder="https://example.com" />
</el-form-item>
<el-form-item label="类型">
<el-select v-model="editForm.siteType" placeholder="选择类型" style="width:100%">
<el-option v-for="t in siteTypes" :key="t.value" :label="t.label" :value="t.value" />
</el-select>
</el-form-item>
<el-form-item label="描述">
<el-input v-model="editForm.description" type="textarea" :rows="3" placeholder="站点描述" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="editVisible = false">取消</el-button>
<el-button type="primary" @click="handleSave" :loading="saving">保存</el-button>
</template>
</el-dialog>
<!-- 查看配置对话框 -->
<el-dialog v-model="configVisible" :title="'配置:' + configSiteId" width="700px" :close-on-click-modal="false">
<div v-if="configLoading" style="text-align:center;padding:40px">加载中...</div>
<div v-else-if="configError" style="color:#f56c6c;text-align:center;padding:20px">{{ configError }}</div>
<div v-else-if="!configContent" style="text-align:center;padding:40px;color:#909399">暂无配置</div>
<pre v-else class="config-json" v-html="highlightedJson"></pre>
<template #footer>
<el-button @click="configVisible = false">关闭</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup>
import api from '../api'
const sites = ref([])
const loading = ref(false)
const editVisible = ref(false)
const saving = ref(false)
const editForm = ref({ id: null, siteId: '', name: '', baseUrl: '', siteType: '', description: '' })
const siteTypes = [
{ value: 'nexusphp', label: 'NexusPHP' },
{ value: 'mtorrent', label: 'MTorrent' },
{ value: 'mteam', label: 'M-Team API' },
{ value: 'pttime', label: 'PTTime' },
{ value: 'gazelle', label: 'Gazelle' },
{ value: 'unit3d', label: 'UNIT3D' },
{ value: 'jsonapi', label: 'JSON API' },
{ value: 'unknown', label: '未知' },
]
// 配置查看
const configVisible = ref(false)
const configLoading = ref(false)
const configError = ref('')
const configContent = ref('')
const configSiteId = ref('')
const highlightedJson = computed(() => {
try {
const obj = JSON.parse(configContent.value)
return syntaxHighlight(JSON.stringify(obj, null, 2))
} catch {
return escapeHtml(configContent.value)
}
})
const handleViewConfig = async (row) => {
configVisible.value = true
configSiteId.value = row.siteId
configLoading.value = true
configError.value = ''
configContent.value = ''
try {
const res = await api.configs.bySite(row.siteId)
const configs = res.data || []
if (configs.length === 0) {
configContent.value = ''
} else {
const latest = configs[0] // already sorted by created_at desc
const resp = await api.configs.content(latest.siteId, latest.version)
const content = resp.data
configContent.value = typeof content === 'string' ? content : JSON.stringify(content, null, 2)
}
} catch (e) {
configError.value = e.response?.data?.message || '获取配置失败'
} finally {
configLoading.value = false
}
}
// syntax highlighting (reused from Configs.vue)
const syntaxHighlight = (json) => {
return escapeHtml(json).replace(
/("(\\u[a-fA-F0-9]{4}|\\[^u]|[^"\\])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g,
(match) => {
let cls = 'number'
if (/^"/.test(match)) {
cls = /:$/.test(match) ? 'key' : 'string'
} else if (/true|false/.test(match)) {
cls = 'boolean'
} else if (/null/.test(match)) {
cls = 'null'
}
return `<span class="${cls}">${match}</span>`
}
)
}
const escapeHtml = (str) => {
return str.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;')
}
const fetchSites = async () => {
loading.value = true
try {
const res = await api.sites.list()
sites.value = res.data || []
} catch (e) {
ElMessage.error('获取站点列表失败')
} finally {
loading.value = false
}
}
const handleEdit = (row) => {
editForm.value = {
id: row.id,
siteId: row.siteId,
name: row.name || '',
baseUrl: row.baseUrl || '',
siteType: row.siteType || '',
description: row.description || '',
}
editVisible.value = true
}
const handleSave = async () => {
if (!editForm.value.name.trim() || !editForm.value.baseUrl.trim()) {
ElMessage.warning('名称和基础URL不能为空')
return
}
saving.value = true
try {
await api.sites.update(editForm.value.siteId, {
name: editForm.value.name.trim(),
baseUrl: editForm.value.baseUrl.trim(),
siteType: editForm.value.siteType.trim() || 'unknown',
description: editForm.value.description.trim() || null,
})
ElMessage.success('保存成功')
editVisible.value = false
fetchSites()
} catch (e) {
ElMessage.error(e.response?.data?.message || '保存失败')
} finally {
saving.value = false
}
}
onMounted(fetchSites)
</script>
<style scoped>
.config-json {
background: #1e1e1e;
color: #d4d4d4;
padding: 12px;
border-radius: 4px;
font-size: 12px;
line-height: 1.5;
margin: 0;
white-space: pre-wrap;
word-break: break-all;
max-height: 500px;
overflow: auto;
}
.config-json :deep(.key) { color: #9cdcfe; }
.config-json :deep(.string) { color: #ce9178; }
.config-json :deep(.number) { color: #b5cea8; }
.config-json :deep(.boolean) { color: #569cd6; }
.config-json :deep(.null) { color: #569cd6; }
</style>