Files
Academic-Affairs-System/web/src/views/OfficialDocumentsView.vue
T

431 lines
18 KiB
Vue

<script setup lang="ts">
import { getDisplayLocale } from '../i18n'
import { useI18n } from 'vue-i18n'
const { t } = useI18n()
import { computed, onMounted, reactive, ref } from 'vue'
import { Download, Plus, Refresh, Search } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import http, { apiErrorMessage } from '../api/http'
import { useAuthStore } from '../stores/auth'
type DocumentType = 'Transcript' | 'StudentStatusCertificate'
type DocumentStatus = 'Valid' | 'Invalidated' | 'Superseded'
interface OfficialDocumentRow {
id: string
documentNumber: string
type: DocumentType
status: DocumentStatus
studentId: string
studentNumber: string
studentName: string
collegeName: string
purpose?: string
issuedAt: string
issuedByName: string
invalidatedAt?: string
invalidationReason?: string
reissuedFromDocumentId?: string
downloadCount: number
lastDownloadedAt?: string
}
interface StudentOption {
id: string
studentNumber: string
name: string
status: string
className: string
majorName: string
collegeName: string
}
const auth = useAuthStore()
const isManager = computed(() =>
auth.user?.roles.some((role) => ['SuperAdmin', 'AcademicAdmin', 'CollegeAdmin'].includes(role)),
)
const loading = ref(false)
const issuing = ref(false)
const documents = ref<OfficialDocumentRow[]>([])
const students = ref<StudentOption[]>([])
const issueDialog = ref(false)
const historyDrawer = ref(false)
const historyLoading = ref(false)
const downloadHistory = ref<any[]>([])
const historyPage = ref(1)
const historyTotal = ref(0)
const historyPageSize = 20
const selectedDocument = ref<OfficialDocumentRow>()
const filters = reactive<{ type?: DocumentType; status?: DocumentStatus }>({})
const page = ref(1)
const total = ref(0)
const pageSize = 20
const issueForm = reactive<{ studentId: string; type: DocumentType; purpose: string }>({
studentId: '',
type: 'Transcript',
purpose: '',
})
const typeLabels = computed<Record<DocumentType, string>>(() => ({
Transcript: t('officialDocuments.transcript'),
StudentStatusCertificate: t('officialDocuments.certificate'),
}))
const statusLabels = computed<Record<DocumentStatus, string>>(() => ({
Valid: t('officialDocuments.valid'),
Invalidated: t('officialDocuments.invalidated'),
Superseded: t('officialDocuments.superseded'),
}))
async function load(resetPage = false) {
if (resetPage) page.value = 1
loading.value = true
try {
const { data } = await http.get('/official-documents', {
params: { ...filters, page: page.value, pageSize },
})
documents.value = data.items
total.value = data.total
if (documents.value.length === 0 && page.value > 1) {
page.value--
await load()
}
} catch (error) {
ElMessage.error(apiErrorMessage(error))
} finally {
loading.value = false
}
}
async function loadStudents(keyword = '') {
if (!isManager.value) return
try {
const { data } = await http.get('/official-documents/students/options', {
params: { keyword: keyword || undefined, page: 1, pageSize: 30 },
})
students.value = data.items
} catch (error) {
ElMessage.error(apiErrorMessage(error))
}
}
async function openIssue() {
issueForm.studentId = ''
issueForm.type = 'Transcript'
issueForm.purpose = ''
issueDialog.value = true
if (isManager.value) await loadStudents()
}
async function issue() {
if (isManager.value && !issueForm.studentId) {
ElMessage.warning(t('officialDocuments.selectStudent'))
return
}
issuing.value = true
try {
const response = isManager.value
? await http.post('/official-documents', issueForm)
: await http.post('/official-documents/mine', {
type: issueForm.type,
purpose: issueForm.purpose,
})
issueDialog.value = false
ElMessage.success(
response.data?.reused
? t('officialDocuments.reused')
: isManager.value
? t('officialDocuments.issued')
: t('officialDocuments.generated'),
)
await load()
} catch (error) {
ElMessage.error(apiErrorMessage(error))
} finally {
issuing.value = false
}
}
async function download(row: OfficialDocumentRow) {
try {
const response = await http.get(`/official-documents/${row.id}/download`, {
responseType: 'blob',
timeout: 30000,
})
const url = URL.createObjectURL(response.data)
const anchor = document.createElement('a')
anchor.href = url
anchor.download = `${row.documentNumber}.pdf`
anchor.click()
URL.revokeObjectURL(url)
await load()
} catch (error) {
ElMessage.error(apiErrorMessage(error))
}
}
async function invalidate(row: OfficialDocumentRow) {
try {
const { value } = await ElMessageBox.prompt(
t('officialDocuments.invalidatePrompt'),
t('officialDocuments.confirmInvalidate'),
{ inputPattern: /^.{2,500}$/, inputErrorMessage: t('officialDocuments.reasonLength'), confirmButtonText: t('officialDocuments.confirmInvalidate') },
)
await http.post(`/official-documents/${row.id}/invalidate`, { reason: value })
ElMessage.success(t('officialDocuments.invalidateSuccess'))
await load()
} catch (error: any) {
if (error === 'cancel' || error === 'close') return
ElMessage.error(apiErrorMessage(error))
}
}
async function reissue(row: OfficialDocumentRow) {
try {
const { value } = await ElMessageBox.prompt(
t('officialDocuments.reissuePrompt'),
t('officialDocuments.reissueTitle'),
{ inputPattern: /^.{2,500}$/, inputErrorMessage: t('officialDocuments.reasonLength'), confirmButtonText: t('officialDocuments.confirmReissue') },
)
await http.post(`/official-documents/${row.id}/reissue`, { reason: value })
ElMessage.success(t('officialDocuments.reissueSuccess'))
await load()
} catch (error: any) {
if (error === 'cancel' || error === 'close') return
ElMessage.error(apiErrorMessage(error))
}
}
async function showHistory(row: OfficialDocumentRow) {
selectedDocument.value = row
historyDrawer.value = true
historyPage.value = 1
await loadHistory()
}
async function loadHistory() {
if (!selectedDocument.value) return
historyLoading.value = true
try {
const { data } = await http.get(
`/official-documents/${selectedDocument.value.id}/downloads`,
{ params: { page: historyPage.value, pageSize: historyPageSize } },
)
downloadHistory.value = data.items
historyTotal.value = data.total
} catch (error) {
ElMessage.error(apiErrorMessage(error))
} finally {
historyLoading.value = false
}
}
function formatTime(value?: string) {
return value ? new Date(value).toLocaleString(getDisplayLocale(), { hour12: false }) : '—'
}
function statusTagType(status: DocumentStatus) {
return status === 'Valid' ? 'success' : status === 'Superseded' ? 'warning' : 'danger'
}
onMounted(load)
</script>
<template>
<div class="document-page">
<header class="document-hero">
<div>
<span>OFFICIAL DOCUMENT SERVICE</span>
<h2>{{ isManager ? t('officialDocuments.managerTitle') : t('officialDocuments.studentTitle') }}</h2>
<p>{{ isManager ? t('officialDocuments.managerDescription') : t('officialDocuments.studentDescription') }}</p>
</div>
<el-button type="primary" :icon="Plus" @click="openIssue">
{{ isManager ? t('officialDocuments.issueNew') : t('officialDocuments.request') }}
</el-button>
</header>
<section class="trust-strip">
<div><b>{{ t('officialDocuments.uniqueNumber') }}</b><span>{{ t('officialDocuments.uniqueHint') }}</span></div>
<div><b>{{ t('officialDocuments.qr') }}</b><span>{{ t('officialDocuments.qrHint') }}</span></div>
<div><b>{{ t('officialDocuments.downloadTracking') }}</b><span>{{ t('officialDocuments.downloadHint') }}</span></div>
<div><b>{{ t('officialDocuments.lifecycle') }}</b><span>{{ t('officialDocuments.lifecycleHint') }}</span></div>
</section>
<section class="document-toolbar">
<el-select v-model="filters.type" clearable :placeholder="t('officialDocuments.allTypes')" @change="load(true)">
<el-option v-for="(label, value) in typeLabels" :key="value" :label="label" :value="value" />
</el-select>
<el-select v-model="filters.status" clearable :placeholder="t('businessLabels.allStatus')" @change="load(true)">
<el-option v-for="(label, value) in statusLabels" :key="value" :label="label" :value="value" />
</el-select>
<el-button :icon="Refresh" @click="() => load()">{{ t('businessLabels.refresh') }}</el-button>
<span>{{ t('officialDocuments.total', { count: total }) }}</span>
</section>
<section class="document-list" v-loading="loading">
<article v-for="row in documents" :key="row.id" class="document-card">
<div class="document-mark" :class="row.type === 'Transcript' ? 'transcript' : 'certificate'">
<span>{{ row.type === 'Transcript' ? 'TR' : 'SC' }}</span>
<small>PDF</small>
</div>
<div class="document-main">
<div class="document-title">
<div>
<span>{{ typeLabels[row.type] }}</span>
<h3>{{ row.documentNumber }}</h3>
</div>
<el-tag :type="statusTagType(row.status)" effect="plain">{{ statusLabels[row.status] }}</el-tag>
</div>
<p class="student-line">
<b>{{ row.studentName }}</b>
<span>{{ row.studentNumber }} · {{ row.collegeName }}</span>
</p>
<p class="document-meta">
{{ t('officialDocuments.issuedBy', { date: formatTime(row.issuedAt), name: row.issuedByName }) }}
<template v-if="row.purpose"> · {{ t('officialDocuments.purposeValue', { purpose: row.purpose }) }}</template>
</p>
<el-alert
v-if="row.status !== 'Valid'"
:title="t('officialDocuments.statusReason', { status: statusLabels[row.status], reason: row.invalidationReason || t('officialDocuments.missingReason') })"
type="warning"
:closable="false"
/>
</div>
<div class="document-actions">
<el-button v-if="row.status === 'Valid'" type="primary" :icon="Download" @click="download(row)">{{ t('officialDocuments.downloadPdf') }}</el-button>
<el-button v-if="isManager" :icon="Search" @click="showHistory(row)">{{ t('officialDocuments.downloadCount', { count: row.downloadCount }) }}</el-button>
<el-button v-if="isManager && row.status === 'Valid'" type="danger" plain @click="invalidate(row)">{{ t('officialDocuments.invalidate') }}</el-button>
<el-button v-if="isManager && row.status !== 'Superseded'" type="warning" plain @click="reissue(row)">{{ t('officialDocuments.reissue') }}</el-button>
</div>
</article>
<el-empty
v-if="!documents.length"
:description="isManager ? t('officialDocuments.emptyManager') : t('officialDocuments.emptyStudent')"
/>
<el-pagination
v-if="total > pageSize"
v-model:current-page="page"
:page-size="pageSize"
:total="total"
layout="total, prev, pager, next"
class="document-pagination"
@current-change="() => load()"
/>
</section>
<el-dialog v-model="issueDialog" :title="isManager ? t('officialDocuments.issueTitle') : t('officialDocuments.request')" width="620px">
<el-alert
:title="isManager
? t('officialDocuments.managerNotice')
: t('officialDocuments.studentNotice')"
type="info"
:closable="false"
/>
<el-form label-position="top" class="issue-form">
<el-form-item v-if="isManager" :label="t('officialDocuments.student')" required>
<el-select
v-model="issueForm.studentId"
filterable
remote
:remote-method="loadStudents"
:placeholder="t('officialDocuments.searchStudent')"
style="width: 100%"
>
<el-option
v-for="student in students"
:key="student.id"
:value="student.id"
:label="`${student.studentNumber} · ${student.name}`"
>
<div class="student-option">
<b>{{ student.studentNumber }} · {{ student.name }}</b>
<span>{{ student.collegeName }} · {{ student.majorName }} · {{ student.className }}</span>
</div>
</el-option>
</el-select>
</el-form-item>
<el-form-item :label="t('documentVerify.type')" required>
<el-radio-group v-model="issueForm.type">
<el-radio-button value="Transcript">{{ t('documentVerify.types.Transcript') }}</el-radio-button>
<el-radio-button value="StudentStatusCertificate">{{ t('documentVerify.types.StudentStatusCertificate') }}</el-radio-button>
</el-radio-group>
</el-form-item>
<el-form-item :label="t('officialDocuments.purpose')">
<el-input v-model="issueForm.purpose" maxlength="200" show-word-limit :placeholder="t('officialDocuments.purposeHint')" />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="issueDialog = false">{{ t('businessLabels.cancel') }}</el-button>
<el-button type="primary" :loading="issuing" @click="issue">
{{ isManager ? t('officialDocuments.confirmIssue') : t('officialDocuments.submitRequest') }}
</el-button>
</template>
</el-dialog>
<el-drawer v-model="historyDrawer" :title="t('officialDocuments.history')" size="560px">
<p class="drawer-number">{{ selectedDocument?.documentNumber }}</p>
<el-table :data="downloadHistory" v-loading="historyLoading">
<el-table-column :label="t('officialDocuments.downloadedBy')" prop="downloadedByName" min-width="110" />
<el-table-column :label="t('officialDocuments.downloadTime')" min-width="165">
<template #default="{ row }">{{ formatTime(row.createdAt) }}</template>
</el-table-column>
<el-table-column :label="t('officialDocuments.sourceIp')" prop="ipAddress" min-width="130" />
</el-table>
<el-empty v-if="!historyLoading && !downloadHistory.length" :description="t('officialDocuments.emptyHistory')" />
<el-pagination
v-if="historyTotal > historyPageSize"
v-model:current-page="historyPage"
:page-size="historyPageSize"
:total="historyTotal"
layout="total, prev, pager, next"
class="document-pagination"
@current-change="loadHistory"
/>
</el-drawer>
</div>
</template>
<style scoped>
.document-page { display: grid; gap: 18px; }
.document-hero { display: flex; align-items: end; justify-content: space-between; gap: 24px; padding: 26px 28px; border-radius: 12px; color: #f4fbf8; background: linear-gradient(120deg, #173b3a, #245e55 68%, #33746a); box-shadow: 0 14px 34px rgba(25, 65, 60, .16); }
.document-hero span { font: 650 10px/1.2 Consolas, monospace; letter-spacing: .18em; opacity: .72; }
.document-hero h2 { margin: 8px 0 5px; font: 700 27px/1.25 Georgia, "Noto Serif SC", serif; }
.document-hero p { margin: 0; color: rgba(244, 251, 248, .74); font-size: 13px; }
.trust-strip { display: grid; grid-template-columns: repeat(4, 1fr); gap: 1px; overflow: hidden; border: 1px solid #dce7e4; border-radius: 10px; background: #dce7e4; }
.trust-strip div { display: grid; gap: 3px; padding: 14px 18px; background: #f8fbfa; }
.trust-strip b { color: #244d48; font-size: 13px; }
.trust-strip span { color: #71807e; font-size: 11px; }
.document-toolbar { display: flex; align-items: center; gap: 10px; }
.document-toolbar .el-select { width: 180px; }
.document-toolbar > span { margin-left: auto; color: #7b8785; font-size: 12px; }
.document-list { display: grid; gap: 12px; min-height: 180px; }
.document-card { display: grid; grid-template-columns: 74px minmax(0, 1fr) auto; gap: 18px; align-items: center; padding: 18px; border: 1px solid #e0e7e5; border-radius: 10px; background: white; }
.document-mark { display: grid; place-content: center; width: 62px; height: 76px; border-radius: 5px 5px 12px 5px; color: white; background: #245e55; box-shadow: 0 8px 18px rgba(36, 94, 85, .18); }
.document-mark.certificate { background: #6b5541; }
.document-mark span { font: 800 18px/1 Consolas, monospace; letter-spacing: .1em; }
.document-mark small { margin-top: 7px; text-align: center; font: 700 8px/1 Consolas, monospace; opacity: .68; }
.document-main { min-width: 0; }
.document-title { display: flex; align-items: start; justify-content: space-between; gap: 12px; }
.document-title span { color: #687774; font-size: 11px; }
.document-title h3 { margin: 4px 0 0; overflow: hidden; color: #263d3a; font: 700 15px/1.3 Consolas, monospace; text-overflow: ellipsis; }
.student-line { display: flex; gap: 9px; align-items: baseline; margin: 11px 0 4px; }
.student-line b { color: #263d3a; }
.student-line span, .document-meta { color: #77827f; font-size: 12px; }
.document-meta { margin: 0 0 8px; }
.document-actions { display: flex; flex-direction: column; gap: 7px; min-width: 150px; }
.issue-form { margin-top: 18px; }
.student-option { display: grid; line-height: 1.3; }
.student-option span { color: #8a9693; font-size: 11px; }
.drawer-number { margin: -8px 0 18px; color: #667572; font: 600 12px Consolas, monospace; }
@media (max-width: 760px) {
.document-hero { align-items: stretch; flex-direction: column; padding: 22px; }
.trust-strip { grid-template-columns: 1fr 1fr; }
.document-toolbar { align-items: stretch; flex-wrap: wrap; }
.document-toolbar .el-select { width: calc(50% - 5px); }
.document-toolbar > span { width: 100%; margin-left: 0; }
.document-card { grid-template-columns: 54px minmax(0, 1fr); align-items: start; padding: 14px; }
.document-mark { width: 50px; height: 62px; }
.document-actions { grid-column: 1 / -1; display: grid; grid-template-columns: 1fr 1fr; }
.student-line { align-items: start; flex-direction: column; gap: 2px; }
}
</style>