feat(customer): 新增客户相关配卷和财务信息查询接口 fix(base.vue): 修复发货单时间条件显示问题 refactor(CustomerEdit): 替换地址选择组件为普通输入框 feat(CoilSelector): 增加入场卷号查询条件并调整对话框宽度 style(OrderEdit): 调整客户名称和销售员选择框宽度 refactor(ChinaAreaSelect): 优化地址解析逻辑并支持空对象处理 feat(FileUpload/FileList): 新增文件预览功能组件 refactor(KLPService/CustomerSelect): 优化客户选择组件并支持自定义字段绑定 fix(AbnormalForm): 修复异常位置校验逻辑并保留当前卷号 feat(ContractTabs): 新增合同附件展示功能 refactor(warehouse/record): 重构操作记录统计展示方式 feat(contract): 集成客户选择组件并优化合同信息填充 refactor(order): 调整订单表单布局并集成合同信息 feat(FilePreview): 新增文件预览组件 feat(customer): 新增财务状态和发货配卷展示 refactor(CustomerOrder): 移除冗余代码并优化布局 feat(PlanDetailForm): 新增合同附件查看功能 feat(dict): 新增字典管理页面
98 lines
3.9 KiB
JavaScript
98 lines
3.9 KiB
JavaScript
import areaData from './data.js';
|
||
|
||
/**
|
||
* 辅助函数:将code数组/字符串转为name字符串
|
||
* @param {String|Array} codeVal - 如 "110000/110101" 或 ["110000", "110101"]
|
||
* @returns {String} 如 "北京市/东城区"
|
||
*/
|
||
function convertCodeToName(codeVal) {
|
||
if (!codeVal) return '';
|
||
const codeArr = Array.isArray(codeVal) ? codeVal : codeVal.split('/').filter(Boolean);
|
||
return codeArr.map(code => areaData[code] || '').filter(Boolean).join('/');
|
||
}
|
||
|
||
/**
|
||
* 格式化/解析区域地址文本(兼容「[标准地址](自定义地址)」格式)
|
||
* @param {String|Object} value - 输入值:
|
||
* - 字符串:如 "[北京市/东城区](XX街道1号)"、"北京市/东城区"、"" 等
|
||
* - 对象:{ standard: '北京市/东城区', custom: 'XX街道1号' }
|
||
* @returns {Object|String} 输出规则:
|
||
* - 输入为字符串 → 返回结构化对象 { standard: '', custom: '' }
|
||
* - 输入为对象 → 返回格式化后的组合字符串 "[标准地址](自定义地址)"
|
||
* - 输入为空/非预期类型 → 返回空字符串(对象模式)或空结构(字符串模式)
|
||
*/
|
||
function formatAreaText(value) {
|
||
// 核心正则:匹配「[标准地址](自定义地址)」格式,分组提取标准地址和自定义地址
|
||
const areaReg = /^\[([^\]]*)\]\(([^)]*)\)$/;
|
||
|
||
// ========== 场景1:输入为空/非预期类型 → 容错返回 ==========
|
||
if (!value || (typeof value !== 'string' && typeof value !== 'object')) {
|
||
// 输入非字符串/对象 → 按类型返回默认值
|
||
return typeof value === 'object' ? '' : { standard: '', custom: '' };
|
||
}
|
||
|
||
// ========== 场景2:输入是字符串 → 解析为结构化对象 ==========
|
||
if (typeof value === 'string') {
|
||
// 去除首尾空格
|
||
const trimVal = value.trim();
|
||
// 空字符串 → 返回空结构
|
||
if (!trimVal) return { standard: '', custom: '' };
|
||
|
||
// 匹配「[标准地址](自定义地址)」格式
|
||
const matchResult = trimVal.match(areaReg);
|
||
if (matchResult) {
|
||
// 提取分组值并去空格
|
||
const standard = matchResult[1]?.trim() || '';
|
||
const custom = matchResult[2]?.trim() || '';
|
||
return { standard, custom };
|
||
}
|
||
|
||
// 非组合格式(纯任意字符串)→ 归为custom
|
||
return { standard: '', custom: trimVal };
|
||
}
|
||
|
||
// ========== 场景3:输入是对象 → 格式化为组合字符串 ==========
|
||
if (typeof value === 'object' && !Array.isArray(value)) {
|
||
// 处理空对象
|
||
if (Object.keys(value).length === 0) {
|
||
return '[]()';
|
||
}
|
||
|
||
const { standard = '', custom = '' } = value;
|
||
// 转字符串并去空格
|
||
const standardStr = String(standard).trim();
|
||
const customStr = String(custom).trim();
|
||
|
||
// 组合为「[标准地址](自定义地址)」格式
|
||
return `[${standardStr}](${customStr})`;
|
||
}
|
||
|
||
// 兜底:其他情况返回空(如数组输入)
|
||
return typeof value === 'object' ? '' : { standard: '', custom: '' };
|
||
}
|
||
|
||
/**
|
||
* 增强版:支持code自动转name的格式化函数
|
||
*/
|
||
function formatAreaTextEnhanced(value, keyType = 'name') {
|
||
// 先调用基础版解析/格式化
|
||
let result = formatAreaText(value);
|
||
|
||
// 如果是解析模式(返回对象)且keyType为name,检查是否为代码输入
|
||
if (typeof result === 'object' && keyType === 'name') {
|
||
// 检查custom是否可能是代码(不包含中文)
|
||
if (result.custom && !/[\u4e00-\u9fa5]/.test(result.custom)) {
|
||
// 尝试转换code为name
|
||
const convertedName = convertCodeToName(result.custom);
|
||
// 如果转换成功(返回非空字符串),则将其移到standard字段
|
||
if (convertedName) {
|
||
result = { standard: convertedName, custom: '' };
|
||
}
|
||
}
|
||
}
|
||
|
||
return result;
|
||
}
|
||
|
||
// 输出:{ standard: "北京市/东城区", custom: "XX街道1号" }
|
||
export { formatAreaTextEnhanced }; |