refactor(品牌工具页): 抽取 formatDateTime 与 uploadPathsToJava 公共工具

- 新增 shared/utils/datetime.ts:13 个品牌页逐字重复的 formatDateTime 收敛为一处;
  语义不同的 3 个变体(toLocaleString / 字符串切片 / 支持时间戳)保留不动
- 新增 shared/utils/upload-to-java.ts:10 个品牌页的上传循环收敛,api 依赖注入便于单测;
  保留 uploadOss(品牌Tab)与 returnEmptyWhenUnavailable(跟价)两处行为差异
- 补 datetime / upload-to-java 单测 10 例

净减约 327 行;vue-tsc 构建与 690 个前端单测全通过
This commit is contained in:
2026-09-13 23:16:59 +08:00
parent 43b1a0a955
commit 2b860cf79e
18 changed files with 210 additions and 377 deletions
@@ -155,7 +155,6 @@ import AmazonToolPageShell from '@/pages/amazon/components/AmazonToolPageShell.v
import TaskCenterPanel from '@/shared/components/tasks/TaskCenterPanel.vue'
import type { TaskItemView, TaskStatCard } from '@/shared/components/tasks/types'
import { expandBrandFolderRecursive } from '@/shared/api/brand'
import type { BrandExpandFolderItem } from '@/shared/api/brand'
import {
deleteConvertHistory,
getConvertHistory,
@@ -169,6 +168,8 @@ import { getPywebviewApi, type UploadedJavaFile } from '@/shared/bridges/pywebvi
import { saveUrlWithProgress } from '@/shared/utils/download-progress'
import { EXCEL_EXTENSIONS, checkSelectedFiles, guardBlocked } from '@/shared/dispatch-guard'
import { passGuard } from '@/shared/dispatch-guard-ui'
import { formatDateTime } from '@/shared/utils/datetime'
import { uploadPathsToJava } from '@/shared/utils/upload-to-java'
const convertSelectedPaths = ref<string[]>([])
const convertArchiveName = ref('')
@@ -210,19 +211,6 @@ function itemSource(item: TaskItemView): ConvertResultItem {
return item.source as ConvertResultItem
}
function formatDateTime(value?: string) {
if (!value) return '-'
const date = new Date(value)
if (Number.isNaN(date.getTime())) return value
const year = date.getFullYear()
const month = String(date.getMonth() + 1).padStart(2, '0')
const day = String(date.getDate()).padStart(2, '0')
const hours = String(date.getHours()).padStart(2, '0')
const minutes = String(date.getMinutes()).padStart(2, '0')
const seconds = String(date.getSeconds()).padStart(2, '0')
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
function convertTaskStatusText(item: ConvertResultItem) {
const status = (item.taskStatus || '').toUpperCase()
if (status === 'RUNNING') return '执行中'
@@ -254,25 +242,6 @@ function toConvertTaskView(item: ConvertResultItem): TaskItemView {
}
}
async function uploadPathsToJava(paths: Array<string | BrandExpandFolderItem>) {
const api = getPywebviewApi()
if (!api?.upload_file_to_java) {
throw new Error('当前桌面端未提供文件上传桥接能力')
}
const uploaded: UploadedJavaFile[] = []
for (const item of paths) {
const filePath = typeof item === 'string' ? item : item.absolutePath
const relativePath = typeof item === 'string' ? undefined : item.relativePath
const result = await api.upload_file_to_java(filePath, relativePath)
if (!result?.success || !result.data) {
throw new Error(result?.error || result?.message || `上传失败:${filePath}`)
}
uploaded.push(result.data)
}
return uploaded
}
async function loadConvertTemplates(preferredTemplateId?: string) {
const templates = await getConvertTemplates()
convertTemplates.value = templates || []
@@ -300,7 +269,7 @@ async function loadConvertTemplates(preferredTemplateId?: string) {
async function handleSelectedPaths(paths: string[], successMessage: string) {
convertSelectedPaths.value = paths
convertArchiveName.value = ''
convertUploadedFiles.value = await uploadPathsToJava(paths)
convertUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), paths)
ElMessage.success(successMessage)
}
@@ -339,7 +308,7 @@ async function selectConvertFolder() {
if (result.success && result.items?.length) {
if (!(await passGuard(checkSelectedFiles(result.items, { allowedExtensions: EXCEL_EXTENSIONS })))) return
convertSelectedPaths.value = result.items.map((item) => item.relativePath)
convertUploadedFiles.value = await uploadPathsToJava(result.items)
convertUploadedFiles.value = await uploadPathsToJava(getPywebviewApi(), result.items)
ElMessage.success(`已选择文件夹内 ${result.items.length} 个 xlsx 文件`)
return
}