liufumingren 266c01538e feat(frontend): 订单与店铺商品交互完善
- 待审核订单增加「含未绑定」开关(默认只显示已绑定),列表与导出条件保持一致
- 店铺商品增加「只看有订单未绑定」筛选与「待处理订单」列;绑定弹窗改为左侧页签 + 右侧可搜索分页列表
- 订单切换仓库改为弹窗选择,新增通用 RecordPickerDialog 组件
- 订单与店铺商品表格改为表体内部滚动,筛选栏与分页固定
- 手工建单必须选择非平台店铺;平台订单编辑时店铺不可修改

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

769 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, onBeforeUnmount, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { QuestionFilled } from '@element-plus/icons-vue'
import {
getShopProducts,
bindShopProduct,
BIND_TYPES,
type ShopProductItem,
} from '@/api/shopProduct'
import { pullExternalPrintShopProducts } from '@/api/externalPrintAccount'
import { getShops, type ShopItem } from '@/api/shop'
import { getProducts, type ProductItem } from '@/api/product'
import { getComboProducts, type ComboProductItem } from '@/api/comboProduct'
import DataTable from '@/components/DataTable.vue'
import ExportButton from '@/components/ExportButton.vue'
import ListPager from '@/components/ListPager.vue'
import { DEFAULT_PAGE_SIZE, useListPage } from '@/composables/useListPage'
import VirtualSelect from '@/components/VirtualSelect.vue'
const BIND_TAGS: Record<number, string> = { 0: 'info', 1: 'success', 2: 'warning' }
const BIND_LABELS: Record<number, string> = { 0: '未绑定', 1: '系统商品', 2: '组合商品' }
const shops = ref<ShopItem[]>([])
const query = reactive({
page: 1,
pageSize: DEFAULT_PAGE_SIZE,
shop_id: '' as string | number,
bind_type: '' as string | number,
status: '' as string | number,
keyword: '',
/** 快速筛选:只看有订单但未绑定系统商品的店铺商品 */
has_unbound_order: false,
})
const { loading, list, total, elapsed, fetchList, search, handlePageChange, handleSizeChange } = useListPage({
query,
fetcher: () => {
const params: Record<string, unknown> = {
page: query.page,
pageSize: query.pageSize,
shop_id: query.shop_id,
bind_type: query.bind_type,
status: query.status,
keyword: query.keyword,
}
if (!params.keyword) delete params.keyword
if (params.shop_id === '') delete params.shop_id
if (params.bind_type === '') delete params.bind_type
if (params.status === '') delete params.status
// 只看有订单未绑定时,绑定状态必然是「未绑定」,避免两个条件互相打架
if (query.has_unbound_order) {
params.has_unbound_order = 1
params.bind_type = 0
}
return getShopProducts(params)
},
})
async function fetchShops() {
try {
const res = await getShops({ page: 1, pageSize: 200 })
shops.value = res.list
} catch {
shops.value = []
}
}
function reset() {
query.shop_id = ''
query.bind_type = ''
query.status = ''
query.keyword = ''
query.has_unbound_order = false
search()
}
/** 切换「有订单未绑定」快速筛选后立即查询 */
function toggleUnboundOrderFilter() {
query.page = 1
search()
}
/** 导出筛选条件(与列表筛选同源) */
const exportFilters = computed(() => ({
shop_id: query.shop_id,
bind_type: query.has_unbound_order ? 0 : query.bind_type,
status: query.status,
keyword: query.keyword.trim(),
has_unbound_order: query.has_unbound_order ? 1 : '',
}))
/* -------------------- 绑定商品 -------------------- */
const bindVisible = ref(false)
const bindSubmitting = ref(false)
const bindId = ref<number | null>(null)
const bindRow = ref<ShopProductItem | null>(null)
const bindTab = ref<'product' | 'combo'>('product')
/** 弹窗内容区高度:按视口计算,保证表格内部滚动、底部按钮始终可见 */
const bindBodyHeight = ref(420)
function updateBindBodyHeight() {
bindBodyHeight.value = Math.min(560, Math.max(220, Math.round(window.innerHeight * 0.6)))
}
/** 正在绑定的行(`p<id>` / `c<id>`),用于展示行内 loading */
const bindingKey = ref('')
const productQuery = reactive({ page: 1, pageSize: DEFAULT_PAGE_SIZE, keyword: '', status: '' as string | number })
const productList = ref<ProductItem[]>([])
const productTotal = ref(0)
const productLoading = ref(false)
const productElapsed = ref<number | null>(null)
const comboQuery = reactive({ page: 1, pageSize: DEFAULT_PAGE_SIZE, keyword: '', status: '' as string | number })
const comboList = ref<ComboProductItem[]>([])
const comboTotal = ref(0)
const comboLoading = ref(false)
const comboElapsed = ref<number | null>(null)
/** 当前店铺商品的绑定信息 */
const currentBindLabel = computed(() => {
const row = bindRow.value
if (!row) return '-'
if (row.bind_type === BIND_TYPES.product) return `系统商品:${row.product?.item_name || `#${row.product_id}`}`
if (row.bind_type === BIND_TYPES.combo) return `组合商品:${row.comboProduct?.name || `#${row.combo_product_id}`}`
return '未绑定'
})
async function loadProducts() {
productLoading.value = true
const started = performance.now()
try {
const params: Record<string, unknown> = { page: productQuery.page, pageSize: productQuery.pageSize }
const keyword = productQuery.keyword.trim()
if (keyword) params.keyword = keyword
if (productQuery.status !== '') params.status = productQuery.status
const res = await getProducts(params)
productList.value = res.list
productTotal.value = res.total
} catch {
productList.value = []
productTotal.value = 0
} finally {
productElapsed.value = Math.round(performance.now() - started)
productLoading.value = false
}
}
async function loadCombos() {
comboLoading.value = true
const started = performance.now()
try {
const params: Record<string, unknown> = { page: comboQuery.page, pageSize: comboQuery.pageSize }
const keyword = comboQuery.keyword.trim()
if (keyword) params.keyword = keyword
if (comboQuery.status !== '') params.status = comboQuery.status
const res = await getComboProducts(params)
comboList.value = res.list
comboTotal.value = res.total
} catch {
comboList.value = []
comboTotal.value = 0
} finally {
comboElapsed.value = Math.round(performance.now() - started)
comboLoading.value = false
}
}
function searchProducts() {
productQuery.page = 1
loadProducts()
}
function resetProductQuery() {
productQuery.keyword = ''
productQuery.status = ''
productQuery.page = 1
loadProducts()
}
function searchCombos() {
comboQuery.page = 1
loadCombos()
}
function resetComboQuery() {
comboQuery.keyword = ''
comboQuery.status = ''
comboQuery.page = 1
loadCombos()
}
function onProductPageChange(page: number) {
productQuery.page = page
loadProducts()
}
function onProductSizeChange(size: number) {
productQuery.pageSize = size
productQuery.page = 1
loadProducts()
}
function onComboPageChange(page: number) {
comboQuery.page = page
loadCombos()
}
function onComboSizeChange(size: number) {
comboQuery.pageSize = size
comboQuery.page = 1
loadCombos()
}
function openBind(row: ShopProductItem) {
bindId.value = row.id
bindRow.value = row
bindTab.value = row.bind_type === BIND_TYPES.combo ? 'combo' : 'product'
productQuery.keyword = ''
productQuery.status = ''
productQuery.page = 1
comboQuery.keyword = ''
comboQuery.status = ''
comboQuery.page = 1
bindVisible.value = true
loadProducts()
loadCombos()
}
/** 当前行是否已绑定该商品/组合商品 */
function isBoundProduct(row: ProductItem) {
return bindRow.value?.bind_type === BIND_TYPES.product && Number(bindRow.value.product_id) === Number(row.id)
}
function isBoundCombo(row: ComboProductItem) {
return bindRow.value?.bind_type === BIND_TYPES.combo && Number(bindRow.value.combo_product_id) === Number(row.id)
}
/** 直接把店铺商品绑定到选中的系统商品 / 组合商品 */
async function bindTo(targetType: number, targetId: number) {
if (bindId.value == null || bindingKey.value) return
bindingKey.value = `${targetType === BIND_TYPES.combo ? 'c' : 'p'}${targetId}`
bindSubmitting.value = true
try {
await bindShopProduct(bindId.value, {
bind_type: targetType,
product_id: targetType === BIND_TYPES.product ? targetId : null,
combo_product_id: targetType === BIND_TYPES.combo ? targetId : null,
})
ElMessage.success('绑定成功')
bindVisible.value = false
await fetchList()
} catch {
// 拦截器已提示
} finally {
bindingKey.value = ''
bindSubmitting.value = false
}
}
/** 解除当前店铺商品的绑定 */
async function unbind() {
if (bindId.value == null) return
try {
await ElMessageBox.confirm('确认解除当前绑定?', '解绑确认', {
type: 'warning',
confirmButtonText: '解绑',
cancelButtonText: '取消',
})
} catch {
return
}
bindSubmitting.value = true
try {
await bindShopProduct(bindId.value, { bind_type: BIND_TYPES.none, product_id: null, combo_product_id: null })
ElMessage.success('已解绑')
bindVisible.value = false
await fetchList()
} catch {
// 拦截器已提示
} finally {
bindSubmitting.value = false
}
}
/** 已绑定的商品名称 */
function boundName(row: ShopProductItem) {
if (row.bind_type === BIND_TYPES.product) return row.product?.item_name || `商品#${row.product_id}`
if (row.bind_type === BIND_TYPES.combo) return row.comboProduct?.name || `组合#${row.combo_product_id}`
return '-'
}
/* -------------------- 按平台商品ID补拉 -------------------- */
const pullVisible = ref(false)
const pullSubmitting = ref(false)
const pullForm = reactive({
shop_id: '' as string | number,
outer_item_id: '',
outer_sku_id: '',
})
/** 只允许选择外部打单平台商家店铺(其它类型没有平台商品可拉) */
const externalShops = computed(() => shops.value.filter((shop) => Number(shop.type) === 2))
function openPull() {
pullForm.shop_id = query.shop_id !== '' ? query.shop_id : (externalShops.value[0]?.id ?? '')
pullForm.outer_item_id = ''
pullForm.outer_sku_id = ''
pullVisible.value = true
}
async function submitPull() {
const shop = shops.value.find((item) => Number(item.id) === Number(pullForm.shop_id))
const outerItemId = pullForm.outer_item_id.trim()
if (!shop) {
ElMessage.warning('请选择外部打单平台店铺')
return
}
if (!shop.external_print_account_id) {
ElMessage.warning('该店铺未关联外部打单账号,无法拉取商品')
return
}
if (!outerItemId) {
ElMessage.warning('请输入平台商品ID')
return
}
pullSubmitting.value = true
try {
const res = await pullExternalPrintShopProducts(shop.external_print_account_id, shop.id, [
{ outer_item_id: outerItemId, outer_sku_id: pullForm.outer_sku_id.trim() },
])
if (!res.matched) {
const reason = res.missing[0]?.reason || '平台未查询到该商品'
ElMessage.warning(`未拉取到商品:${reason}`)
return
}
ElMessage.success(`拉取成功:新增 ${res.created},更新 ${res.updated},来源 ${res.hosts.join('、')}`)
pullVisible.value = false
await fetchList()
} catch {
// 拦截器已提示
} finally {
pullSubmitting.value = false
}
}
onMounted(async () => {
updateBindBodyHeight()
window.addEventListener('resize', updateBindBodyHeight)
await fetchShops()
await fetchList()
})
onBeforeUnmount(() => {
window.removeEventListener('resize', updateBindBodyHeight)
})
</script>
<template>
<el-card class="shop-product-card">
<template #header>
<div class="page-header">
<span>店铺商品</span>
<div class="header-actions">
<el-button @click="openPull">按商品ID拉取</el-button>
<ExportButton type="shop_product" :filters="exportFilters" />
</div>
</div>
</template>
<el-form :inline="true" class="search-bar">
<el-form-item label="店铺">
<VirtualSelect
v-model="query.shop_id"
:options="shops"
label-key="name"
value-key="id"
placeholder="全部"
clearable
style="width: 160px"
/>
</el-form-item>
<el-form-item label="关键词">
<el-input
v-model="query.keyword"
placeholder="标题 / 商家编码 / 平台商品ID"
clearable
@keyup.enter="search"
/>
</el-form-item>
<el-form-item label="绑定状态">
<el-select
v-model="query.bind_type"
placeholder="全部"
clearable
:disabled="query.has_unbound_order"
style="width: 130px"
>
<el-option label="未绑定" :value="0" />
<el-option label="系统商品" :value="1" />
<el-option label="组合商品" :value="2" />
</el-select>
</el-form-item>
<el-form-item label="状态">
<el-select v-model="query.status" placeholder="全部" clearable style="width: 120px">
<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-item>
<el-checkbox
v-model="query.has_unbound_order"
class="unbound-filter"
@change="toggleUnboundOrderFilter"
>
只看有订单未绑定
</el-checkbox>
<el-tooltip
content="商品的订单里还有未绑定系统商品的明细,绑定后订单明细会自动关联"
placement="top"
>
<el-icon class="unbound-hint"><QuestionFilled /></el-icon>
</el-tooltip>
</el-form-item>
</el-form>
<div class="list-table-wrap">
<DataTable :loading="loading" :data="list" height="100%">
<el-table-column label="店铺" width="140">
<template #default="{ row }">{{ row.shop?.name || '-' }}</template>
</el-table-column>
<el-table-column label="平台类型" width="110">
<template #default="{ row }">{{ row.platform_type || '-' }}</template>
</el-table-column>
<el-table-column prop="outer_item_id" label="平台商品ID" width="140" />
<el-table-column prop="outer_sku_id" label="规格ID" width="120">
<template #default="{ row }">{{ row.outer_sku_id || '-' }}</template>
</el-table-column>
<el-table-column prop="title" label="商品标题" min-width="180" show-overflow-tooltip />
<el-table-column prop="outer_code" label="商家编码" width="120">
<template #default="{ row }">{{ row.outer_code || '-' }}</template>
</el-table-column>
<el-table-column label="售价" width="100" align="right">
<template #default="{ row }">{{ row.price ?? '-' }}</template>
</el-table-column>
<el-table-column prop="stock" label="库存" width="90" align="right" />
<el-table-column label="绑定" min-width="150">
<template #default="{ row }">
<el-tag :type="BIND_TAGS[row.bind_type]" size="small">
{{ BIND_LABELS[row.bind_type] }}
</el-tag>
<span v-if="row.bind_type !== 0" class="bound-name">{{ boundName(row) }}</span>
</template>
</el-table-column>
<el-table-column label="待处理订单" width="110" align="center">
<template #default="{ row }">
<el-tag v-if="row.unbound_order_count" type="danger" size="small">
{{ row.unbound_order_count }} 单待绑定
</el-tag>
<span v-else class="muted">-</span>
</template>
</el-table-column>
<el-table-column label="状态" width="90">
<template #default="{ row }">
<el-tag :type="row.status === 1 ? 'success' : 'info'" size="small">
{{ row.status === 1 ? '在售' : '下架' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="操作" width="100" fixed="right">
<template #default="{ row }">
<el-button link type="warning" @click="openBind(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="pullVisible" title="按平台商品ID拉取" width="520px">
<el-alert
type="info"
:closable="false"
show-icon
title="用于订单明细匹配不到店铺商品时补拉:填平台商品ID(可选规格ID),后端按店铺分区及其它域名查询后写入店铺商品"
class="pull-tip"
/>
<el-form :model="pullForm" label-width="100px">
<el-form-item label="店铺" required>
<VirtualSelect
v-model="pullForm.shop_id"
:options="externalShops"
label-key="name"
value-key="id"
placeholder="请选择外部打单平台店铺"
style="width: 100%"
/>
</el-form-item>
<el-form-item label="平台商品ID" required>
<el-input v-model="pullForm.outer_item_id" placeholder="订单明细的 outer_item_id" clearable />
</el-form-item>
<el-form-item label="平台规格ID">
<el-input v-model="pullForm.outer_sku_id" placeholder="留空则拉取该商品的全部规格" clearable />
</el-form-item>
</el-form>
<template #footer>
<el-button @click="pullVisible = false">取消</el-button>
<el-button type="primary" :loading="pullSubmitting" @click="submitPull">拉取</el-button>
</template>
</el-dialog>
<el-dialog
v-model="bindVisible"
title="绑定商品"
width="min(1100px, 94vw)"
top="5vh"
class="bind-dialog"
destroy-on-close
>
<div class="bind-layout" :style="{ height: `${bindBodyHeight}px` }">
<el-tabs v-model="bindTab" tab-position="left" class="bind-tabs">
<el-tab-pane label="系统商品" name="product">
<div class="bind-pane">
<div class="bind-filters">
<el-input
v-model="productQuery.keyword"
placeholder="商品编码 / 名称"
clearable
style="width: 240px"
@keyup.enter="searchProducts"
/>
<el-select v-model="productQuery.status" placeholder="状态" clearable style="width: 120px">
<el-option label="上架" :value="1" />
<el-option label="下架" :value="0" />
</el-select>
<el-button type="primary" @click="searchProducts">查询</el-button>
<el-button @click="resetProductQuery">重置</el-button>
</div>
<div class="bind-table">
<DataTable :loading="productLoading" :data="productList" height="100%">
<el-table-column prop="item_code" label="商品编码" width="150" />
<el-table-column prop="item_name" label="商品名称" min-width="200" show-overflow-tooltip />
<el-table-column prop="sku_property" label="规格" width="140">
<template #default="{ row }">{{ row.sku_property || '-' }}</template>
</el-table-column>
<el-table-column prop="stock_unit" label="单位" width="80">
<template #default="{ row }">{{ row.stock_unit || '-' }}</template>
</el-table-column>
<el-table-column label="零售价" width="100" align="right">
<template #default="{ row }">{{ row.retail_price ?? '-' }}</template>
</el-table-column>
<el-table-column label="状态" width="80">
<template #default="{ row }">
<el-tag :type="row.status === 1 ? 'success' : 'info'" size="small">
{{ row.status === 1 ? '上架' : '下架' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="操作" width="100" fixed="right">
<template #default="{ row }">
<el-button
link
type="primary"
:disabled="isBoundProduct(row) || !!bindingKey"
:loading="bindingKey === `p${row.id}`"
@click="bindTo(BIND_TYPES.product, row.id)"
>
{{ isBoundProduct(row) ? '已绑定' : '绑定' }}
</el-button>
</template>
</el-table-column>
</DataTable>
</div>
<ListPager
:total="productTotal"
:elapsed="productElapsed"
:page="productQuery.page"
:page-size="productQuery.pageSize"
@page-change="onProductPageChange"
@size-change="onProductSizeChange"
/>
</div>
</el-tab-pane>
<el-tab-pane label="组合商品" name="combo">
<div class="bind-pane">
<div class="bind-filters">
<el-input
v-model="comboQuery.keyword"
placeholder="组合编码 / 名称"
clearable
style="width: 240px"
@keyup.enter="searchCombos"
/>
<el-select v-model="comboQuery.status" placeholder="状态" clearable style="width: 120px">
<el-option label="上架" :value="1" />
<el-option label="下架" :value="0" />
</el-select>
<el-button type="primary" @click="searchCombos">查询</el-button>
<el-button @click="resetComboQuery">重置</el-button>
</div>
<div class="bind-table">
<DataTable :loading="comboLoading" :data="comboList" height="100%">
<el-table-column prop="code" label="组合编码" width="160" />
<el-table-column prop="name" label="组合名称" min-width="220" show-overflow-tooltip />
<el-table-column label="价格" width="110" align="right">
<template #default="{ row }">{{ row.price }}</template>
</el-table-column>
<el-table-column label="状态" width="80">
<template #default="{ row }">
<el-tag :type="row.status === 1 ? 'success' : 'info'" size="small">
{{ row.status === 1 ? '上架' : '下架' }}
</el-tag>
</template>
</el-table-column>
<el-table-column label="操作" width="100" fixed="right">
<template #default="{ row }">
<el-button
link
type="primary"
:disabled="isBoundCombo(row) || !!bindingKey"
:loading="bindingKey === `c${row.id}`"
@click="bindTo(BIND_TYPES.combo, row.id)"
>
{{ isBoundCombo(row) ? '已绑定' : '绑定' }}
</el-button>
</template>
</el-table-column>
</DataTable>
</div>
<ListPager
:total="comboTotal"
:elapsed="comboElapsed"
:page="comboQuery.page"
:page-size="comboQuery.pageSize"
@page-change="onComboPageChange"
@size-change="onComboSizeChange"
/>
</div>
</el-tab-pane>
</el-tabs>
</div>
<template #footer>
<div class="bind-footer">
<span class="bind-current">当前绑定:{{ currentBindLabel }}</span>
<div>
<el-button
v-if="bindRow && bindRow.bind_type !== BIND_TYPES.none"
:loading="bindSubmitting"
@click="unbind"
>
解绑
</el-button>
<el-button @click="bindVisible = false">关闭</el-button>
</div>
</div>
</template>
</el-dialog>
</el-card>
</template>
<style scoped>
/* 整页撑满可视区域:头部与筛选固定,只有表格主体滚动,分页固定在底部 */
.shop-product-card {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
}
.shop-product-card :deep(.el-card__body) {
flex: 1;
min-height: 0;
display: flex;
flex-direction: column;
overflow: hidden;
padding: 10px 12px;
}
.page-header {
display: flex;
align-items: center;
justify-content: space-between;
font-weight: 600;
}
.search-bar {
flex: 0 0 auto;
margin-bottom: 12px;
}
.list-table-wrap {
flex: 1 1 auto;
min-height: 0;
}
.bound-name {
margin-left: 8px;
}
.unbound-filter {
font-size: 12px;
}
.unbound-hint {
margin-left: 4px;
font-size: 13px;
color: var(--el-text-color-secondary);
cursor: help;
}
.muted {
color: var(--el-text-color-secondary);
}
.pull-tip {
margin-bottom: 16px;
}
/* 绑定弹窗:左侧页签 + 右侧列表(表格内部滚动,分页固定在底部) */
.bind-layout {
/* 高度由脚本按视口计算(bindBodyHeight),避免小屏溢出 */
overflow: hidden;
}
.bind-tabs {
height: 100%;
}
.bind-tabs :deep(.el-tabs__content) {
height: 100%;
overflow: hidden;
}
.bind-tabs :deep(.el-tab-pane) {
height: 100%;
}
.bind-pane {
display: flex;
flex-direction: column;
height: 100%;
padding-left: 12px;
}
.bind-filters {
flex: 0 0 auto;
display: flex;
gap: 8px;
margin-bottom: 10px;
padding-right: 12px;
}
.bind-table {
flex: 1 1 auto;
min-height: 0;
}
.bind-footer {
display: flex;
align-items: center;
justify-content: space-between;
}
.bind-current {
font-size: 12px;
color: var(--el-text-color-secondary);
}
</style>