feat(mes): 新增 BOM 子物料选择器组件及弹窗

pull/871/MERGE
YunaiV 2026-04-07 18:48:03 +08:00
parent 917b2102bd
commit 5af93de4f5
4 changed files with 355 additions and 4 deletions

View File

@ -0,0 +1,183 @@
<!--
MES 产品 BOM 子物料选择器只读输入框 + 点击弹窗选择
从指定产品的 BOM 子物料中选择候选范围限定为产品已配好的 BOM 子项
交互显示为只读 el-input点击打开弹窗单选模式进行选择
Props:
modelValue 绑定的 BOM 子物料 IDv-model对应 bomItemId
itemId 父产品 ID决定 BOM 候选范围
disabled 是否禁用
clearable 是否允许清空鼠标悬停时显示清除图标
placeholder 占位文字
Events:
update:modelValue v-model 更新
change(bom) 选中 BOM 变化时触发传递完整 MdProductBomVO清空时为 undefined
-->
<template>
<div
v-bind="attrs"
class="w-full"
:class="disabled ? 'cursor-not-allowed' : 'cursor-pointer'"
@click="handleClick"
@mouseenter="hovering = true"
@mouseleave="hovering = false"
>
<el-tooltip :disabled="!selectedBom" placement="top" :show-after="500">
<template #content>
<div v-if="selectedBom" class="leading-6">
<div>编码{{ selectedBom.bomItemCode }}</div>
<div>名称{{ selectedBom.bomItemName }}</div>
<div>规格{{ selectedBom.bomItemSpecification || '-' }}</div>
<div>单位{{ selectedBom.unitMeasureName || '-' }}</div>
<div>用量比例{{ selectedBom.quantity }}</div>
</div>
</template>
<el-input
:model-value="displayLabel"
:placeholder="placeholder"
:disabled="disabled"
readonly
:suffix-icon="suffixIcon"
:class="disabled ? 'is-select-disabled' : 'is-select-clickable'"
/>
</el-tooltip>
</div>
<!-- 弹窗必须放在 div 外部否则弹窗内的点击事件会冒泡到 div 触发 handleClick -->
<MdProductBomSelectDialog ref="dialogRef" @selected="handleSelected" />
</template>
<script setup lang="ts">
import { MdProductBomApi, MdProductBomVO } from '@/api/mes/md/item/productBom'
import { Search, CircleClose } from '@element-plus/icons-vue'
import MdProductBomSelectDialog from './MdProductBomSelectDialog.vue'
// div + DialogVue attrs
// div class / style
const attrs = useAttrs()
defineOptions({ name: 'MdProductBomSelect', inheritAttrs: false })
const props = withDefaults(
defineProps<{
modelValue?: number // BOM IDbomItemId
itemId?: number // ID BOM
disabled?: boolean //
clearable?: boolean //
placeholder?: string //
}>(),
{
disabled: false,
clearable: true,
placeholder: '请选择 BOM 物料'
}
)
const emit = defineEmits<{
'update:modelValue': [value: number | undefined]
change: [bom: MdProductBomVO | undefined]
}>()
const dialogRef = ref() // Ref
const hovering = ref(false) //
// ==================== ====================
const selectedBom = ref<MdProductBomVO | undefined>() // BOM
/** 输入框显示文本 */
const displayLabel = computed(() => {
return selectedBom.value?.bomItemName ?? ''
})
/** 是否显示清除图标 */
const showClear = computed(() => {
return props.clearable && !props.disabled && hovering.value && props.modelValue != null
})
/** 后缀图标:悬停且有值时显示清除,否则显示搜索 */
const suffixIcon = computed(() => {
return showClear.value ? CircleClose : Search
})
/** 根据 bomItemId 从 BOM 列表中回显(用于编辑回显) */
const resolveBomById = async (bomItemId: number | undefined) => {
if (bomItemId == null || props.itemId == null) {
selectedBom.value = undefined
return
}
if (selectedBom.value?.bomItemId === bomItemId) {
return
}
try {
const list = await MdProductBomApi.getProductBomListByItemId(props.itemId)
selectedBom.value = list.find((item: MdProductBomVO) => item.bomItemId === bomItemId)
} catch (e) {
console.error('[MdProductBomSelect] resolveBomById failed:', e)
}
}
/** 监听 modelValue 变化,触发回显 */
watch(
() => props.modelValue,
(val) => {
resolveBomById(val)
},
{ immediate: true }
)
/** 监听 itemId 变化清空选中产品变了BOM 候选也变了) */
watch(
() => props.itemId,
() => {
selectedBom.value = undefined
emit('update:modelValue', undefined)
emit('change', undefined)
}
)
// ==================== ====================
/** 点击组件:清除或打开弹窗 */
const handleClick = (e: MouseEvent) => {
if (props.disabled) {
return
}
if (props.itemId == null) {
return
}
//
const target = e.target as HTMLElement
if (showClear.value && target.closest('.el-input__suffix')) {
e.stopPropagation()
selectedBom.value = undefined
emit('update:modelValue', undefined)
emit('change', undefined)
return
}
//
dialogRef.value.open(props.itemId, props.modelValue)
}
/** 弹窗选中回调 */
const handleSelected = (row: MdProductBomVO) => {
selectedBom.value = row
emit('update:modelValue', row.bomItemId)
emit('change', row)
}
</script>
<style lang="scss" scoped>
/* :deep 用于穿透 el-input 内部元素的 cursor 样式 */
.is-select-clickable {
:deep(.el-input__wrapper),
:deep(.el-input__inner) {
cursor: pointer;
}
}
.is-select-disabled {
:deep(.el-input__wrapper),
:deep(.el-input__inner) {
cursor: not-allowed;
}
}
</style>

