Files
crawler-plugin/admin-frontend-vue/src/pages/records/WorkbenchClientPackagePage.vue
T

503 lines
16 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<script setup lang="ts">
import { formatDateTime } from '@/utils/datetime'
/** 数富AI工作台客户端包管理页:上传 ZIP → 直传 MinIO → 服务端复算 sha256 落库。
* 列表默认按上传时间倒序,第一条即当前生效版本;删除最新包后自动回落到上一版。 */
import { computed, onMounted, ref, watch } from 'vue'
import OldPagination from '@/components/OldPagination.vue'
import { ElMessage } from 'element-plus'
import {
deleteWorkbenchClientPackages,
fetchWorkbenchClientPackages,
uploadWorkbenchClientPackage,
} from './workbench-client-api.ts'
import { formatFileSize, isValidWorkbenchClientVersion, WORKBENCH_CLIENT_VERSION_MAX_LENGTH } from './workbench-client-model.ts'
import type { WorkbenchClientPackageItem } from './workbench-client-dto.ts'
import { showAdminFeedback } from '@/components/admin-feedback-ui'
import { openAdminConfirm } from '@/components/admin-confirm'
import LoadFailureBar from '@/components/LoadFailureBar.vue'
const loading = ref(false)
const loadFailure = ref('')
const items = ref<WorkbenchClientPackageItem[]>([])
/** 版本号/文件名搜索(客户端过滤)。 */
const keyword = ref('')
const filteredItems = computed(() => {
const kw = (keyword.value || '').trim().toLowerCase()
if (!kw) return items.value
return items.value.filter((item) =>
`${item.version}`.toLowerCase().includes(kw) || item.fileName.toLowerCase().includes(kw),
)
})
/** 全站分页统一:客户端分页(10/20/50/100)。 */
const page = ref(1)
const pageSize = ref(10)
const pagedItems = computed(() => filteredItems.value.slice((page.value - 1) * pageSize.value, page.value * pageSize.value))
function changePage(p: number) { page.value = p }
function changeSize(size: number) { pageSize.value = size; page.value = 1 }
// 搜索导致数据收缩时回钳页码,避免停在空页。
watch(filteredItems, () => {
page.value = Math.min(page.value, Math.max(1, Math.ceil(filteredItems.value.length / pageSize.value)))
})
/** 当前生效版本 = 列表第一条(客户端更新接口下发的就是它)。 */
const activeId = computed(() => items.value[0]?.id ?? null)
// 勾选删除:选中跨页累计;表头复选只作用于当前页,避免误选整表。
const selectedIds = ref<Set<number>>(new Set())
const selectedCount = computed(() => selectedIds.value.size)
function toggleSelect(id: number) {
const next = new Set(selectedIds.value)
if (next.has(id)) next.delete(id)
else next.add(id)
selectedIds.value = next
}
function toggleSelectAllPage() {
const pageRows = pagedItems.value
const allOn = pageRows.length > 0 && pageRows.every((row) => selectedIds.value.has(row.id))
const next = new Set(selectedIds.value)
if (allOn) pageRows.forEach((row) => next.delete(row.id))
else pageRows.forEach((row) => next.add(row.id))
selectedIds.value = next
}
async function removeSelected() {
if (!selectedCount.value) return
const rows = items.value.filter((row) => selectedIds.value.has(row.id))
const sample = rows.slice(0, 3).map((row) => row.version).join('、')
const summary = rows.length > 3 ? `${sample} 等 ${rows.length} 个` : sample
if (!(await openAdminConfirm({ message: `确认删除选中的 ${rows.length} 个客户端包(${summary})?此操作不可恢复。`, danger: true }))) return
try {
await deleteWorkbenchClientPackages(Array.from(selectedIds.value))
ElMessage.success(`已删除 ${rows.length} 个客户端包`)
selectedIds.value = new Set()
await load()
} catch (error) {
showAdminFeedback(error instanceof Error ? error.message : '删除失败', 'error')
}
}
async function removeOne(row: WorkbenchClientPackageItem) {
if (!(await openAdminConfirm({ message: `确认删除客户端包 ${row.version}(${row.fileName})?此操作不可恢复。`, danger: true }))) return
try {
await deleteWorkbenchClientPackages([row.id])
ElMessage.success('删除成功')
selectedIds.value = new Set([...selectedIds.value].filter((id) => id !== row.id))
await load()
} catch (error) {
showAdminFeedback(error instanceof Error ? error.message : '删除失败', 'error')
}
}
const uploadVisible = ref(false)
const uploading = ref(false)
const uploadPercent = ref(0)
const newVersion = ref('')
const pickedFile = ref<File | null>(null)
const uploadMsg = ref('')
const uploadMsgOk = ref(false)
async function load() {
loading.value = true
loadFailure.value = ''
try {
items.value = (await fetchWorkbenchClientPackages()).items
} catch (error) {
loadFailure.value = error instanceof Error ? error.message : '客户端包列表加载失败'
showAdminFeedback(loadFailure.value, 'error')
} finally {
loading.value = false
}
}
function onFileChange(file: File) {
pickedFile.value = file
}
function openUpload() {
uploadMsg.value = ''
uploadMsgOk.value = false
newVersion.value = ''
pickedFile.value = null
uploadVisible.value = true
}
async function submitUpload() {
uploadMsg.value = ''
uploadMsgOk.value = false
uploadPercent.value = 0
if (!newVersion.value.trim()) {
uploadMsg.value = '请填写版本号'
return
}
if (!isValidWorkbenchClientVersion(newVersion.value)) {
uploadMsg.value = '版本号必须是纯数字 x.y.z 格式(如 1.0.113),不能带 v 前缀或其他后缀'
return
}
if (!pickedFile.value) {
uploadMsg.value = '请选择 zip 压缩包'
return
}
if (!/\.zip$/i.test(pickedFile.value.name)) {
uploadMsg.value = '仅支持 .zip 格式'
return
}
if (pickedFile.value.size > 512 * 1024 * 1024) {
uploadMsg.value = '文件超过允许的大小限制'
return
}
uploading.value = true
try {
await uploadWorkbenchClientPackage(pickedFile.value, pickedFile.value.name, newVersion.value.trim(), (p) => {
uploadPercent.value = p
uploadMsg.value = p >= 100 ? '上传完成,正在复算 sha256 并登记...' : `正在上传:${p}%`
})
uploadMsg.value = '上传成功,客户端已可检测到新版本'
uploadMsgOk.value = true
newVersion.value = ''
pickedFile.value = null
uploadPercent.value = 0
load()
} catch (error) {
uploadMsg.value = error instanceof Error ? error.message : '上传失败'
} finally {
uploading.value = false
}
}
onMounted(load)
</script>
<template>
<div class="workbench-view">
<section class="panel-box">
<div class="workbench-head">
<h3>客户端包列表</h3>
<div class="workbench-head-tools admin-header-controls">
<input v-model="keyword" type="text" placeholder="搜索版本号或文件名" class="workbench-keyword" />
<button class="btn btn-danger" type="button" :disabled="!selectedCount" @click="removeSelected">删除选中{{ selectedCount ? `(${selectedCount})` : '' }}</button>
<button class="btn" type="button" @click="openUpload">上传客户端包</button>
</div>
</div>
<p class="page-hint">最新上传的一条即当前生效版本。</p>
<LoadFailureBar v-if="loadFailure" :message="loadFailure" @retry="load" />
<div class="table-scroll workbench-table-scroll">
<table>
<thead>
<tr>
<th style="width: 40px" class="th-select">
<input type="checkbox" :checked="pagedItems.length > 0 && pagedItems.every((r) => selectedIds.has(r.id))" @change="toggleSelectAllPage" :disabled="!pagedItems.length" />
</th>
<th style="width: 120px">版本号</th>
<th style="width: 220px">文件名</th>
<th style="width: 100px">大小</th>
<th style="width: 220px">SHA-256</th>
<th style="width: 150px">上传时间</th>
<th>下载链接</th>
<th style="width: 170px">操作</th>
</tr>
</thead>
<tbody>
<template v-if="filteredItems.length">
<tr v-for="row in pagedItems" :key="row.id" :class="{ 'row-selected': selectedIds.has(row.id) }">
<td class="td-select">
<input type="checkbox" :checked="selectedIds.has(row.id)" @change="toggleSelect(row.id)" />
</td>
<td>
<span class="version-cell" :title="row.version">{{ row.version }}</span>
<span v-if="row.id === activeId" class="tag-active">当前生效</span>
</td>
<td>
<span class="file-name" :title="row.fileName">{{ row.fileName }}</span>
</td>
<td>{{ formatFileSize(row.fileSize) }}</td>
<td>
<span v-if="row.sha256" class="sha-cell" :title="row.sha256">{{ row.sha256 }}</span>
<span v-else class="dim">—</span>
</td>
<td>{{ formatDateTime(row.createdAt) }}</td>
<td>
<a v-if="row.fileUrl" class="link-cell" :href="row.fileUrl" target="_blank" rel="noopener" :title="row.fileUrl">{{ row.fileUrl }}</a>
<span v-else class="dim">—</span>
</td>
<td class="ops-cell">
<a v-if="row.fileUrl" class="btn btn-sm dl-btn" :href="row.fileUrl" download>下载</a>
<span v-else class="dim">—</span>
<button class="btn btn-sm btn-danger" type="button" @click="removeOne(row)">删除</button>
</td>
</tr>
</template>
<tr v-else-if="loading">
<td colspan="8" class="empty-tip">加载中...</td>
</tr>
<tr v-else>
<td colspan="8" class="empty-tip">{{ keyword ? '暂无匹配客户端包' : '暂无客户端包记录' }}</td>
</tr>
</tbody>
</table>
</div>
<OldPagination v-if="filteredItems.length > 0" :total="filteredItems.length" :page="page" :page-size="pageSize" @change="changePage" @size-change="changeSize" />
</section>
<el-dialog v-model="uploadVisible" title="上传客户端包" width="540px">
<p class="upload-desc">上传数富AI工作台客户端 ZIP 包后,客户端会从公开版本接口自动检测到新版本并升级;服务端会复算 sha256 供客户端校验。</p>
<el-form label-width="110px">
<el-form-item label="版本号" required>
<el-input v-model="newVersion" :placeholder="`例如:1.0.113(纯数字 x.y.z,最长 ${WORKBENCH_CLIENT_VERSION_MAX_LENGTH} 字符)`" :maxlength="WORKBENCH_CLIENT_VERSION_MAX_LENGTH" />
<p class="zip-hint">客户端按版本号判断是否需要升级,必填;不要带 v 前缀</p>
</el-form-item>
<el-form-item label="ZIP 压缩包" required>
<el-upload :auto-upload="false" :limit="1" accept=".zip" :on-change="(file: any) => onFileChange(file.raw)" :on-remove="() => (pickedFile = null)">
<el-button>选择文件</el-button>
</el-upload>
<p class="zip-hint">仅支持 .zip 格式,最大 512MB</p>
<div v-if="pickedFile" class="dim">{{ pickedFile.name }}({{ formatFileSize(pickedFile.size) }})</div>
</el-form-item>
<el-progress v-if="uploading && uploadPercent > 0" :percentage="uploadPercent" :stroke-width="10" :status="uploadPercent >= 100 ? 'success' : undefined" class="upload-progress" />
<el-alert v-if="uploadMsg" :title="uploadMsg" :type="uploadMsgOk ? 'success' : 'error'" :closable="false" show-icon class="upload-msg" />
</el-form>
<template #footer>
<el-button @click="uploadVisible = false">取消</el-button>
<el-button type="primary" :loading="uploading" @click="submitUpload">上传客户端包</el-button>
</template>
</el-dialog>
</div>
</template>
<style scoped>
/* 沿用软件版本/教程管理页(旧版 admin.html panel-version)的样式语言。 */
.workbench-view {
font-family: inherit;
color: #24384d;
}
.panel-box {
width: 100%;
min-width: 0;
padding: 20px 22px 24px;
border: 1px solid #d8e3ee;
border-radius: 14px;
background: linear-gradient(145deg, #ffffff, #f9fbfd);
box-shadow: 0 1px 2px rgba(39, 67, 94, 0.04), 0 14px 30px -24px rgba(39, 67, 94, 0.28);
}
h3 {
margin: 0;
font-size: 15px;
font-weight: 650;
color: #24384d;
letter-spacing: 0.2px;
}
.workbench-head {
display: flex;
align-items: center;
justify-content: space-between;
gap: 12px;
margin-bottom: 16px;
}
.workbench-head-tools {
display: flex;
align-items: center;
gap: 10px;
}
.workbench-keyword {
width: 220px;
min-height: 42px;
padding: 6px 12px;
border: 1px solid #cbd9e6;
border-radius: 9px;
background: #f8fbfd;
color: #24384d;
font-size: 13.5px;
font-family: inherit;
color-scheme: light;
outline: none;
transition: border-color 160ms ease, box-shadow 160ms ease, background 160ms ease;
}
.workbench-keyword:hover {
border-color: #9fb7cd;
}
.workbench-keyword:focus {
background: #ffffff;
border-color: #5f85ad;
box-shadow: 0 0 0 3px rgba(95, 133, 173, 0.16);
}
.page-hint {
margin: -6px 0 14px;
color: #5b6f83;
font-size: 12.5px;
}
.page-hint code {
padding: 1px 6px;
border-radius: 5px;
background: #edf4fa;
color: #2f5d8b;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
}
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
gap: 8px;
min-height: 42px;
padding: 9px 18px;
border: 1px solid #4f78a5;
border-radius: 9px;
background: linear-gradient(135deg, #5f85ad, #4f78a5);
color: #ffffff;
font-family: inherit;
font-size: 13.5px;
cursor: pointer;
box-shadow: 0 8px 18px -14px rgba(79, 120, 165, 0.72);
transition: background 160ms ease, box-shadow 160ms ease, transform 120ms ease;
}
.btn:hover:not(:disabled) {
background: linear-gradient(135deg, #7094ba, #5d83ac);
box-shadow: 0 11px 22px -14px rgba(79, 120, 165, 0.8);
}
.btn:disabled {
cursor: not-allowed;
opacity: 0.55;
}
.btn-sm {
min-height: 36px;
padding: 7px 12px;
}
.btn-danger {
background: linear-gradient(135deg, #c06d77, #b35f6a);
border-color: #b35f6a;
}
.btn-danger:hover:not(:disabled) {
background: linear-gradient(135deg, #cb7c84, #b96570);
}
.table-scroll {
width: 100%;
min-width: 0;
overflow-x: auto;
border: 1px solid #dbe5ee;
border-radius: 10px;
background: #ffffff;
}
.table-scroll table {
width: 100%;
border-collapse: collapse;
table-layout: fixed;
}
.workbench-table-scroll > table {
min-width: 1200px;
}
.table-scroll th,
.table-scroll td {
padding: 10px 12px;
text-align: left;
font-size: 13.5px;
line-height: 1.5;
border-bottom: 1px solid #e0e8ef;
vertical-align: middle;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.table-scroll th {
background: #edf4fa;
color: #4e6479;
border-bottom-color: #d5e1eb;
font-size: 12.5px;
font-weight: 600;
letter-spacing: 0.4px;
}
.table-scroll tbody tr:hover td {
background: #f1f7fb;
}
.table-scroll tbody tr:last-child td {
border-bottom: 0;
}
.file-name,
.version-cell {
display: inline-block;
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
vertical-align: middle;
}
.tag-active {
display: inline-block;
margin-left: 6px;
padding: 1px 7px;
border: 1px solid #4f9a72;
border-radius: 999px;
background: #eaf6ef;
color: #2f7a55;
font-size: 11.5px;
vertical-align: middle;
}
.sha-cell {
display: block;
overflow: hidden;
color: #5b6f83;
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
font-size: 12px;
text-overflow: ellipsis;
}
.link-cell {
display: block;
color: #2f5d8b;
text-decoration: none;
overflow: hidden;
text-overflow: ellipsis;
}
.link-cell:hover {
text-decoration: underline;
}
.dl-btn {
text-decoration: none;
}
.dim {
color: #8293a5;
font-size: 12px;
}
.empty-tip {
padding: 44px 24px;
text-align: center;
color: #8293a5;
font-size: 13.5px;
}
.upload-desc {
margin: 0 0 12px;
color: #5b6f83;
font-size: 12.5px;
line-height: 1.6;
}
.zip-hint {
margin: 4px 0 0;
color: #5b6f83;
font-size: 12px;
}
.ops-cell {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 8px;
}
.row-selected td {
background: #fdf0ee;
}
.th-select,
.td-select {
text-align: center;
padding-left: 4px !important;
padding-right: 4px !important;
}
.th-select input,
.td-select input {
cursor: pointer;
accent-color: #b33a2e;
}
.upload-progress {
margin: 2px 0 10px;
}
.upload-msg {
margin-bottom: 8px;
}
.upload-msg :deep(.el-alert__title) {
word-break: break-all;
}
</style>