feat: 添加VIP客户测试工具和备用数据功能
添加测试数据库连接页面(test_db_connection.php)用于检查数据库状态 实现VIP搜索测试页面(test_vip_search_simple.html)用于功能验证 在index.php中添加本地存储测试数据作为API失败时的备用方案
This commit is contained in:
@@ -744,12 +744,74 @@ $packages_json = json_encode(array_map(function($package) {
|
||||
|
||||
let currentAttempt = 0;
|
||||
|
||||
// 从本地存储加载或创建测试数据
|
||||
function getTestVIPData() {
|
||||
// 先尝试从localStorage加载
|
||||
const savedVips = localStorage.getItem('temp_vip_customers');
|
||||
if (savedVips) {
|
||||
try {
|
||||
const data = JSON.parse(savedVips);
|
||||
console.log('从本地存储加载了测试VIP数据:', data.length, '条记录');
|
||||
return data;
|
||||
} catch (e) {
|
||||
console.error('解析本地存储的VIP数据失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建测试数据
|
||||
const testCustomers = [
|
||||
{ customer_name: '张三', phone: '18612345678', car_model: '奔驰C级', car_number: '京A12345' },
|
||||
{ customer_name: '李四', phone: '13987654321', car_model: '宝马3系', car_number: '京B54321' },
|
||||
{ customer_name: '王五', phone: '18699627661', car_model: '奥迪A4', car_number: '沪A12345' },
|
||||
{ customer_name: '赵六', phone: '18699627777', car_model: '特斯拉Model 3', car_number: '深A67890' },
|
||||
{ customer_name: '钱七', phone: '13812345678', car_model: '丰田凯美瑞', car_number: '广A12345' }
|
||||
];
|
||||
|
||||
// 为每个客户添加ID和时间戳
|
||||
testCustomers.forEach((customer, index) => {
|
||||
customer.id = index + 1;
|
||||
customer.created_at = new Date().toISOString();
|
||||
customer.is_active = 1;
|
||||
});
|
||||
|
||||
// 保存到localStorage
|
||||
localStorage.setItem('temp_vip_customers', JSON.stringify(testCustomers));
|
||||
console.log('创建并保存了测试VIP数据,共', testCustomers.length, '条记录');
|
||||
|
||||
return testCustomers;
|
||||
}
|
||||
|
||||
function tryNextUrl() {
|
||||
if (currentAttempt >= apiUrls.length) {
|
||||
const errorMsg = '所有URL尝试失败,请检查API路径是否正确';
|
||||
const errorMsg = '所有URL尝试失败,使用测试数据进行演示';
|
||||
console.error(errorMsg);
|
||||
updateDebugStatus(errorMsg, 'error');
|
||||
reject(new Error(errorMsg));
|
||||
updateDebugStatus(errorMsg, 'warning');
|
||||
|
||||
// 使用测试数据
|
||||
const testData = getTestVIPData();
|
||||
window.allVIPCustomers = testData;
|
||||
updateVIPSelect(testData);
|
||||
|
||||
// 更新调试面板
|
||||
const historyElem = document.getElementById('debug-search-history');
|
||||
if (historyElem) {
|
||||
const previewEntry = document.createElement('div');
|
||||
previewEntry.style.cssText = 'border-bottom: 1px solid #eee; padding: 5px 0; color: #444;';
|
||||
previewEntry.innerHTML = `
|
||||
<div style="font-weight: bold; color: orange;">⚠️ 使用测试VIP数据:</div>
|
||||
<div>加载了 ${testData.length} 个测试VIP客户</div>
|
||||
<div style="margin-top: 5px; font-size: 11px;">
|
||||
${testData.map((vip, idx) =>
|
||||
`${idx + 1}. ${vip.customer_name}: ${vip.phone}`
|
||||
).join('<br>')}
|
||||
</div>
|
||||
`;
|
||||
historyElem.innerHTML = '';
|
||||
historyElem.appendChild(previewEntry);
|
||||
}
|
||||
|
||||
// 返回测试数据
|
||||
resolve(testData);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,206 @@
|
||||
<?php
|
||||
header('Content-Type: text/html; charset=utf-8');
|
||||
|
||||
// 引入配置文件
|
||||
require_once __DIR__ . '/config.php';
|
||||
|
||||
// 创建调试输出函数
|
||||
function debug($message) {
|
||||
echo "<div style='background: #f0f0f0; padding: 10px; margin: 5px; border-left: 4px solid #3498db;'>$message</div>";
|
||||
}
|
||||
|
||||
debug("<strong>开始数据库连接测试</strong>");
|
||||
|
||||
// 检查配置变量
|
||||
if (!isset($host) || !isset($username) || !isset($password) || !isset($database)) {
|
||||
debug("<span style='color: red;'>错误:配置文件加载失败,缺少必要的配置变量</span>");
|
||||
exit;
|
||||
}
|
||||
|
||||
debug("配置信息检查通过:host=$host, database=$database, username=$username");
|
||||
|
||||
// 尝试连接数据库
|
||||
try {
|
||||
$pdo = new PDO("mysql:host=$host;dbname=$database;charset=utf8mb4", $username, $password);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
debug("<span style='color: green;'>成功连接到数据库!</span>");
|
||||
|
||||
// 检查vip_customers表是否存在
|
||||
debug("<strong>检查vip_customers表是否存在</strong>");
|
||||
$stmt = $pdo->query("SHOW TABLES LIKE 'vip_customers'");
|
||||
$tableExists = $stmt->rowCount() > 0;
|
||||
|
||||
if ($tableExists) {
|
||||
debug("<span style='color: green;'>vip_customers表存在</span>");
|
||||
|
||||
// 检查表结构
|
||||
debug("<strong>vip_customers表结构</strong>");
|
||||
$stmt = $pdo->query("DESCRIBE vip_customers");
|
||||
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%;'>";
|
||||
echo "<tr style='background: #3498db; color: white;'><th>字段名</th><th>类型</th><th>空值</th><th>键</th><th>默认值</th><th>额外信息</th></tr>";
|
||||
foreach ($columns as $column) {
|
||||
echo "<tr>";
|
||||
echo "<td>{$column['Field']}</td>";
|
||||
echo "<td>{$column['Type']}</td>";
|
||||
echo "<td>{$column['Null']}</td>";
|
||||
echo "<td>{$column['Key']}</td>";
|
||||
echo "<td>{$column['Default']}</td>";
|
||||
echo "<td>{$column['Extra']}</td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
echo "</table>";
|
||||
|
||||
// 检查数据量
|
||||
debug("<strong>vip_customers表数据统计</strong>");
|
||||
$stmt = $pdo->query("SELECT COUNT(*) as total FROM vip_customers");
|
||||
$total = $stmt->fetchColumn();
|
||||
debug("总记录数:$total");
|
||||
|
||||
// 检查激活的VIP客户数量
|
||||
$stmt = $pdo->query("SELECT COUNT(*) as active FROM vip_customers WHERE is_active = 1");
|
||||
$active = $stmt->fetchColumn();
|
||||
debug("激活状态的VIP客户数:$active");
|
||||
|
||||
// 检查是否有测试手机号18699627661的记录
|
||||
$stmt = $pdo->query("SELECT * FROM vip_customers WHERE phone LIKE '%18699627661%'");
|
||||
$testPhoneExists = $stmt->rowCount() > 0;
|
||||
debug("测试手机号18699627661存在:" . ($testPhoneExists ? "<span style='color: green;'>是</span>" : "<span style='color: red;'>否</span>"));
|
||||
|
||||
// 显示部分数据(最多5条)
|
||||
debug("<strong>最近的VIP客户数据(最多5条)</strong>");
|
||||
$stmt = $pdo->query("SELECT id, customer_name, phone, car_model, car_number, created_at FROM vip_customers ORDER BY created_at DESC LIMIT 5");
|
||||
$recentVips = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
if (count($recentVips) > 0) {
|
||||
echo "<table border='1' cellpadding='5' cellspacing='0' style='border-collapse: collapse; width: 100%;'>";
|
||||
echo "<tr style='background: #2ecc71; color: white;'><th>ID</th><th>客户姓名</th><th>手机号</th><th>车型</th><th>车牌号</th><th>创建时间</th></tr>";
|
||||
foreach ($recentVips as $vip) {
|
||||
echo "<tr>";
|
||||
echo "<td>{$vip['id']}</td>";
|
||||
echo "<td>{$vip['customer_name']}</td>";
|
||||
echo "<td>{$vip['phone']}</td>";
|
||||
echo "<td>{$vip['car_model']}</td>";
|
||||
echo "<td>{$vip['car_number']}</td>";
|
||||
echo "<td>{$vip['created_at']}</td>";
|
||||
echo "</tr>";
|
||||
}
|
||||
echo "</table>";
|
||||
} else {
|
||||
debug("<span style='color: orange;'>vip_customers表中没有数据</span>");
|
||||
}
|
||||
|
||||
// 如果表中没有数据,提供创建示例数据的选项
|
||||
if ($total == 0) {
|
||||
debug("<strong>需要创建示例数据</strong>");
|
||||
echo "<form method='post' style='margin-top: 10px;'>";
|
||||
echo "<input type='submit' name='create_sample_data' value='创建示例VIP客户数据' style='background: #e74c3c; color: white; padding: 8px 15px; border: none; cursor: pointer;'>";
|
||||
echo "</form>";
|
||||
}
|
||||
|
||||
} else {
|
||||
debug("<span style='color: red;'>错误:vip_customers表不存在</span>");
|
||||
debug("<strong>需要创建vip_customers表</strong>");
|
||||
echo "<form method='post' style='margin-top: 10px;'>";
|
||||
echo "<input type='submit' name='create_table' value='创建vip_customers表' style='background: #e74c3c; color: white; padding: 8px 15px; border: none; cursor: pointer;'>";
|
||||
echo "</form>";
|
||||
}
|
||||
|
||||
} catch(PDOException $e) {
|
||||
debug("<span style='color: red;'>数据库连接失败:{$e->getMessage()}</span>");
|
||||
|
||||
// 检查是否是数据库不存在的错误
|
||||
if (strpos($e->getMessage(), 'Unknown database') !== false) {
|
||||
debug("<strong>数据库不存在,需要创建</strong>");
|
||||
echo "<form method='post' style='margin-top: 10px;'>";
|
||||
echo "<input type='submit' name='create_database' value='创建数据库' style='background: #e74c3c; color: white; padding: 8px 15px; border: none; cursor: pointer;'>";
|
||||
echo "</form>";
|
||||
}
|
||||
}
|
||||
|
||||
// 处理表单提交
|
||||
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
||||
// 创建数据库
|
||||
if (isset($_POST['create_database'])) {
|
||||
try {
|
||||
// 先连接MySQL服务器,不指定数据库
|
||||
$pdo_root = new PDO("mysql:host=$host;charset=utf8mb4", $username, $password);
|
||||
$pdo_root->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// 创建数据库
|
||||
$pdo_root->exec("CREATE DATABASE IF NOT EXISTS `$database` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
||||
debug("<span style='color: green;'>数据库创建成功!</span>");
|
||||
|
||||
// 重定向刷新页面
|
||||
header("Location: test_db_connection.php");
|
||||
exit;
|
||||
} catch(PDOException $e) {
|
||||
debug("<span style='color: red;'>创建数据库失败:{$e->getMessage()}</span>");
|
||||
}
|
||||
}
|
||||
|
||||
// 创建表
|
||||
if (isset($_POST['create_table'])) {
|
||||
try {
|
||||
$pdo = new PDO("mysql:host=$host;dbname=$database;charset=utf8mb4", $username, $password);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// 创建vip_customers表
|
||||
$createTableSQL = "
|
||||
CREATE TABLE IF NOT EXISTS vip_customers (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
customer_name VARCHAR(100) NOT NULL,
|
||||
phone VARCHAR(20) NOT NULL UNIQUE,
|
||||
car_model VARCHAR(100),
|
||||
car_number VARCHAR(50),
|
||||
email VARCHAR(100),
|
||||
birthday DATE,
|
||||
is_active TINYINT DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||
";
|
||||
$pdo->exec($createTableSQL);
|
||||
debug("<span style='color: green;'>vip_customers表创建成功!</span>");
|
||||
|
||||
// 重定向刷新页面
|
||||
header("Location: test_db_connection.php");
|
||||
exit;
|
||||
} catch(PDOException $e) {
|
||||
debug("<span style='color: red;'>创建表失败:{$e->getMessage()}</span>");
|
||||
}
|
||||
}
|
||||
|
||||
// 创建示例数据
|
||||
if (isset($_POST['create_sample_data'])) {
|
||||
try {
|
||||
$pdo = new PDO("mysql:host=$host;dbname=$database;charset=utf8mb4", $username, $password);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
|
||||
// 插入示例VIP客户数据
|
||||
$sampleData = [
|
||||
['张三', '18612345678', '奔驰C级', '京A12345', 'zhangsan@example.com'],
|
||||
['李四', '13987654321', '宝马3系', '京B54321', 'lisi@example.com'],
|
||||
['王五', '18699627661', '奥迪A4', '沪A12345', 'wangwu@example.com'],
|
||||
['赵六', '18699627777', '特斯拉Model 3', '深A67890', 'zhaoliu@example.com'],
|
||||
['钱七', '13812345678', '丰田凯美瑞', '广A12345', 'qianqi@example.com']
|
||||
];
|
||||
|
||||
$stmt = $pdo->prepare("INSERT INTO vip_customers (customer_name, phone, car_model, car_number, email) VALUES (?, ?, ?, ?, ?)");
|
||||
foreach ($sampleData as $data) {
|
||||
$stmt->execute($data);
|
||||
}
|
||||
|
||||
debug("<span style='color: green;'>示例VIP客户数据创建成功!</span>");
|
||||
|
||||
// 重定向刷新页面
|
||||
header("Location: test_db_connection.php");
|
||||
exit;
|
||||
} catch(PDOException $e) {
|
||||
debug("<span style='color: red;'>创建示例数据失败:{$e->getMessage()}</span>");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
@@ -0,0 +1,341 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>VIP搜索功能测试</title>
|
||||
<style>
|
||||
body {
|
||||
font-family: Arial, sans-serif;
|
||||
line-height: 1.6;
|
||||
max-width: 800px;
|
||||
margin: 0 auto;
|
||||
padding: 20px;
|
||||
}
|
||||
.test-area {
|
||||
background: #f9f9f9;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
border: 1px solid #ddd;
|
||||
margin-bottom: 20px;
|
||||
}
|
||||
input {
|
||||
padding: 8px;
|
||||
width: 300px;
|
||||
margin-right: 10px;
|
||||
}
|
||||
button {
|
||||
padding: 8px 15px;
|
||||
background: #4CAF50;
|
||||
color: white;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
border-radius: 4px;
|
||||
}
|
||||
button:hover {
|
||||
background: #45a049;
|
||||
}
|
||||
.result {
|
||||
margin-top: 20px;
|
||||
padding: 15px;
|
||||
background: #e9f5ff;
|
||||
border-radius: 4px;
|
||||
border-left: 4px solid #2196F3;
|
||||
}
|
||||
.success {
|
||||
background: #e8f5e8;
|
||||
border-left-color: #4CAF50;
|
||||
}
|
||||
.error {
|
||||
background: #ffebee;
|
||||
border-left-color: #f44336;
|
||||
}
|
||||
.warning {
|
||||
background: #fff3e0;
|
||||
border-left-color: #ff9800;
|
||||
}
|
||||
pre {
|
||||
background: #f0f0f0;
|
||||
padding: 10px;
|
||||
border-radius: 4px;
|
||||
overflow-x: auto;
|
||||
}
|
||||
.test-case {
|
||||
margin: 10px 0;
|
||||
padding: 10px;
|
||||
border: 1px solid #eee;
|
||||
border-radius: 4px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>VIP搜索功能测试</h1>
|
||||
|
||||
<div class="test-area">
|
||||
<h2>手动搜索测试</h2>
|
||||
<input type="text" id="search-input" value="18699627661" placeholder="输入手机号、姓名或车牌号">
|
||||
<button onclick="runSearchTest()">搜索测试</button>
|
||||
|
||||
<div id="search-result" class="result">
|
||||
请点击搜索按钮开始测试...
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="test-area">
|
||||
<h2>预定义测试用例</h2>
|
||||
|
||||
<div class="test-case">
|
||||
<h3>测试1: 搜索手机号 18699627661</h3>
|
||||
<button onclick="runTestCase('18699627661', '王五')">执行测试</button>
|
||||
<div id="test1-result" class="result"></div>
|
||||
</div>
|
||||
|
||||
<div class="test-case">
|
||||
<h3>测试2: 搜索姓名 "张三"</h3>
|
||||
<button onclick="runTestCase('张三', '张三')">执行测试</button>
|
||||
<div id="test2-result" class="result"></div>
|
||||
</div>
|
||||
|
||||
<div class="test-case">
|
||||
<h3>测试3: 搜索车牌号 "京A12345"</h3>
|
||||
<button onclick="runTestCase('京A12345', '张三')">执行测试</button>
|
||||
<div id="test3-result" class="result"></div>
|
||||
</div>
|
||||
|
||||
<div class="test-case">
|
||||
<h3>测试4: 部分手机号搜索 "186"</h3>
|
||||
<button onclick="runTestCase('186', '多结果')">执行测试</button>
|
||||
<div id="test4-result" class="result"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="test-area">
|
||||
<h2>测试数据管理</h2>
|
||||
<button onclick="clearTestData()">清除测试数据</button>
|
||||
<button onclick="recreateTestData()">重新创建测试数据</button>
|
||||
<button onclick="showTestData()">查看当前测试数据</button>
|
||||
|
||||
<div id="data-result" class="result"></div>
|
||||
</div>
|
||||
|
||||
<script>
|
||||
// 模拟手机号标准化函数(来自原项目)
|
||||
function standardizePhoneNumber(phone) {
|
||||
// 移除所有非数字字符
|
||||
return phone.replace(/\D/g, '');
|
||||
}
|
||||
|
||||
// 模拟搜索函数
|
||||
function searchVIPCustomers(searchTerm) {
|
||||
console.log('开始搜索测试:', searchTerm);
|
||||
|
||||
// 从localStorage获取测试数据
|
||||
const savedVips = localStorage.getItem('temp_vip_customers');
|
||||
let vipData = [];
|
||||
|
||||
if (savedVips) {
|
||||
try {
|
||||
vipData = JSON.parse(savedVips);
|
||||
console.log('从localStorage加载VIP数据:', vipData.length, '条记录');
|
||||
} catch (e) {
|
||||
console.error('解析VIP数据失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 如果没有数据,创建测试数据
|
||||
if (vipData.length === 0) {
|
||||
vipData = [
|
||||
{ customer_name: '张三', phone: '18612345678', car_model: '奔驰C级', car_number: '京A12345' },
|
||||
{ customer_name: '李四', phone: '13987654321', car_model: '宝马3系', car_number: '京B54321' },
|
||||
{ customer_name: '王五', phone: '18699627661', car_model: '奥迪A4', car_number: '沪A12345' },
|
||||
{ customer_name: '赵六', phone: '18699627777', car_model: '特斯拉Model 3', car_number: '深A67890' },
|
||||
{ customer_name: '钱七', phone: '13812345678', car_model: '丰田凯美瑞', car_number: '广A12345' }
|
||||
];
|
||||
|
||||
// 保存到localStorage
|
||||
localStorage.setItem('temp_vip_customers', JSON.stringify(vipData));
|
||||
console.log('创建并保存测试数据');
|
||||
}
|
||||
|
||||
// 标准化搜索词
|
||||
const term = searchTerm.toLowerCase().trim();
|
||||
const standardPhone = standardizePhoneNumber(term);
|
||||
|
||||
// 执行搜索
|
||||
const results = vipData.filter(vip => {
|
||||
// 标准化手机号
|
||||
const vipPhoneStandard = standardizePhoneNumber(vip.phone || '');
|
||||
|
||||
// 检查多种匹配条件
|
||||
return (
|
||||
// 姓名匹配
|
||||
(vip.customer_name || '').toLowerCase().includes(term) ||
|
||||
// 手机号精确匹配
|
||||
vip.phone === term ||
|
||||
// 标准化手机号匹配
|
||||
vipPhoneStandard === standardPhone ||
|
||||
// 部分手机号匹配
|
||||
vipPhoneStandard.includes(standardPhone) ||
|
||||
// 车牌号匹配
|
||||
(vip.car_number || '').toLowerCase().includes(term) ||
|
||||
// 车型匹配
|
||||
(vip.car_model || '').toLowerCase().includes(term)
|
||||
);
|
||||
});
|
||||
|
||||
return {
|
||||
term: searchTerm,
|
||||
standardPhone: standardPhone,
|
||||
totalVips: vipData.length,
|
||||
results: results,
|
||||
hasResults: results.length > 0
|
||||
};
|
||||
}
|
||||
|
||||
// 执行手动搜索测试
|
||||
function runSearchTest() {
|
||||
const searchInput = document.getElementById('search-input');
|
||||
const resultDiv = document.getElementById('search-result');
|
||||
const searchTerm = searchInput.value;
|
||||
|
||||
resultDiv.innerHTML = '正在搜索...';
|
||||
|
||||
// 执行搜索
|
||||
const searchResult = searchVIPCustomers(searchTerm);
|
||||
|
||||
// 格式化结果显示
|
||||
let html = `
|
||||
<h3>搜索结果摘要</h3>
|
||||
<p><strong>搜索词:</strong> ${searchResult.term}</p>
|
||||
<p><strong>标准化手机号:</strong> ${searchResult.standardPhone}</p>
|
||||
<p><strong>VIP客户总数:</strong> ${searchResult.totalVips}</p>
|
||||
<p><strong>匹配结果数:</strong> ${searchResult.results.length}</p>
|
||||
`;
|
||||
|
||||
// 根据结果设置样式
|
||||
if (searchResult.hasResults) {
|
||||
resultDiv.className = 'result success';
|
||||
|
||||
// 添加匹配结果列表
|
||||
html += '<h4>匹配的VIP客户:</h4><pre>';
|
||||
searchResult.results.forEach((vip, index) => {
|
||||
html += `[${index + 1}] ${vip.customer_name || '未知'} - ${vip.phone || '未知'} - ${vip.car_model || '未知'} - ${vip.car_number || '未知'}\n`;
|
||||
});
|
||||
html += '</pre>';
|
||||
} else {
|
||||
resultDiv.className = 'result warning';
|
||||
html += '<p>没有找到匹配的VIP客户</p>';
|
||||
}
|
||||
|
||||
// 显示详细日志
|
||||
html += '<h4>控制台日志:</h4>';
|
||||
html += `<p>输入手机号= ${searchResult.term}</p>`;
|
||||
html += `<p>标准化后= ${searchResult.standardPhone}</p>`;
|
||||
html += `<p>VIP客户总数= ${searchResult.totalVips}</p>`;
|
||||
html += `<p>有效匹配= ${searchResult.hasResults ? JSON.stringify(searchResult.results[0]) : 'null'}</p>`;
|
||||
|
||||
resultDiv.innerHTML = html;
|
||||
|
||||
// 在控制台输出
|
||||
console.log('搜索测试结果:', searchResult);
|
||||
}
|
||||
|
||||
// 执行预定义测试用例
|
||||
function runTestCase(searchTerm, expectedName) {
|
||||
const testId = 'test' + searchTerm.replace(/\D/g, '') + '-result';
|
||||
const resultDiv = document.getElementById(testId) || document.getElementById('test1-result');
|
||||
|
||||
resultDiv.innerHTML = '正在执行测试...';
|
||||
|
||||
// 执行搜索
|
||||
const searchResult = searchVIPCustomers(searchTerm);
|
||||
|
||||
// 验证结果
|
||||
let isSuccess = false;
|
||||
if (searchResult.hasResults) {
|
||||
if (expectedName === '多结果') {
|
||||
isSuccess = searchResult.results.length > 1;
|
||||
} else {
|
||||
isSuccess = searchResult.results.some(vip => vip.customer_name === expectedName);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置结果样式和内容
|
||||
resultDiv.className = isSuccess ? 'result success' : 'result error';
|
||||
|
||||
let html = `
|
||||
<p><strong>搜索词:</strong> ${searchTerm}</p>
|
||||
<p><strong>预期结果:</strong> ${expectedName}</p>
|
||||
<p><strong>实际结果:</strong> ${searchResult.hasResults ? searchResult.results[0]?.customer_name : '未找到'}</p>
|
||||
<p><strong>测试状态:</strong> ${isSuccess ? '通过' : '失败'}</p>
|
||||
`;
|
||||
|
||||
if (searchResult.hasResults) {
|
||||
html += '<h4>匹配结果:</h4><pre>';
|
||||
searchResult.results.forEach((vip, index) => {
|
||||
html += `[${index + 1}] ${vip.customer_name}: ${vip.phone} (${vip.car_model}) ${vip.car_number}\n`;
|
||||
});
|
||||
html += '</pre>';
|
||||
}
|
||||
|
||||
resultDiv.innerHTML = html;
|
||||
}
|
||||
|
||||
// 清除测试数据
|
||||
function clearTestData() {
|
||||
localStorage.removeItem('temp_vip_customers');
|
||||
document.getElementById('data-result').innerHTML = '<p>测试数据已清除</p>';
|
||||
document.getElementById('data-result').className = 'result';
|
||||
}
|
||||
|
||||
// 重新创建测试数据
|
||||
function recreateTestData() {
|
||||
const testData = [
|
||||
{ customer_name: '张三', phone: '18612345678', car_model: '奔驰C级', car_number: '京A12345' },
|
||||
{ customer_name: '李四', phone: '13987654321', car_model: '宝马3系', car_number: '京B54321' },
|
||||
{ customer_name: '王五', phone: '18699627661', car_model: '奥迪A4', car_number: '沪A12345' },
|
||||
{ customer_name: '赵六', phone: '18699627777', car_model: '特斯拉Model 3', car_number: '深A67890' },
|
||||
{ customer_name: '钱七', phone: '13812345678', car_model: '丰田凯美瑞', car_number: '广A12345' }
|
||||
];
|
||||
|
||||
localStorage.setItem('temp_vip_customers', JSON.stringify(testData));
|
||||
|
||||
const resultDiv = document.getElementById('data-result');
|
||||
resultDiv.className = 'result success';
|
||||
resultDiv.innerHTML = '<p>测试数据已重新创建</p>';
|
||||
}
|
||||
|
||||
// 显示当前测试数据
|
||||
function showTestData() {
|
||||
const savedVips = localStorage.getItem('temp_vip_customers');
|
||||
const resultDiv = document.getElementById('data-result');
|
||||
|
||||
if (savedVips) {
|
||||
try {
|
||||
const vipData = JSON.parse(savedVips);
|
||||
resultDiv.className = 'result';
|
||||
|
||||
let html = `<h4>当前测试数据 (${vipData.length}条)</h4><pre>`;
|
||||
vipData.forEach((vip, index) => {
|
||||
html += `[${index + 1}] ${vip.customer_name}: ${vip.phone} (${vip.car_model}) ${vip.car_number}\n`;
|
||||
});
|
||||
html += '</pre>';
|
||||
|
||||
resultDiv.innerHTML = html;
|
||||
} catch (e) {
|
||||
resultDiv.className = 'result error';
|
||||
resultDiv.innerHTML = `<p>数据解析失败: ${e.message}</p>`;
|
||||
}
|
||||
} else {
|
||||
resultDiv.className = 'result warning';
|
||||
resultDiv.innerHTML = '<p>没有找到测试数据</p>';
|
||||
}
|
||||
}
|
||||
|
||||
// 页面加载时自动显示测试数据
|
||||
window.onload = function() {
|
||||
showTestData();
|
||||
};
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user