WEB/src/views/order/orderManagement/component/BindPackageMaterialByWaybillDialog.vue
2026-06-15 18:13:39 +08:00

208 lines
6.5 KiB
Vue

<template>
<el-dialog
:model-value="modelValue"
title="按运单号绑定包材"
width="520px"
align-center
destroy-on-close
@close="handleClose"
@closed="resetForm"
>
<el-alert
type="info"
:closable="false"
show-icon
style="margin-bottom: 12px"
title="仅需运单号与包材编码,系统自动反查订单运单记录并绑定包材,无需订单 ID。"
/>
<el-form ref="formRef" :model="form" :rules="rules" label-width="96px" @submit.prevent>
<el-form-item label="运单号" prop="waybill_code">
<el-input
v-model.trim="form.waybill_code"
placeholder="请输入运单号"
clearable
@keyup.enter="focusMaterialSelect"
/>
</el-form-item>
<el-form-item label="包材编码" prop="package_material_code">
<el-select
ref="materialSelectRef"
v-model="form.package_material_code"
filterable
remote
clearable
reserve-keyword
placeholder="输入编码/名称搜索"
:remote-method="searchPackageMaterials"
:loading="materialLoading"
style="width: 100%"
@keyup.enter="handleSubmit"
>
<el-option
v-for="item in materialOptions"
:key="item.material_code"
:label="`${item.material_code} / ${item.material_name}`"
:value="item.material_code"
/>
</el-select>
</el-form-item>
</el-form>
<div v-if="lastResult" class="bind-result">
<el-divider content-position="left">绑定结果</el-divider>
<el-descriptions :column="1" size="small" border>
<el-descriptions-item label="订单 ID">{{ lastResult.order_id }}</el-descriptions-item>
<el-descriptions-item label="运单号">{{ lastResult.waybill_code }}</el-descriptions-item>
<el-descriptions-item label="包材 ID">{{ lastResult.package_material_id }}</el-descriptions-item>
<el-descriptions-item label="包材编码">{{ lastResult.package_material_code }}</el-descriptions-item>
<el-descriptions-item label="包材名称">{{ lastResult.package_material_name_snapshot }}</el-descriptions-item>
</el-descriptions>
</div>
<template #footer>
<el-button @click="handleClose">取消</el-button>
<el-button type="primary" :loading="submitting" @click="handleSubmit">绑定</el-button>
</template>
</el-dialog>
</template>
<script setup lang="ts">
import { nextTick, reactive, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import {
orderBindExpressPackageMaterialByWaybill,
type BindExpressPackageMaterialByWaybillResponse
} from '@/api/order.index/orderindex'
import { packageMaterialGet } from '@/api/setting.packagematerial/packagematerial'
import { parsePackageMaterialListResponse } from '@/views/material/packageMaterial/package-material-config'
import { isOrderIndexApiSuccess } from '../utils/order-index-helpers'
const props = defineProps<{
modelValue: boolean
/** 打开时预填运单号(如从运单列表带入) */
initialWaybillCode?: string
}>()
const emit = defineEmits<{
(e: 'update:modelValue', value: boolean): void
(e: 'success', payload: BindExpressPackageMaterialByWaybillResponse): void
}>()
const formRef = ref<FormInstance>()
const materialSelectRef = ref<{ focus?: () => void } | null>(null)
const submitting = ref(false)
const materialLoading = ref(false)
const materialOptions = ref<Array<{ material_code: string; material_name: string }>>([])
const lastResult = ref<BindExpressPackageMaterialByWaybillResponse | null>(null)
const form = reactive({
waybill_code: '',
package_material_code: ''
})
const rules: FormRules = {
waybill_code: [{ required: true, message: '请输入运单号', trigger: 'blur' }],
package_material_code: [{ required: true, message: '请选择包材编码', trigger: 'change' }]
}
watch(
() => props.modelValue,
(visible) => {
if (!visible) return
lastResult.value = null
form.waybill_code = String(props.initialWaybillCode || '').trim()
form.package_material_code = ''
materialOptions.value = []
}
)
function handleClose() {
emit('update:modelValue', false)
}
function resetForm() {
form.waybill_code = ''
form.package_material_code = ''
materialOptions.value = []
lastResult.value = null
formRef.value?.clearValidate()
}
function focusMaterialSelect() {
nextTick(() => materialSelectRef.value?.focus?.())
}
async function searchPackageMaterials(query: string) {
const keyword = String(query || '').trim()
if (!keyword) {
materialOptions.value = []
return
}
materialLoading.value = true
try {
const res: any = await packageMaterialGet({
pagination: { page: 1, pageSize: 50 },
where: { quickWhere: keyword }
})
const data = parsePackageMaterialListResponse(res)
materialOptions.value = (data.list || [])
.map((item: any) => ({
material_code: String(item.material_code || '').trim(),
material_name: String(item.material_name || '').trim()
}))
.filter((item) => item.material_code)
} catch {
materialOptions.value = []
} finally {
materialLoading.value = false
}
}
function unwrapBindPayload(res: any): BindExpressPackageMaterialByWaybillResponse | null {
const raw = res?.data
if (raw && typeof raw === 'object' && !Array.isArray(raw)) {
return raw as BindExpressPackageMaterialByWaybillResponse
}
return null
}
async function handleSubmit() {
if (!formRef.value || submitting.value) return
const valid = await formRef.value.validate().catch(() => false)
if (!valid) return
const waybill_code = form.waybill_code.trim()
const package_material_code = String(form.package_material_code || '').trim().toUpperCase()
if (!waybill_code || !package_material_code) return
submitting.value = true
try {
const res: any = await orderBindExpressPackageMaterialByWaybill({
waybill_code,
package_material_code
})
if (!isOrderIndexApiSuccess(res)) {
throw new Error(res?.message || '绑定包材失败')
}
const payload = unwrapBindPayload(res)
if (payload) {
lastResult.value = payload
emit('success', payload)
}
ElMessage.success('包材绑定成功')
} catch (error: any) {
ElMessage.error(error?.message || '绑定包材失败')
} finally {
submitting.value = false
}
}
</script>
<style scoped lang="scss">
.bind-result {
margin-top: 4px;
}
</style>