WEB/src/views/warehouse/WarehouseViewer.vue
2026-03-20 18:18:53 +08:00

585 lines
22 KiB
Vue

<template>
<el-card shadow="never" class="warehouse-plan-card">
<!-- 工具栏 -->
<template #header>
<div class="card-header">
<span class="card-title">{{ props.warehouseData.name }} - 平面图</span>
<div class="toolbar">
<el-button-group>
<el-button size="small" @click="zoomIn">放大</el-button>
<el-button size="small" @click="zoomOut">缩小</el-button>
<el-button size="small" @click="resetZoom">重置</el-button>
<el-button size="small" @click="centerView">居中</el-button>
</el-button-group>
<span class="scale-info ml-3">
缩放: {{ (scale * 100).toFixed(0) }}%
</span>
</div>
</div>
</template>
<!-- 平面图容器 -->
<div class="warehouse-container" ref="containerRef">
<div ref="svgContainerRef" class="svg-container" @wheel="handleWheel" @mousedown="handleMouseDown"
@mousemove="handleMouseMove" @mouseup="handleMouseUp" @mouseleave="handleMouseLeave">
<svg ref="svgRef" :width="svgWidth" :height="svgHeight" :viewBox="viewBox" preserveAspectRatio="xMidYMid meet">
<!-- 仓库边界 -->
<rect :x="0" :y="0" :width="warehouseLength" :height="warehouseWidth" fill="#fafafa" stroke="#000"
stroke-width="20" rx="4" />
<!-- 绘制所有区域 -->
<g v-for="area in (props.warehouseData.children || [])" :key="area.id" v-show="area.type === 'area'" class="area-group">
<!-- 区域背景 -->
<rect :x="area.data.x" :y="area.data.y" :width="area.data.length" :height="area.data.width"
:fill="getAreaColor(area)" :stroke="area.data.isActive ? '#67c23a' : '#909399'" stroke-width="1.5"
:stroke-dasharray:number="area.data.isActive ? null : '4,3'" rx="3" />
<!-- 区域名称 -->
<g>
<rect :x="area.data.x + area.data.length/2 - 40" :y="area.data.y + area.data.width/2 - 15" width="80"
height="30" fill="rgba(255,255,255,0.9)" stroke="#dcdfe6" stroke-width="1" rx="4" />
<text :x="area.data.x + area.data.length/2" :y="area.data.y + area.data.width/2" text-anchor="middle"
dominant-baseline="middle" fill="#303133" font-size="84" font-weight="bold">
{{ area.name }}
</text>
</g>
<!-- 区域内的货架 -->
<g v-for="rack in (area.children || [])" :key="rack.id">
<!-- U型货架 -->
<g v-if="rack.type === 'rack'">
<!-- 货架背景 -->
<rect :x="getRackX(rack, area)" :y="getRackY(rack, area)" :width="getRackDisplayWidth(rack)"
:height="getRackDisplayHeight(rack)" fill="url(#rackPattern)" stroke="#409EFF" stroke-width="1"
:stroke-dasharray:numbers="rack.data.isActive ? null : '3,2'" class="rack-background" />
<!-- U型槽位 -->
<g v-for="col in rack.data.rackColumns" :key="col">
<!-- 单个U型槽 -->
<path :d="getUSlotPath(rack, area, col)" :fill="getSlotColor(col)" stroke="#333" stroke-width="0.5"
:filter="isHoveredSlot(rack.id, col) ? 'url(#highlight)' : ''" @mouseenter="() => hoverSlot(rack.id, col)"
@mouseleave="clearHover" class="u-slot" />
<!-- 槽位编号 -->
<text :x="getSlotTextX(rack, area, col)" :y="getSlotTextY(rack, area, col)" text-anchor="middle"
dominant-baseline="middle" fill="#303133" font-size="50" font-weight="bold">
{{ col }}
</text>
</g>
<!-- 货架名称标签 -->
<g v-if="rack.code">
<rect :x="getRackLabelX(rack, area)" :y="getRackLabelY(rack, area)" width="120" height="64"
fill="rgba(255,255,255,0.9)" stroke="#409EFF" stroke-width="1" rx="3" />
<text :x="getRackLabelX(rack, area) + 60" :y="getRackLabelY(rack, area) + 32" text-anchor="middle"
dominant-baseline="middle" fill="#409EFF" font-size="42" font-weight="500">
{{ rack.code }}
</text>
</g>
</g>
<!-- 拣货起点 -->
<g v-else-if="rack.type === 'pick_start'">
<circle :cx="getPickStartX(rack, area)" :cy="getPickStartY(rack, area)"
:r="Math.max(rack.data.length, rack.data.width) / 2" fill="#67c23a" stroke="#5daf34" stroke-width="2"
class="pick-start" />
<text :x="getPickStartX(rack, area)" :y="getPickStartY(rack, area)" text-anchor="middle"
dominant-baseline="middle" fill="white" font-size="10" font-weight="bold">
起点
</text>
</g>
</g>
</g>
<!-- 图案定义 -->
<defs>
<!-- 货架背景图案 -->
<pattern id="rackPattern" patternUnits="userSpaceOnUse" width="20" height="20">
<rect width="20" height="20" fill="#f0f9ff" />
<path d="M0,0 L20,20 M20,0 L0,20" stroke="#e6f7ff" stroke-width="1" />
</pattern>
<!-- 高亮效果 -->
<filter id="highlight" x="-20%" y="-20%" width="140%" height="140%">
<feGaussianBlur in="SourceAlpha" stdDeviation="2" result="blur" />
<feOffset in="blur" dx="0" dy="0" result="offsetBlur" />
<feFlood flood-color="#409EFF" flood-opacity="0.2" result="offsetColor" />
<feComposite in="offsetColor" in2="offsetBlur" operator="in" result="offsetBlur" />
<feMerge>
<feMergeNode in="offsetBlur" />
<feMergeNode in="SourceGraphic" />
</feMerge>
</filter>
<!-- 朝向指示箭头 -->
<marker id="directionArrow" markerWidth="6" markerHeight="4" refX="5" refY="2" orient="auto">
<polygon points="0,0 6,2 0,4" fill="#e6a23c" />
</marker>
</defs>
</svg>
</div>
</div>
</el-card>
</template>
<script setup lang="ts">
import { ref, reactive, computed, onMounted, watch } from 'vue'
const props = defineProps({ warehouseData: { type: Object, required: true } })
const containerRef = ref<HTMLElement | null>(null)
const svgContainerRef = ref<HTMLElement | null>(null)
const svgRef = ref<SVGElement | null>(null)
const state = reactive({
scale: 0.1,
minScale: 0.01,
maxScale: 1,
svgWidth: 1200,
svgHeight: 800,
viewBoxX: 0,
viewBoxY: 0,
isDragging: false,
dragStart: { x: 0, y: 0, viewBoxX: 0, viewBoxY: 0 },
hoveredElement: null as any,
hoveredSlot: null as any
})
const scale = computed({ get: () => state.scale, set: (v: number) => (state.scale = v) })
const minScale = computed(() => state.minScale)
const maxScale = computed(() => state.maxScale)
const svgWidth = computed({ get: () => state.svgWidth, set: (v: number) => (state.svgWidth = v) })
const svgHeight = computed({ get: () => state.svgHeight, set: (v: number) => (state.svgHeight = v) })
const viewBox = computed(() => {
const width = svgWidth.value / state.scale
const height = svgHeight.value / state.scale
return `${state.viewBoxX} ${state.viewBoxY} ${width} ${height}`
})
const warehouseLength = computed(() => props.warehouseData.data.length)
const warehouseWidth = computed(() => props.warehouseData.data.width)
onMounted(() => {
updateSvgSize()
centerView()
if (svgContainerRef.value) svgContainerRef.value.style.cursor = 'grab'
})
watch(() => props.warehouseData, () => {
updateSvgSize()
centerView()
})
function updateSvgSize() {
if (!containerRef.value) return
const containerRect = containerRef.value.getBoundingClientRect()
const containerWidth = containerRect.width
const containerHeight = containerRect.height
svgWidth.value = Math.max(containerWidth, 100)
svgHeight.value = Math.max(containerHeight, 100)
const size = svgWidth.value / warehouseLength.value > svgHeight.value / warehouseWidth.value ?
parseInt(((svgHeight.value / warehouseWidth.value) * 100).toString()) : parseInt(((svgWidth.value / warehouseLength.value) * 100).toString())
state.scale = size / 100
state.minScale = size / 100
}
function getAreaColor(area: any) {
if (area.data.color && area.data.color !== '#3B82F6') return area.data.color
const colors = [
'rgba(64, 158, 255, 0.1)',
'rgba(103, 194, 58, 0.1)',
'rgba(230, 162, 60, 0.1)',
'rgba(245, 108, 108, 0.1)',
'rgba(144, 147, 153, 0.1)'
]
const index = (area.id % colors.length)
return colors[index]
}
function getSlotColor(column: number) {
return column % 2 === 0 ? '#a5d8ff' : '#74c0fc'
}
function getRackDisplayWidth(rack: any) {
const direction = rack.data.direction
if (direction === 'east' || direction === 'west') return rack.data.width
return rack.data.length
}
function getRackDisplayHeight(rack: any) {
const direction = rack.data.direction
if (direction === 'east' || direction === 'west') return rack.data.length
return rack.data.width
}
function getRackX(rack: any, area: any) {
const baseX = area.data.x + (rack.data.x || 0)
return baseX
}
function getRackY(rack: any, area: any) {
const baseY = area.data.y + (rack.data.y || 0)
return baseY
}
function getUSlotPath(rack: any, area: any, column: number) {
const direction = rack.data.direction
const rackX = getRackX(rack, area)
const rackY = getRackY(rack, area)
const displayWidth = getRackDisplayWidth(rack)
const displayHeight = getRackDisplayHeight(rack)
const columnCount = rack.data.rackColumns
let slotWidth: number, slotHeight: number, slotX: number, slotY: number
if (direction === 'east' || direction === 'west') {
slotWidth = displayWidth
slotHeight = displayHeight / columnCount
slotX = rackX
slotY = rackY + (column - 1) * slotHeight
} else {
slotWidth = displayWidth / columnCount
slotHeight = displayHeight
slotX = rackX + (column - 1) * slotWidth
slotY = rackY
}
const uDepth = (direction === 'east' || direction === 'west') ? slotWidth * 0.6 : slotHeight * 0.6
const uWidth = (direction === 'east' || direction === 'west') ? slotHeight * 0.8 : slotWidth * 0.8
switch (direction) {
case 'north':
return `M ${slotX + slotWidth/2 - uWidth/2},${slotY} L ${slotX + slotWidth/2 - uWidth/2},${slotY + slotHeight - uDepth} Q ${slotX + slotWidth/2 - uWidth/2},${slotY + slotHeight} ${slotX + slotWidth/2},${slotY + slotHeight} Q ${slotX + slotWidth/2 + uWidth/2},${slotY + slotHeight} ${slotX + slotWidth/2 + uWidth/2},${slotY + slotHeight - uDepth} L ${slotX + slotWidth/2 + uWidth/2},${slotY} Z`
case 'south':
return `M ${slotX + slotWidth/2 - uWidth/2},${slotY + slotHeight} L ${slotX + slotWidth/2 - uWidth/2},${slotY + uDepth} Q ${slotX + slotWidth/2 - uWidth/2},${slotY} ${slotX + slotWidth/2},${slotY} Q ${slotX + slotWidth/2 + uWidth/2},${slotY} ${slotX + slotWidth/2 + uWidth/2},${slotY + uDepth} L ${slotX + slotWidth/2 + uWidth/2},${slotY + slotHeight} Z`
case 'west':
return `M ${slotX},${slotY + slotHeight/2 - uWidth/2} L ${slotX + slotWidth - uDepth},${slotY + slotHeight/2 - uWidth/2} Q ${slotX + slotWidth},${slotY + slotHeight/2 - uWidth/2} ${slotX + slotWidth},${slotY + slotHeight/2} Q ${slotX + slotWidth},${slotY + slotHeight/2 + uWidth/2} ${slotX + slotWidth - uDepth},${slotY + slotHeight/2 + uWidth/2} L ${slotX},${slotY + slotHeight/2 + uWidth/2} Z`
case 'east':
return `M ${slotX + slotWidth},${slotY + slotHeight/2 - uWidth/2} L ${slotX + uDepth},${slotY + slotHeight/2 - uWidth/2} Q ${slotX},${slotY + slotHeight/2 - uWidth/2} ${slotX},${slotY + slotHeight/2} Q ${slotX},${slotY + slotHeight/2 + uWidth/2} ${slotX + uDepth},${slotY + slotHeight/2 + uWidth/2} L ${slotX + slotWidth},${slotY + slotHeight/2 + uWidth/2} Z`
default:
return ''
}
}
function getSlotTextX(rack: any, area: any, column: number) {
const direction = rack.data.direction
const rackX = getRackX(rack, area)
const displayWidth = getRackDisplayWidth(rack)
const columnCount = rack.data.rackColumns
if (direction === 'east' || direction === 'west') {
const slotWidth = displayWidth
return rackX + slotWidth / 2
} else {
const slotWidth = displayWidth / columnCount
return rackX + (column - 0.5) * slotWidth
}
}
function getSlotTextY(rack: any, area: any, column: number) {
const direction = rack.data.direction
const rackY = getRackY(rack, area)
const displayHeight = getRackDisplayHeight(rack)
const columnCount = rack.data.rackColumns
if (direction === 'east' || direction === 'west') {
const slotHeight = displayHeight / columnCount
return rackY + (column - 0.5) * slotHeight
} else {
const slotHeight = displayHeight
return rackY + slotHeight / 2
}
}
function getRackLabelX(rack: any, area: any) {
const direction = rack.data.direction
const rackX = getRackX(rack, area)
const displayWidth = getRackDisplayWidth(rack)
if (direction === 'east' || direction === 'west') return rackX + displayWidth + 5
return rackX + displayWidth / 2 - 40
}
function getRackLabelY(rack: any, area: any) {
const direction = rack.data.direction
const rackY = getRackY(rack, area)
const displayHeight = getRackDisplayHeight(rack)
if (direction === 'north') return rackY - 30
if (direction === 'south') return rackY + displayHeight + 5
return rackY + displayHeight / 2 - 12
}
function getPickStartX(pickStart: any, area: any) {
return area.data.x + (pickStart.data.x || 0) + (pickStart.data.length || 0) / 2
}
function getPickStartY(pickStart: any, area: any) {
return area.data.y + (pickStart.data.y || 0) + (pickStart.data.width || 0) / 2
}
function hoverSlot(rackId: number, column: number) {
const rack = findRackById(rackId)
if (rack) {
state.hoveredElement = rack
state.hoveredSlot = { rackId, column }
}
}
function isHoveredSlot(rackId: number, column: number) {
return state.hoveredSlot && state.hoveredSlot.rackId === rackId && state.hoveredSlot.column === column
}
function clearHover() { state.hoveredSlot = null }
function findRackById(rackId: number) {
for (const area of (props.warehouseData.children || [])) {
for (const child of (area.children || [])) {
if (child.id === rackId) return child
}
}
return null
}
function getDirectionText(direction: string) {
const map: any = { north: '北', south: '南', east: '东', west: '西' }
return map[direction] || direction
}
function getElementTypeText(type: string) {
const map: any = { warehouse: '仓库', area: '区域', rack: '货架', pick_start: '拣货点' }
return map[type] || type
}
function getElementTagType(element: any) {
const map: any = { warehouse: 'primary', area: 'success', rack: 'warning', pick_start: 'info' }
return map[element.type] || 'info'
}
function zoomIn() {
if (state.scale < state.maxScale) state.scale = Math.min(state.maxScale, state.scale * 1.2)
centerView()
}
function zoomOut() {
if (state.scale > state.minScale) state.scale = Math.max(state.minScale, state.scale / 1.2)
centerView()
}
function resetZoom() { updateSvgSize(); centerView() }
function centerView() {
const width = svgWidth.value / state.scale
const height = svgHeight.value / state.scale
state.viewBoxX = -(width - warehouseLength.value) / 2
state.viewBoxY = -(height - warehouseWidth.value) / 2
}
function handleWheel(event: WheelEvent) {
event.preventDefault()
const delta = event.deltaY > 0 ? -1 : 1
if (event.ctrlKey || (event as any).metaKey) {
const zoomFactor = 1.1
const newScale = delta > 0 ? state.scale * zoomFactor : state.scale / zoomFactor
state.scale = Math.max(state.minScale, Math.min(state.maxScale, newScale))
}
}
function handleMouseDown(event: MouseEvent) {
if (event.button !== 0) return
state.isDragging = true
state.dragStart = { x: event.clientX, y: event.clientY, viewBoxX: state.viewBoxX, viewBoxY: state.viewBoxY }
if (svgContainerRef.value) svgContainerRef.value.style.cursor = 'grabbing'
}
function handleMouseMove(event: MouseEvent) {
if (!state.isDragging) return
const dx = event.clientX - state.dragStart.x
const dy = event.clientY - state.dragStart.y
state.viewBoxX = state.dragStart.viewBoxX - dx / state.scale
state.viewBoxY = state.dragStart.viewBoxY - dy / state.scale
}
function handleMouseUp() {
state.isDragging = false
if (svgContainerRef.value) svgContainerRef.value.style.cursor = 'grab'
}
function handleMouseLeave() { state.isDragging = false }
defineExpose({ updateSvgSize, centerView })
</script>
<style scoped>
.warehouse-plan-card {
height: 100%;
display: flex;
flex-direction: column;
.el-card__body {
flex: 1;
height: 100px;
}
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
}
.card-title {
font-size: 16px;
font-weight: 600;
color: var(--el-text-color-primary);
}
.toolbar {
display: flex;
align-items: center;
}
.scale-info {
color: var(--el-text-color-secondary);
font-size: 14px;
margin-left: 16px;
}
.warehouse-container {
flex: 1;
display: flex;
position: relative;
min-height: 600px;
height: 100%;
background: var(--el-fill-color-light);
border-radius: var(--el-border-radius-base);
overflow: hidden;
}
.svg-container {
flex: 1;
overflow: hidden;
position: relative;
}
svg {
display: block;
background: white;
}
.u-slot {
cursor: pointer;
transition: filter 0.2s;
}
.u-slot:hover {
filter: url(#highlight);
}
.rack-background {
pointer-events: none;
}
.pick-start {
cursor: pointer;
transition: transform 0.2s;
}
.pick-start:hover {
transform: scale(1.1);
}
.info-panel {
position: absolute;
top: 16px;
right: 16px;
width: 280px;
background: white;
border-radius: var(--el-border-radius-base);
box-shadow: var(--el-box-shadow-light);
border: 1px solid var(--el-border-color-lighter);
z-index: 10;
}
.info-header {
padding: 16px;
border-bottom: 1px solid var(--el-border-color-lighter);
display: flex;
justify-content: space-between;
align-items: center;
}
.info-header h4 {
margin: 0;
font-size: 14px;
font-weight: 600;
color: var(--el-text-color-primary);
}
.info-content {
padding: 16px;
}
.info-section {
margin-bottom: 16px;
}
.info-section:last-child {
margin-bottom: 0;
}
.info-row {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 8px;
font-size: 13px;
}
.info-row:last-child {
margin-bottom: 0;
}
.info-row .label {
color: var(--el-text-color-secondary);
}
.info-row .value {
color: var(--el-text-color-primary);
font-weight: 500;
text-align: right;
}
.grid-layer text {
user-select: none;
pointer-events: none;
}
/* 响应式调整 */
@media (max-width: 992px) {
.warehouse-container {
flex-direction: column;
}
.info-panel {
position: static;
width: 100%;
margin-top: 16px;
}
.svg-container {
min-height: 500px;
}
}
@media (max-width: 768px) {
.card-header {
flex-direction: column;
align-items: flex-start;
gap: 12px;
}
.toolbar {
flex-wrap: wrap;
gap: 8px;
}
.scale-info {
margin-left: 0;
flex-basis: 100%;
}
}
</style>