359 lines
10 KiB
Vue
359 lines
10 KiB
Vue
<template>
|
|
<el-dialog :title="title" v-model="visible" width="760px" append-to-body>
|
|
<basic-form ref="formRef" v-model="form" class="surcharge-form" :schemas="schemas" label-width="118px" />
|
|
<template #footer>
|
|
<el-button type="primary" @click="submitForm">确 定</el-button>
|
|
<el-button @click="visible = false">取 消</el-button>
|
|
</template>
|
|
</el-dialog>
|
|
</template>
|
|
|
|
<script setup>
|
|
import BasicForm from '@/components/BasicForm'
|
|
import { getSurcharge, addSurcharge, updateSurcharge, listSurchargeBlockOptions } from '@/api/price/surcharge'
|
|
import { listRegionOptions } from '@/api/data/region'
|
|
|
|
const emit = defineEmits(['success'])
|
|
const { proxy } = getCurrentInstance()
|
|
|
|
const formRef = ref()
|
|
const visible = ref(false)
|
|
const title = ref('')
|
|
const form = ref(emptyForm())
|
|
const regionOptions = ref([])
|
|
const blockOptions = ref([])
|
|
const loadingBlocks = ref(false)
|
|
const suppressCountryWatch = ref(false)
|
|
|
|
const calcModeOptions = [
|
|
{ value: 'MANUAL', label: '暂不计费' },
|
|
{ value: 'ALWAYS', label: '每票都收' },
|
|
{ value: 'POSTCODE', label: '命中邮编' },
|
|
{ value: 'ZONE', label: '命中分区' },
|
|
{ value: 'EXTRA_KG', label: '超重续收' },
|
|
{ value: 'WEIGHT', label: '超重' },
|
|
{ value: 'SIZE', label: '超尺寸' },
|
|
{ value: 'RETURN', label: '退件' }
|
|
]
|
|
|
|
const countrySelectOptions = computed(() => {
|
|
const all = [{ label: '全部国家', value: '' }]
|
|
return all.concat((regionOptions.value || []).map(r => ({
|
|
label: regionLabel(r),
|
|
value: r.countryCode2
|
|
})))
|
|
})
|
|
|
|
const blockSelectOptions = computed(() =>
|
|
(blockOptions.value || []).map(title => ({ label: title, value: title }))
|
|
)
|
|
|
|
const chargeType = computed(() => String(form.value.chargeType || '').toUpperCase())
|
|
|
|
const schemas = computed(() => [
|
|
{
|
|
label: '附加费名称',
|
|
prop: 'scName',
|
|
component: 'input',
|
|
placeholder: '如 燃油附加费',
|
|
span: 12,
|
|
width: '100%',
|
|
props: { maxlength: 100 },
|
|
rules: [{ required: true, message: '附加费名称不能为空', trigger: 'blur' }]
|
|
},
|
|
{
|
|
label: '费用类型',
|
|
prop: 'feeType',
|
|
component: 'select',
|
|
placeholder: '请选择',
|
|
dict: 'bms_fee_type',
|
|
span: 12,
|
|
width: '100%',
|
|
disabled: true,
|
|
rules: [{ required: true, message: '请选择费用类型', trigger: 'change' }]
|
|
},
|
|
{
|
|
label: '计费方式',
|
|
prop: 'chargeType',
|
|
component: 'select',
|
|
placeholder: '请选择',
|
|
options: calcModeOptions,
|
|
span: 12,
|
|
width: '100%',
|
|
rules: [{ required: true, message: '请选择计费方式', trigger: 'change' }]
|
|
},
|
|
{
|
|
label: '长(cm)',
|
|
prop: 'lengthCm',
|
|
component: 'inputNumber',
|
|
span: 12,
|
|
width: '100%',
|
|
show: chargeType.value === 'SIZE',
|
|
props: { min: 0, precision: 2, controls: false, align: 'left' },
|
|
rules: chargeType.value === 'SIZE' ? [{ required: true, message: '请填写长', trigger: 'blur' }] : undefined
|
|
},
|
|
{
|
|
label: '宽(cm)',
|
|
prop: 'widthCm',
|
|
component: 'inputNumber',
|
|
span: 12,
|
|
width: '100%',
|
|
show: chargeType.value === 'SIZE',
|
|
props: { min: 0, precision: 2, controls: false, align: 'left' },
|
|
rules: chargeType.value === 'SIZE' ? [{ required: true, message: '请填写宽', trigger: 'blur' }] : undefined
|
|
},
|
|
{
|
|
label: '高(cm)',
|
|
prop: 'heightCm',
|
|
component: 'inputNumber',
|
|
span: 12,
|
|
width: '100%',
|
|
show: chargeType.value === 'SIZE',
|
|
props: { min: 0, precision: 2, controls: false, align: 'left' },
|
|
rules: chargeType.value === 'SIZE' ? [{ required: true, message: '请填写高', trigger: 'blur' }] : undefined
|
|
},
|
|
{
|
|
label: '超过重量(kg)',
|
|
prop: 'overWeightKg',
|
|
component: 'inputNumber',
|
|
placeholder: '超过这个重量才收取',
|
|
span: 12,
|
|
width: '100%',
|
|
show: chargeType.value === 'WEIGHT',
|
|
props: { min: 0, precision: 3, controls: false, align: 'left' },
|
|
rules: chargeType.value === 'WEIGHT' ? [{ required: true, message: '请填写超过重量', trigger: 'blur' }] : undefined
|
|
},
|
|
{
|
|
label: '续重起始(kg)',
|
|
prop: 'extraWeightKg',
|
|
component: 'inputNumber',
|
|
placeholder: '超过这个重量后按超出公斤计',
|
|
span: 12,
|
|
width: '100%',
|
|
show: chargeType.value === 'EXTRA_KG',
|
|
props: { min: 0, precision: 3, controls: false, align: 'left' },
|
|
rules: chargeType.value === 'EXTRA_KG' ? [{ required: true, message: '请填写续重起始重量', trigger: 'blur' }] : undefined
|
|
},
|
|
{
|
|
label: '邮编',
|
|
prop: 'postcodeText',
|
|
component: 'textarea',
|
|
placeholder: '多个可用逗号分隔',
|
|
span: 24,
|
|
width: '100%',
|
|
show: chargeType.value === 'POSTCODE',
|
|
props: { rows: 2, maxlength: 1000 },
|
|
rules: chargeType.value === 'POSTCODE' ? [{ required: true, message: '请填写邮编', trigger: 'blur' }] : undefined
|
|
},
|
|
{
|
|
label: '计费数值',
|
|
prop: 'chargeValue',
|
|
component: 'inputNumber',
|
|
span: 12,
|
|
width: '100%',
|
|
props: { min: 0, precision: 6, controls: false, align: 'left' },
|
|
rules: [{ required: true, message: '计费数值不能为空', trigger: 'blur' }]
|
|
},
|
|
{
|
|
label: '币种',
|
|
prop: 'currency',
|
|
component: 'select',
|
|
placeholder: '请选择',
|
|
dict: 'bms_currency',
|
|
span: 12,
|
|
width: '100%'
|
|
},
|
|
{
|
|
label: '取值方式',
|
|
prop: 'roundMode',
|
|
component: 'select',
|
|
placeholder: '请选择',
|
|
dict: 'bms_round_mode',
|
|
span: 12,
|
|
width: '100%',
|
|
rules: [{ required: true, message: '请选择取值方式', trigger: 'change' }]
|
|
},
|
|
{
|
|
label: '适用国家',
|
|
prop: 'countryCode',
|
|
component: 'select',
|
|
placeholder: '全部国家',
|
|
options: countrySelectOptions.value,
|
|
span: 12,
|
|
width: '100%',
|
|
props: { filterable: true, clearable: true }
|
|
},
|
|
{
|
|
label: '分区',
|
|
prop: 'blockTitle',
|
|
component: 'select',
|
|
placeholder: form.value.countryCode ? (loadingBlocks.value ? '加载中...' : '请选择分区/区块') : '请先选择国家',
|
|
options: blockSelectOptions.value,
|
|
span: 12,
|
|
width: '100%',
|
|
show: chargeType.value === 'ZONE',
|
|
disabled: !form.value.countryCode || loadingBlocks.value,
|
|
props: { filterable: true, clearable: true }
|
|
},
|
|
{
|
|
label: '备注',
|
|
prop: 'scNote',
|
|
component: 'textarea',
|
|
placeholder: '请输入备注',
|
|
span: 24,
|
|
width: '100%',
|
|
props: { rows: 2, maxlength: 500 }
|
|
}
|
|
])
|
|
|
|
function emptyForm() {
|
|
return {
|
|
scId: undefined,
|
|
scName: undefined,
|
|
feeType: 'SURCHARGE',
|
|
chargeType: 'MANUAL',
|
|
chargeValue: 0,
|
|
currency: 'CNY',
|
|
roundMode: 'HALF_UP',
|
|
countryCode: undefined,
|
|
blockTitle: undefined,
|
|
scNote: undefined,
|
|
lengthCm: undefined,
|
|
widthCm: undefined,
|
|
heightCm: undefined,
|
|
overWeightKg: undefined,
|
|
extraWeightKg: undefined,
|
|
postcodeText: undefined
|
|
}
|
|
}
|
|
|
|
function regionLabel(r) {
|
|
const cn = r.countryNameCn || r.countryNameEn || ''
|
|
return cn ? `${r.flagEmoji || ''} ${cn} (${r.countryCode2})`.trim() : r.countryCode2
|
|
}
|
|
|
|
function ensureRegions() {
|
|
if (regionOptions.value.length) return Promise.resolve()
|
|
return listRegionOptions().then(res => {
|
|
regionOptions.value = res.data || []
|
|
})
|
|
}
|
|
|
|
function resolveCountryName(code) {
|
|
if (!code) return ''
|
|
const region = (regionOptions.value || []).find(item => item.countryCode2 === code)
|
|
return region?.countryNameCn || region?.countryNameEn || ''
|
|
}
|
|
|
|
function loadBlockOptions(code, keepCurrent) {
|
|
if (!code) {
|
|
blockOptions.value = []
|
|
if (!keepCurrent) form.value.blockTitle = undefined
|
|
return Promise.resolve()
|
|
}
|
|
loadingBlocks.value = true
|
|
const countryName = resolveCountryName(code)
|
|
return listSurchargeBlockOptions({ countryCode: code, countryName: countryName || undefined })
|
|
.then(res => {
|
|
const list = (res.data || []).filter(Boolean)
|
|
blockOptions.value = list
|
|
if (!keepCurrent) {
|
|
form.value.blockTitle = undefined
|
|
} else if (form.value.blockTitle && !list.includes(form.value.blockTitle)) {
|
|
// keep current title even if not in options (legacy)
|
|
blockOptions.value = [form.value.blockTitle].concat(list)
|
|
}
|
|
})
|
|
.catch(() => {
|
|
blockOptions.value = []
|
|
})
|
|
.finally(() => {
|
|
loadingBlocks.value = false
|
|
})
|
|
}
|
|
|
|
function reset() {
|
|
form.value = emptyForm()
|
|
blockOptions.value = []
|
|
nextTick(() => formRef.value?.clearValidate())
|
|
}
|
|
|
|
function open(row) {
|
|
reset()
|
|
ensureRegions().then(() => {
|
|
if (!row) {
|
|
title.value = '新建附加费'
|
|
visible.value = true
|
|
return
|
|
}
|
|
getSurcharge(row.scId).then(res => {
|
|
const data = res.data || {}
|
|
suppressCountryWatch.value = true
|
|
form.value = { ...emptyForm(), ...data }
|
|
title.value = '修改附加费'
|
|
visible.value = true
|
|
loadBlockOptions(form.value.countryCode, true).finally(() => {
|
|
suppressCountryWatch.value = false
|
|
nextTick(() => formRef.value?.clearValidate())
|
|
})
|
|
})
|
|
})
|
|
}
|
|
|
|
function submitForm() {
|
|
formRef.value.validate(valid => {
|
|
if (!valid) return
|
|
const payload = { ...form.value }
|
|
if (!payload.countryCode) payload.countryCode = undefined
|
|
delete payload.dictValue
|
|
if (!payload.blockTitle) payload.blockTitle = undefined
|
|
if (!payload.chargeType) payload.chargeType = 'MANUAL'
|
|
if (payload.chargeValue != null) payload.chargeValue = Number(payload.chargeValue)
|
|
const req = payload.scId != null ? updateSurcharge(payload) : addSurcharge(payload)
|
|
req.then(() => {
|
|
proxy.$modal.msgSuccess(payload.scId != null ? '修改成功' : '新增成功')
|
|
visible.value = false
|
|
emit('success')
|
|
}).catch(() => {
|
|
// backend warn(601) handled globally
|
|
})
|
|
})
|
|
}
|
|
|
|
watch(() => form.value.countryCode, (code, prev) => {
|
|
if (suppressCountryWatch.value) return
|
|
if (code === prev) return
|
|
loadBlockOptions(code, false)
|
|
})
|
|
|
|
watch(() => form.value.chargeType, (type, prev) => {
|
|
if (suppressCountryWatch.value || !prev || type === prev) return
|
|
form.value.lengthCm = undefined
|
|
form.value.widthCm = undefined
|
|
form.value.heightCm = undefined
|
|
form.value.overWeightKg = undefined
|
|
form.value.extraWeightKg = undefined
|
|
form.value.postcodeText = undefined
|
|
if (String(type || '').toUpperCase() !== 'ZONE') form.value.blockTitle = undefined
|
|
nextTick(() => formRef.value?.clearValidate())
|
|
})
|
|
|
|
defineExpose({ open })
|
|
</script>
|
|
|
|
<style scoped>
|
|
.surcharge-form :deep(.el-form-item__content) {
|
|
flex: 1;
|
|
min-width: 0;
|
|
}
|
|
.surcharge-form :deep(.el-input),
|
|
.surcharge-form :deep(.el-select),
|
|
.surcharge-form :deep(.el-input-number),
|
|
.surcharge-form :deep(.el-textarea) {
|
|
width: 100%;
|
|
}
|
|
.surcharge-form :deep(.el-input-number .el-input__inner) {
|
|
text-align: left;
|
|
}
|
|
</style>
|