Skip to content

Commit 0cdad5a

Browse files
committed
Realtime Route Detail Button Fix
1 parent b6d6255 commit 0cdad5a

1 file changed

Lines changed: 91 additions & 92 deletions

File tree

templates/index.html

Lines changed: 91 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ <h3 class="text-xl font-semibold text-gray-600 mb-2">未找到路径</h3>
12311231
// 解析路线详细信息,添加默认值
12321232
// 路线详情数组结构:
12331233
// v3: [起点, 终点, 颜色, 路线名, 终点站信息, 时长, 等车时间, 发车间隔, 交通类型, 站台编号, [route_id, station_1, station_2], ban_route_string]
1234-
// v4(实时模式): [起点, 终点, 颜色, 路线名, 终点站信息, 时长, 等车时间, 交通类型, 站台编号, [route_id, station_1, station_2]] (与v3保持一致)
1234+
// v4(实时模式): [起点, 终点, 颜色, 路线名, 终点站信息, 时长, 等车时间, 交通类型, 站台编号, 原始路线名]
12351235
let originalRouteName = '';
12361236
let platform = '';
12371237
let departureInterval = 0;
@@ -1265,9 +1265,9 @@ <h3 class="text-xl font-semibold text-gray-600 mb-2">未找到路径</h3>
12651265
// 尝试提取站台编号,不管数组长度如何
12661266
// 对于v4版本,站台编号在第9个位置(索引8)
12671267
platform = routeDetail[8] || '';
1268-
// v4版本中,路线信息在索引9位置,是一个数组:[route_id, station_1, station_2]
1269-
if (routeDetail.length > 9 && Array.isArray(routeDetail[9])) {
1270-
routeIdFromData = routeDetail[9][0];
1268+
// v4版本中,索引9是原始路线名
1269+
if (routeDetail.length > 9) {
1270+
originalRouteName = routeDetail[9];
12711271
}
12721272
banRouteString = '';
12731273
}
@@ -1397,30 +1397,65 @@ <h3 class="text-xl font-semibold text-gray-600 mb-2">未找到路径</h3>
13971397
const routeId = this.dataset.routeId;
13981398
console.log('Route button clicked, raw routeId:', routeId);
13991399

1400-
// 直接从原始routeId中提取第一个ID,不需要复杂的正则表达式
1401-
// 因为routeId中已经包含了线路ID,只是可能有多个用逗号分隔
14021400
let detailUrl;
1401+
let finalRouteId = null;
14031402

