refactor(components): 提取 WMS 表格为独立组件并优化逻辑

将 EnamellingMachineStatus.vue 中的 WMS 表格逻辑提取为独立组件 WmsTable
优化数据加载和修改逻辑,减少重复代码
添加组件 props 和 emits 实现更好的封装和复用
master
huangjinysf 2 months ago
parent 826d3dec99
commit 78d422f3f7

@ -0,0 +1,233 @@
<template>
<div>
<div v-if="loading" v-loading="true" :style="{ height: loadingHeight + 'px', marginBottom: '20px' }"></div>
<el-table
v-else-if="data.length > 0"
:data="data"
border
style="width: 100%; margin-bottom: 20px;"
:header-cell-style="{ backgroundColor: '#f5f7fa', color: '#606266' }"
:max-height="maxHeight"
>
<el-table-column prop="model" label="型号" width="120" />
<el-table-column prop="specification" label="规格" width="120" />
<el-table-column prop="wire_disc" label="线盘" width="120" />
<el-table-column prop="total_number" label="总箱数" width="100" />
<el-table-column label="最小箱数" width="120">
<template #default="scope">
<el-input
v-model="scope.row.warning_number_min"
type="number"
size="small"
@input="onValueChange(scope.$index)"
/>
</template>
</el-table-column>
<el-table-column label="最大箱数" width="120">
<template #default="scope">
<el-input
v-model="scope.row.warning_number_max"
type="number"
size="small"
@input="onValueChange(scope.$index)"
/>
</template>
</el-table-column>
<el-table-column prop="total_gross_weight" label="总毛重(kg)" width="120" />
<el-table-column v-if="showAction" label="操作" width="100" fixed="right">
<template #default="scope">
<el-button
type="success"
size="small"
:disabled="!modifiedFlags[scope.$index]"
@click="handleSubmit(scope.$index)"
>
提交修改
</el-button>
</template>
</el-table-column>
</el-table>
<div v-else class="no-data" :style="{ height: loadingHeight + 'px' }">
暂无数据
</div>
</div>
</template>
<script setup>
import { ref, onMounted, watch } from 'vue'
import { API_CONFIG } from "@/config/api"
const props = defineProps({
model: {
type: String,
default: ''
},
specification: {
type: String,
default: ''
},
loadingHeight: {
type: Number,
default: 200
},
maxHeight: {
type: Number,
default: 300
},
showAction: {
type: Boolean,
default: true
}
})
const emit = defineEmits(['data-loaded', 'modified'])
const loading = ref(false)
const data = ref([])
const modifiedFlags = ref({})
const originalData = ref({})
const buildKey = (item) => {
return `${item.model}_${item.specification}_${item.wire_disc || 'null'}`
}
const recordOriginalData = () => {
originalData.value = {}
modifiedFlags.value = {}
data.value.forEach((item, index) => {
const key = buildKey(item)
originalData.value[key] = {
warning_number_min: item.warning_number_min,
warning_number_max: item.warning_number_max
}
modifiedFlags.value[index] = false
})
}
const fetchData = async () => {
if (!props.model || !props.specification) {
data.value = []
return
}
loading.value = true
const apiUrl = `${API_CONFIG.BASE_URL}/api/wms/records/model-spec?model=${encodeURIComponent(props.model)}&specification=${encodeURIComponent(props.specification)}`
try {
const response = await fetch(apiUrl)
const result = await response.json()
if (result.code === 200 && result.data && result.data.records) {
data.value = result.data.records
recordOriginalData()
emit('data-loaded', data.value)
} else {
data.value = []
emit('data-loaded', [])
}
} catch (error) {
console.error('WMS数据API调用失败:', error)
data.value = []
emit('data-loaded', [])
} finally {
loading.value = false
}
}
onMounted(() => {
if (props.model && props.specification) {
fetchData()
}
})
const onValueChange = (index) => {
const item = data.value[index]
if (!item) return
const key = buildKey(item)
const original = originalData.value[key]
if (original) {
const isModified = item.warning_number_min !== original.warning_number_min ||
item.warning_number_max !== original.warning_number_max
modifiedFlags.value[index] = isModified
emit('modified', { index, modified: isModified, item })
}
}
const handleSubmit = async (index) => {
const item = data.value[index]
if (!item) return
const params = new URLSearchParams({
model: item.model,
specification: item.specification,
warning_number_min: item.warning_number_min || 0,
warning_number_max: item.warning_number_max || 300
})
if (item.wire_disc) {
params.append('wire_disc', item.wire_disc)
}
const apiUrl = `${API_CONFIG.BASE_URL}/api/wms/warning/upsert?${params.toString()}`
try {
const response = await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
})
const result = await response.json()
if (result.code === 200) {
const key = buildKey(item)
originalData.value[key] = {
warning_number_min: item.warning_number_min,
warning_number_max: item.warning_number_max
}
modifiedFlags.value[index] = false
console.log('WMS修改成功')
} else {
console.error('WMS修改失败:', result.message)
item.warning_number_min = originalData.value[key]?.warning_number_min
item.warning_number_max = originalData.value[key]?.warning_number_max
}
} catch (error) {
console.error('WMS修改API调用失败:', error)
}
}
const isModified = (index) => {
return modifiedFlags.value[index] || false
}
watch(
() => [props.model, props.specification],
([newModel, newSpecification]) => {
if (newModel && newSpecification) {
fetchData()
}
},
{ immediate: true }
)
defineExpose({
fetchData,
isModified,
data
})
</script>
<style scoped>
.no-data {
display: flex;
align-items: center;
justify-content: center;
color: #909399;
font-size: 14px;
}
</style>

