产品和原材料配置BOM
This commit is contained in:
221
klp-ui/src/views/wms/bom/components/BomPanel.vue
Normal file
221
klp-ui/src/views/wms/bom/components/BomPanel.vue
Normal file
@@ -0,0 +1,221 @@
|
||||
<template>
|
||||
<div class="bom-container">
|
||||
<!-- 当没有传入id时显示创建按钮 -->
|
||||
<div v-if="!id" class="create-bom">
|
||||
<button @click="handleCreate" class="create-button">
|
||||
+ 创建BOM
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<!-- 当传入id时显示数据区域 -->
|
||||
<div v-else class="bom-details">
|
||||
<div v-if="loading" class="loading-indicator">
|
||||
加载中...
|
||||
<div class="spinner"></div>
|
||||
</div>
|
||||
|
||||
<div v-if="error" class="error-message">
|
||||
加载失败: {{ error }}
|
||||
<button @click="fetchData" class="retry-button">重试</button>
|
||||
</div>
|
||||
|
||||
<div v-if="data && !loading">
|
||||
<el-form label-width="100px">
|
||||
<el-form-item label="BOM名称">
|
||||
<el-input v-model="info.bomName" @blur="handleUpdateBom"/>
|
||||
</el-form-item>
|
||||
<el-form-item label="BOM编码">
|
||||
<el-input v-model="info.bomCode" @blur="handleUpdateBom"/>
|
||||
</el-form-item>
|
||||
</el-form>
|
||||
<BomItem :bomId="id" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { addBom, updateBom, getBom } from '@/api/wms/bom';
|
||||
import { listBomItem } from '@/api/wms/bomItem';
|
||||
import { updateProduct } from '@/api/wms/product';
|
||||
import { updateRawMaterial } from '@/api/wms/rawMaterial';
|
||||
|
||||
import BomItem from './BomItem.vue';
|
||||
|
||||
export default {
|
||||
props: {
|
||||
id: [String, Number], // 支持字符串或数字类型的ID
|
||||
itemId: [String, Number],
|
||||
type: String // 可选类型参数
|
||||
},
|
||||
components: {
|
||||
BomItem
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
loading: false,
|
||||
error: null,
|
||||
data: null,
|
||||
info: {}
|
||||
};
|
||||
},
|
||||
watch: {
|
||||
id: {
|
||||
immediate: true, // 组件创建时立即执行
|
||||
handler(newVal) {
|
||||
if (newVal) {
|
||||
console.log('侦听到变化')
|
||||
this.fetchBomDetails();
|
||||
} else {
|
||||
// 没有ID时重置数据
|
||||
this.data = null;
|
||||
this.error = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
async fetchBomDetails() {
|
||||
try {
|
||||
this.loading = true;
|
||||
this.error = null;
|
||||
|
||||
// 实际项目中替换为真实API调用
|
||||
const response = await listBomItem({
|
||||
bomId: this.id
|
||||
})
|
||||
this.data = response.rows;
|
||||
|
||||
const response2 = await getBom(this.id)
|
||||
this.info = response2.data;
|
||||
} catch (err) {
|
||||
this.error = err.message || '请求失败,请重试';
|
||||
console.error('获取BOM详情失败:', err);
|
||||
} finally {
|
||||
this.loading = false;
|
||||
}
|
||||
},
|
||||
|
||||
handleCreate() {
|
||||
// 触发自定义事件
|
||||
addBom({
|
||||
bomName: (this.type == 'product' ? '产品BOM' : '原材料BOM') + new Date().getTime(),
|
||||
bomCode: (this.type == 'product' ? 'P' : 'R') + new Date().getTime()
|
||||
}).then(bom => {
|
||||
this.$message.success('创建BOM成功');
|
||||
console.log(bom, this.itemId)
|
||||
if (this.type == 'product') {
|
||||
updateProduct({
|
||||
productId: this.itemId,
|
||||
bomId: bom.data.bomId
|
||||
}).then(_ => {
|
||||
this.$emit('addBom', bom.data);
|
||||
})
|
||||
}
|
||||
else if (this.type == 'raw_material') {
|
||||
updateRawMaterial({
|
||||
rawMaterialId: this.itemId,
|
||||
bomId: bom.data.bomId
|
||||
}).then(_ => {
|
||||
this.$emit('addBom', bom.data);
|
||||
})
|
||||
}
|
||||
|
||||
})
|
||||
console.log('创建BOM操作');
|
||||
},
|
||||
|
||||
handleUpdateBom() {
|
||||
this.$message.warning('正在更新BOM...');
|
||||
updateBom({
|
||||
bomId: this.id,
|
||||
bomName: this.info.bomName,
|
||||
bomCode: this.info.bomCode
|
||||
}).then(_ => {
|
||||
this.$message.success('更新BOM成功');
|
||||
})
|
||||
},
|
||||
|
||||
fetchData() {
|
||||
// 重试获取数据
|
||||
if (this.id) this.fetchBomDetails();
|
||||
}
|
||||
}
|
||||
};
|
||||
</script>
|
||||
|
||||
<style scoped>
|
||||
.create-bom {
|
||||
text-align: center;
|
||||
padding: 40px 0;
|
||||
}
|
||||
|
||||
.create-button {
|
||||
background-color: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
padding: 12px 24px;
|
||||
font-size: 16px;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
transition: background-color 0.3s;
|
||||
}
|
||||
|
||||
.create-button:hover {
|
||||
background-color: #388E3C;
|
||||
}
|
||||
|
||||
.loading-indicator {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
gap: 12px;
|
||||
padding: 20px;
|
||||
color: #666;
|
||||
}
|
||||
|
||||
.spinner {
|
||||
width: 20px;
|
||||
height: 20px;
|
||||
border: 3px solid rgba(0,0,0,0.1);
|
||||
border-radius: 50%;
|
||||
border-top-color: #3498db;
|
||||
animation: spin 1s linear infinite;
|
||||
}
|
||||
|
||||
@keyframes spin {
|
||||
to { transform: rotate(360deg); }
|
||||
}
|
||||
|
||||
.error-message {
|
||||
padding: 20px;
|
||||
background-color: #ffebee;
|
||||
color: #d32f2f;
|
||||
border-radius: 4px;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.retry-button {
|
||||
padding: 8px 16px;
|
||||
background-color: #f44336;
|
||||
color: white;
|
||||
border: none;
|
||||
border-radius: 4px;
|
||||
cursor: pointer;
|
||||
width: 120px;
|
||||
}
|
||||
|
||||
.bom-info {
|
||||
margin-top: 20px;
|
||||
padding: 15px;
|
||||
background-color: #f9f9f9;
|
||||
border-radius: 4px;
|
||||
border-left: 4px solid #2196F3;
|
||||
}
|
||||
|
||||
.bom-info p {
|
||||
margin: 10px 0;
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user