View File

@ -0,0 +1,143 @@
<!--
MES 产品 BOM 子物料弹窗选择器单选
从指定产品的 BOM 子物料中选择候选范围限定为该产品已配好的 BOM 子项
Props:
无外部 props通过 open 方法传参
Events:
selected(row: MdProductBomVO) 确认选择后触发传递完整 BOM
Expose:
open(itemId: number, selectedBomItemId?: number) 打开弹窗
-->
<template>
<Dialog title="产品 BOM 物料选择" v-model="dialogVisible" width="800px">
<!-- 数据表格 -->
<el-table
ref="tableRef"
v-loading="loading"
:data="list"
:stripe="true"
:show-overflow-tooltip="true"
row-key="bomItemId"
highlight-current-row
@row-click="handleRowClick"
@row-dblclick="handleRowDblClick"
>
<!-- 单选 radio -->
<el-table-column width="50" align="center">
<template #default="{ row }">
<el-radio
v-model="selectedRadioId"
:value="row.bomItemId"
class="radio-no-label"
@change="handleRadioChange(row)"
/>
</template>
</el-table-column>
<el-table-column label="物料编码" align="center" prop="bomItemCode" width="160" />
<el-table-column label="物料名称" align="left" prop="bomItemName" min-width="150" />
<el-table-column label="规格型号" align="left" prop="bomItemSpecification" min-width="120" />
<el-table-column label="单位" align="center" prop="unitMeasureName" width="80" />
<el-table-column label="物料/产品" align="center" prop="itemOrProduct" width="100">
<template #default="scope">
<dict-tag :type="DICT_TYPE.MES_MD_ITEM_OR_PRODUCT" :value="scope.row.itemOrProduct" />
</template>
</el-table-column>
<el-table-column label="用量比例" align="center" prop="quantity" width="100" />
<el-table-column
label="备注"
align="center"
prop="remark"
min-width="120"
:show-overflow-tooltip="true"
/>
</el-table>
<template #footer>
<el-button type="primary" @click="confirmSelect"> </el-button>
<el-button @click="dialogVisible = false"> </el-button>
</template>
</Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE } from '@/utils/dict'
import { MdProductBomApi, MdProductBomVO } from '@/api/mes/md/item/productBom'
defineOptions({ name: 'MdProductBomSelectDialog' })
const message = useMessage()
const emit = defineEmits<{
selected: [row: MdProductBomVO]
}>()
const dialogVisible = ref(false) //
const loading = ref(false) //
const list = ref<MdProductBomVO[]>([]) // BOM
// ==================== ====================
const tableRef = ref() // Ref
const selectedRadioId = ref<number>() // bomItemId
const currentRow = ref<MdProductBomVO>() //
/** 单选 radio 变化 */
const handleRadioChange = (row: MdProductBomVO) => {
currentRow.value = row
}
/** 单击行:点击整行即选中 */
const handleRowClick = (row: MdProductBomVO) => {
selectedRadioId.value = row.bomItemId
currentRow.value = row
}
/** 双击行:直接确认 */
const handleRowDblClick = (row: MdProductBomVO) => {
selectedRadioId.value = row.bomItemId
currentRow.value = row
confirmSelect()
}
/** 确认选择 */
const confirmSelect = () => {
if (!currentRow.value) {
message.warning('请选择一条数据')
return
}
emit('selected', currentRow.value)
dialogVisible.value = false
}
// ==================== ====================
/** 打开弹窗 */
const open = async (itemId: number, selectedBomItemId?: number) => {
dialogVisible.value = true
//
selectedRadioId.value = selectedBomItemId
currentRow.value = undefined
// BOM
loading.value = true
try {
list.value = await MdProductBomApi.getProductBomListByItemId(itemId)
//
if (selectedBomItemId != null) {
const match = list.value.find((row) => row.bomItemId === selectedBomItemId)
if (match) {
currentRow.value = match
}
}
} finally {
loading.value = false
}
}
defineExpose({ open })
</script>
<style lang="scss" scoped>
/* 隐藏 radio 的 label 文字,只保留圆圈 */
.radio-no-label {
:deep(.el-radio__label) {
display: none;
}
}
</style>

