157 lines
6.2 KiB
Vue
157 lines
6.2 KiB
Vue
<script setup lang="ts">
|
||
import { formatDateTime } from '@/utils/datetime'
|
||
/** 软件版本管理页:客户端版本列表 + 上传新版本(成功留驻弹窗含链接,对齐 admin panel-version)。 */
|
||
import { computed, onMounted, ref } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import { fetchSoftwareVersions, uploadSoftwareVersion } from './version-api.ts'
|
||
import type { SoftwareVersionItem } from './version-dto.ts'
|
||
import { isNonEmptyVersion } from './version-dto.ts'
|
||
|
||
const loading = ref(false)
|
||
const items = ref<SoftwareVersionItem[]>([])
|
||
// 客户端分页:全量拉取后按页切片展示。
|
||
const page = ref(1)
|
||
const pageSize = 10
|
||
const total = computed(() => items.value.length)
|
||
const pagedItems = computed(() => items.value.slice((page.value - 1) * pageSize, page.value * pageSize))
|
||
|
||
function changePage(p: number) {
|
||
page.value = p
|
||
}
|
||
|
||
const uploadVisible = ref(false)
|
||
const uploading = ref(false)
|
||
const newVersion = ref('')
|
||
const pickedFile = ref<File | null>(null)
|
||
/** 弹窗内成功/失败文案(对齐 admin.js msgVersion 留驻)。 */
|
||
const uploadMsg = ref('')
|
||
const uploadMsgOk = ref(false)
|
||
|
||
async function load() {
|
||
loading.value = true
|
||
try {
|
||
items.value = (await fetchSoftwareVersions()).items
|
||
} catch (error) {
|
||
ElMessage.error(error instanceof Error ? error.message : '版本列表加载失败')
|
||
} 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
|
||
if (!isNonEmptyVersion(newVersion.value)) {
|
||
uploadMsg.value = '请填写版本号'
|
||
return
|
||
}
|
||
if (!pickedFile.value) {
|
||
uploadMsg.value = '请选择 zip 压缩包'
|
||
return
|
||
}
|
||
if (!/\.zip$/i.test(pickedFile.value.name)) {
|
||
uploadMsg.value = '仅支持 .zip 格式'
|
||
return
|
||
}
|
||
uploading.value = true
|
||
try {
|
||
const created = await uploadSoftwareVersion(newVersion.value.trim(), pickedFile.value, pickedFile.value.name)
|
||
const version = created?.version || newVersion.value.trim()
|
||
const link = created?.fileUrl
|
||
// 对齐 admin.js:6484-6488:弹窗保持打开,成功文案(含链接)留驻可复制,仅清空输入并刷新列表。
|
||
uploadMsg.value = link ? `发布成功。版本:${version},链接:${link}` : `发布成功。版本:${version}`
|
||
uploadMsgOk.value = true
|
||
newVersion.value = ''
|
||
pickedFile.value = null
|
||
load()
|
||
} catch (error) {
|
||
uploadMsg.value = error instanceof Error ? error.message : '上传失败'
|
||
} finally {
|
||
uploading.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(load)
|
||
</script>
|
||
|
||
<template>
|
||
<div class="page-stack">
|
||
<div class="page-heading">
|
||
<div>
|
||
<h2>软件版本管理</h2>
|
||
<p>管理客户端更新包版本(用于 /api/version/latest 下发)。</p>
|
||
</div>
|
||
<div class="actions">
|
||
<el-button type="primary" @click="openUpload">上传新版本</el-button>
|
||
</div>
|
||
</div>
|
||
|
||
<el-card shadow="never">
|
||
<el-table v-loading="loading" :data="pagedItems" stripe border empty-text="暂无版本记录">
|
||
<el-table-column prop="version" label="版本号" min-width="180" />
|
||
<el-table-column label="下载链接" min-width="240">
|
||
<template #default="{ row }">
|
||
<a v-if="(row as SoftwareVersionItem).fileUrl" class="el-link el-link--primary" :href="(row as SoftwareVersionItem).fileUrl" target="_blank" rel="noopener">{{ (row as SoftwareVersionItem).fileUrl }}</a>
|
||
<span v-else class="dim">—</span>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column prop="createdAt" label="创建时间" min-width="190">
|
||
<template #default="{ row }">{{ formatDateTime(row.createdAt) }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="操作" min-width="90" fixed="right">
|
||
<template #default="{ row }">
|
||
<a v-if="(row as SoftwareVersionItem).fileUrl" class="el-link el-link--primary" :href="(row as SoftwareVersionItem).fileUrl" download>下载</a>
|
||
<span v-else class="dim">—</span>
|
||
</template>
|
||
</el-table-column>
|
||
</el-table>
|
||
<div class="table-footer">
|
||
<el-pagination background layout="prev, pager, next, jumper" :total="total" :page-size="pageSize" :current-page="page" @current-change="changePage" />
|
||
</div>
|
||
</el-card>
|
||
|
||
<el-dialog v-model="uploadVisible" title="上传新版本" width="520px">
|
||
<p class="upload-desc">上传软件 ZIP 包,系统会自动计算 MD5 并存储到 OSS。</p>
|
||
<el-form label-width="110px">
|
||
<el-form-item label="版本号" required>
|
||
<el-input v-model="newVersion" placeholder="例如:1.0.0" />
|
||
</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 格式</p>
|
||
<div v-if="pickedFile" class="dim">{{ pickedFile.name }}</div>
|
||
</el-form-item>
|
||
<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>
|
||
.page-heading { display: flex; justify-content: space-between; align-items: flex-start; }
|
||
.actions { display: flex; align-items: center; gap: 10px; }
|
||
.dim { color: var(--el-text-color-secondary); font-size: 12px; }
|
||
.upload-desc { margin: 0 0 12px; color: var(--el-text-color-secondary); font-size: 12.5px; line-height: 1.6; }
|
||
.zip-hint { margin: 4px 0 0; color: var(--el-text-color-secondary); font-size: 12px; }
|
||
.upload-msg { margin-bottom: 8px; }
|
||
.upload-msg :deep(.el-alert__title) { word-break: break-all; }
|
||
</style>
|