与店铺商品页统一风格: - 去掉操作列的「日志」按钮(操作列宽度 180 → 140) - 点击行在表格下方(分页之后)展开操作记录面板,再次点击同一行收起 - 表格保持内部滚动,展开面板后表格自动让出高度 - 点击行内「编辑 / 删除」按钮不会误触发展开 - 移除原日志弹窗与 log-dialog-body 样式,补充 log-wrap / log-head / log-body 样式 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
946 lines
30 KiB
Vue
946 lines
30 KiB
Vue
<script setup lang="ts">
|
||
import { computed, onMounted, reactive, ref } from 'vue'
|
||
import { useRoute } from 'vue-router'
|
||
import { ElMessage, ElMessageBox, type FormInstance, type FormRules, type UploadFile, type UploadInstance } from 'element-plus'
|
||
import {
|
||
getProducts,
|
||
createProduct,
|
||
updateProduct,
|
||
deleteProduct,
|
||
downloadImportTemplate,
|
||
importProducts,
|
||
type ProductItem,
|
||
type ProductImportResult,
|
||
} from '@/api/product'
|
||
import {
|
||
getProductCategoryOptions,
|
||
createProductCategory,
|
||
type ProductAttributeOption as CategoryOption,
|
||
} from '@/api/productCategory'
|
||
import {
|
||
getProductBrandOptions,
|
||
createProductBrand,
|
||
type ProductAttributeOption as BrandOption,
|
||
} from '@/api/productBrand'
|
||
import {
|
||
getProductUnitOptions,
|
||
createProductUnit,
|
||
type ProductAttributeOption as UnitOption,
|
||
} from '@/api/productUnit'
|
||
import ImageUploader from '@/components/ImageUploader.vue'
|
||
import ExportButton from '@/components/ExportButton.vue'
|
||
import DataTable from '@/components/DataTable.vue'
|
||
import ListPager from '@/components/ListPager.vue'
|
||
import OperationLogPanel from '@/components/OperationLogPanel.vue'
|
||
import { DEFAULT_PAGE_SIZE, useListPage } from '@/composables/useListPage'
|
||
import { mediaUrl } from '@/utils/media'
|
||
|
||
const route = useRoute()
|
||
|
||
const query = reactive({
|
||
page: 1,
|
||
pageSize: DEFAULT_PAGE_SIZE,
|
||
keyword: '',
|
||
category_name: '',
|
||
brand_name: '',
|
||
stock_unit: '',
|
||
status: '' as string | number,
|
||
})
|
||
|
||
const { loading, list, total, elapsed, fetchList, search, handlePageChange, handleSizeChange } = useListPage({
|
||
query,
|
||
fetcher: () => {
|
||
const params: Record<string, unknown> = { ...query }
|
||
if (!params.keyword) delete params.keyword
|
||
if (!params.category_name) delete params.category_name
|
||
if (!params.brand_name) delete params.brand_name
|
||
if (!params.stock_unit) delete params.stock_unit
|
||
if (params.status === '') delete params.status
|
||
return getProducts(params)
|
||
},
|
||
})
|
||
|
||
function reset() {
|
||
query.keyword = ''
|
||
query.category_name = ''
|
||
query.brand_name = ''
|
||
query.stock_unit = ''
|
||
query.status = ''
|
||
search()
|
||
}
|
||
|
||
/** 导出筛选条件(与列表筛选同一套条件,字段名与后端 product 类型对齐) */
|
||
const exportFilters = computed(() => ({
|
||
keyword: query.keyword.trim(),
|
||
category_name: query.category_name,
|
||
brand_name: query.brand_name,
|
||
stock_unit: query.stock_unit,
|
||
status: query.status,
|
||
}))
|
||
|
||
/** 从路由 query 带入筛选条件(如由商品属性页「使用商品数」跳转而来) */
|
||
function applyRouteQuery() {
|
||
const q = route.query || {}
|
||
if (typeof q.category_name === 'string') query.category_name = q.category_name
|
||
if (typeof q.brand_name === 'string') query.brand_name = q.brand_name
|
||
if (typeof q.stock_unit === 'string') query.stock_unit = q.stock_unit
|
||
}
|
||
|
||
/* -------------------- 属性下拉(分类/品牌/单位) -------------------- */
|
||
const categoryOptions = ref<CategoryOption[]>([])
|
||
const brandOptions = ref<BrandOption[]>([])
|
||
const unitOptions = ref<UnitOption[]>([])
|
||
|
||
async function loadAttributeOptions() {
|
||
try {
|
||
const [ c, b, u ] = await Promise.all([
|
||
getProductCategoryOptions(),
|
||
getProductBrandOptions(),
|
||
getProductUnitOptions(),
|
||
])
|
||
categoryOptions.value = c
|
||
brandOptions.value = b
|
||
unitOptions.value = u
|
||
} catch {
|
||
// 拦截器已提示
|
||
}
|
||
}
|
||
|
||
/** 选中属性后回填 id + 文本(文本冗余存储,便于列表展示/兼容外部系统) */
|
||
function onCategoryChange(id: number | null) {
|
||
form.category_id = id
|
||
form.category_name = categoryOptions.value.find(o => o.id === id)?.name || ''
|
||
}
|
||
function onBrandChange(id: number | null) {
|
||
form.brand_id = id
|
||
form.brand_name = brandOptions.value.find(o => o.id === id)?.name || ''
|
||
}
|
||
function onUnitChange(id: number | null) {
|
||
form.unit_id = id
|
||
form.stock_unit = unitOptions.value.find(o => o.id === id)?.name || ''
|
||
}
|
||
|
||
/* -------------------- 快速新增属性(分类/品牌/单位) -------------------- */
|
||
type QuickAddType = 'category' | 'brand' | 'unit'
|
||
const quickAddVisible = ref(false)
|
||
const quickAddType = ref<QuickAddType | null>(null)
|
||
const quickAddName = ref('')
|
||
const quickAdding = ref(false)
|
||
const quickAddLabelMap: Record<QuickAddType, string> = {
|
||
category: '分类',
|
||
brand: '品牌',
|
||
unit: '单位',
|
||
}
|
||
|
||
function openQuickAdd(type: QuickAddType) {
|
||
quickAddType.value = type
|
||
quickAddName.value = ''
|
||
quickAddVisible.value = true
|
||
}
|
||
|
||
async function submitQuickAdd() {
|
||
const type = quickAddType.value
|
||
const name = quickAddName.value.trim()
|
||
if (!type || !name) {
|
||
ElMessage.warning('请输入名称')
|
||
return
|
||
}
|
||
quickAdding.value = true
|
||
try {
|
||
let res: { id: number }
|
||
if (type === 'category') res = await createProductCategory({ name })
|
||
else if (type === 'brand') res = await createProductBrand({ name })
|
||
else res = await createProductUnit({ name })
|
||
|
||
await loadAttributeOptions()
|
||
// 自动选中新建项并回填名称
|
||
if (type === 'category') onCategoryChange(res.id)
|
||
else if (type === 'brand') onBrandChange(res.id)
|
||
else onUnitChange(res.id)
|
||
|
||
ElMessage.success('已添加')
|
||
quickAddVisible.value = false
|
||
} catch {
|
||
// 拦截器已提示
|
||
} finally {
|
||
quickAdding.value = false
|
||
}
|
||
}
|
||
|
||
/* -------------------- 新增 / 编辑 -------------------- */
|
||
const drawerVisible = ref(false)
|
||
const submitting = ref(false)
|
||
const formRef = ref<FormInstance>()
|
||
const editId = ref<number | null>(null)
|
||
const form = reactive({
|
||
item_code: '',
|
||
item_name: '',
|
||
goods_code: '',
|
||
barcode: '',
|
||
item_type: 1,
|
||
category_id: null as number | null,
|
||
category_name: '',
|
||
brand_id: null as number | null,
|
||
brand_name: '',
|
||
unit_id: null as number | null,
|
||
sku_property: '',
|
||
stock_unit: '',
|
||
length: null as number | null,
|
||
width: null as number | null,
|
||
height: null as number | null,
|
||
volume: null as number | null,
|
||
gross_weight: null as number | null,
|
||
net_weight: null as number | null,
|
||
is_area_sale: 'N',
|
||
is_sn_mgmt: 'N',
|
||
is_shelf_life_mgmt: 'N',
|
||
is_batch_mgmt: 'N',
|
||
tag_price: null as number | null,
|
||
retail_price: 0,
|
||
cost_price: null as number | null,
|
||
image: '',
|
||
images: [] as string[],
|
||
status: 1,
|
||
remark: '',
|
||
})
|
||
const EMPTY_FORM = {
|
||
item_code: '',
|
||
item_name: '',
|
||
goods_code: '',
|
||
barcode: '',
|
||
item_type: 1,
|
||
category_id: null,
|
||
category_name: '',
|
||
brand_id: null,
|
||
brand_name: '',
|
||
unit_id: null,
|
||
sku_property: '',
|
||
stock_unit: '',
|
||
length: null,
|
||
width: null,
|
||
height: null,
|
||
volume: null,
|
||
gross_weight: null,
|
||
net_weight: null,
|
||
is_area_sale: 'N',
|
||
is_sn_mgmt: 'N',
|
||
is_shelf_life_mgmt: 'N',
|
||
is_batch_mgmt: 'N',
|
||
tag_price: null,
|
||
retail_price: 0,
|
||
cost_price: null,
|
||
image: '',
|
||
images: [] as string[],
|
||
status: 1,
|
||
remark: '',
|
||
}
|
||
const rules: FormRules = {
|
||
item_code: [{ required: true, message: '请输入商品编码', trigger: 'blur' }],
|
||
item_name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
|
||
retail_price: [{ required: true, message: '请输入零售价', trigger: 'blur' }],
|
||
status: [{ required: true, message: '请选择状态', trigger: 'change' }],
|
||
}
|
||
|
||
function openCreate() {
|
||
editId.value = null
|
||
Object.assign(form, EMPTY_FORM)
|
||
// EMPTY_FORM 里的数组会被 Object.assign 共享引用,这里换成新数组
|
||
form.images = []
|
||
drawerVisible.value = true
|
||
}
|
||
|
||
function openEdit(row: ProductItem) {
|
||
editId.value = row.id
|
||
Object.assign(form, {
|
||
item_code: row.item_code,
|
||
item_name: row.item_name,
|
||
goods_code: row.goods_code || '',
|
||
barcode: row.barcode || '',
|
||
item_type: row.item_type,
|
||
category_id: row.category_id,
|
||
category_name: row.category_name || '',
|
||
brand_id: row.brand_id,
|
||
brand_name: row.brand_name || '',
|
||
unit_id: row.unit_id,
|
||
sku_property: row.sku_property || '',
|
||
stock_unit: row.stock_unit || '',
|
||
length: toNumberOrNull(row.length),
|
||
width: toNumberOrNull(row.width),
|
||
height: toNumberOrNull(row.height),
|
||
volume: toNumberOrNull(row.volume),
|
||
gross_weight: toNumberOrNull(row.gross_weight),
|
||
net_weight: toNumberOrNull(row.net_weight),
|
||
is_area_sale: row.is_area_sale || 'N',
|
||
is_sn_mgmt: row.is_sn_mgmt || 'N',
|
||
is_shelf_life_mgmt: row.is_shelf_life_mgmt || 'N',
|
||
is_batch_mgmt: row.is_batch_mgmt || 'N',
|
||
tag_price: toNumberOrNull(row.tag_price),
|
||
retail_price: Number(row.retail_price) || 0,
|
||
cost_price: toNumberOrNull(row.cost_price),
|
||
image: row.image || '',
|
||
images: imagesOf(row),
|
||
status: row.status,
|
||
remark: row.remark || '',
|
||
})
|
||
drawerVisible.value = true
|
||
}
|
||
|
||
/** 空值原样返回 null,避免把空串塞进 number 输入框 */
|
||
function toNumberOrNull(value: number | string | null | undefined) {
|
||
if (value === null || value === undefined || value === '') return null
|
||
return Number(value)
|
||
}
|
||
|
||
/** 图片组:优先取 images,兼容历史单图 image */
|
||
function imagesOf(row: ProductItem): string[] {
|
||
const list = Array.isArray(row.images) ? row.images.filter(Boolean) : []
|
||
if (list.length) return list
|
||
return row.image ? [ row.image ] : []
|
||
}
|
||
|
||
/** 主图:图片组首张 */
|
||
function coverOf(row: ProductItem): string {
|
||
return imagesOf(row)[0] || ''
|
||
}
|
||
|
||
/** 随机生成商品编码(仅新增时使用):P + 时间戳 + 随机数,降低与手输编码冲突概率 */
|
||
function genRandomCode() {
|
||
const pad = (n: number) => String(n).padStart(2, '0')
|
||
const d = new Date()
|
||
const ts =
|
||
`${pad(d.getFullYear() % 100)}${pad(d.getMonth() + 1)}${pad(d.getDate())}` +
|
||
`${pad(d.getHours())}${pad(d.getMinutes())}${pad(d.getSeconds())}`
|
||
const rand = Math.floor(100000 + Math.random() * 900000)
|
||
form.item_code = `P${ts}${rand}`
|
||
formRef.value?.clearValidate('item_code')
|
||
}
|
||
|
||
async function submitForm() {
|
||
if (!formRef.value) return
|
||
await formRef.value.validate(async (valid) => {
|
||
if (!valid) return
|
||
submitting.value = true
|
||
try {
|
||
// 主图冗余取图片组首张,继续兼容外部系统与历史单图字段
|
||
const payload = { ...form, image: form.images[0] || '' }
|
||
if (editId.value == null) {
|
||
await createProduct(payload)
|
||
ElMessage.success('新增成功')
|
||
} else {
|
||
await updateProduct(editId.value, payload)
|
||
ElMessage.success('保存成功')
|
||
}
|
||
drawerVisible.value = false
|
||
await fetchList()
|
||
} catch {
|
||
// 拦截器已提示
|
||
} finally {
|
||
submitting.value = false
|
||
}
|
||
})
|
||
}
|
||
|
||
async function handleDelete(row: ProductItem) {
|
||
try {
|
||
await ElMessageBox.confirm(`确认删除商品「${row.item_name}」?`, '删除确认', {
|
||
type: 'warning',
|
||
confirmButtonText: '删除',
|
||
cancelButtonText: '取消',
|
||
})
|
||
} catch {
|
||
return
|
||
}
|
||
try {
|
||
await deleteProduct(row.id)
|
||
ElMessage.success('已删除')
|
||
await fetchList()
|
||
} catch {
|
||
// 拦截器已提示
|
||
}
|
||
}
|
||
|
||
/* -------------------- 操作记录(点击行后在下方展开,与店铺商品一致) -------------------- */
|
||
const logRow = ref<ProductItem | null>(null)
|
||
const logPanelHeight = ref(240)
|
||
|
||
function onRowClick(row: ProductItem, _column?: unknown, event?: Event) {
|
||
// 点行内按钮(编辑/删除)时不展开操作记录
|
||
const target = event && (event.target as HTMLElement | null)
|
||
if (target && target.closest && target.closest('button')) return
|
||
// 再次点击同一行则收起
|
||
if (logRow.value && logRow.value.id === row.id) {
|
||
closeLog()
|
||
return
|
||
}
|
||
logRow.value = row
|
||
}
|
||
|
||
function closeLog() {
|
||
logRow.value = null
|
||
}
|
||
|
||
const logTargetName = computed(() => {
|
||
const row = logRow.value
|
||
if (!row) return '操作记录'
|
||
return `${row.item_name || ''}(${row.item_code || ''})`
|
||
})
|
||
|
||
/* -------------------- 批量导入 -------------------- */
|
||
const importVisible = ref(false)
|
||
const importing = ref(false)
|
||
const importFileName = ref('')
|
||
const importFile = ref<File | null>(null)
|
||
const importResult = ref<ProductImportResult | null>(null)
|
||
const uploadRef = ref<UploadInstance>()
|
||
|
||
function openImport() {
|
||
importVisible.value = true
|
||
importResult.value = null
|
||
importFileName.value = ''
|
||
importFile.value = null
|
||
uploadRef.value?.clearFiles()
|
||
}
|
||
|
||
function onImportFileChange(file: UploadFile) {
|
||
importFileName.value = file.name
|
||
importFile.value = (file.raw as File | undefined) || null
|
||
importResult.value = null
|
||
}
|
||
|
||
function onImportFileRemove() {
|
||
importFileName.value = ''
|
||
importFile.value = null
|
||
importResult.value = null
|
||
}
|
||
|
||
async function downloadTemplate() {
|
||
try {
|
||
const blob = await downloadImportTemplate()
|
||
const url = window.URL.createObjectURL(blob)
|
||
const link = document.createElement('a')
|
||
link.href = url
|
||
link.download = 'product-import-template.xlsx'
|
||
link.click()
|
||
window.URL.revokeObjectURL(url)
|
||
} catch {
|
||
// 拦截器已提示
|
||
}
|
||
}
|
||
|
||
async function submitImport() {
|
||
const raw = importFile.value
|
||
if (!raw) {
|
||
ElMessage.warning('请先选择需要导入的 .xlsx 文件')
|
||
return
|
||
}
|
||
importing.value = true
|
||
try {
|
||
importResult.value = await importProducts(raw)
|
||
const result = importResult.value
|
||
if (result.created || result.updated) {
|
||
ElMessage.success(`导入完成:新增 ${result.created},更新 ${result.updated}`)
|
||
await fetchList()
|
||
} else {
|
||
ElMessage.warning('没有导入成功的数据,请查看失败明细')
|
||
}
|
||
} catch {
|
||
// 拦截器已提示
|
||
} finally {
|
||
importing.value = false
|
||
}
|
||
}
|
||
|
||
onMounted(async () => {
|
||
await loadAttributeOptions()
|
||
applyRouteQuery()
|
||
await fetchList()
|
||
})
|
||
</script>
|
||
|
||
<template>
|
||
<el-card class="list-card">
|
||
<template #header>
|
||
<div class="page-header">
|
||
<span>商品</span>
|
||
<div class="header-actions">
|
||
<el-button type="primary" @click="openCreate">新增商品</el-button>
|
||
<el-button @click="openImport">批量导入</el-button>
|
||
<ExportButton type="product" :filters="exportFilters" />
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<el-form :inline="true" class="search-bar">
|
||
<el-form-item label="关键词">
|
||
<el-input
|
||
v-model="query.keyword"
|
||
placeholder="商品编码/货物编码/条码/名称"
|
||
clearable
|
||
@keyup.enter="search"
|
||
/>
|
||
</el-form-item>
|
||
<el-form-item label="分类名称">
|
||
<el-select
|
||
v-model="query.category_name"
|
||
placeholder="精确匹配"
|
||
clearable
|
||
filterable
|
||
style="width: 160px"
|
||
>
|
||
<el-option v-for="o in categoryOptions" :key="o.id" :label="o.name" :value="o.name" />
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="品牌">
|
||
<el-select
|
||
v-model="query.brand_name"
|
||
placeholder="精确匹配"
|
||
clearable
|
||
filterable
|
||
style="width: 160px"
|
||
>
|
||
<el-option v-for="o in brandOptions" :key="o.id" :label="o.name" :value="o.name" />
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="单位">
|
||
<el-select
|
||
v-model="query.stock_unit"
|
||
placeholder="精确匹配"
|
||
clearable
|
||
filterable
|
||
style="width: 140px"
|
||
>
|
||
<el-option v-for="o in unitOptions" :key="o.id" :label="o.name" :value="o.name" />
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="状态">
|
||
<el-select v-model="query.status" placeholder="全部" clearable style="width: 140px">
|
||
<el-option label="启用" :value="1" />
|
||
<el-option label="禁用" :value="0" />
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item>
|
||
<el-button type="primary" @click="search">查询</el-button>
|
||
<el-button @click="reset">重置</el-button>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<div class="list-table-wrap" :class="{ 'list-table-wrap--with-log': !!logRow }">
|
||
<DataTable :loading="loading" :data="list" height="100%" highlight-current-row @row-click="onRowClick">
|
||
<el-table-column prop="item_code" label="商品编码" width="130" />
|
||
<el-table-column prop="goods_code" label="货物编码" width="120" />
|
||
<el-table-column prop="barcode" label="条码" width="120" />
|
||
<el-table-column prop="item_name" label="商品名称" min-width="160" />
|
||
<el-table-column label="图片" width="80" align="center">
|
||
<template #default="{ row }">
|
||
<el-image
|
||
v-if="coverOf(row)"
|
||
:src="mediaUrl(coverOf(row))"
|
||
fit="cover"
|
||
style="width: 44px; height: 44px; border-radius: 4px"
|
||
:preview-src-list="imagesOf(row).map(mediaUrl)"
|
||
preview-teleported
|
||
/>
|
||
<span v-else>-</span>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column prop="category_name" label="分类名称" width="110" />
|
||
<el-table-column prop="sku_property" label="SKU规格" width="110" />
|
||
<el-table-column prop="stock_unit" label="库存单位" width="90" />
|
||
<el-table-column label="正品可用" width="90" align="right">
|
||
<template #default="{ row }">{{ row.zc_available ?? 0 }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="正品锁定" width="90" align="right">
|
||
<template #default="{ row }">{{ row.zc_locked ?? 0 }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="次品可用" width="90" align="right">
|
||
<template #default="{ row }">{{ row.cc_available ?? 0 }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="次品锁定" width="90" align="right">
|
||
<template #default="{ row }">{{ row.cc_locked ?? 0 }}</template>
|
||
</el-table-column>
|
||
<el-table-column prop="brand_name" label="品牌" width="100" />
|
||
<el-table-column label="零售价" width="100" align="right">
|
||
<template #default="{ row }">{{ row.retail_price }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="成本价" width="100" align="right">
|
||
<template #default="{ row }">{{ row.cost_price ?? '-' }}</template>
|
||
</el-table-column>
|
||
<el-table-column label="状态" width="90">
|
||
<template #default="{ row }">
|
||
<el-tag :type="row.status === 1 ? 'success' : 'info'">
|
||
{{ row.status === 1 ? '启用' : '禁用' }}
|
||
</el-tag>
|
||
</template>
|
||
</el-table-column>
|
||
<el-table-column label="操作" width="140" fixed="right">
|
||
<template #default="{ row }">
|
||
<el-button link type="primary" @click="openEdit(row)">编辑</el-button>
|
||
<el-button link type="danger" @click="handleDelete(row)">删除</el-button>
|
||
</template>
|
||
</el-table-column>
|
||
</DataTable>
|
||
</div>
|
||
|
||
<ListPager
|
||
:total="total"
|
||
:elapsed="elapsed"
|
||
:page="query.page"
|
||
:page-size="query.pageSize"
|
||
@page-change="handlePageChange"
|
||
@size-change="handleSizeChange"
|
||
/>
|
||
|
||
<!-- 操作记录:点击商品行后在下方展开(新增 / 修改 / 删除留痕) -->
|
||
<div v-if="logRow" class="log-wrap" :style="{ height: `${logPanelHeight}px` }">
|
||
<div class="log-head">
|
||
<span class="log-name">{{ logTargetName }}</span>
|
||
<el-button link size="small" class="log-close" @click="closeLog">收起</el-button>
|
||
</div>
|
||
<div class="log-body">
|
||
<OperationLogPanel
|
||
:target-type="'product'"
|
||
:target-id="logRow.id"
|
||
:table-height="'100%'"
|
||
hide-header
|
||
/>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 批量导入:上传 xlsx,按商品编码新增/更新 -->
|
||
<el-dialog v-model="importVisible" title="批量导入商品" width="min(720px, 94vw)" top="8vh" destroy-on-close>
|
||
<div class="import-body">
|
||
<el-alert type="info" :closable="false" show-icon>
|
||
<template #title>
|
||
仅识别 12 个字段:商品编码、商品名称、货物编码、SKU规格、分类、品牌、条码、单位、零售价、成本价、标签价、备注;同一主体下商品编码已存在则更新(空单元格不覆盖原值),否则新建。
|
||
</template>
|
||
</el-alert>
|
||
|
||
<div class="import-actions">
|
||
<el-button link type="primary" @click="downloadTemplate">下载导入模板</el-button>
|
||
</div>
|
||
|
||
<el-upload
|
||
ref="uploadRef"
|
||
:auto-upload="false"
|
||
:limit="1"
|
||
accept=".xlsx"
|
||
:on-change="onImportFileChange"
|
||
:on-remove="onImportFileRemove"
|
||
>
|
||
<el-button type="primary" plain>选择 .xlsx 文件</el-button>
|
||
<template #tip>
|
||
<div class="import-tip">第一行必须是表头(可用导入模板填好再上传)</div>
|
||
</template>
|
||
</el-upload>
|
||
|
||
<div v-if="importResult" class="import-result">
|
||
<div class="import-summary">
|
||
共 {{ importResult.total }} 行:新增 <b>{{ importResult.created }}</b>,更新
|
||
<b>{{ importResult.updated }}</b>
|
||
<template v-if="importResult.restored">(其中恢复已删除 {{ importResult.restored }})</template>
|
||
,失败 <b>{{ importResult.failed }}</b>
|
||
</div>
|
||
<div v-if="importResult.ignored_columns.length" class="import-ignored">
|
||
已忽略的未知列:{{ importResult.ignored_columns.join('、') }}
|
||
</div>
|
||
<DataTable
|
||
v-if="importResult.errors.length"
|
||
:data="importResult.errors"
|
||
height="220"
|
||
size="small"
|
||
>
|
||
<el-table-column prop="row" label="行号" width="80" />
|
||
<el-table-column prop="item_code" label="商品编码" width="180" />
|
||
<el-table-column prop="message" label="失败原因" min-width="240" />
|
||
</DataTable>
|
||
</div>
|
||
</div>
|
||
<template #footer>
|
||
<el-button @click="importVisible = false">关闭</el-button>
|
||
<el-button type="primary" :loading="importing" :disabled="!importFileName" @click="submitImport">
|
||
开始导入
|
||
</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<el-drawer
|
||
v-model="drawerVisible"
|
||
:title="editId == null ? '新增商品' : '编辑商品'"
|
||
direction="rtl"
|
||
size="580px"
|
||
>
|
||
<el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
|
||
<el-form-item label="商品编码" prop="item_code">
|
||
<el-input v-model="form.item_code" placeholder="同一主体下唯一">
|
||
<template v-if="editId == null" #append>
|
||
<el-button @click="genRandomCode">随机生成</el-button>
|
||
</template>
|
||
</el-input>
|
||
</el-form-item>
|
||
<el-form-item label="商品名称" prop="item_name">
|
||
<el-input v-model="form.item_name" placeholder="请输入商品名称" />
|
||
</el-form-item>
|
||
<el-form-item label="货物编码" prop="goods_code">
|
||
<el-input v-model="form.goods_code" placeholder="选填,与条码可二选一" />
|
||
</el-form-item>
|
||
<el-form-item label="条码" prop="barcode">
|
||
<el-input v-model="form.barcode" placeholder="选填,商品条码" />
|
||
</el-form-item>
|
||
<el-form-item label="分类" prop="category_name">
|
||
<div class="select-with-add">
|
||
<el-select
|
||
v-model="form.category_id"
|
||
placeholder="选填(也可在商品属性中维护)"
|
||
clearable
|
||
filterable
|
||
style="flex: 1"
|
||
@change="onCategoryChange"
|
||
>
|
||
<el-option v-for="o in categoryOptions" :key="o.id" :label="o.name" :value="o.id" />
|
||
</el-select>
|
||
<el-button title="快速新增分类" @click="openQuickAdd('category')">+</el-button>
|
||
</div>
|
||
</el-form-item>
|
||
<el-form-item label="SKU规格" prop="sku_property">
|
||
<el-input v-model="form.sku_property" placeholder="选填" />
|
||
</el-form-item>
|
||
<el-form-item label="单位" prop="stock_unit">
|
||
<div class="select-with-add">
|
||
<el-select
|
||
v-model="form.unit_id"
|
||
placeholder="选填(也可在商品属性中维护)"
|
||
clearable
|
||
filterable
|
||
style="flex: 1"
|
||
@change="onUnitChange"
|
||
>
|
||
<el-option v-for="o in unitOptions" :key="o.id" :label="o.name" :value="o.id" />
|
||
</el-select>
|
||
<el-button title="快速新增单位" @click="openQuickAdd('unit')">+</el-button>
|
||
</div>
|
||
</el-form-item>
|
||
<el-form-item label="品牌" prop="brand_name">
|
||
<div class="select-with-add">
|
||
<el-select
|
||
v-model="form.brand_id"
|
||
placeholder="选填(也可在商品属性中维护)"
|
||
clearable
|
||
filterable
|
||
style="flex: 1"
|
||
@change="onBrandChange"
|
||
>
|
||
<el-option v-for="o in brandOptions" :key="o.id" :label="o.name" :value="o.id" />
|
||
</el-select>
|
||
<el-button title="快速新增品牌" @click="openQuickAdd('brand')">+</el-button>
|
||
</div>
|
||
</el-form-item>
|
||
<el-form-item label="标价" prop="tag_price">
|
||
<el-input-number
|
||
v-model="form.tag_price"
|
||
:min="0"
|
||
:precision="2"
|
||
style="width: 100%"
|
||
placeholder="选填"
|
||
/>
|
||
</el-form-item>
|
||
<el-form-item label="零售价" prop="retail_price">
|
||
<el-input-number v-model="form.retail_price" :min="0" :precision="2" style="width: 100%" />
|
||
</el-form-item>
|
||
<el-form-item label="成本价" prop="cost_price">
|
||
<el-input-number
|
||
v-model="form.cost_price"
|
||
:min="0"
|
||
:precision="2"
|
||
style="width: 100%"
|
||
placeholder="选填"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<el-divider content-position="left">物流属性(选填)</el-divider>
|
||
<el-form-item label="长/宽/高 (cm)">
|
||
<div class="field-row">
|
||
<el-input-number v-model="form.length" :min="0" :precision="2" placeholder="长(cm)" />
|
||
<el-input-number v-model="form.width" :min="0" :precision="2" placeholder="宽(cm)" />
|
||
<el-input-number v-model="form.height" :min="0" :precision="2" placeholder="高(cm)" />
|
||
</div>
|
||
</el-form-item>
|
||
<el-form-item label="体积 (cm³)">
|
||
<el-input-number
|
||
v-model="form.volume"
|
||
:min="0"
|
||
:precision="2"
|
||
style="width: 100%"
|
||
placeholder="留空可由长×宽×高自动推算"
|
||
/>
|
||
</el-form-item>
|
||
<el-form-item label="毛重/净重 (g)">
|
||
<div class="field-row">
|
||
<el-input-number v-model="form.gross_weight" :min="0" :precision="0" placeholder="毛重(g)" />
|
||
<el-input-number v-model="form.net_weight" :min="0" :precision="0" placeholder="净重(g)" />
|
||
</div>
|
||
</el-form-item>
|
||
<el-form-item label="区域销售">
|
||
<el-switch v-model="form.is_area_sale" active-value="Y" inactive-value="N" />
|
||
</el-form-item>
|
||
<el-form-item label="序列号管理">
|
||
<el-switch v-model="form.is_sn_mgmt" active-value="Y" inactive-value="N" />
|
||
</el-form-item>
|
||
<el-form-item label="保质期管理">
|
||
<el-switch v-model="form.is_shelf_life_mgmt" active-value="Y" inactive-value="N" />
|
||
</el-form-item>
|
||
<el-form-item label="批次管理">
|
||
<el-switch v-model="form.is_batch_mgmt" active-value="Y" inactive-value="N" />
|
||
</el-form-item>
|
||
|
||
<el-divider content-position="left">图片</el-divider>
|
||
<el-form-item label="图片组" prop="images">
|
||
<ImageUploader v-model="form.images" />
|
||
</el-form-item>
|
||
|
||
<el-divider content-position="left">其它</el-divider>
|
||
<el-form-item label="状态" prop="status">
|
||
<el-select v-model="form.status" style="width: 100%">
|
||
<el-option label="启用" :value="1" />
|
||
<el-option label="禁用" :value="0" />
|
||
</el-select>
|
||
</el-form-item>
|
||
<el-form-item label="备注" prop="remark">
|
||
<el-input v-model="form.remark" type="textarea" :rows="2" placeholder="选填" />
|
||
</el-form-item>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button @click="drawerVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="submitting" @click="submitForm">保存</el-button>
|
||
</template>
|
||
</el-drawer>
|
||
|
||
<el-dialog
|
||
v-model="quickAddVisible"
|
||
:title="`快速新增${quickAddType ? quickAddLabelMap[quickAddType] : ''}`"
|
||
width="420px"
|
||
append-to-body
|
||
>
|
||
<el-form label-width="72px" @submit.prevent>
|
||
<el-form-item :label="quickAddType ? quickAddLabelMap[quickAddType] : '名称'">
|
||
<el-input
|
||
v-model="quickAddName"
|
||
placeholder="请输入名称"
|
||
maxlength="50"
|
||
show-word-limit
|
||
@keyup.enter="submitQuickAdd"
|
||
/>
|
||
</el-form-item>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button @click="quickAddVisible = false">取消</el-button>
|
||
<el-button type="primary" :loading="quickAdding" @click="submitQuickAdd">确定</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</el-card>
|
||
</template>
|
||
|
||
<style scoped>
|
||
/* 列表页统一版式:卡片撑满可视高度,筛选与分页固定,只有表格主体滚动 */
|
||
.list-card {
|
||
flex: 1;
|
||
min-height: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
}
|
||
.list-card :deep(.el-card__body) {
|
||
flex: 1;
|
||
min-height: 0;
|
||
display: flex;
|
||
flex-direction: column;
|
||
overflow: hidden;
|
||
padding: 10px 12px;
|
||
}
|
||
.list-table-wrap {
|
||
flex: 1;
|
||
min-height: 0;
|
||
overflow: hidden;
|
||
}
|
||
.page-header {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
font-weight: 600;
|
||
}
|
||
/* 底部展开操作记录后,表格让出一部分高度(表格内部仍可滚动) */
|
||
.log-wrap {
|
||
flex: 0 0 auto;
|
||
display: flex;
|
||
flex-direction: column;
|
||
min-height: 0;
|
||
margin-top: 8px;
|
||
padding-top: 8px;
|
||
border-top: 1px solid var(--el-border-color-lighter);
|
||
}
|
||
.log-head {
|
||
flex: 0 0 auto;
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
margin-bottom: 4px;
|
||
}
|
||
.log-name {
|
||
font-size: 12px;
|
||
font-weight: 600;
|
||
overflow: hidden;
|
||
text-overflow: ellipsis;
|
||
white-space: nowrap;
|
||
}
|
||
.log-close {
|
||
margin-left: auto;
|
||
}
|
||
.log-body {
|
||
flex: 1 1 auto;
|
||
min-height: 0;
|
||
}
|
||
.import-body {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 12px;
|
||
}
|
||
.import-actions {
|
||
display: flex;
|
||
justify-content: flex-end;
|
||
}
|
||
.import-tip {
|
||
color: var(--el-text-color-secondary);
|
||
font-size: 12px;
|
||
}
|
||
.import-result {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 8px;
|
||
}
|
||
.import-summary {
|
||
font-size: 13px;
|
||
}
|
||
.import-ignored {
|
||
font-size: 12px;
|
||
color: var(--el-color-warning);
|
||
}
|
||
.search-bar {
|
||
margin-bottom: 16px;
|
||
}
|
||
.field-row {
|
||
display: flex;
|
||
gap: 8px;
|
||
width: 100%;
|
||
}
|
||
.select-with-add {
|
||
display: flex;
|
||
gap: 8px;
|
||
width: 100%;
|
||
}
|
||
.select-with-add .el-button {
|
||
flex: 0 0 auto;
|
||
padding-left: 12px;
|
||
padding-right: 12px;
|
||
font-size: 16px;
|
||
line-height: 1;
|
||
}
|
||
</style>
|