feat: 重构项目心跳数据结构并实现项目列表API

- 新增统一项目列表Redis键和迁移工具
- 实现GET /api/projects端点获取项目列表
- 实现POST /api/projects/migrate端点支持数据迁移
- 更新前端ProjectSelector组件使用真实项目数据
- 扩展projectStore状态管理
- 更新相关文档和OpenSpec规范
- 添加测试用例验证新功能
This commit is contained in:
2026-01-13 19:45:05 +08:00
parent 19e65d78dc
commit 282f7268ed
66 changed files with 4378 additions and 456 deletions

View File

@@ -5,6 +5,17 @@
<h2>项目选择</h2>
</div>
<!-- 加载/错误提示 -->
<div v-if="loading" class="state-text">
正在加载项目列表...
</div>
<div v-else-if="error" class="state-text state-error">
{{ error }}
</div>
<div v-else-if="noneConnected" class="state-text">
暂无连接
</div>
<!-- 项目列表 -->
<div class="project-list">
<div v-for="project in projects" :key="project.id" class="project-item"
@@ -18,13 +29,15 @@
{{ project.name }}
</div>
<div class="project-description">
{{ project.description }}
{{ project.apiBaseUrl || '未上报 API 地址' }}
</div>
<div class="project-meta">
<span class="project-type" :class="`type-${project.type}`">
{{ project.type === 'backend' ? '后端' : '前端' }}
<span class="project-status-text">
{{ getStatusText(project.status) }}
</span>
<span class="project-heartbeat">
{{ formatLastActiveAt(project.lastActiveAt) }}
</span>
<span class="project-status-text">{{ getStatusText(project.status) }}</span>
</div>
</div>
@@ -38,9 +51,10 @@
</template>
<script setup>
import { ref } from 'vue';
import { computed, onMounted, onUnmounted, ref } from 'vue';
import axios from 'axios';
import { projectsList, setProjectsList } from '../store/projectStore.js';
// 定义props和emits
defineProps({
selectedProjectId: {
type: String,
@@ -50,66 +64,92 @@ defineProps({
const emit = defineEmits(['project-selected']);
// 模拟项目数据
const projects = ref([
{
id: 'proj-1',
name: '用户管理系统',
type: 'backend',
description: '基于Node.js的用户管理后端服务',
status: 'running',
},
{
id: 'proj-2',
name: '数据可视化平台',
type: 'frontend',
description: '基于Vue.js的数据可视化前端应用',
status: 'running',
},
{
id: 'proj-3',
name: '订单处理系统',
type: 'backend',
description: '高性能订单处理后端服务',
status: 'stopped',
},
{
id: 'proj-4',
name: '移动端应用',
type: 'frontend',
description: '基于React Native的移动端应用',
status: 'error',
},
{
id: 'proj-5',
name: 'API网关',
type: 'backend',
description: '微服务架构的API网关',
status: 'running',
},
{
id: 'proj-6',
name: '管理后台',
type: 'frontend',
description: '基于Vue 3的管理后台系统',
status: 'running',
},
]);
const loading = ref(false);
const error = ref('');
const noneConnected = ref(false);
const initialized = ref(false);
let refreshTimer = null;
const projects = computed(() => projectsList.value || []);
function scheduleRefresh(ms) {
if (refreshTimer) clearInterval(refreshTimer);
refreshTimer = setInterval(fetchProjects, ms);
}
const fetchProjects = async () => {
if (!initialized.value) loading.value = true;
error.value = '';
try {
const resp = await axios.get('/api/projects');
if (resp.status === 200 && resp.data && resp.data.success) {
const list = resp.data.projects || [];
setProjectsList(Array.isArray(list) ? list : []);
if (Array.isArray(list) && list.length === 0) {
// Redis 就绪但没有任何心跳数据 → 显示“暂无连接”每2秒重试
noneConnected.value = true;
error.value = '';
scheduleRefresh(2000);
} else {
noneConnected.value = false;
scheduleRefresh(5000);
}
} else if (resp.status === 503) {
// Redis 未就绪显示错误并每5秒重试
setProjectsList([]);
noneConnected.value = false;
error.value = resp.data?.message || 'Redis 未就绪';
scheduleRefresh(5000);
} else {
// 其他非 200 情况当作错误
setProjectsList([]);
noneConnected.value = false;
error.value = resp.data?.message || `获取项目列表失败 (${resp.status})`;
scheduleRefresh(5000);
}
} catch (err) {
console.error('Failed to fetch projects:', err);
error.value = err?.response?.data?.message || err?.message || '获取项目列表失败';
setProjectsList([]);
noneConnected.value = false;
scheduleRefresh(5000);
} finally {
loading.value = false;
initialized.value = true;
}
};
// 选择项目
const selectProject = (project) => {
emit('project-selected', project);
};
// 获取状态文本
const getStatusText = (status) => {
const statusMap = {
running: '运行中',
stopped: '已停止',
error: '错误',
online: '在线',
offline: '离线',
unknown: '未知',
};
return statusMap[status] || status;
};
const formatLastActiveAt = (lastActiveAt) => {
if (!lastActiveAt) return '无心跳时间';
const ts = typeof lastActiveAt === 'number' ? lastActiveAt : Number(lastActiveAt);
if (!Number.isFinite(ts)) return '无心跳时间';
const date = new Date(ts);
return `心跳: ${date.toLocaleString('zh-CN')}`;
};
onMounted(() => {
fetchProjects();
refreshTimer = setInterval(fetchProjects, 5000);
});
onUnmounted(() => {
if (refreshTimer) clearInterval(refreshTimer);
});
</script>
<style scoped>
@@ -142,6 +182,15 @@ const getStatusText = (status) => {
max-height: calc(100vh - 150px);
}
.state-text {
padding: 0.8rem;
color: #b0b0b0;
}
.state-error {
color: #ea4335;
}
.project-item {
display: flex;
align-items: center;
@@ -176,15 +225,15 @@ const getStatusText = (status) => {
flex-shrink: 0;
}
.status-running {
.status-online {
background-color: #34a853;
}
.status-stopped {
.status-offline {
background-color: #fbbc05;
}
.status-error {
.status-unknown {
background-color: #ea4335;
}
@@ -219,6 +268,12 @@ const getStatusText = (status) => {
align-items: center;
}
.project-heartbeat {
font-size: 0.75rem;
color: #999;
white-space: nowrap;
}
.project-type {
font-size: 0.75rem;
padding: 0.15rem 0.5rem;
@@ -274,4 +329,4 @@ const getStatusText = (status) => {
.project-list::-webkit-scrollbar-thumb:hover {
background: #a8a8a8;
}
</style>
</style>