add: 为前后端添加exam数据库管理
This commit is contained in:
516
src/views/ExamView.vue
Normal file
516
src/views/ExamView.vue
Normal file
@@ -0,0 +1,516 @@
|
||||
<template>
|
||||
<div class="exam-page">
|
||||
<div class="page-header">
|
||||
<h1>实验列表</h1>
|
||||
<div class="header-actions">
|
||||
<button @click="refreshExams" class="btn btn-primary" :disabled="loading">
|
||||
<i class="icon-refresh" :class="{ spinning: loading }"></i>
|
||||
刷新
|
||||
</button>
|
||||
<button v-if="isAdmin" @click="scanExams" class="btn btn-secondary" :disabled="scanning">
|
||||
<i class="icon-scan" :class="{ spinning: scanning }"></i>
|
||||
扫描实验
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-if="loading" class="loading-container">
|
||||
<div class="loading-spinner"></div>
|
||||
<p>正在加载实验列表...</p>
|
||||
</div>
|
||||
|
||||
<div v-else-if="error" class="error-container">
|
||||
<div class="error-message">
|
||||
<i class="icon-error"></i>
|
||||
<h3>加载失败</h3>
|
||||
<p>{{ error }}</p>
|
||||
<button @click="refreshExams" class="btn btn-primary">重试</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div v-else class="exam-list">
|
||||
<div v-if="exams.length === 0" class="empty-state">
|
||||
<i class="icon-empty"></i>
|
||||
<h3>暂无实验</h3>
|
||||
<p>当前没有可用的实验,请联系管理员添加实验内容。</p>
|
||||
</div>
|
||||
|
||||
<div v-else class="exam-grid">
|
||||
<div
|
||||
v-for="exam in exams"
|
||||
:key="exam.id"
|
||||
class="exam-card"
|
||||
@click="viewExam(exam.id)"
|
||||
>
|
||||
<div class="exam-card-header">
|
||||
<h3 class="exam-title">{{ exam.title || exam.id }}</h3>
|
||||
<span class="exam-id">{{ exam.id }}</span>
|
||||
</div>
|
||||
<div class="exam-card-content">
|
||||
<div class="exam-meta">
|
||||
<div class="meta-item">
|
||||
<i class="icon-calendar"></i>
|
||||
<span>创建时间:{{ formatDate(exam.createdTime) }}</span>
|
||||
</div>
|
||||
<div class="meta-item">
|
||||
<i class="icon-clock"></i>
|
||||
<span>更新时间:{{ formatDate(exam.updatedTime) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="exam-card-footer">
|
||||
<button class="btn btn-outline">查看详情</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 实验详情模态框 -->
|
||||
<div v-if="selectedExam" class="modal-overlay" @click="closeExamDetail">
|
||||
<div class="modal-content exam-detail" @click.stop>
|
||||
<div class="modal-header">
|
||||
<h2>{{ selectedExam.id }}</h2>
|
||||
<button @click="closeExamDetail" class="btn-close">×</button>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<div class="exam-info">
|
||||
<div class="info-row">
|
||||
<span class="label">实验ID:</span>
|
||||
<span class="value">{{ selectedExam.id }}</span>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<span class="label">创建时间:</span>
|
||||
<span class="value">{{ formatDate(selectedExam.createdTime) }}</span>
|
||||
</div>
|
||||
<div class="info-row">
|
||||
<span class="label">更新时间:</span>
|
||||
<span class="value">{{ formatDate(selectedExam.updatedTime) }}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="exam-content">
|
||||
<MarkdownRenderer :content="selectedExam.docContent" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 扫描结果提示 -->
|
||||
<div v-if="scanResult" class="toast" :class="{ show: showToast }">
|
||||
<i class="icon-success"></i>
|
||||
<span>{{ scanResult.message }}</span>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted, computed } from 'vue'
|
||||
import { AuthManager } from '@/utils/AuthManager'
|
||||
import { ExamClient, DataClient, type ExamSummary, type ExamInfo, type ScanResult } from '@/APIClient'
|
||||
import MarkdownRenderer from '@/components/MarkdownRenderer.vue'
|
||||
|
||||
// 响应式数据
|
||||
const exams = ref<ExamSummary[]>([])
|
||||
const selectedExam = ref<ExamInfo | null>(null)
|
||||
const loading = ref(false)
|
||||
const scanning = ref(false)
|
||||
const error = ref<string>('')
|
||||
const scanResult = ref<ScanResult | null>(null)
|
||||
const showToast = ref(false)
|
||||
const isAdmin = ref(false)
|
||||
|
||||
// 方法
|
||||
const checkAdminStatus = async () => {
|
||||
try {
|
||||
isAdmin.value = await AuthManager.verifyAdminAuth()
|
||||
} catch (err) {
|
||||
console.warn('无法验证管理员权限:', err)
|
||||
isAdmin.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const refreshExams = async () => {
|
||||
loading.value = true
|
||||
error.value = ''
|
||||
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedExamClient()
|
||||
exams.value = await client.getExamList()
|
||||
} catch (err: any) {
|
||||
error.value = err.message || '获取实验列表失败'
|
||||
console.error('获取实验列表失败:', err)
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const viewExam = async (examId: string) => {
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedExamClient()
|
||||
selectedExam.value = await client.getExam(examId)
|
||||
} catch (err: any) {
|
||||
error.value = err.message || '获取实验详情失败'
|
||||
console.error('获取实验详情失败:', err)
|
||||
}
|
||||
}
|
||||
|
||||
const closeExamDetail = () => {
|
||||
selectedExam.value = null
|
||||
}
|
||||
|
||||
const scanExams = async () => {
|
||||
scanning.value = true
|
||||
|
||||
try {
|
||||
const client = AuthManager.createAuthenticatedExamClient()
|
||||
scanResult.value = await client.scanExams()
|
||||
showToast.value = true
|
||||
setTimeout(() => {
|
||||
showToast.value = false
|
||||
}, 3000)
|
||||
|
||||
// 刷新实验列表
|
||||
await refreshExams()
|
||||
} catch (err: any) {
|
||||
error.value = err.message || '扫描实验失败'
|
||||
console.error('扫描实验失败:', err)
|
||||
} finally {
|
||||
scanning.value = false
|
||||
}
|
||||
}
|
||||
|
||||
const formatDate = (date: Date | string) => {
|
||||
const dateObj = typeof date === 'string' ? new Date(date) : date
|
||||
return dateObj.toLocaleString('zh-CN', {
|
||||
year: 'numeric',
|
||||
month: '2-digit',
|
||||
day: '2-digit',
|
||||
hour: '2-digit',
|
||||
minute: '2-digit'
|
||||
})
|
||||
}
|
||||
|
||||
// 生命周期
|
||||
onMounted(async () => {
|
||||
await checkAdminStatus()
|
||||
await refreshExams()
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.exam-page {
|
||||
padding: 20px;
|
||||
max-width: 1200px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
.page-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
margin-bottom: 30px;
|
||||
padding-bottom: 20px;
|
||||
border-bottom: 1px solid #e1e1e1;
|
||||
}
|
||||
|
||||
.page-header h1 {
|
||||
margin: 0;
|
||||
color: #333;
|
||||
font-size: 2rem;
|
||||
}
|
||||
|
||||
.header-actions {
|
||||
display: flex;
|
||||
gap: 10px;
|
||||
}
|
||||
|
||||
.btn {
|
||||
padding: 8px 16px;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
font-size: 14px;
|
||||
transition: all 0.2s;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
}
|
||||
|
||||
.btn:disabled {
|
||||
opacity: 0.6;
|
||||
cursor: not-allowed;
|
||||
}
|
||||
|
||||
.btn-primary {
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-primary:hover:not(:disabled) {
|
||||
background-color: #0056b3;
|
||||
}
|
||||
|
||||
.btn-secondary {
|
||||
background-color: #6c757d;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.btn-secondary:hover:not(:disabled) {
|
||||
background-color: #545b62;
|
||||
}
|
||||
|
||||
.btn-outline {
|
||||
background-color: transparent;
|
||||
color: #007bff;
|
||||
border: 1px solid #007bff;
|
||||
}
|
||||
|
||||
.btn-outline:hover {
|
||||
background-color: #007bff;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.loading-container, .error-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
min-height: 300px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.loading-spinner {
|
||||
width: 40px;
|
||||
height: 40px;
|
||||
border: 4px solid #f3f3f3;
|
||||
border-top: 4px solid #007bff;
|
||||
border-radius: 50%;
|
||||
animation: spin 1s linear infinite;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.error-message {
|
||||
max-width: 400px;
|
||||
}
|
||||
|
||||
.error-message h3 {
|
||||
color: #dc3545;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.exam-grid {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
|
||||
gap: 20px;
|
||||
}
|
||||
|
||||
.exam-card {
|
||||
background: white;
|
||||
border: 1px solid #e1e1e1;
|
||||
border-radius: 8px;
|
||||
padding: 20px;
|
||||
cursor: pointer;
|
||||
transition: all 0.2s;
|
||||
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
||||
}
|
||||
|
||||
.exam-card:hover {
|
||||
transform: translateY(-2px);
|
||||
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
||||
}
|
||||
|
||||
.exam-card-header {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.exam-title {
|
||||
margin: 0 0 5px 0;
|
||||
color: #333;
|
||||
font-size: 1.2rem;
|
||||
}
|
||||
|
||||
.exam-id {
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
background: #f8f9fa;
|
||||
padding: 2px 8px;
|
||||
border-radius: 12px;
|
||||
}
|
||||
|
||||
.exam-meta {
|
||||
margin-bottom: 15px;
|
||||
}
|
||||
|
||||
.meta-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 5px;
|
||||
margin-bottom: 5px;
|
||||
color: #666;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
background: rgba(0,0,0,0.5);
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 1000;
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background: white;
|
||||
border-radius: 8px;
|
||||
max-width: 90vw;
|
||||
max-height: 90vh;
|
||||
overflow-y: auto;
|
||||
box-shadow: 0 10px 25px rgba(0,0,0,0.2);
|
||||
}
|
||||
|
||||
.exam-detail {
|
||||
width: 800px;
|
||||
}
|
||||
|
||||
.modal-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding: 20px;
|
||||
border-bottom: 1px solid #e1e1e1;
|
||||
}
|
||||
|
||||
.modal-header h2 {
|
||||
margin: 0;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.btn-close {
|
||||
background: none;
|
||||
border: none;
|
||||
font-size: 1.5rem;
|
||||
cursor: pointer;
|
||||
color: #666;
|
||||
padding: 0;
|
||||
width: 30px;
|
||||
height: 30px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
}
|
||||
|
||||
.btn-close:hover {
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.modal-body {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.exam-info {
|
||||
background: #f8f9fa;
|
||||
padding: 15px;
|
||||
border-radius: 4px;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
|
||||
.info-row {
|
||||
display: flex;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.info-row:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.label {
|
||||
font-weight: bold;
|
||||
color: #333;
|
||||
min-width: 100px;
|
||||
}
|
||||
|
||||
.value {
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.exam-content {
|
||||
border: 1px solid #e1e1e1;
|
||||
border-radius: 4px;
|
||||
padding: 20px;
|
||||
background: white;
|
||||
max-height: 400px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.empty-state {
|
||||
text-align: center;
|
||||
padding: 60px 20px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.empty-state h3 {
|
||||
margin-bottom: 10px;
|
||||
color: #999;
|
||||
}
|
||||
|
||||
.toast {
|
||||
position: fixed;
|
||||
top: 20px;
|
||||
right: 20px;
|
||||
background: #28a745;
|
||||
color: white;
|
||||
padding: 12px 20px;
|
||||
border-radius: 4px;
|
||||
box-shadow: 0 4px 12px rgba(0,0,0,0.15);
|
||||
transform: translateX(100%);
|
||||
transition: transform 0.3s;
|
||||
z-index: 1001;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.toast.show {
|
||||
transform: translateX(0);
|
||||
}
|
||||
|
||||
.spinning {
|
||||
animation: spin 1s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
0% { transform: rotate(0deg); }
|
||||
100% { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
/* 深色主题适配 */
|
||||
@media (prefers-color-scheme: dark) {
|
||||
.exam-page {
|
||||
background: #1a1a1a;
|
||||
color: #e1e1e1;
|
||||
}
|
||||
|
||||
.exam-card {
|
||||
background: #2d2d2d;
|
||||
border-color: #404040;
|
||||
color: #e1e1e1;
|
||||
}
|
||||
|
||||
.modal-content {
|
||||
background: #2d2d2d;
|
||||
color: #e1e1e1;
|
||||
}
|
||||
|
||||
.exam-info {
|
||||
background: #404040;
|
||||
}
|
||||
|
||||
.exam-content {
|
||||
background: #2d2d2d;
|
||||
border-color: #404040;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user