1404-
// 简单直接的方法:直接取第一个逗号前的部分作为ID
1405-
const firstId = String(routeId).split(',')[0].trim();
1406-
1407-
// 检查firstId是否是有效的线路ID,如果不是,尝试使用findRouteIdByName函数查找
1408-
let finalRouteId = firstId;
1409-
if (!finalRouteId || finalRouteId === routeName) {
1410-
// 如果finalRouteId是线路名称,使用findRouteIdByName函数查找真正的线路ID
1403+
// 1. 首先检查routeId是否已经是有效的线路ID(直接从数据中获取的)
1404+
if (routeId && routeId !== routeName) {
1405+
// 如果routeId不是线路名称,直接使用
1406+
finalRouteId = String(routeId).split(',')[0].trim();
1407+
console.log('Using provided route ID:', finalRouteId);
1408+
} else {
1409+
// 2. 使用v4原程序返回的原始线路名查找对应的线路ID
14111410
console.log('Route ID is route name, trying to find actual ID using findRouteIdByName...');
1412-
const actualRouteId = findRouteIdByName(firstId);
1413-
if (actualRouteId) {
1414-
finalRouteId = actualRouteId;
1415-
console.log('Found actual route ID:', finalRouteId);
1411+
// 优先使用originalRouteName(从routeDetail[9]获取的原始路线名)
1412+
if (originalRouteName) {
1413+
finalRouteId = findRouteIdByName(originalRouteName);
1414+
if (finalRouteId) {
1415+
console.log('Found actual route ID using original route name from routeDetail[9]:', finalRouteId);
1416+
} else {
1417+
console.log('Failed to find route ID with original route name from routeDetail[9], trying with routeName...');
1418+
// 如果originalRouteName查找失败,尝试使用routeName
1419+
finalRouteId = findRouteIdByName(routeName);
1420+
if (finalRouteId) {
1421+
console.log('Found actual route ID using routeName:', finalRouteId);
1422+
} else {
1423+
// 如果还是找不到,尝试使用处理后的线路名
1424+
console.log('Failed to find route ID with routeName, trying with processed route name...');
1425+
finalRouteId = findRouteIdByName(processedRouteName);
1426+
if (finalRouteId) {
1427+
console.log('Found actual route ID using processed route name:', finalRouteId);
1428+
}
1429+
}
1430+
}
1431+
} else {
1432+
// 如果没有originalRouteName,尝试使用routeName
1433+
finalRouteId = findRouteIdByName(routeName);
1434+
if (finalRouteId) {
1435+
console.log('Found actual route ID using routeName:', finalRouteId);
1436+
} else {
1437+
// 如果还是找不到,尝试使用处理后的线路名
1438+
console.log('Failed to find route ID with routeName, trying with processed route name...');
1439+
finalRouteId = findRouteIdByName(processedRouteName);
1440+
if (finalRouteId) {
1441+
console.log('Found actual route ID using processed route name:', finalRouteId);
1442+
}
1443+
}
14161444
}
14171445
}
14181446

1419-
// 不检查UUID格式,直接使用finalRouteId
1420-
detailUrl = `/routes/${encodeURIComponent(finalRouteId)}`;
1447+
// 构建最终的URL
1448+
if (finalRouteId) {
1449+
// 使用查找到的线路ID构建链接
1450+
detailUrl = `/routes/${encodeURIComponent(finalRouteId)}`;
1451+
console.log('Constructed detail URL with route ID:', detailUrl);
1452+
} else {
1453+
// 如果无法找到线路ID,降级处理,使用线路名称作为备选
1454+
console.warn('Failed to find route ID, using route name as fallback');
1455+
detailUrl = `/routes/${encodeURIComponent(processedRouteName)}`;
1456+
console.log('Constructed detail URL with route name fallback:', detailUrl);
1457+
}
14211458

1422-
console.log('Using final route ID:', finalRouteId);
1423-
console.log('Constructed detail URL:', detailUrl);
14241459
window.open(detailUrl, '_blank');
14251460
});
14261461

@@ -2597,97 +2632,61 @@ <h3 class="text-xl font-semibold text-gray-600 mb-2">未找到路径</h3>
25972632
return null;
25982633
}
25992634

