liufumingren cdde7183e4 feat: 快递编码支持自定义 & 快递策略列表显示名称
- 移除 getExpressList 的 systemOnly 参数,快递编码下拉不再限定系统编码 (pendingOrder, exceptionOrder)
- 新增货主快递绑定策略页面 (storeExpressBindingPolicy)
- 策略列表快递编码列改为显示快递名称 (express_name)
- 路由注册 storeExpressBindingPolicy 页面
2026-08-04 08:22:24 +08:00

710 lines
23 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<div class="store-express-binding-policy-container" :style="{ height: containerHeight }">
<scan-box
ref="scanBox"
width="250px"
:tree-props="{ children: 'children', label: 'label' }"
:auto-submit="false"
:preset-tree="presetTree"
:available-fields="availableFieldsRef"
@condition-change="handleConditionChange"
@reset="handleConditionReset"
@condition-clear="handleConditionClear"
@submit="handleSubmit"
/>
<div class="main-content">
<div class="toolbar">
<div class="toolbar-left">
<el-button size="small" type="primary" :loading="loading" @click="loadList">刷新</el-button>
<el-button type="success" size="small" @click="openCreate">新增策略</el-button>
</div>
</div>
<div class="upper-wrapper">
<upper-table
ref="upperTable"
title="快递策略"
default-align="center"
table-id="setting-storeExpressBindingPolicy"
:data="tableData"
:columns="columns"
:table-size="tableSize"
:pagination="pagination"
:show-index="true"
:show-actions="true"
:loading="loading"
:query-time="queryTime"
row-key="id"
@page-change="handlePageChange"
@page-size-change="handlePageSizeChange"
@refresh="loadList"
>
<template #column-is_enabled="{ row }">
<el-tag :type="formatPolicyStatus(row.is_enabled).type" size="small">
{{ formatPolicyStatus(row.is_enabled).text }}
</el-tag>
</template>
<template #column-store_ids="{ row }">
<span>{{ Array.isArray(row.store_names) && row.store_names.length ? row.store_names.join(',') : '不限' }}</span>
</template>
<template #column-weight_type="{ row }">
<span>{{ WEIGHT_TYPE_MAP[row.weight_type] ?? row.weight_type ?? '--' }}</span>
</template>
<template #column-shop_name="{ row }">
<span>{{ Array.isArray(row.shop_name) ? row.shop_name.join(',') || '不限' : (row.shop_name || '不限') }}</span>
</template>
<template #column-regions="{ row }">
<span>{{ Array.isArray(row.regions) ? row.regions.join(',') || '不限' : (row.regions || '不限') }}</span>
</template>
<template #column-exclude_regions="{ row }">
<span>{{ Array.isArray(row.exclude_regions) ? row.exclude_regions.join(',') || '不限' : (row.exclude_regions || '不限') }}</span>
</template>
<template #column-min_weight_g="{ row }">
<span>{{ row.min_weight_g != null ? row.min_weight_g : '不限' }}</span>
</template>
<template #column-max_weight_g="{ row }">
<span>{{ row.max_weight_g != null ? row.max_weight_g : '不限' }}</span>
</template>
<template #column-prohibited_type_id="{ row }">
<span>{{ Array.isArray(row.prohibited_type_names) && row.prohibited_type_names.length ? row.prohibited_type_names.join(',') : '不限' }}</span>
</template>
<template #column-actions="{ row }">
<el-button link type="primary" size="small" @click.stop="openEdit(row)">编辑</el-button>
<el-button link type="danger" size="small" @click.stop="handleDelete(row)">删除</el-button>
</template>
<template #empty>
<div class="empty-tip">暂无策略数据,可调整左侧筛选条件</div>
</template>
</upper-table>
</div>
</div>
<el-dialog
v-model="dialogVisible"
:title="dialogMode === 'create' ? '新增策略' : '编辑策略'"
width="640px"
align-center
@close="resetForm"
>
<el-form ref="formRef" :model="formData" :rules="rules" label-width="100px">
<el-form-item label="策略名称" prop="custom_name">
<el-input v-model="formData.custom_name" placeholder="请输入策略名称" maxlength="100" show-word-limit />
</el-form-item>
<el-form-item label="快递编码" prop="express_code">
<el-select
v-model="formData.express_code"
placeholder="请选择快递"
filterable
style="width: 100%"
>
<el-option
v-for="item in expressOptions"
:key="item.code"
:label="`${item.name} (${item.code})`"
:value="item.code"
/>
</el-select>
</el-form-item>
<el-form-item label="包含店铺名称" prop="shop_name">
<div class="tag-input-wrapper">
<div class="tag-list" v-if="formData.shopNameList.length">
<el-tag
v-for="(item, idx) in formData.shopNameList"
:key="idx"
closable
size="small"
@close="formData.shopNameList.splice(idx, 1)"
>{{ item }}</el-tag>
</div>
<div class="tag-input-row">
<el-input
v-model="shopNameInput"
placeholder="输入店铺名称,回车添加"
maxlength="100"
@keyup.enter="addShopName"
/>
<el-button size="small" @click="addShopName" :disabled="!shopNameInput.trim()">添加</el-button>
</div>
</div>
</el-form-item>
<el-form-item label="限定货主ID" prop="store_ids">
<el-select
v-model="formData.storeIdsList"
placeholder="请选择货主"
multiple
filterable
clearable
style="width: 100%"
>
<el-option
v-for="item in storeOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
<el-form-item label="违禁品类型" prop="prohibited_type_id">
<el-select
v-model="formData.prohibited_type_id"
placeholder="请选择违禁品类型"
multiple
filterable
clearable
style="width: 100%"
>
<el-option
v-for="item in prohibitedOptions"
:key="item.value"
:label="item.label"
:value="item.value"
/>
</el-select>
</el-form-item>
<el-form-item label="重量段类型" prop="weight_type">
<el-select v-model="formData.weight_type" placeholder="请选择重量段类型" style="width: 100%">
<el-option label="常规" :value="0" />
<el-option label="轻货" :value="1" />
<el-option label="重货" :value="2" />
<el-option label="冷链" :value="3" />
</el-select>
</el-form-item>
<el-row :gutter="10">
<el-col :span="12">
<el-form-item label="最小重量(g)" prop="min_weight_g">
<el-input-number
v-model="formData.min_weight_g"
:min="0"
placeholder="不限制"
:controls="false"
style="width: 100%"
/>
</el-form-item>
</el-col>
<el-col :span="12">
<el-form-item label="最大重量(g)" prop="max_weight_g">
<el-input-number
v-model="formData.max_weight_g"
:min="0"
placeholder="不限制"
:controls="false"
style="width: 100%"
/>
</el-form-item>
</el-col>
</el-row>
<el-form-item label="限定地区" prop="regions">
<div class="tag-input-wrapper">
<div class="tag-list" v-if="formData.regionsList.length">
<el-tag
v-for="(item, idx) in formData.regionsList"
:key="idx"
closable
size="small"
@close="formData.regionsList.splice(idx, 1)"
>{{ item }}</el-tag>
</div>
<div class="tag-input-row">
<el-input
v-model="regionInput"
placeholder="输入省/市名,回车添加"
maxlength="50"
@keyup.enter="addRegion"
/>
<el-button size="small" @click="addRegion" :disabled="!regionInput.trim()">添加</el-button>
</div>
</div>
</el-form-item>
<el-form-item label="排除地区" prop="exclude_regions">
<div class="tag-input-wrapper">
<div class="tag-list" v-if="formData.excludeRegionsList.length">
<el-tag
v-for="(item, idx) in formData.excludeRegionsList"
:key="idx"
closable
size="small"
@close="formData.excludeRegionsList.splice(idx, 1)"
>{{ item }}</el-tag>
</div>
<div class="tag-input-row">
<el-input
v-model="excludeRegionInput"
placeholder="输入省/市名,回车添加"
maxlength="50"
@keyup.enter="addExcludeRegion"
/>
<el-button size="small" @click="addExcludeRegion" :disabled="!excludeRegionInput.trim()">添加</el-button>
</div>
</div>
</el-form-item>
<el-form-item prop="priority">
<template #label>
<span>优先级 <el-tooltip content="优先级越大越优先匹配,命中后将不再匹配其他策略" placement="top"><el-icon style="vertical-align: middle; cursor: help;"><QuestionFilled /></el-icon></el-tooltip></span>
</template>
<el-input-number
v-model="formData.priority"
:min="0"
:max="9999"
placeholder="请输入优先级"
style="width: 100%"
/>
</el-form-item>
<el-form-item label="状态" prop="is_enabled">
<el-select v-model="formData.is_enabled" placeholder="请选择状态" style="width: 100%">
<el-option label="启用" :value="1" />
<el-option label="禁用" :value="0" />
</el-select>
</el-form-item>
<el-form-item label="备注" prop="remark">
<el-input
v-model="formData.remark"
type="textarea"
:rows="3"
placeholder="请输入备注(选填)"
maxlength="200"
show-word-limit
/>
</el-form-item>
</el-form>
<template #footer>
<el-button @click="dialogVisible = false">取消</el-button>
<el-button type="primary" :loading="saving" @click="submitForm">保存</el-button>
</template>
</el-dialog>
</div>
</template>
<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import type { FormInstance, FormRules } from 'element-plus'
import ScanBox from '@/components/ScanBox/index.vue'
import UpperTable from '@/components/UpperTable/index.vue'
import {
storeExpressBindingPolicyAdd,
storeExpressBindingPolicyDel,
storeExpressBindingPolicyEdit,
storeExpressBindingPolicyList
} from '@/api/setting.storeExpressBindingPolicy/storeExpressBindingPolicy'
import { expressIndexGetExpressList } from '@/api/setting.express/express'
import { getProhibitedType } from '@/api/stock/stock'
import { loadStores } from '@/api/index/index'
import { useSettingsStore } from '@/stores/modules/settings'
import {
buildPolicyListPayload,
formatPolicyStatus,
normalizeStoreExpressBindingPolicyScanConditionToWhere,
storeExpressBindingPolicyColumns,
storeExpressBindingPolicyScanFields,
WEIGHT_TYPE_MAP,
type StoreExpressBindingPolicyRecord
} from './storeExpressBindingPolicy-config'
const settingsStore = useSettingsStore()
const scanBox = ref<any>(null)
const upperTable = ref<any>(null)
const presetTree = ref<any[]>([])
const availableFieldsRef = reactive(JSON.parse(JSON.stringify(storeExpressBindingPolicyScanFields)))
const shortcutExpanded = computed(() => settingsStore.shortcutExpanded)
const containerHeight = computed(() =>
shortcutExpanded.value ? 'calc(100vh - 150px)' : 'calc(100vh - 100px)'
)
const columns = storeExpressBindingPolicyColumns
const loading = ref(false)
const saving = ref(false)
const tableData = ref<StoreExpressBindingPolicyRecord[]>([])
const expressOptions = ref<{ code: string; name: string }[]>([])
const prohibitedOptions = ref<{ value: number; label: string }[]>([])
const storeOptions = ref<{ value: number; label: string }[]>([])
const queryTime = ref(0)
const tableSize = ref<'small' | 'default' | 'large'>('small')
const currentConditionRaw = ref<any>(null)
const pagination = reactive({ currentPage: 1, pageSize: 20, total: 0 })
const dialogVisible = ref(false)
const dialogMode = ref<'create' | 'edit'>('create')
const formRef = ref<FormInstance>()
const formData = reactive({
id: undefined as number | undefined,
custom_name: '',
express_code: '',
prohibited_type_id: [] as number[],
weight_type: 0 as number,
shopNameList: [] as string[],
storeIdsList: [] as number[],
regionsList: [] as string[],
excludeRegionsList: [] as string[],
min_weight_g: undefined as number | undefined,
max_weight_g: undefined as number | undefined,
priority: 0,
is_enabled: 1 as number,
remark: ''
})
const shopNameInput = ref('')
const regionInput = ref('')
const excludeRegionInput = ref('')
const rules: FormRules = {
custom_name: [{ required: true, message: '请输入自定义名称', trigger: 'blur' }],
express_code: [{ required: true, message: '请输入快递编码', trigger: 'blur' }]
}
const isSuccess = (res: any) => res && [0, 1, 200].includes(res.code)
async function loadList() {
if (loading.value) return
loading.value = true
const start = Date.now()
try {
const where = normalizeStoreExpressBindingPolicyScanConditionToWhere(currentConditionRaw.value)
const res: any = await storeExpressBindingPolicyList(
buildPolicyListPayload({
page: pagination.currentPage,
pageSize: pagination.pageSize,
where
})
)
if (!isSuccess(res)) {
ElMessage.error(res?.message || '获取策略列表失败')
tableData.value = []
pagination.total = 0
return
}
const data = res?.data || {}
tableData.value = data.list || []
pagination.total = Number(data.pagination?.total ?? data.total ?? 0)
} catch {
tableData.value = []
pagination.total = 0
ElMessage.error('获取策略列表失败')
} finally {
queryTime.value = Date.now() - start
loading.value = false
}
}
function handleConditionChange(condition: any) {
currentConditionRaw.value = condition
pagination.currentPage = 1
loadList()
}
function handleSubmit() {
pagination.currentPage = 1
loadList()
}
function handleConditionClear() {
currentConditionRaw.value = null
pagination.currentPage = 1
loadList()
}
function handleConditionReset() {
currentConditionRaw.value = null
pagination.currentPage = 1
if (scanBox.value?.reset) {
scanBox.value.reset()
} else if (scanBox.value?.clear) {
scanBox.value.clear()
}
loadList()
}
function handlePageChange(page: number) {
pagination.currentPage = page
loadList()
}
function handlePageSizeChange(size: number) {
pagination.pageSize = size
pagination.currentPage = 1
loadList()
}
function resetForm() {
formData.id = undefined
formData.custom_name = ''
formData.express_code = ''
formData.prohibited_type_id = []
formData.weight_type = 0
formData.shopNameList = []
formData.storeIdsList = []
formData.regionsList = []
formData.excludeRegionsList = []
formData.min_weight_g = undefined
formData.max_weight_g = undefined
formData.priority = 0
formData.is_enabled = 1
formData.remark = ''
shopNameInput.value = ''
regionInput.value = ''
excludeRegionInput.value = ''
formRef.value?.clearValidate()
}
function addShopName() {
const val = shopNameInput.value.trim()
if (!val) return
if (!formData.shopNameList.includes(val)) {
formData.shopNameList.push(val)
}
shopNameInput.value = ''
}
function addRegion() {
const val = regionInput.value.trim()
if (!val) return
if (!formData.regionsList.includes(val)) {
formData.regionsList.push(val)
}
regionInput.value = ''
}
function addExcludeRegion() {
const val = excludeRegionInput.value.trim()
if (!val) return
if (!formData.excludeRegionsList.includes(val)) {
formData.excludeRegionsList.push(val)
}
excludeRegionInput.value = ''
}
function openCreate() {
dialogMode.value = 'create'
dialogVisible.value = true
resetForm()
}
function openEdit(row: StoreExpressBindingPolicyRecord) {
if (!row.id) return
dialogMode.value = 'edit'
dialogVisible.value = true
formData.id = row.id
formData.custom_name = row.custom_name || ''
formData.express_code = row.express_code || ''
formData.prohibited_type_id = Array.isArray(row.prohibited_type_id) ? row.prohibited_type_id.map((v: any) => Number(v)).filter((v: number) => !isNaN(v)) : []
formData.weight_type = Number(row.weight_type ?? 0)
formData.shopNameList = Array.isArray(row.shop_name) ? row.shop_name.filter((s: any) => s != null && String(s).trim()) : []
formData.storeIdsList = Array.isArray(row.store_ids) ? row.store_ids.map((v: any) => Number(v)).filter((v: number) => !isNaN(v)) : []
formData.regionsList = Array.isArray(row.regions) ? row.regions.filter((s: any) => s != null && String(s).trim()) : []
formData.excludeRegionsList = Array.isArray(row.exclude_regions) ? row.exclude_regions.filter((s: any) => s != null && String(s).trim()) : []
formData.min_weight_g = row.min_weight_g != null ? Number(row.min_weight_g) : undefined
formData.max_weight_g = row.max_weight_g != null ? Number(row.max_weight_g) : undefined
formData.priority = Number(row.priority ?? 0)
formData.is_enabled = Number(row.is_enabled ?? 1)
formData.remark = row.remark || ''
shopNameInput.value = ''
regionInput.value = ''
excludeRegionInput.value = ''
formRef.value?.clearValidate()
}
async function submitForm() {
if (!formRef.value || saving.value) return
const valid = await formRef.value.validate().catch(() => false)
if (!valid) return
saving.value = true
try {
const buildPayload = (base: Record<string, unknown> = {}) => {
const p: Record<string, unknown> = { ...base }
p.custom_name = formData.custom_name.trim()
p.express_code = formData.express_code.trim()
p.prohibited_type_id = formData.prohibited_type_id.length ? formData.prohibited_type_id : undefined
p.weight_type = formData.weight_type
p.shop_name = formData.shopNameList.length ? formData.shopNameList : undefined
p.store_ids = formData.storeIdsList.length ? formData.storeIdsList : undefined
p.regions = formData.regionsList.length ? formData.regionsList : undefined
p.exclude_regions = formData.excludeRegionsList.length ? formData.excludeRegionsList : undefined
p.min_weight_g = formData.min_weight_g
p.max_weight_g = formData.max_weight_g
p.priority = formData.priority
p.is_enabled = formData.is_enabled
p.remark = formData.remark.trim() || undefined
return p
}
if (dialogMode.value === 'create') {
const payload = buildPayload()
const res: any = await storeExpressBindingPolicyAdd(payload)
if (!isSuccess(res)) {
ElMessage.error(res?.message || '新增失败')
return
}
ElMessage.success('新增成功')
} else {
if (!formData.id) return
const payload = buildPayload({ id: formData.id })
const res: any = await storeExpressBindingPolicyEdit(payload)
if (!isSuccess(res)) {
ElMessage.error(res?.message || '更新失败')
return
}
ElMessage.success('更新成功')
}
dialogVisible.value = false
loadList()
} catch {
ElMessage.error('保存失败')
} finally {
saving.value = false
}
}
function handleDelete(row: StoreExpressBindingPolicyRecord) {
if (!row.id) return
const name = row.custom_name || String(row.id)
ElMessageBox.confirm(`确认删除策略「${name}」吗?`, '提示', { type: 'warning' })
.then(async () => {
const res: any = await storeExpressBindingPolicyDel({ id: row.id as number })
if (!isSuccess(res)) {
ElMessage.error(res?.message || '删除失败')
return
}
ElMessage.success('删除成功')
if (tableData.value.length === 1 && pagination.currentPage > 1) {
pagination.currentPage -= 1
}
loadList()
})
.catch(() => undefined)
}
async function loadExpressOptions() {
try {
const res: any = await expressIndexGetExpressList({})
if (isSuccess(res)) {
expressOptions.value = (res?.data || []).map((item: any) => ({
code: item.code,
name: item.name
}))
}
} catch {
expressOptions.value = []
}
}
async function loadProhibitedOptions() {
try {
const res: any = await getProhibitedType()
if (isSuccess(res) && Array.isArray(res?.data)) {
prohibitedOptions.value = res.data
.map((item: any) => {
if (item && typeof item === 'object') {
if (item.value != null && item.label != null) {
return { value: Number(item.value), label: String(item.label) }
}
if (item.id != null && item.name != null) {
return { value: Number(item.id), label: String(item.name) }
}
}
return null
})
.filter((x): x is { value: number; label: string } => x != null)
}
// 同步到 ScanBox 字段选项
const prohibitedField = availableFieldsRef.find((f: any) => f.value === 'prohibited_type_id')
if (prohibitedField) {
prohibitedField.options = prohibitedOptions.value.map((item) => ({
label: item.label,
value: item.value
}))
}
} catch {
prohibitedOptions.value = []
}
}
async function loadStoreOptions() {
try {
const res: any = await loadStores()
if (res && res.stores && Array.isArray(res.stores)) {
storeOptions.value = res.stores.map((item: any) => ({
value: Number(item.value ?? item.id),
label: String(item.label ?? item.name ?? '')
}))
}
} catch {
storeOptions.value = []
}
}
onMounted(() => {
loadList()
loadExpressOptions()
loadProhibitedOptions()
loadStoreOptions()
})
</script>
<style scoped lang="scss">
.store-express-binding-policy-container {
display: flex;
gap: 12px;
padding: 12px;
box-sizing: border-box;
.main-content {
flex: 1;
min-width: 0;
display: flex;
flex-direction: column;
gap: 10px;
}
.toolbar {
display: flex;
align-items: center;
justify-content: space-between;
&-left {
display: flex;
align-items: center;
gap: 8px;
}
}
.upper-wrapper {
flex: 1;
min-height: 0;
}
}
.tag-input-wrapper {
width: 100%;
.tag-list {
display: flex;
flex-wrap: wrap;
gap: 6px;
margin-bottom: 8px;
}
.tag-input-row {
display: flex;
gap: 8px;
.el-input {
flex: 1;
}
}
}
</style>