877 lines
32 KiB
Vue
877 lines
32 KiB
Vue
<!--
|
||
WarehouseSettingsForm — 仓库设置「配置表单」页(表单项页面)
|
||
对接接口:
|
||
- setting.warehouse.getSettings + getFieldMeta:加载当前仓库配置与字段元信息
|
||
- setting.warehouse.saveSettings:保存配置
|
||
该页为表单页形态(不使用 scan-box / upper-table / 明细组件),由 newview 模板以
|
||
viewType='form' 渲染替换默认的列表结构。
|
||
-->
|
||
<template>
|
||
<div class="warehouse-settings-form" v-loading="pageLoading">
|
||
<div class="settings-header">
|
||
<div class="header-text">
|
||
<span class="title">仓库设置</span>
|
||
<span v-if="warehouseId != null" class="sub">当前仓库 ID:{{ warehouseId }}</span>
|
||
</div>
|
||
<div class="header-actions">
|
||
<el-button :loading="pageLoading" @click="loadAll">刷新</el-button>
|
||
<el-button type="primary" :loading="saving" @click="handleSave">保存</el-button>
|
||
</div>
|
||
</div>
|
||
|
||
<el-alert
|
||
v-if="loadError"
|
||
type="error"
|
||
:closable="false"
|
||
show-icon
|
||
class="mb-16"
|
||
:title="loadError"
|
||
/>
|
||
|
||
<div class="settings-body">
|
||
<el-form label-width="200px" label-position="right" class="settings-form">
|
||
<template v-if="displayFields.length">
|
||
<el-form-item
|
||
v-for="field in displayFields"
|
||
:key="field.key"
|
||
:required="field.required"
|
||
>
|
||
<template #label>
|
||
<span :title="field.description">{{ field.label }}</span>
|
||
</template>
|
||
|
||
<div class="field-block">
|
||
<el-switch
|
||
v-if="field.type === 'boolean'"
|
||
v-model="subjectConfig[field.key]"
|
||
:active-value="true"
|
||
:inactive-value="false"
|
||
/>
|
||
<div v-else-if="field.type === 'string_array'" class="string-array-editor">
|
||
<div v-if="getStringArrayFieldItems(field.key).length" class="tag-list">
|
||
<el-tag
|
||
v-for="(item, idx) in getStringArrayFieldItems(field.key)"
|
||
:key="`${field.key}-${idx}`"
|
||
closable
|
||
size="small"
|
||
@close="removeStringArrayFieldItem(field.key, idx)"
|
||
>{{ item }}</el-tag>
|
||
</div>
|
||
<div class="tag-input-row">
|
||
<el-input
|
||
v-model="stringArrayInputs[field.key]"
|
||
:placeholder="`输入关键字,回车添加`"
|
||
maxlength="100"
|
||
@keyup.enter="addStringArrayFieldItem(field.key)"
|
||
/>
|
||
<el-button size="small" :disabled="!stringArrayInputs[field.key]?.trim()" @click="addStringArrayFieldItem(field.key)">添加</el-button>
|
||
</div>
|
||
</div>
|
||
<div v-else-if="field.type === 'array'" class="array-editor">
|
||
<div v-if="field.itemFields && field.itemFields.length" class="array-table">
|
||
<div class="array-table__head">
|
||
<div v-for="itemField in field.itemFields" :key="`${field.key}-${itemField.key}-head`" class="array-table__cell">
|
||
{{ itemField.label }}
|
||
</div>
|
||
<div class="array-table__op">操作</div>
|
||
</div>
|
||
|
||
<div
|
||
v-for="(item, idx) in getArrayFieldItems(field.key)"
|
||
:key="`${field.key}-${idx}`"
|
||
class="array-table__row"
|
||
>
|
||
<div v-for="itemField in field.itemFields" :key="`${field.key}-${idx}-${itemField.key}`" class="array-table__cell">
|
||
<el-switch
|
||
v-if="itemField.type === 'boolean'"
|
||
:model-value="getArrayObjectFieldValue(item, itemField.key) as boolean"
|
||
:active-value="true"
|
||
:inactive-value="false"
|
||
@update:model-value="onArrayObjectItemInput(field, idx, itemField.key, $event)"
|
||
/>
|
||
<el-select
|
||
v-else-if="itemField.type === 'select'"
|
||
:model-value="getArrayObjectFieldValue(item, itemField.key)"
|
||
clearable
|
||
filterable
|
||
placeholder="请选择"
|
||
@update:model-value="onArrayObjectItemInput(field, idx, itemField.key, $event)"
|
||
>
|
||
<el-option
|
||
v-for="opt in itemField.options || []"
|
||
:key="`${field.key}-${idx}-${itemField.key}-${String(opt.value)}`"
|
||
:label="opt.label"
|
||
:value="opt.value"
|
||
/>
|
||
</el-select>
|
||
<el-color-picker
|
||
v-else-if="itemField.type === 'color'"
|
||
:model-value="String(getArrayObjectFieldValue(item, itemField.key) ?? '')"
|
||
show-alpha
|
||
:predefine="PREDEFINED_COLORS"
|
||
@update:model-value="onArrayObjectItemInput(field, idx, itemField.key, $event)"
|
||
/>
|
||
<el-input-number
|
||
v-else-if="itemField.type === 'number'"
|
||
:model-value="getArrayObjectFieldValue(item, itemField.key) as number | undefined"
|
||
:min="itemField.min"
|
||
:max="itemField.max"
|
||
:step="itemField.step ?? 1"
|
||
:precision="itemField.precision"
|
||
controls-position="right"
|
||
placeholder="留空"
|
||
@update:model-value="onArrayObjectItemInput(field, idx, itemField.key, $event)"
|
||
/>
|
||
<el-input
|
||
v-else
|
||
:model-value="String(getArrayObjectFieldValue(item, itemField.key) ?? '')"
|
||
clearable
|
||
placeholder="请输入"
|
||
:maxlength="itemField.maxLength"
|
||
:show-word-limit="Boolean(itemField.maxLength)"
|
||
@update:model-value="onArrayObjectItemInput(field, idx, itemField.key, $event)"
|
||
/>
|
||
</div>
|
||
<div class="array-table__op">
|
||
<el-button type="danger" plain @click="removeArrayFieldItem(field.key, idx)">删除</el-button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div v-else>
|
||
<div
|
||
v-for="(item, idx) in getArrayFieldItems(field.key)"
|
||
:key="`${field.key}-${idx}`"
|
||
class="array-editor__row"
|
||
>
|
||
<el-input
|
||
:model-value="formatArrayItemText(item)"
|
||
type="textarea"
|
||
:rows="2"
|
||
class="field-control"
|
||
placeholder="可输入文本,或输入 JSON(对象/数组)"
|
||
@update:model-value="onArrayItemInput(field.key, idx, $event)"
|
||
/>
|
||
<el-button type="danger" plain @click="removeArrayFieldItem(field.key, idx)">删除</el-button>
|
||
</div>
|
||
</div>
|
||
<el-button type="primary" plain @click="addArrayFieldItem(field)">新增一项</el-button>
|
||
</div>
|
||
<el-select
|
||
v-else-if="field.type === 'select'"
|
||
v-model="subjectConfig[field.key]"
|
||
clearable
|
||
filterable
|
||
class="field-control"
|
||
placeholder="请选择"
|
||
>
|
||
<el-option
|
||
v-for="opt in field.options || []"
|
||
:key="String(opt.value)"
|
||
:label="opt.label"
|
||
:value="opt.value"
|
||
/>
|
||
</el-select>
|
||
<el-color-picker
|
||
v-else-if="field.type === 'color'"
|
||
v-model="subjectConfig[field.key] as string"
|
||
class="field-color-control"
|
||
show-alpha
|
||
:predefine="PREDEFINED_COLORS"
|
||
/>
|
||
<el-input-number
|
||
v-else-if="field.type === 'number'"
|
||
v-model="subjectConfig[field.key] as number | undefined"
|
||
class="field-control"
|
||
:min="field.min"
|
||
:max="field.max"
|
||
:step="field.step ?? 1"
|
||
:precision="field.precision"
|
||
controls-position="right"
|
||
placeholder="未设置则留空"
|
||
/>
|
||
<div v-else-if="field.type === 'object' && field.itemFields && field.itemFields.length" class="object-editor">
|
||
<div
|
||
v-for="itemKey in field.itemFields"
|
||
:key="`${field.key}-${itemKey.key}`"
|
||
class="object-editor__row"
|
||
>
|
||
<span class="object-editor__label">{{ itemKey.label }}</span>
|
||
<el-switch
|
||
v-if="itemKey.type === 'boolean'"
|
||
:model-value="getObjectFieldValue(field.key, itemKey.key) as boolean"
|
||
:active-value="true"
|
||
:inactive-value="false"
|
||
@update:model-value="onObjectFieldChange(field.key, itemKey.key, $event)"
|
||
/>
|
||
<el-input
|
||
v-else
|
||
:model-value="String(getObjectFieldValue(field.key, itemKey.key) ?? '')"
|
||
size="small"
|
||
style="width:200px"
|
||
@update:model-value="onObjectFieldChange(field.key, itemKey.key, $event)"
|
||
/>
|
||
<span v-if="itemKey.description" class="object-editor__desc">{{ itemKey.description }}</span>
|
||
</div>
|
||
</div>
|
||
<SpacePicker
|
||
v-else-if="field.type === 'location'"
|
||
:model-value="locationFieldValue(field.key)"
|
||
class="field-control"
|
||
placeholder="请选择库位"
|
||
@update:model-value="onLocationFieldChange(field.key, $event)"
|
||
/>
|
||
<el-input
|
||
v-else
|
||
v-model="subjectConfig[field.key] as string"
|
||
class="field-control"
|
||
clearable
|
||
placeholder="请输入"
|
||
:maxlength="field.maxLength"
|
||
:show-word-limit="Boolean(field.maxLength)"
|
||
/>
|
||
<div v-if="field.description" class="field-desc">{{ field.description }}</div>
|
||
</div>
|
||
</el-form-item>
|
||
</template>
|
||
<el-empty v-else description="暂无可编辑字段,请检查接口返回" />
|
||
</el-form>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { computed, reactive, ref } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import SpacePicker from '@/components/SpacePicker/index.vue'
|
||
import {
|
||
settingWarehouseGetSettings,
|
||
settingWarehouseGetFieldMeta,
|
||
settingWarehouseSaveSettings,
|
||
unwrapSettingWarehousePayload,
|
||
buildSaveSettingsRequest,
|
||
normalizeGetSettingsPayload,
|
||
normalizeSaveSettingsPayload,
|
||
type SettingWarehouseSettingsPayload
|
||
} from '@/api/setting.warehouse/settingwarehouse'
|
||
|
||
defineOptions({ name: 'WarehouseSettingsForm' })
|
||
|
||
const okCode = (code: unknown) => code === 1 || code === 200 || code === 0
|
||
const PREDEFINED_COLORS = ['#ff4d4f', '#ff7a45', '#faad14', '#fadb14', '#95de64', '#52c41a']
|
||
|
||
type NormalizedField = {
|
||
key: string
|
||
label: string
|
||
type: 'number' | 'string' | 'boolean' | 'select' | 'array' | 'string_array' | 'color' | 'object' | 'location'
|
||
description?: string
|
||
required?: boolean
|
||
min?: number
|
||
max?: number
|
||
step?: number
|
||
precision?: number
|
||
maxLength?: number
|
||
default?: unknown
|
||
defaultValue?: unknown
|
||
itemFields?: NormalizedField[]
|
||
options?: { label: string; value: string | number | boolean }[]
|
||
}
|
||
|
||
const FALLBACK_FIELDS: NormalizedField[] = [
|
||
{ key: 'max_wave_quantity', label: '波次最大订单数', type: 'number', min: 1, precision: 0, description: '自动加团等场景会跳过订单数已达该上限的波次(与波次模块逻辑一致)。' },
|
||
{ key: 'min_group_order_fetch_quantity', label: '团购判定最小同组订单数', type: 'number', min: 1, precision: 0, description: '抓取团购/散单分组阈值,未配置时后端常用默认值 13。' },
|
||
{ key: 'max_notice_webhook_count', label: 'Webhook 通知上限', type: 'number', min: 0, precision: 0 },
|
||
{ key: 'default_express_code', label: '默认快递编码', type: 'string' },
|
||
{ key: 'default_platform_code', label: '默认平台编码', type: 'string' },
|
||
{ key: 'group_wave_prefix', label: '团购波次号前缀', type: 'string' },
|
||
{ key: 'enable_external_express_code', label: '启用外部快递编码', type: 'boolean', description: '开启后允许按外部系统传入的快递编码进行处理(如无匹配仍以业务校验为准);默认关闭。' },
|
||
{
|
||
key: 'cutoff_order_enabled',
|
||
label: '启用截单',
|
||
type: 'select',
|
||
default: 1,
|
||
options: [
|
||
{ label: '关闭', value: 0 },
|
||
{ label: '开启', value: 1 }
|
||
],
|
||
description: '启用后订单创建时间早于开始自动分配时间或超过截单时间(开始时间晚于截单时间时截单时间视为次日),不再自动分配快递和储位、不再自动加团;默认开启'
|
||
},
|
||
{ key: 'allocate_start_time', label: '开始自动分配时间', type: 'string', maxLength: 5, default: '08:00', description: '格式 HH:mm,默认 08:00,范围 00:00-23:59;早于该时间的订单与超过截单时间行为一致,不自动分配快递和储位、不自动加团' },
|
||
{ key: 'cutoff_order_time', label: '截单时间', type: 'string', maxLength: 5, default: '18:00', description: '格式 HH:mm,默认 18:00,范围 00:00-23:59;作为最后允许自动分配的时间,开始时间晚于截单时间时截单时间视为次日(跨天窗口)' },
|
||
{ key: 'sender_name', label: '默认发件人姓名', type: 'string', maxLength: 50 },
|
||
{ key: 'sender_tel', label: '默认发件人联系电话', type: 'string', maxLength: 20 },
|
||
{ key: 'province_name', label: '默认发件省份', type: 'string', maxLength: 50 },
|
||
{ key: 'city_name', label: '默认发件城市', type: 'string', maxLength: 50 },
|
||
{ key: 'district_name', label: '默认发件区/县', type: 'string', maxLength: 50 },
|
||
{ key: 'town_name', label: '默认发件镇/街道', type: 'string', maxLength: 50 },
|
||
{ key: 'address', label: '默认发件详细地址', type: 'string', maxLength: 200 },
|
||
{ key: 'receiver_address', label: '仓库收货地址', type: 'string', maxLength: 500, description: '退货/退件收件地址,一行文本' },
|
||
{
|
||
key: 'auto_outbound_rules',
|
||
label: '自动出库规则',
|
||
type: 'object',
|
||
description: '全部条件满足时自动标记待出库。',
|
||
itemFields: [
|
||
{ key: 'enabled', label: '启用自动出库', type: 'boolean', description: '默认关闭' },
|
||
{ key: 'require_recheck', label: '要求已复核', type: 'boolean', description: 'recheck_status=fin,默认开启' },
|
||
{ key: 'require_pack', label: '要求已打包', type: 'boolean', description: 'pack_status=fin,默认开启' },
|
||
{ key: 'require_weigh', label: '要求已称重', type: 'boolean', description: 'weighing_weight>0,默认开启' },
|
||
{ key: 'require_sync', label: '要求已同步', type: 'boolean', description: 'sync_status=fin,默认开启' },
|
||
{ key: 'require_print', label: '要求已打印', type: 'boolean', description: 'print_status=fin,默认开启' },
|
||
{ key: 'require_pick', label: '要求已拣货', type: 'boolean', description: 'pick_status=fin,默认开启' }
|
||
]
|
||
},
|
||
{
|
||
key: 'auto_allocate_express_exclude_remarks',
|
||
label: '自动分配快递排除备注',
|
||
type: 'string_array',
|
||
default: [],
|
||
required: false,
|
||
description: '订单备注命中任一关键字时,跳过自动分配快递,默认 [](不排除任何订单)'
|
||
},
|
||
{
|
||
key: 'default_location_id',
|
||
label: '默认库位ID',
|
||
type: 'number',
|
||
min: 0,
|
||
precision: 0,
|
||
description: '默认库位(库位ID,未配置时为 null;须属于当前仓库且未删除、已启用)'
|
||
}
|
||
]
|
||
|
||
function pickStr(obj: Record<string, unknown>, keys: string[]): string | undefined {
|
||
for (const k of keys) {
|
||
const v = obj[k]
|
||
if (v !== undefined && v !== null && String(v).trim() !== '') return String(v)
|
||
}
|
||
return undefined
|
||
}
|
||
|
||
function parseOptions(raw: unknown): { label: string; value: string | number | boolean }[] {
|
||
if (!Array.isArray(raw)) return []
|
||
return raw.map((item) => {
|
||
if (item !== null && typeof item === 'object') {
|
||
const o = item as Record<string, unknown>
|
||
const value = o.value ?? o.key ?? o.id
|
||
const label = String(o.label ?? o.text ?? o.name ?? value ?? '')
|
||
return { label, value: value as string | number | boolean }
|
||
}
|
||
return { label: String(item), value: item as string | number | boolean }
|
||
})
|
||
}
|
||
|
||
function normalizeFieldMetaItem(raw: Record<string, unknown>): NormalizedField | null {
|
||
const key = pickStr(raw, ['key', 'field', 'name', 'prop', 'id'])
|
||
if (!key) return null
|
||
const label = pickStr(raw, ['label', 'name', 'title', 'displayName']) || key
|
||
const description = pickStr(raw, ['description', 'desc', 'help', 'remark'])
|
||
const required = Boolean(raw.required ?? raw.is_required)
|
||
|
||
const rawType = String(pickStr(raw, ['type', 'valueType', 'component', 'inputType']) || 'string').toLowerCase()
|
||
const options = parseOptions(raw.options ?? raw.enum)
|
||
let type: NormalizedField['type'] = 'string'
|
||
if (rawType.includes('int') || rawType.includes('number') || rawType === 'float' || rawType === 'decimal') {
|
||
type = 'number'
|
||
} else if (rawType.includes('bool') || rawType === 'switch' || rawType === 'flag') {
|
||
type = 'boolean'
|
||
} else if (rawType.includes('color')) {
|
||
type = 'color'
|
||
} else if (rawType.includes('array')) {
|
||
type = rawType === 'string_array' ? 'string_array' : 'array'
|
||
} else if (rawType.includes('object')) {
|
||
type = 'object'
|
||
} else if (rawType.includes('location')) {
|
||
type = 'location'
|
||
} else if (rawType === 'select' || rawType === 'enum' || options.length > 0) {
|
||
type = 'select'
|
||
}
|
||
|
||
const toNumber = (v: unknown) => {
|
||
const n = typeof v === 'number' ? v : Number(v)
|
||
return Number.isFinite(n) ? n : undefined
|
||
}
|
||
const min = toNumber(raw.min)
|
||
const max = toNumber(raw.max)
|
||
const step = toNumber(raw.step)
|
||
const precision = toNumber(raw.precision)
|
||
const maxLength = toNumber(raw.maxLength ?? raw.max_length)
|
||
const defaultValue = raw.default
|
||
const itemFields = (type === 'array' || type === 'object') ? normalizeArrayItemFields(raw.item_keys ?? raw.item_key) : undefined
|
||
|
||
return {
|
||
key, label, type, description, required, min, max, step, precision, maxLength,
|
||
defaultValue, itemFields,
|
||
options: options.length ? options : undefined
|
||
}
|
||
}
|
||
|
||
function normalizeArrayItemFields(raw: unknown): NormalizedField[] {
|
||
if (!raw || typeof raw !== 'object') return []
|
||
if (Array.isArray(raw)) {
|
||
return raw
|
||
.map((item) => (item && typeof item === 'object' ? normalizeFieldMetaItem(item as Record<string, unknown>) : null))
|
||
.filter((x): x is NormalizedField => !!x)
|
||
.filter((x) => x.type !== 'array')
|
||
}
|
||
const obj = raw as Record<string, unknown>
|
||
const out: NormalizedField[] = []
|
||
for (const [k, v] of Object.entries(obj)) {
|
||
if (!v || typeof v !== 'object' || Array.isArray(v)) continue
|
||
const candidate = { key: k, ...(v as Record<string, unknown>) }
|
||
const normalized = normalizeFieldMetaItem(candidate)
|
||
if (normalized && normalized.type !== 'array') out.push(normalized)
|
||
}
|
||
return out
|
||
}
|
||
|
||
function normalizeFieldMetaList(list: unknown): NormalizedField[] {
|
||
if (!Array.isArray(list)) return []
|
||
const out: NormalizedField[] = []
|
||
for (const item of list) {
|
||
if (item && typeof item === 'object') {
|
||
const n = normalizeFieldMetaItem(item as Record<string, unknown>)
|
||
if (n) out.push(n)
|
||
}
|
||
}
|
||
return out
|
||
}
|
||
|
||
function cloneSubjectConfig(src: unknown): Record<string, unknown> {
|
||
if (src && typeof src === 'object' && !Array.isArray(src)) return JSON.parse(JSON.stringify(src))
|
||
return {}
|
||
}
|
||
|
||
function cloneValue<T>(value: T): T {
|
||
if (value === null || value === undefined) return value
|
||
if (typeof value === 'object') {
|
||
try { return JSON.parse(JSON.stringify(value)) as T } catch { return value }
|
||
}
|
||
return value
|
||
}
|
||
|
||
function ensureFieldDefaults(target: Record<string, unknown>, fields: NormalizedField[]) {
|
||
for (const f of fields) {
|
||
if (f.type === 'object' && f.itemFields?.length) {
|
||
const current = target[f.key]
|
||
const obj = current && typeof current === 'object' && !Array.isArray(current)
|
||
? { ...(current as Record<string, unknown>) } : ({} as Record<string, unknown>)
|
||
for (const sub of f.itemFields) {
|
||
if (!(sub.key in obj)) {
|
||
if (sub.type === 'boolean') obj[sub.key] = sub.defaultValue === true
|
||
else if (sub.defaultValue !== undefined) obj[sub.key] = cloneValue(sub.defaultValue)
|
||
}
|
||
}
|
||
target[f.key] = obj
|
||
continue
|
||
}
|
||
if (!(f.key in target)) {
|
||
if (f.defaultValue !== undefined) target[f.key] = cloneValue(f.defaultValue)
|
||
else if (f.type === 'boolean') target[f.key] = false
|
||
else if (f.type === 'array' || f.type === 'string_array') target[f.key] = []
|
||
else if (f.type === 'object') target[f.key] = f.defaultValue !== undefined ? cloneValue(f.defaultValue) : {}
|
||
else if (f.type === 'number' || f.type === 'select' || f.type === 'location') { /* 不默认写入 */ }
|
||
else target[f.key] = ''
|
||
}
|
||
}
|
||
}
|
||
|
||
function getArrayFieldItems(key: string): unknown[] {
|
||
const current = subjectConfig[key]
|
||
if (Array.isArray(current)) return current
|
||
if (current === undefined || current === null || current === '') {
|
||
subjectConfig[key] = []
|
||
return subjectConfig[key] as unknown[]
|
||
}
|
||
subjectConfig[key] = [current]
|
||
return subjectConfig[key] as unknown[]
|
||
}
|
||
|
||
function parseArrayItemText(text: string): unknown {
|
||
const trimmed = text.trim()
|
||
if (!trimmed) return ''
|
||
try { return JSON.parse(trimmed) } catch { return text }
|
||
}
|
||
|
||
function formatArrayItemText(value: unknown): string {
|
||
if (value === null || value === undefined) return ''
|
||
if (typeof value === 'string') return value
|
||
try { return JSON.stringify(value) } catch { return String(value) }
|
||
}
|
||
|
||
function updateArrayFieldItem(key: string, index: number, text: string) {
|
||
const list = [...getArrayFieldItems(key)]
|
||
list[index] = parseArrayItemText(text)
|
||
subjectConfig[key] = list
|
||
}
|
||
|
||
function onArrayItemInput(key: string, index: number, val: string | number) {
|
||
updateArrayFieldItem(key, index, String(val ?? ''))
|
||
}
|
||
|
||
function addArrayFieldItem(field: NormalizedField) {
|
||
const list = [...getArrayFieldItems(field.key)]
|
||
let next: unknown = ''
|
||
if (field.itemFields && field.itemFields.length) next = buildArrayObjectDefault(field.itemFields)
|
||
else if (Array.isArray(field.defaultValue) && field.defaultValue.length > 0) next = cloneValue(field.defaultValue[0])
|
||
list.push(next)
|
||
subjectConfig[field.key] = list
|
||
}
|
||
|
||
function removeArrayFieldItem(key: string, index: number) {
|
||
const list = [...getArrayFieldItems(key)]
|
||
list.splice(index, 1)
|
||
subjectConfig[key] = list
|
||
}
|
||
|
||
function getStringArrayFieldItems(key: string): string[] {
|
||
const current = subjectConfig[key]
|
||
if (Array.isArray(current)) return current.filter((v): v is string => typeof v === 'string' && v.trim() !== '')
|
||
subjectConfig[key] = []
|
||
return []
|
||
}
|
||
|
||
function addStringArrayFieldItem(key: string) {
|
||
const input = (stringArrayInputs[key] || '').trim()
|
||
if (!input) return
|
||
const list = [...getStringArrayFieldItems(key)]
|
||
if (!list.includes(input)) {
|
||
list.push(input)
|
||
subjectConfig[key] = list
|
||
}
|
||
stringArrayInputs[key] = ''
|
||
}
|
||
|
||
function removeStringArrayFieldItem(key: string, index: number) {
|
||
const list = [...getStringArrayFieldItems(key)]
|
||
list.splice(index, 1)
|
||
subjectConfig[key] = list
|
||
}
|
||
|
||
function buildArrayObjectDefault(itemFields: NormalizedField[]): Record<string, unknown> {
|
||
const out: Record<string, unknown> = {}
|
||
for (const f of itemFields) {
|
||
if (f.defaultValue !== undefined) out[f.key] = cloneValue(f.defaultValue)
|
||
else if (f.type === 'boolean') out[f.key] = false
|
||
else if (f.type === 'number') out[f.key] = undefined
|
||
else out[f.key] = ''
|
||
}
|
||
return out
|
||
}
|
||
|
||
function getArrayObjectFieldValue(item: unknown, childKey: string): unknown {
|
||
if (!item || typeof item !== 'object' || Array.isArray(item)) return undefined
|
||
return (item as Record<string, unknown>)[childKey]
|
||
}
|
||
|
||
function onArrayObjectItemInput(field: NormalizedField, index: number, childKey: string, value: string | number | boolean | null | undefined) {
|
||
const list = [...getArrayFieldItems(field.key)]
|
||
const current = list[index]
|
||
const row: Record<string, unknown> = current && typeof current === 'object' && !Array.isArray(current) ? { ...(current as Record<string, unknown>) } : {}
|
||
row[childKey] = value
|
||
list[index] = row
|
||
subjectConfig[field.key] = list
|
||
}
|
||
|
||
function getObjectFieldValue(fieldKey: string, subKey: string): unknown {
|
||
const obj = subjectConfig[fieldKey]
|
||
if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return undefined
|
||
return (obj as Record<string, unknown>)[subKey]
|
||
}
|
||
|
||
function onObjectFieldChange(fieldKey: string, subKey: string, value: unknown) {
|
||
const current = subjectConfig[fieldKey]
|
||
const obj: Record<string, unknown> = current && typeof current === 'object' && !Array.isArray(current) ? { ...(current as Record<string, unknown>) } : {}
|
||
obj[subKey] = value
|
||
subjectConfig[fieldKey] = obj
|
||
}
|
||
|
||
function locationFieldValue(key: string): number | string | null {
|
||
const value = subjectConfig[key]
|
||
if (value === null || value === undefined || value === '') return null
|
||
return value as number | string
|
||
}
|
||
|
||
function onLocationFieldChange(key: string, value: number | string | null) {
|
||
subjectConfig[key] = value
|
||
}
|
||
|
||
function applySettingsPayload(settingsRes: SettingWarehouseSettingsPayload | Record<string, unknown>) {
|
||
const normalized = normalizeGetSettingsPayload(settingsRes as Record<string, unknown>)
|
||
const wid = normalized.warehouse_id
|
||
warehouseId.value = typeof wid === 'number' && wid > 0 ? wid : null
|
||
const cfg = cloneSubjectConfig(normalized.subject_config)
|
||
Object.keys(subjectConfig).forEach((k) => delete subjectConfig[k])
|
||
Object.assign(subjectConfig, cfg)
|
||
ensureFieldDefaults(subjectConfig, displayFields.value)
|
||
if (warehouseId.value != null) cacheWarehouseId.value = warehouseId.value
|
||
}
|
||
|
||
const pageLoading = ref(false)
|
||
const saving = ref(false)
|
||
const loadError = ref('')
|
||
const warehouseId = ref<number | null>(null)
|
||
const subjectConfig = reactive<Record<string, unknown>>({})
|
||
const normalizedFields = ref<NormalizedField[]>([])
|
||
const useMetaForm = ref(true)
|
||
const cacheWarehouseId = ref<number | undefined>(undefined)
|
||
const stringArrayInputs = reactive<Record<string, string>>({})
|
||
|
||
const displayFields = computed(() =>
|
||
useMetaForm.value && normalizedFields.value.length ? normalizedFields.value : FALLBACK_FIELDS
|
||
)
|
||
|
||
async function loadAll() {
|
||
loadError.value = ''
|
||
pageLoading.value = true
|
||
try {
|
||
const [settingsRes, metaRes] = await Promise.all([
|
||
settingWarehouseGetSettings(),
|
||
settingWarehouseGetFieldMeta()
|
||
])
|
||
if (!settingsRes || !okCode(settingsRes.code)) {
|
||
throw new Error(settingsRes?.message || '获取仓库设置失败(getSettings)')
|
||
}
|
||
const settingsPayload = normalizeGetSettingsPayload(unwrapSettingWarehousePayload(settingsRes as Record<string, unknown>))
|
||
applySettingsPayload(settingsPayload)
|
||
const metaPayload = unwrapSettingWarehousePayload(metaRes as Record<string, unknown>)
|
||
const metaList = metaRes && okCode(metaRes.code) ? metaPayload.list : null
|
||
const parsed = normalizeFieldMetaList(metaList)
|
||
normalizedFields.value = parsed
|
||
useMetaForm.value = parsed.length > 0
|
||
ensureFieldDefaults(subjectConfig, displayFields.value)
|
||
if (metaRes && !okCode(metaRes.code)) {
|
||
ElMessage.warning(metaRes.message || '字段元信息获取失败,已使用默认表单字段')
|
||
}
|
||
} catch (e: unknown) {
|
||
const msg = e instanceof Error ? e.message : '加载失败'
|
||
loadError.value = msg
|
||
ElMessage.error(msg)
|
||
} finally {
|
||
pageLoading.value = false
|
||
}
|
||
}
|
||
|
||
async function handleSave() {
|
||
const payloadConfig = { ...subjectConfig }
|
||
saving.value = true
|
||
try {
|
||
const body = buildSaveSettingsRequest({ subject_config: payloadConfig, cache_warehouse_id: cacheWarehouseId.value })
|
||
const res = await settingWarehouseSaveSettings(body)
|
||
if (!res || !okCode(res.code)) {
|
||
throw new Error(res?.message || '保存失败')
|
||
}
|
||
ElMessage.success(res.message || '保存成功')
|
||
const savePayload = normalizeSaveSettingsPayload(unwrapSettingWarehousePayload(res as Record<string, unknown>))
|
||
applySettingsPayload(savePayload)
|
||
const cleared = savePayload.cache_cleared_warehouse_id
|
||
if (cleared != null) ElMessage.info(`已清理仓库缓存:${cleared}`)
|
||
} catch (e: unknown) {
|
||
ElMessage.error(e instanceof Error ? e.message : '保存失败')
|
||
} finally {
|
||
saving.value = false
|
||
}
|
||
}
|
||
|
||
loadAll()
|
||
</script>
|
||
|
||
<style lang="scss" scoped>
|
||
.warehouse-settings-form {
|
||
padding: 12px 16px 24px;
|
||
box-sizing: border-box;
|
||
background: #fff;
|
||
}
|
||
|
||
.settings-header {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 12px;
|
||
flex-wrap: wrap;
|
||
margin-bottom: 12px;
|
||
}
|
||
|
||
.header-text {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 4px;
|
||
}
|
||
|
||
.title {
|
||
font-size: 16px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
}
|
||
|
||
.sub {
|
||
font-size: 13px;
|
||
color: #909399;
|
||
}
|
||
|
||
.header-actions {
|
||
display: flex;
|
||
gap: 8px;
|
||
}
|
||
|
||
.mb-16 {
|
||
margin-bottom: 16px;
|
||
}
|
||
|
||
.settings-body {
|
||
flex: 1;
|
||
min-height: 0;
|
||
}
|
||
|
||
.settings-form {
|
||
max-width: 720px;
|
||
}
|
||
|
||
.field-block {
|
||
width: 100%;
|
||
}
|
||
|
||
.array-editor {
|
||
width: 100%;
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 8px;
|
||
max-width: 560px;
|
||
|
||
.array-table {
|
||
border: 1px solid #ebeef5;
|
||
border-radius: 4px;
|
||
overflow-x: auto;
|
||
overflow-y: hidden;
|
||
|
||
&__head,
|
||
&__row {
|
||
display: flex;
|
||
flex-wrap: nowrap;
|
||
border-bottom: 1px solid #ebeef5;
|
||
min-width: max-content;
|
||
}
|
||
|
||
&__head {
|
||
background: #f8fafc;
|
||
font-size: 12px;
|
||
color: #606266;
|
||
font-weight: 600;
|
||
}
|
||
|
||
&__row:last-child {
|
||
border-bottom: none;
|
||
}
|
||
|
||
&__cell,
|
||
&__op {
|
||
padding: 8px;
|
||
border-right: 1px solid #ebeef5;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
&__cell {
|
||
flex: 0 0 140px;
|
||
min-width: 140px;
|
||
}
|
||
|
||
&__cell:last-child,
|
||
&__op:last-child {
|
||
border-right: none;
|
||
}
|
||
|
||
&__op {
|
||
flex: 0 0 72px;
|
||
min-width: 72px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
|
||
:deep(.el-input),
|
||
:deep(.el-select),
|
||
:deep(.el-input-number),
|
||
:deep(.el-color-picker) {
|
||
width: 100%;
|
||
}
|
||
}
|
||
|
||
&__row {
|
||
display: flex;
|
||
gap: 8px;
|
||
align-items: flex-start;
|
||
|
||
.el-button {
|
||
flex-shrink: 0;
|
||
margin-top: 2px;
|
||
}
|
||
}
|
||
}
|
||
|
||
.string-array-editor {
|
||
width: 100%;
|
||
max-width: 560px;
|
||
|
||
.tag-list {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 6px;
|
||
margin-bottom: 8px;
|
||
}
|
||
|
||
.tag-input-row {
|
||
display: flex;
|
||
gap: 8px;
|
||
align-items: center;
|
||
}
|
||
}
|
||
|
||
.object-editor {
|
||
width: 100%;
|
||
max-width: 560px;
|
||
|
||
&__row {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
margin-bottom: 8px;
|
||
flex-wrap: wrap;
|
||
}
|
||
|
||
&__label {
|
||
flex: 0 0 120px;
|
||
font-size: 13px;
|
||
color: #606266;
|
||
}
|
||
|
||
&__desc {
|
||
font-size: 12px;
|
||
color: #909399;
|
||
}
|
||
}
|
||
|
||
.field-control {
|
||
width: 100%;
|
||
max-width: 420px;
|
||
}
|
||
|
||
.field-color-control {
|
||
max-width: 420px;
|
||
}
|
||
|
||
.field-desc {
|
||
margin-top: 6px;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
line-height: 1.45;
|
||
max-width: 520px;
|
||
|
||
code {
|
||
font-size: 11px;
|
||
padding: 0 4px;
|
||
background: #f4f4f5;
|
||
border-radius: 3px;
|
||
}
|
||
}
|
||
|
||
</style>
|