liufumingren ce636c15f4 style(frontend): 列表页统一版式(分页固定底部)+ 操作记录面板
列表统一版式(对齐订单页):
- 所有列表页卡片撑满可视高度(flex:1 + min-height:0),el-card__body 改为 flex 列并 overflow:hidden
- 筛选区固定(flex:0 0 auto),表格区 flex:1 且 overflow:hidden,表格传 height="100%" 只在表体滚动
- 分页组件固定在底部,不再跟着表格滚走
- 涉及页面:商品、组合商品、主体管理、账号管理、外部打单平台
  (导出任务、库存单据、库存流水、店铺、店铺商品、出入库单此前已统一)

操作记录面板:
- 新增 components/OperationLogPanel.vue(分页表格:时间/操作/说明/操作人/来源)与 api/operationLog.ts
- 店铺商品页:点击商品行在表格下方展开操作记录,再次点击或「收起」关闭
- 商品、组合商品页:行内新增「日志」按钮,弹窗展示操作记录

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-11-03 03:43:35 +08:00

759 lines
25 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 { computed, onMounted, reactive, ref } from 'vue'
import { useRoute } from 'vue-router'
import { ElMessage, ElMessageBox, type FormInstance, type FormRules } from 'element-plus'
import {
getProducts,
createProduct,
updateProduct,
deleteProduct,
type ProductItem,
} 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 logVisible = ref(false)
const logRow = ref<ProductItem | null>(null)
function openLog(row: ProductItem) {
logRow.value = row
logVisible.value = true
}
const logTitle = computed(() => {
const row = logRow.value
if (!row) return '操作记录'
return `操作记录:${row.item_name || ''}(${row.item_code || ''})`
})
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>
<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">
<DataTable :loading="loading" :data="list" height="100%">
<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="180" fixed="right">
<template #default="{ row }">
<el-button link type="primary" @click="openEdit(row)">编辑</el-button>
<el-button link type="info" @click="openLog(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"
/>
<!-- 操作记录:新增 / 修改 / 删除留痕 -->
<el-dialog v-model="logVisible" :title="logTitle" width="min(900px, 94vw)" top="6vh" destroy-on-close>
<div class="log-dialog-body">
<OperationLogPanel
v-if="logRow"
:target-type="'product'"
:target-id="logRow.id"
:table-height="360"
hide-header
/>
</div>
<template #footer>
<el-button @click="logVisible = false">关闭</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-dialog-body {
height: 420px;
display: flex;
flex-direction: column;
}
.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>