创建BOM防止重复点击

This commit is contained in:
砂糖
2025-07-29 15:22:06 +08:00
parent ef7035cfbd
commit ae33b44468

View File

@@ -2,9 +2,20 @@
<div class="bom-container"> <div class="bom-container">
<!-- 当没有传入id时显示创建按钮 --> <!-- 当没有传入id时显示创建按钮 -->
<div v-if="!id" class="create-bom"> <div v-if="!id" class="create-bom">
<button @click="handleCreate" class="create-button"> <el-button
+ 创建BOM type="primary"
</button> :loading="createLoading"
:disabled="createLoading"
@click="handleCreate"
class="create-button"
>
<template v-if="!createLoading">
<i class="el-icon-plus"></i> 创建BOM
</template>
<template v-else>
创建中...
</template>
</el-button>
</div> </div>
<!-- 当传入id时显示数据区域 --> <!-- 当传入id时显示数据区域 -->
@@ -56,7 +67,8 @@ export default {
loading: false, loading: false,
error: null, error: null,
data: null, data: null,
info: {} info: {},
createLoading: false,
}; };
}, },
watch: { watch: {
@@ -96,33 +108,41 @@ export default {
} }
}, },
handleCreate() { async handleCreate() {
// 触发自定义事件 // 防止重复点击
addBom({ if (this.createLoading) return;
try {
this.createLoading = true;
const bomResponse = await addBom({
bomName: (this.type == 'product' ? '产品BOM' : '原材料BOM') + new Date().getTime(), bomName: (this.type == 'product' ? '产品BOM' : '原材料BOM') + new Date().getTime(),
bomCode: (this.type == 'product' ? 'P' : 'R') + new Date().getTime() bomCode: (this.type == 'product' ? 'P' : 'R') + new Date().getTime()
}).then(bom => { });
this.$message.success('创建BOM成功'); this.$message.success('创建BOM成功');
console.log(bom, this.itemId) const bomData = bomResponse.data;
if (this.type == 'product') {
updateProduct({ // 根据类型更新产品/原材料
if (this.type == 'product' && this.itemId) {
await updateProduct({
productId: this.itemId, productId: this.itemId,
bomId: bom.data.bomId bomId: bomData.bomId
}).then(_ => { });
this.$emit('addBom', bom.data); } else if (this.type == 'raw_material' && this.itemId) {
}) await updateRawMaterial({
}
else if (this.type == 'raw_material') {
updateRawMaterial({
rawMaterialId: this.itemId, rawMaterialId: this.itemId,
bomId: bom.data.bomId bomId: bomData.bomId
}).then(_ => { });
this.$emit('addBom', bom.data);
})
} }
}) // 触发事件
console.log('创建BOM操作'); this.$emit('addBom', bomData);
} catch (error) {
console.error('创建失败:', error);
this.$message.error(`创建失败: ${error.message || '未知错误'}`);
} finally {
this.createLoading = false;
}
}, },
handleUpdateBom() { handleUpdateBom() {