267 lines
7.1 KiB
Vue
267 lines
7.1 KiB
Vue
<template>
|
||
<el-dialog
|
||
v-model="visible"
|
||
title="运单称重"
|
||
width="480px"
|
||
align-center
|
||
destroy-on-close
|
||
@closed="handleClosed"
|
||
>
|
||
<!-- <el-alert
|
||
type="info"
|
||
:closable="false"
|
||
show-icon
|
||
class="weighing-form-alert"
|
||
>
|
||
<template #title>
|
||
支持运单号模式(order_id=0)与订单模式(order_id 为正整数)。称重重量单位为 g,须大于 0 的整数。
|
||
</template>
|
||
</el-alert> -->
|
||
|
||
<el-form
|
||
ref="formRef"
|
||
:model="formModel"
|
||
:rules="rules"
|
||
label-width="96px"
|
||
class="weighing-form"
|
||
@submit.prevent
|
||
>
|
||
<el-form-item label="称重方式" prop="weighing_mode">
|
||
<el-radio-group v-model="formModel.weighing_mode">
|
||
<el-radio value="waybill">运单号模式</el-radio>
|
||
<el-radio value="order">订单模式</el-radio>
|
||
</el-radio-group>
|
||
</el-form-item>
|
||
|
||
<el-form-item v-if="formModel.weighing_mode === 'waybill'" label="运单号" prop="waybill_code">
|
||
<el-input
|
||
v-model="formModel.waybill_code"
|
||
placeholder="请输入运单号"
|
||
clearable
|
||
maxlength="64"
|
||
@keyup.enter="handleSubmit"
|
||
/>
|
||
</el-form-item>
|
||
|
||
<template v-else>
|
||
<el-form-item label="订单 ID" prop="order_id">
|
||
<el-input-number
|
||
v-model="formModel.order_id"
|
||
:min="1"
|
||
:precision="0"
|
||
:step="1"
|
||
controls-position="right"
|
||
placeholder="请输入订单 ID"
|
||
style="width: 100%"
|
||
/>
|
||
</el-form-item>
|
||
<el-form-item label="运单号" prop="order_waybill_code">
|
||
<el-input
|
||
v-model="formModel.order_waybill_code"
|
||
placeholder="单运单订单可不填;多运单订单必填"
|
||
clearable
|
||
maxlength="64"
|
||
@keyup.enter="handleSubmit"
|
||
/>
|
||
</el-form-item>
|
||
</template>
|
||
|
||
<el-form-item label="称重重量(g)" prop="weighing_weight">
|
||
<el-input-number
|
||
v-model="formModel.weighing_weight"
|
||
:min="1"
|
||
:precision="0"
|
||
:step="1"
|
||
controls-position="right"
|
||
placeholder="请输入称重重量"
|
||
style="width: 100%"
|
||
/>
|
||
</el-form-item>
|
||
</el-form>
|
||
|
||
<template #footer>
|
||
<el-button @click="visible = false">取消</el-button>
|
||
<el-button type="primary" :loading="loading" @click="handleSubmit">保存</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { reactive, ref, watch } from 'vue'
|
||
import type { FormInstance, FormRules } from 'element-plus'
|
||
import { ElMessage } from 'element-plus'
|
||
import { orderUpdateExpressWeighingWeight } from '@/api/order.index/orderindex'
|
||
import type { UpdateExpressWeighingWeightParams } from '@/api/order.index/orderindex'
|
||
import {
|
||
isOrderIndexApiSuccess,
|
||
parseUpdateExpressWeighingWeightResponse
|
||
} from '@/views/order/orderManagement/utils/order-index-helpers'
|
||
|
||
export type ExpressWeighingFormResult = ReturnType<typeof parseUpdateExpressWeighingWeightResponse> & {
|
||
waybill?: string
|
||
}
|
||
|
||
const visible = defineModel<boolean>({ default: false })
|
||
|
||
const props = defineProps<{
|
||
initialWaybillCode?: string
|
||
}>()
|
||
|
||
const emit = defineEmits<{
|
||
(e: 'success', payload: ExpressWeighingFormResult): void
|
||
}>()
|
||
|
||
const loading = ref(false)
|
||
const formRef = ref<FormInstance>()
|
||
|
||
const formModel = reactive({
|
||
weighing_mode: 'waybill' as 'waybill' | 'order',
|
||
waybill_code: '',
|
||
order_id: undefined as number | undefined,
|
||
order_waybill_code: '',
|
||
weighing_weight: undefined as number | undefined
|
||
})
|
||
|
||
function validateWeighingWeight(_rule: unknown, value: unknown, callback: (error?: Error) => void) {
|
||
const num = Number(value)
|
||
if (!Number.isFinite(num) || num <= 0) {
|
||
callback(new Error('称重重量须大于 0'))
|
||
return
|
||
}
|
||
if (!Number.isInteger(num)) {
|
||
callback(new Error('称重重量必须为整数(g)'))
|
||
return
|
||
}
|
||
callback()
|
||
}
|
||
|
||
const rules: FormRules = {
|
||
weighing_mode: [{ required: true, message: '请选择称重方式', trigger: 'change' }],
|
||
waybill_code: [
|
||
{
|
||
validator: (_rule, value, callback) => {
|
||
if (formModel.weighing_mode !== 'waybill') {
|
||
callback()
|
||
return
|
||
}
|
||
if (!String(value ?? '').trim()) {
|
||
callback(new Error('运单号模式下运单号必填'))
|
||
return
|
||
}
|
||
callback()
|
||
},
|
||
trigger: 'blur'
|
||
}
|
||
],
|
||
order_id: [
|
||
{
|
||
validator: (_rule, value, callback) => {
|
||
if (formModel.weighing_mode !== 'order') {
|
||
callback()
|
||
return
|
||
}
|
||
const id = Number(value)
|
||
if (!Number.isFinite(id) || id <= 0 || !Number.isInteger(id)) {
|
||
callback(new Error('订单 ID 须为正整数'))
|
||
return
|
||
}
|
||
callback()
|
||
},
|
||
trigger: 'blur'
|
||
}
|
||
],
|
||
weighing_weight: [
|
||
{ required: true, message: '请输入称重重量', trigger: 'blur' },
|
||
{ validator: validateWeighingWeight, trigger: 'blur' }
|
||
]
|
||
}
|
||
|
||
function resetForm() {
|
||
formModel.weighing_mode = 'waybill'
|
||
formModel.waybill_code = props.initialWaybillCode?.trim() || ''
|
||
formModel.order_id = undefined
|
||
formModel.order_waybill_code = ''
|
||
formModel.weighing_weight = undefined
|
||
formRef.value?.clearValidate()
|
||
}
|
||
|
||
function buildPayload(): UpdateExpressWeighingWeightParams {
|
||
const weighingWeight = Number(formModel.weighing_weight)
|
||
|
||
if (formModel.weighing_mode === 'waybill') {
|
||
return {
|
||
order_id: 0,
|
||
waybill_code: formModel.waybill_code.trim(),
|
||
weighing_weight: weighingWeight
|
||
}
|
||
}
|
||
|
||
const payload: UpdateExpressWeighingWeightParams = {
|
||
order_id: Number(formModel.order_id),
|
||
weighing_weight: weighingWeight
|
||
}
|
||
const waybill = formModel.order_waybill_code.trim()
|
||
if (waybill) payload.waybill_code = waybill
|
||
return payload
|
||
}
|
||
|
||
function applySuccessResult(payload: UpdateExpressWeighingWeightParams, detail: ExpressWeighingFormResult) {
|
||
emit('success', {
|
||
...detail,
|
||
waybill: detail.waybill_code || payload.waybill_code || ''
|
||
})
|
||
}
|
||
|
||
async function handleSubmit() {
|
||
if (!formRef.value || loading.value) return
|
||
const valid = await formRef.value.validate().catch(() => false)
|
||
if (!valid) return
|
||
|
||
const payload = buildPayload()
|
||
loading.value = true
|
||
try {
|
||
const res: any = await orderUpdateExpressWeighingWeight(payload)
|
||
if (!isOrderIndexApiSuccess(res)) {
|
||
throw new Error(res?.message || '称重保存失败')
|
||
}
|
||
|
||
const detail = parseUpdateExpressWeighingWeightResponse(res)
|
||
ElMessage.success(res?.message || '称重保存成功')
|
||
applySuccessResult(payload, detail)
|
||
visible.value = false
|
||
} catch (error: any) {
|
||
ElMessage.error(error?.message || '称重保存失败')
|
||
} finally {
|
||
loading.value = false
|
||
}
|
||
}
|
||
|
||
function handleClosed() {
|
||
resetForm()
|
||
}
|
||
|
||
watch(
|
||
() => visible.value,
|
||
(open) => {
|
||
if (open) resetForm()
|
||
}
|
||
)
|
||
|
||
watch(
|
||
() => formModel.weighing_mode,
|
||
() => {
|
||
formRef.value?.clearValidate(['waybill_code', 'order_id', 'order_waybill_code'])
|
||
}
|
||
)
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
.weighing-form-alert {
|
||
margin-bottom: 16px;
|
||
}
|
||
|
||
.weighing-form {
|
||
padding-top: 4px;
|
||
}
|
||
</style>
|