2600-
// 根据线路名称查找线路ID
2635+
// 根据原始线路名查找对应的线路ID
26012636
function findRouteIdByName(routeName) {
26022637
// 调试日志
26032638
console.log('=== findRouteIdByName called ===');
26042639
console.log('Input routeName:', routeName);
26052640
console.log('Total routes available:', routesData.length);
26062641

2607-
// 处理输入的线路名称
2608-
const originalSearchName = routeName.trim();
2609-
const searchName = originalSearchName.toLowerCase();
2642+
// 确保routesData是数组
2643+
if (!Array.isArray(routesData)) {
2644+
console.error('routesData is not an array');
2645+
return null;
2646+
}
26102647

2611-
// 直接匹配模式:只查找完全匹配的线路
2612-
// 这是最可靠的匹配方式,避免模糊匹配导致的错误
2648+
// 使用原始线路名直接匹配
2649+
const originalRouteName = routeName.trim();
2650+
2651+
// 直接匹配模式:查找完全匹配的线路
26132652
for (const route of routesData) {
2614-
if (route.name) {
2615-
// 处理线路数据中的名称
2616-
const routeNameFromData = route.name;
2617-
// 只比较核心线路名称(||前面的部分)
2618-
const coreRouteName = routeNameFromData.split('||')[0];
2619-
// 比较每种语言的名称
2620-
const languageNames = coreRouteName.split('|').map(part => part.trim().toLowerCase());
2621-
2622-
// 检查是否有完全匹配
2623-
if (languageNames.includes(searchName)) {
2653+
if (route && route.name) {
2654+
// 检查线路名称是否完全匹配
2655+
if (route.name === originalRouteName) {
26242656
// 确保只返回一个ID,即使线路数据中包含多个ID
26252657
const singleId = String(route.id).split(',')[0].trim();
2626-
console.log('Exact match found, returning single ID:', singleId);
2658+
console.log('Exact match found with original route name, returning single ID:', singleId);
26272659
return singleId;
26282660
}
26292661
}
26302662
}
26312663

2632-
console.log('No exact match found, trying partial match...');
2633-
2634-
// 如果没有找到完全匹配的线路,尝试更精确的部分匹配
2635-
let bestMatch = null;
2636-
let bestScore = 0;
2637-
2664+
// 如果没有找到完全匹配,尝试使用核心线路名称(||前面的部分)匹配
2665+
const coreRouteName = originalRouteName.split('||')[0].trim();
26382666
for (const route of routesData) {
2639-
if (route.name) {
2640-
const routeNameFromData = route.name;
2641-
const coreRouteName = routeNameFromData.split('||')[0];
2642-
const languageNames = coreRouteName.split('|').map(part => part.trim().toLowerCase());
2643-
const cleanRouteName = languageNames.join(' ');
2644-
2645-
// 计算匹配评分
2646-
let score = 0;
2647-
2648-
// 1. 搜索词完全匹配任何语言的线路名称
2649-
if (languageNames.includes(searchName)) {
2650-
score += 100;
2651-
}
2652-
2653-
// 2. 搜索词完全匹配清理后的线路名称
2654-
if (searchName === cleanRouteName) {
2655-
score += 90;
2656-
}
2657-
2658-
// 3. 搜索词是清理后线路名称的前缀
2659-
if (cleanRouteName.startsWith(searchName)) {
2660-
score += 80;
2661-
}
2662-
2663-
// 4. 清理后的线路名称是搜索词的前缀
2664-
if (searchName.startsWith(cleanRouteName)) {
2665-
score += 70;
2666-
}
2667-
2668-
// 5. 搜索词包含清理后的线路名称
2669-
if (searchName.includes(cleanRouteName)) {
2670-
score += 60;
2671-
}
2672-
2673-
// 6. 清理后的线路名称包含搜索词
2674-
if (cleanRouteName.includes(searchName)) {
2675-
score += 50;
2676-
}
2677-
2678-
// 更新最佳匹配
2679-
if (score > bestScore) {
2680-
bestScore = score;
2681-
bestMatch = route;
2667+
if (route && route.name) {
2668+
const routeCoreName = route.name.split('||')[0].trim();
2669+
if (routeCoreName === coreRouteName) {
2670+
// 确保只返回一个ID,即使线路数据中包含多个ID
2671+
const singleId = String(route.id).split(',')[0].trim();
2672+
console.log('Core match found, returning single ID:', singleId);
2673+
return singleId;
26822674
}
26832675
}
26842676
}
26852677

2686-
if (bestMatch) {
2687-
// 确保只返回一个ID,即使线路数据中包含多个ID
2688-
const singleId = String(bestMatch.id).split(',')[0].trim();
2689-
console.log('Best partial match found, returning single ID:', singleId);
2690-
return singleId;
2678+
// 如果没有找到完全匹配,尝试使用不区分大小写的核心线路名称匹配
2679+
const lowerCoreRouteName = coreRouteName.toLowerCase();
2680+
for (const route of routesData) {
2681+
if (route && route.name) {
2682+
const routeCoreName = route.name.split('||')[0].trim().toLowerCase();
2683+
if (routeCoreName === lowerCoreRouteName) {
2684+
// 确保只返回一个ID,即使线路数据中包含多个ID
2685+
const singleId = String(route.id).split(',')[0].trim();
2686+
console.log('Case-insensitive core match found, returning single ID:', singleId);
2687+
return singleId;
2688+
}
2689+
}
26912690
}
26922691

26932692
console.log('No match found for route:', routeName);

0 commit comments

Comments
 (0)