提交新增内容
This commit is contained in:
411
frontend-vue/src/pages/brand/components/BrandConvertTab.vue
Normal file
411
frontend-vue/src/pages/brand/components/BrandConvertTab.vue
Normal file
@@ -0,0 +1,411 @@
|
||||
<template>
|
||||
<div class="main-content">
|
||||
<aside class="left-panel">
|
||||
<div class="section-title">选择文件</div>
|
||||
<div class="upload-zone">
|
||||
<div class="hint">选择需要转换的 Excel 文件或文件夹,将按所选模板生成 txt 文件。</div>
|
||||
<div class="btns">
|
||||
<button type="button" class="opt-btn" @click="selectConvertFiles">选择 Excel 文件</button>
|
||||
<button type="button" class="opt-btn" @click="selectConvertFolder">选择文件夹</button>
|
||||
</div>
|
||||
|
||||
<el-alert
|
||||
v-if="!hasPywebviewSupport"
|
||||
class="desktop-alert"
|
||||
type="warning"
|
||||
:closable="false"
|
||||
title="当前环境未检测到 pywebview,文件选择与本地格式转换能力需要在桌面端使用。"
|
||||
/>
|
||||
|
||||
<div class="selected-files clean-placeholder">
|
||||
<template v-if="convertSelectedPaths.length">
|
||||
<span v-for="path in convertDisplayPaths" :key="path">{{ path }}</span>
|
||||
<span v-if="convertSelectedPaths.length > convertDisplayPaths.length" class="more-line">
|
||||
还有 {{ convertSelectedPaths.length - convertDisplayPaths.length }} 个文件未展开显示
|
||||
</span>
|
||||
</template>
|
||||
<span v-else>暂未选择待转换文件</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="section-title">模板选择</div>
|
||||
<div class="template-toolbar">
|
||||
<input v-model="convertTemplateName" class="template-name-input" type="text" placeholder="输入模板自定义名称(可选)" />
|
||||
<button type="button" class="opt-btn small" @click="importConvertTemplate">上传模板</button>
|
||||
</div>
|
||||
<div class="option-group">
|
||||
<label
|
||||
v-for="template in convertTemplates"
|
||||
:key="template.id"
|
||||
class="radio-item"
|
||||
:class="{ active: convertTemplateId === template.id }"
|
||||
>
|
||||
<input v-model="convertTemplateId" type="radio" :value="template.id" name="convert-template" />
|
||||
<div>
|
||||
<span class="label">
|
||||
{{ template.label }}
|
||||
<span v-if="template.is_default" class="template-tag">默认</span>
|
||||
<span v-else-if="template.built_in" class="template-tag muted">内置</span>
|
||||
</span>
|
||||
<div class="desc">输出文件:{{ template.output_filename }}</div>
|
||||
</div>
|
||||
</label>
|
||||
</div>
|
||||
<div class="run-row compact-row">
|
||||
<button type="button" class="opt-btn small" :disabled="!convertTemplateId" @click="setConvertDefaultTemplate">
|
||||
设为默认模板
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="section-title">转换说明</div>
|
||||
<div class="option-group">
|
||||
<label class="radio-item active">
|
||||
<input type="checkbox" checked disabled />
|
||||
<div>
|
||||
<span class="label">按当前模板输出</span>
|
||||
<div class="desc">当前模板:{{ currentConvertTemplate?.label || '未加载模板' }}</div>
|
||||
</div>
|
||||
</label>
|
||||
|
||||
<label class="radio-item active">
|
||||
<input type="checkbox" checked disabled />
|
||||
<div>
|
||||
<span class="label">SKU 使用雪花ID-序号</span>
|
||||
<div class="desc">例如 1773816146796-1</div>
|
||||
</div>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="run-row">
|
||||
<button type="button" class="btn-run" :disabled="convertRunning" @click="submitConvertRun">
|
||||
{{ convertRunning ? '转换中...' : '开始转换' }}
|
||||
</button>
|
||||
<span class="loading-msg">
|
||||
{{ convertRunning ? '正在生成英国.txt,请稍候…' : '选择文件后即可执行格式转换' }}
|
||||
</span>
|
||||
</div>
|
||||
</aside>
|
||||
|
||||
<section class="right-panel">
|
||||
<div class="panel-header">转换结果</div>
|
||||
|
||||
<div class="task-list-wrap">
|
||||
<div class="clean-result-summary">
|
||||
<div class="summary-card">
|
||||
<span class="summary-label">已处理文件</span>
|
||||
<strong>{{ convertSummary.total }}</strong>
|
||||
</div>
|
||||
<div class="summary-card">
|
||||
<span class="summary-label">成功结果</span>
|
||||
<strong>{{ convertSummary.success_count }}</strong>
|
||||
</div>
|
||||
<div class="summary-card">
|
||||
<span class="summary-label">失败文件</span>
|
||||
<strong>{{ convertSummary.failed_count }}</strong>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="result-list-wrap">
|
||||
<div class="result-list-header">
|
||||
<span>生成的 txt 列表</span>
|
||||
<button type="button" class="download-all" :disabled="!convertOutputDir" @click="openConvertOutputDir">
|
||||
打开输出目录
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div v-if="convertResultItems.length === 0" class="empty-tasks">
|
||||
暂无转换结果,完成格式转换后会在这里展示输出文件
|
||||
</div>
|
||||
|
||||
<ul v-else class="task-list clean-result-list">
|
||||
<li
|
||||
v-for="item in convertResultItems"
|
||||
:key="`${item.source_path}-${item.output_path || item.error || 'convert'}`"
|
||||
class="task-item"
|
||||
>
|
||||
<div class="left">
|
||||
<span class="id" :title="item.source_path">{{ shorten(item.source_path, 42) }}</span>
|
||||
<div v-if="item.output_path" class="files">输出文件:{{ item.output_path }}</div>
|
||||
<div v-else-if="item.error" class="files">错误信息:{{ item.error }}</div>
|
||||
</div>
|
||||
|
||||
<div class="task-right">
|
||||
<span class="status" :class="item.success ? 'success' : 'failed'">
|
||||
{{ item.success ? '已完成' : '失败' }}
|
||||
</span>
|
||||
<button
|
||||
v-if="item.success && item.output_path"
|
||||
type="button"
|
||||
class="download"
|
||||
@click="openConvertResult(item)"
|
||||
>
|
||||
打开文件
|
||||
</button>
|
||||
</div>
|
||||
</li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, ref } from 'vue'
|
||||
import { ElMessage } from 'element-plus'
|
||||
import { expandBrandFolder } from '@/shared/api/brand'
|
||||
import { type ConvertTxtResultItem, type ConvertTxtTemplateItem, getPywebviewApi, hasPywebview } from '@/shared/bridges/pywebview'
|
||||
|
||||
const hasPywebviewSupport = hasPywebview()
|
||||
const convertSelectedPaths = ref<string[]>([])
|
||||
const convertRunning = ref(false)
|
||||
const convertOutputDir = ref('')
|
||||
const convertResultItems = ref<ConvertTxtResultItem[]>([])
|
||||
const convertSummary = ref({ total: 0, success_count: 0, failed_count: 0 })
|
||||
const convertTemplates = ref<ConvertTxtTemplateItem[]>([])
|
||||
const convertTemplateId = ref('uk_offer')
|
||||
const convertTemplateName = ref('')
|
||||
const convertDisplayPaths = computed(() => convertSelectedPaths.value.slice(0, 8))
|
||||
const currentConvertTemplate = computed(
|
||||
() => convertTemplates.value.find((item) => item.id === convertTemplateId.value) || null,
|
||||
)
|
||||
|
||||
function shorten(value: string, maxLength: number) {
|
||||
return value.length > maxLength ? `${value.slice(0, maxLength)}...` : value
|
||||
}
|
||||
|
||||
function resetConvertResults() {
|
||||
convertResultItems.value = []
|
||||
convertSummary.value = { total: 0, success_count: 0, failed_count: 0 }
|
||||
convertOutputDir.value = ''
|
||||
}
|
||||
|
||||
async function loadConvertTemplates() {
|
||||
const api = getPywebviewApi()
|
||||
const templates = await api?.list_txt_templates?.()
|
||||
convertTemplates.value = Array.isArray(templates) ? templates : []
|
||||
const defaultTemplate = convertTemplates.value.find((item) => item.is_default)
|
||||
if (defaultTemplate) {
|
||||
convertTemplateId.value = defaultTemplate.id
|
||||
return
|
||||
}
|
||||
if (convertTemplates.value.length && !convertTemplates.value.some((item) => item.id === convertTemplateId.value)) {
|
||||
convertTemplateId.value = convertTemplates.value[0].id
|
||||
}
|
||||
}
|
||||
|
||||
async function importConvertTemplate() {
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.import_txt_template) {
|
||||
ElMessage.warning('当前环境不支持模板上传,请在桌面端打开')
|
||||
return
|
||||
}
|
||||
|
||||
const result = await api.import_txt_template(convertTemplateName.value.trim() || undefined)
|
||||
if (!result.success) {
|
||||
if (result.error && result.error !== '用户取消') {
|
||||
ElMessage.error(result.error)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
convertTemplateName.value = ''
|
||||
await loadConvertTemplates()
|
||||
if (result.template?.id) {
|
||||
convertTemplateId.value = result.template.id
|
||||
}
|
||||
ElMessage.success('模板导入成功')
|
||||
}
|
||||
|
||||
async function setConvertDefaultTemplate() {
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.set_default_txt_template || !convertTemplateId.value) {
|
||||
ElMessage.warning('当前环境不支持设置默认模板')
|
||||
return
|
||||
}
|
||||
|
||||
const result = await api.set_default_txt_template(convertTemplateId.value)
|
||||
if (!result.success) {
|
||||
ElMessage.error(result.error || '设置默认模板失败')
|
||||
return
|
||||
}
|
||||
|
||||
await loadConvertTemplates()
|
||||
ElMessage.success('默认模板已更新')
|
||||
}
|
||||
|
||||
async function selectConvertFiles() {
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.select_clean_xlsx_files) {
|
||||
ElMessage.warning('当前环境不支持文件选择,请在桌面端打开')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const paths = await api.select_clean_xlsx_files()
|
||||
if (!paths?.length) return
|
||||
convertSelectedPaths.value = paths
|
||||
resetConvertResults()
|
||||
ElMessage.success(`已选择 ${paths.length} 个待转换文件`)
|
||||
} catch (error) {
|
||||
ElMessage.error(error instanceof Error ? error.message : '选择失败')
|
||||
}
|
||||
}
|
||||
|
||||
async function selectConvertFolder() {
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.select_clean_folder) {
|
||||
ElMessage.warning('当前环境不支持文件夹选择,请在桌面端打开')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const folder = await api.select_clean_folder()
|
||||
if (!folder) return
|
||||
|
||||
const result = await expandBrandFolder(folder)
|
||||
if (result.success && result.paths?.length) {
|
||||
convertSelectedPaths.value = result.paths
|
||||
resetConvertResults()
|
||||
ElMessage.success(`已选择文件夹内 ${result.paths.length} 个 xlsx 文件`)
|
||||
return
|
||||
}
|
||||
|
||||
ElMessage.warning(result.error || '该文件夹下没有 xlsx 文件')
|
||||
} catch (error) {
|
||||
ElMessage.error(error instanceof Error ? error.message : '选择失败')
|
||||
}
|
||||
}
|
||||
|
||||
async function submitConvertRun() {
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.convert_excel_to_uk_txt || !api?.select_folder) {
|
||||
ElMessage.warning('当前环境不支持格式转换,请在桌面端打开')
|
||||
return
|
||||
}
|
||||
if (!convertSelectedPaths.value.length) {
|
||||
ElMessage.warning('请先选择待转换 Excel 文件或文件夹')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const outputDir = await api.select_folder()
|
||||
if (!outputDir) return
|
||||
|
||||
convertRunning.value = true
|
||||
const result = await api.convert_excel_to_uk_txt({
|
||||
paths: convertSelectedPaths.value,
|
||||
output_dir: outputDir,
|
||||
template_id: convertTemplateId.value,
|
||||
})
|
||||
|
||||
if (!result.success) {
|
||||
ElMessage.error(result.error || '格式转换失败')
|
||||
return
|
||||
}
|
||||
|
||||
convertOutputDir.value = result.summary?.output_dir || outputDir
|
||||
convertSummary.value = {
|
||||
total: result.summary?.total || 0,
|
||||
success_count: result.summary?.success_count || 0,
|
||||
failed_count: result.summary?.failed_count || 0,
|
||||
}
|
||||
convertResultItems.value = result.items || []
|
||||
ElMessage.success('格式转换完成')
|
||||
} catch (error) {
|
||||
ElMessage.error(error instanceof Error ? error.message : '格式转换失败')
|
||||
} finally {
|
||||
convertRunning.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function openConvertOutputDir() {
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.open_path || !convertOutputDir.value) {
|
||||
ElMessage.warning('当前环境不支持打开输出目录')
|
||||
return
|
||||
}
|
||||
|
||||
const result = await api.open_path(convertOutputDir.value)
|
||||
if (!result.success) {
|
||||
ElMessage.error(result.error || '打开目录失败')
|
||||
}
|
||||
}
|
||||
|
||||
async function openConvertResult(item: ConvertTxtResultItem) {
|
||||
const api = getPywebviewApi()
|
||||
if (!api?.open_path || !item.output_path) {
|
||||
ElMessage.warning('当前环境不支持打开结果文件')
|
||||
return
|
||||
}
|
||||
|
||||
const result = await api.open_path(item.output_path)
|
||||
if (!result.success) {
|
||||
ElMessage.error(result.error || '打开文件失败')
|
||||
}
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
loadConvertTemplates().catch(() => undefined)
|
||||
})
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.main-content { display: flex; height: calc(100vh - 56px); }
|
||||
.left-panel { width: 380px; background: #1e1e1e; padding: 20px; overflow-y: auto; border-right: 1px solid #2a2a2a; }
|
||||
.right-panel { flex: 1; display: flex; flex-direction: column; min-width: 0; background: #1a1a1a; }
|
||||
.section-title { font-size: 13px; color: #bbb; margin-bottom: 10px; }
|
||||
.upload-zone { border: 1px dashed #3a3a3a; border-radius: 10px; padding: 24px; text-align: center; background: #252525; margin-bottom: 20px; transition: all 0.2s ease; }
|
||||
.upload-zone:hover { border-color: #3498db; background: #2a2a2a; }
|
||||
.hint { color: #888; font-size: 13px; margin-bottom: 12px; line-height: 1.5; }
|
||||
.btns { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
|
||||
.opt-btn, .btn-run, .download { border: none; cursor: pointer; transition: all 0.2s ease; }
|
||||
.opt-btn { padding: 8px 16px; font-size: 13px; color: #ccc; background: #2a2a2a; border: 1px solid #3a3a3a; border-radius: 6px; }
|
||||
.opt-btn.small { padding: 6px 10px; font-size: 12px; }
|
||||
.opt-btn:hover { color: #3498db; background: #333; border-color: #3498db; }
|
||||
.desktop-alert { margin-top: 14px; text-align: left; }
|
||||
.selected-files { margin-top: 14px; font-size: 12px; color: #888; max-height: 112px; overflow-y: auto; text-align: left; }
|
||||
.selected-files span { display: block; margin: 4px 0; word-break: break-all; }
|
||||
.more-line { color: #b8c1cc; }
|
||||
.option-group { margin-bottom: 20px; }
|
||||
.template-toolbar { display: flex; gap: 10px; margin-bottom: 12px; }
|
||||
.template-name-input { flex: 1; min-width: 0; padding: 8px 12px; border-radius: 8px; border: 1px solid #3a3a3a; background: #202020; color: #e0e0e0; font-size: 13px; }
|
||||
.template-name-input::placeholder { color: #7f8790; }
|
||||
.template-tag { margin-left: 8px; padding: 2px 8px; border-radius: 999px; font-size: 11px; color: #8fd0ff; background: rgba(52, 152, 219, 0.14); }
|
||||
.template-tag.muted { color: #b8b8b8; background: rgba(255, 255, 255, 0.08); }
|
||||
.compact-row { margin-top: 4px; }
|
||||
.radio-item { display: flex; align-items: flex-start; gap: 10px; cursor: pointer; padding: 10px 12px; border-radius: 8px; background: #252525; border: 1px solid #2a2a2a; margin-bottom: 8px; transition: all 0.2s ease; }
|
||||
.radio-item:hover, .radio-item.active { background: #2a2a2a; border-color: #3a3a3a; }
|
||||
.radio-item input { margin-top: 3px; }
|
||||
.label { font-weight: 500; color: #e0e0e0; font-size: 13px; }
|
||||
.desc { font-size: 12px; color: #888; margin-top: 2px; line-height: 1.5; font-weight: 400; }
|
||||
.run-row { display: flex; align-items: center; flex-wrap: wrap; gap: 10px 12px; margin-top: 16px; }
|
||||
.btn-run { padding: 10px 22px; background: #3498db; color: #fff; border-radius: 8px; font-size: 14px; }
|
||||
.btn-run:hover { background: #2980b9; }
|
||||
.btn-run:disabled { background: #555; color: #999; cursor: not-allowed; }
|
||||
.loading-msg { color: #3498db; font-size: 13px; }
|
||||
.panel-header { padding: 16px 20px; border-bottom: 1px solid #2a2a2a; font-size: 15px; font-weight: 600; color: #ddd; }
|
||||
.task-list-wrap { flex: 1; overflow-y: auto; padding: 16px; }
|
||||
.task-list { list-style: none; margin: 0; padding: 0; }
|
||||
.task-item { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 16px; border: 1px solid #2a2a2a; border-radius: 8px; margin-bottom: 10px; background: #1e1e1e; }
|
||||
.left { flex: 1; min-width: 0; }
|
||||
.id { display: inline-block; font-weight: 600; color: #e0e0e0; font-size: 13px; }
|
||||
.files { font-size: 12px; color: #888; margin-top: 4px; }
|
||||
.task-right { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; justify-content: flex-end; }
|
||||
.status { padding: 4px 10px; border-radius: 6px; font-size: 12px; white-space: nowrap; }
|
||||
.status.success { background: rgba(46, 204, 113, 0.18); color: #2ecc71; }
|
||||
.status.failed { background: rgba(231, 76, 60, 0.18); color: #ff6b6b; }
|
||||
.download { padding: 6px 10px; border-radius: 6px; font-size: 12px; background: rgba(52, 152, 219, 0.18); color: #69b6ff; }
|
||||
.download:hover { background: rgba(52, 152, 219, 0.28); }
|
||||
.empty-tasks { color: #666; font-size: 13px; padding: 24px 8px; }
|
||||
.clean-placeholder { max-height: none; }
|
||||
.clean-result-summary { display: grid; grid-template-columns: repeat(3, minmax(0, 220px)); gap: 16px; margin-bottom: 18px; }
|
||||
.summary-card { padding: 16px 18px; border: 1px solid #2a2a2a; border-radius: 10px; background: #1e1e1e; }
|
||||
.summary-card strong { display: block; margin-top: 8px; font-size: 24px; color: #eaf4ff; }
|
||||
.summary-label { font-size: 12px; color: #8d8d8d; }
|
||||
.result-list-wrap { border: 1px solid #2a2a2a; border-radius: 10px; background: #1e1e1e; min-height: 260px; }
|
||||
.result-list-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px; border-bottom: 1px solid #2a2a2a; font-size: 14px; color: #ddd; }
|
||||
.download-all { padding: 7px 12px; border: 1px solid #2f5f85; border-radius: 6px; background: rgba(52, 152, 219, 0.14); color: #69b6ff; font-size: 12px; cursor: pointer; }
|
||||
.download-all:disabled { cursor: not-allowed; opacity: 0.55; }
|
||||
@media (max-width: 1100px) { .main-content { flex-direction: column; height: auto; } .left-panel { width: 100%; border-right: none; border-bottom: 1px solid #2a2a2a; } .right-panel { min-height: 420px; } .task-item { flex-direction: column; align-items: flex-start; } .task-right { width: 100%; justify-content: flex-start; } .clean-result-summary { grid-template-columns: 1fr; } }
|
||||
</style>
|
||||
Reference in New Issue
Block a user