@ -185,54 +185,14 @@
destroy-on-close
@close="closeChart"
>
<div v-if="wmsDataLoading" v-loading="true" style="height: 200px; margin-bottom: 20px;"></div>
<el-table
v-else-if="wmsData.length > 0"
:data="wmsData"
border
style="width: 100%; margin-bottom: 20px;"
:header-cell-style="{ backgroundColor: '#f5f7fa', color: '#606266' }"
max-height="300"
>
<el-table-column prop="model" label="型号" width="120" />
<el-table-column prop="specification" label="规格" width="120" />
<el-table-column prop="wire_disc" label="线盘" width="120" />
<!-- <el-table-column prop="date" label="日期" width="120" /> -->
<el-table-column prop="total_number" label="总箱数" width="100" />
<el-table-column label="最小箱数" width="120">
<template #default="scope">
<el-input
v-model="scope.row.warning_number_min"
type="number"
size="small"
@input="onWmsValueChange(scope.$index)"
/>
</template>
</el-table-column>
<el-table-column label="最大箱数" width="120">
<template #default="scope">
<el-input
v-model="scope.row.warning_number_max"
type="number"
size="small"
@input="onWmsValueChange(scope.$index)"
/>
</template>
</el-table-column>
<el-table-column prop="total_gross_weight" label="总毛重(kg)" width="120" />
<el-table-column label="操作" width="100" fixed="right">
<template #default="scope">
<el-button
type="success"
size="small"
:disabled="!wmsModifiedFlags[scope.$index]"
@click="submitWmsModification(scope.$index)"
>
提交修改
</el-button>
</template>
</el-table-column>
</el-table>
<WmsTable
ref="wmsTableDialogRef"
:model="currentSalesAxle?.model"
:specification="currentSalesAxle?.specification"
:loading-height="200"
:show-action="true"
@data-loaded="onWmsDataLoaded"
/>
<div class="chart-controls">
<span class="filter-label">时间范围:</span>
<el-select v-model="selectedTimeRange" @change="onChartTimeRangeChange" style="width: 120px">
@ -260,6 +220,14 @@
<h4>销售历史 - {{ salesChartTitle }}</h4>
<el-button size="small" @click="closeChart"></el-button>
</div>
<WmsTable
ref="wmsTableRef"
:model="currentSalesAxle?.model"
:specification="currentSalesAxle?.specification"
:loading-height="200"
:show-action="true"
@data-loaded="onWmsDataLoaded"
/>
<div class="chart-controls">
<span class="filter-label">时间范围:</span>
<el-select v-model="selectedTimeRange" @change="onChartTimeRangeChange" style="width: 120px">
@ -288,6 +256,7 @@ import { ref, computed, onMounted, nextTick, watch } from 'vue'
import { API_CONFIG } from "@/config/api"
import { getDateRangeByTimeRange } from '@/utils/dateFormat';
import * as echarts from 'echarts';
import WmsTable from '@/components/WmsTable/index.vue'
//
const props = defineProps({
@ -323,6 +292,8 @@ const salesChartTitle = ref('')
const currentSalesAxle = ref(null)
const salesChartRef = ref(null)
const salesChartDialogRef = ref(null)
const wmsTableRef = ref(null)
const wmsTableDialogRef = ref(null)
let salesChart = null
//
@ -556,7 +527,7 @@ const onCompletionLevelChange = () => {
};
//
const showSalesHistory = (row) => {
const showSalesHistory = async (row) => {
if (!row.update_time || !row.model || !row.specification) {
console.error('缺少必要的数据参数');
return;
@ -579,7 +550,13 @@ const showSalesHistory = (row) => {
fetchSalesData(row.model, row.specification);
fetchHistoryData(row.model, row.specification, row.wire_disc);
fetchWmsData(row.model, row.specification);
await nextTick()
if (selectedEquipment.value === 'all' && wmsTableDialogRef.value) {
wmsTableDialogRef.value.fetchData()
} else if (showChart.value && wmsTableRef.value) {
wmsTableRef.value.fetchData()
}
};
//
@ -592,6 +569,12 @@ const closeChart = () => {
}
};
// WmsTable
const onWmsDataLoaded = (loadedData) => {
wmsData.value = loadedData
recordWmsOriginalData()
};
//
const onChartTimeRangeChange = () => {
if (currentSalesAxle.value) {

Loading…
Cancel
Save