diff --git a/index.php b/index.php index c55032b..5c164d1 100644 --- a/index.php +++ b/index.php @@ -941,6 +941,12 @@ $packages_json = json_encode(array_map(function($package) { function searchVIPCustomers() { console.log('开始搜索VIP客户...'); + // 确保allVIPCustomers已初始化 + if (!window.allVIPCustomers || !Array.isArray(window.allVIPCustomers)) { + window.allVIPCustomers = []; + console.warn('allVIPCustomers未初始化,已创建空数组'); + } + const searchInput = document.getElementById('vip_search'); const searchResultsDiv = document.getElementById('vip_search_results'); const vipSelect = document.getElementById('vip_id'); @@ -956,6 +962,14 @@ $packages_json = json_encode(array_map(function($package) { // 检查数据加载状态 console.log('VIP客户总数:', window.allVIPCustomers.length); + // 立即处理空搜索情况 + if (searchTerm === '') { + searchResultsDiv.innerHTML = ''; + searchResultsDiv.style.display = 'none'; + vipSelect.style.display = 'block'; + return; + } + // 始终显示搜索结果容器 searchResultsDiv.style.display = 'block'; @@ -978,43 +992,76 @@ $packages_json = json_encode(array_map(function($package) { } function performSearch(term) { - if (term === '') { - searchResultsDiv.innerHTML = ''; - searchResultsDiv.style.display = 'none'; - vipSelect.style.display = 'block'; - return; - } + // 标准化手机号函数 + const normalizePhone = (phoneStr) => { + return phoneStr ? phoneStr.replace(/[^0-9]/g, '') : ''; + }; - // 模糊搜索:支持姓名和手机号搜索 + // 模糊搜索:支持姓名、手机号和车牌号搜索,增强匹配逻辑 const filteredCustomers = window.allVIPCustomers.filter(vip => { - const name = (vip.customer_name || '').toLowerCase(); - const phone = (vip.phone || '').toLowerCase(); - const searchLower = term.toLowerCase(); + // 确保vip对象有效 + if (!vip || typeof vip !== 'object') return false; - return name.includes(searchLower) || phone.includes(searchLower); + const searchLower = term.toLowerCase(); + const name = (vip.customer_name || '').toLowerCase(); + const phone = vip.phone || ''; + const carModel = (vip.car_model || '').toLowerCase(); + const carNumber = (vip.car_number || '').toLowerCase(); + + // 基本匹配 + let isMatch = name.includes(searchLower) || + phone.includes(term) || + carModel.includes(searchLower) || + carNumber.includes(searchLower); + + // 手机号标准化匹配(处理数字搜索) + if (!isMatch && /^\d+$/.test(term)) { + const normalizedPhone = normalizePhone(phone); + const normalizedSearch = normalizePhone(term); + isMatch = normalizedPhone.includes(normalizedSearch) || + normalizedSearch.includes(normalizedPhone); + } + + // 调试日志 + console.log('搜索项检查:', { + id: vip.id, + name: vip.customer_name, + phone: phone, + match: isMatch + }); + + return isMatch; }); console.log('搜索结果数量:', filteredCustomers.length); // 显示搜索结果 if (filteredCustomers.length === 0) { - searchResultsDiv.innerHTML = '
未找到匹配的VIP客户
'; + searchResultsDiv.innerHTML = '
未找到匹配的VIP客户
请尝试使用姓名、手机号或车牌号搜索
'; } else { let html = ''; filteredCustomers.forEach(vip => { - const name = vip.customer_name || ''; - const phone = vip.phone || ''; + // 安全获取数据并设置默认值 + const name = vip.customer_name || '未知客户'; + const phone = vip.phone || '未知手机号'; const id = vip.id || ''; + const carModel = vip.car_model || ''; + const carNumber = vip.car_number || ''; + + // 转义引号,防止JS语法错误 + const escapedId = id.toString().replace(/'/g, "\\'"); + const escapedName = name.replace(/'/g, "\\'"); + const escapedPhone = phone.replace(/'/g, "\\'"); // 高亮处理 const highlightedName = name.replace(new RegExp(`(${term})`, 'gi'), '$1'); const highlightedPhone = phone.replace(new RegExp(`(${term})`, 'gi'), '$1'); html += ` -
+
${highlightedName}
${highlightedPhone}
- ${vip.car_model || vip.car_number ? `
${vip.car_model || ''} ${vip.car_number || ''}
` : ''} + ${carModel || carNumber ? `
${carModel} ${carNumber}
` : ''}
`; }); diff --git a/test_vip_search.html b/test_vip_search.html new file mode 100644 index 0000000..e34b4ba --- /dev/null +++ b/test_vip_search.html @@ -0,0 +1,273 @@ + + + + + + VIP搜索功能测试 + + + +
+

VIP搜索功能测试

+ +
+

1. 测试数据初始化

+ +

将创建模拟的VIP客户数据用于测试

+
+ +
+

2. 测试搜索功能

+ + +

测试修改后的搜索逻辑

+
+ +
+

3. 测试手机号标准化

+ + +

测试手机号标准化匹配功能

+
+ +
+

4. 测试边界情况

+ + +

测试各种边界情况和异常处理

+
+ +
+

测试结果

+
请点击上面的按钮开始测试...
+
+
+ + + + \ No newline at end of file