View File

@ -35,7 +35,12 @@
<Dialog :title="formTitle" v-model="formVisible" width="500px"> <Dialog :title="formTitle" v-model="formVisible" width="500px">
<el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px"> <el-form ref="formRef" :model="formData" :rules="formRules" label-width="100px">
<el-form-item label="BOM 物料" prop="itemId"> <el-form-item label="BOM 物料" prop="itemId">
<MdItemSelect v-model="formData.itemId" placeholder="请选择 BOM 物料" /> <MdProductBomSelect
v-model="formData.itemId"
:itemId="productId"
placeholder="请选择 BOM 物料"
@change="handleBomItemChange"
/>
</el-form-item> </el-form-item>
<el-form-item label="用料比例" prop="quantity"> <el-form-item label="用料比例" prop="quantity">
<el-input-number <el-input-number
@ -60,7 +65,8 @@
<script setup lang="ts"> <script setup lang="ts">
import { ProRouteProductBomApi, ProRouteProductBomVO } from '@/api/mes/pro/route/productbom' import { ProRouteProductBomApi, ProRouteProductBomVO } from '@/api/mes/pro/route/productbom'
import { ProRouteProcessApi } from '@/api/mes/pro/route/process' import { ProRouteProcessApi } from '@/api/mes/pro/route/process'
import MdItemSelect from '@/views/mes/md/item/components/MdItemSelect.vue' import MdProductBomSelect from '@/views/mes/md/item/components/MdProductBomSelect.vue'
import { MdProductBomVO } from '@/api/mes/md/item/productBom'
defineOptions({ name: 'RouteProductBomList' }) defineOptions({ name: 'RouteProductBomList' })
@ -139,6 +145,13 @@ const openForm = (type: string, row?: ProRouteProductBomVO) => {
formRef.value?.resetFields() formRef.value?.resetFields()
} }
/** BOM 物料选中变化:自动回填用量比例 */
const handleBomItemChange = (bom: MdProductBomVO | undefined) => {
if (bom) {
formData.value.quantity = bom.quantity ?? 1
}
}
/** 提交表单 */ /** 提交表单 */
const submitForm = async () => { const submitForm = async () => {
const valid = await formRef.value.validate() const valid = await formRef.value.validate()

View File

@ -67,7 +67,11 @@
v-loading="formLoading" v-loading="formLoading"
> >
<el-form-item v-if="bomFormType === 'create'" label="物料" prop="itemId"> <el-form-item v-if="bomFormType === 'create'" label="物料" prop="itemId">
<MdItemSelect v-model="formData.itemId" /> <MdProductBomSelect
v-model="formData.itemId"
:itemId="props.workOrder.productId"
@change="handleBomItemChange"
/>
</el-form-item> </el-form-item>
<el-form-item v-else label="物料"> <el-form-item v-else label="物料">
<el-input :model-value="formData.itemName" disabled /> <el-input :model-value="formData.itemName" disabled />
@ -94,7 +98,8 @@
import { ProWorkOrderBomApi, ProWorkOrderBomVO } from '@/api/mes/pro/workorder/bom' import { ProWorkOrderBomApi, ProWorkOrderBomVO } from '@/api/mes/pro/workorder/bom'
import { MesProWorkOrderStatusEnum, MesProWorkOrderTypeEnum } from '@/views/mes/utils/constants' import { MesProWorkOrderStatusEnum, MesProWorkOrderTypeEnum } from '@/views/mes/utils/constants'
import { DICT_TYPE } from '@/utils/dict' import { DICT_TYPE } from '@/utils/dict'
import MdItemSelect from '@/views/mes/md/item/components/MdItemSelect.vue' import MdProductBomSelect from '@/views/mes/md/item/components/MdProductBomSelect.vue'
import { MdProductBomVO } from '@/api/mes/md/item/productBom'
defineOptions({ name: 'WorkOrderBomList' }) defineOptions({ name: 'WorkOrderBomList' })
@ -188,6 +193,13 @@ const resetForm = () => {
formRef.value?.resetFields() formRef.value?.resetFields()
} }
/** BOM 物料选中变化:自动回填预计使用量 */
const handleBomItemChange = (bom: MdProductBomVO | undefined) => {
if (bom) {
formData.value.quantity = bom.quantity ?? undefined
}
}
/** 打开 BOM 弹窗 */ /** 打开 BOM 弹窗 */
const openForm = (type: string, row?: any) => { const openForm = (type: string, row?: any) => {
resetForm() resetForm()