Skip to content

Commit c39a6c9

Browse files
frankslinclaude
andcommitted
修復人物頁面所有年份欄位的0值顯示問題
在所有涉及年份顯示的地方添加值驗證,當年份為0或無效值時顯示為「未詳」,而非直接輸出0。 主要改進: - 新增全局 isValidValue() 函數用於統一驗證年份值的有效性 - 修復基本資訊中的指數年、生年、卒年、享年等欄位 - 修復地理資訊中的起始年和終止年欄位 - 修復入仕資訊中的年份和年齡欄位 - 修復任官記錄的年份顯示(移除局部函數定義) - 修復社會區分中的起始年和終止年欄位 - 修復社會關係和著述中的年份欄位 這確保了用戶界面不會顯示誤導性的0值,而是清楚標示為「未詳」。 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 851b66b commit c39a6c9

1 file changed

Lines changed: 66 additions & 53 deletions

File tree

resources/views/cbdbapi/person.blade.php

Lines changed: 66 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -397,12 +397,16 @@ function line(label, value) {
397397
return '<div class="info-row"><span class="info-label">' + label + ':</span><span class="info-value">' + (value || '<span class="empty">(空)</span>') + '</span></div>';
398398
}
399399
400+
function isValidValue(val) {
401+
return val && val !== '0' && val !== 0 && val !== '未詳';
402+
}
403+
400404
function renderBasicInfo(info) {
401405
var html = '<div class="section-block"><div class="section-header">基本資訊</div><div class="section-content">';
402406
html += line('CBDB ID', escapeHtml(info.PersonId));
403407
var names = escapeHtml(info.ChName || '') + ' / ' + escapeHtml(info.EngName || '');
404408
html += line('中文名 / 英文名', names);
405-
html += line('指數年', escapeHtml(info.IndexYear));
409+
html += line('指數年', isValidValue(info.IndexYear) ? escapeHtml(info.IndexYear) : '未詳');
406410
var indexAddrParts = [];
407411
if (info.IndexAddr) {
408412
indexAddrParts.push(formatIdLabel(info.IndexAddr, info.IndexAddrId));
@@ -412,26 +416,26 @@ function renderBasicInfo(info) {
412416
var birthParts = [];
413417
birthParts.push(formatIdLabel(info.DynastyBirth, info.DynastyBirthId));
414418
birthParts.push(formatIdLabel(info.EraBirth, info.EraBirthId));
415-
if (info.EraYearBirth) {
419+
if (info.EraYearBirth && isValidValue(info.EraYearBirth)) {
416420
birthParts.push(escapeHtml(info.EraYearBirth) + '');
417421
}
418-
if (info.YearBirth) {
422+
if (info.YearBirth && isValidValue(info.YearBirth)) {
419423
birthParts.push('(' + escapeHtml(info.YearBirth) + ')');
420424
}
421425
html += line('生年', joinParts(birthParts, ' '));
422426
423427
var deathParts = [];
424428
deathParts.push(formatIdLabel(info.DynastyDeath, info.DynastyDeathId));
425429
deathParts.push(formatIdLabel(info.EraDeath, info.EraDeathId));
426-
if (info.EraYearDeath) {
430+
if (info.EraYearDeath && isValidValue(info.EraYearDeath)) {
427431
deathParts.push(escapeHtml(info.EraYearDeath) + '');
428432
}
429-
if (info.YearDeath) {
433+
if (info.YearDeath && isValidValue(info.YearDeath)) {
430434
deathParts.push('(' + escapeHtml(info.YearDeath) + ')');
431435
}
432436
html += line('卒年', joinParts(deathParts, ' '));
433437
434-
html += line('享年', escapeHtml(info.YearsLived));
438+
html += line('享年', isValidValue(info.YearsLived) ? escapeHtml(info.YearsLived) : '未詳');
435439
html += line('朝代', formatIdLabel(info.Dynasty, info.DynastyId));
436440
html += line('性別', info.Gender === '1' ? '女性' : (info.Gender === '0' ? '男性' : '<span class="empty">(空)</span>'));
437441
html += line('郡望', info.JunWang ? formatIdLabel(info.JunWang, info.JunWangId) : '<span class="empty">(空)</span>');
@@ -556,10 +560,10 @@ function renderAddresses(title, items) {
556560
if (item.MoveCount) {
557561
extra.push('順序:' + escapeHtml(item.MoveCount));
558562
}
559-
if (item.FirstYear) {
563+
if (item.FirstYear && isValidValue(item.FirstYear)) {
560564
extra.push('起始年:' + escapeHtml(item.FirstYear));
561565
}
562-
if (item.LastYear) {
566+
if (item.LastYear && isValidValue(item.LastYear)) {
563567
extra.push('終止年:' + escapeHtml(item.LastYear));
564568
}
565569
if (extra.length) {
@@ -606,10 +610,10 @@ function renderEntries(title, items) {
606610
}
607611
itemHtml += pieces.join('');
608612
var tail = [];
609-
if (item.RuShiYear) {
613+
if (item.RuShiYear && isValidValue(item.RuShiYear)) {
610614
tail.push('年份:' + escapeHtml(item.RuShiYear));
611615
}
612-
if (item.RuShiAge) {
616+
if (item.RuShiAge && isValidValue(item.RuShiAge)) {
613617
tail.push('年齡:' + escapeHtml(item.RuShiAge));
614618
}
615619
if (tail.length) {
@@ -636,65 +640,73 @@ function renderPostings(title, items) {
636640
if (!Array.isArray(items) || items.length === 0) {
637641
return '';
638642
}
643+
639644
var html = '<div class="section-block"><div class="section-header">' + title + '</div><div class="section-content">';
640645
items.forEach(function (item) {
641646
var itemHtml = '<div class="item-box">';
642-
var header = '';
643-
if (item.FirstYearRange) {
644-
header += '<span class="badge me-2">' + escapeHtml(item.FirstYearRange) + '</span>';
645-
} else if (item.FirstYear) {
646-
header += '<span class="badge me-2">' + escapeHtml(item.FirstYear) + '</span>';
647-
} else {
648-
header += '<span class="badge me-2">未詳</span>';
649-
}
647+
648+
// 官职名称
650649
if (item.OfficeName) {
651-
header += '<strong>' + escapeHtml(item.OfficeName) + '</strong>';
650+
itemHtml += '<strong>' + escapeHtml(item.OfficeName) + '</strong>';
652651
}
653-
itemHtml += header;
652+
654653
var details = [];
655-
if (item.FirstYear || item.FirstYearNianhao || item.FirstYearNiaohaoYear) {
656-
var startParts = [];
657-
if (item.FirstYearNianhao) {
658-
startParts.push(escapeHtml(item.FirstYearNianhao));
659-
}
660-
if (item.FirstYearNiaohaoYear) {
661-
startParts.push(escapeHtml(item.FirstYearNiaohaoYear) + '');
662-
}
663-
if (item.FirstYear) {
664-
startParts.push('(' + escapeHtml(item.FirstYear) + ')');
665-
}
666-
details.push('起始年:' + startParts.join(' '));
667-
} else {
668-
details.push('起始年:未詳');
654+
655+
// 起始年
656+
var startParts = [];
657+
if (item.FirstYearNianhao && isValidValue(item.FirstYearNianhao)) {
658+
startParts.push(escapeHtml(item.FirstYearNianhao));
669659
}
670-
if (item.LastYear || item.LastYearNianhao || item.LastYearNianhaoYear) {
671-
var endParts = [];
672-
if (item.LastYearNianhao) {
673-
endParts.push(escapeHtml(item.LastYearNianhao));
674-
}
675-
if (item.LastYearNianhaoYear) {
676-
endParts.push(escapeHtml(item.LastYearNianhaoYear) + '');
677-
}
678-
if (item.LastYear) {
679-
endParts.push('(' + escapeHtml(item.LastYear) + ')');
680-
}
681-
details.push('終止年:' + endParts.join(' '));
682-
} else {
683-
details.push('終止年:未詳');
660+
if (item.FirstYearNiaohaoYear && isValidValue(item.FirstYearNiaohaoYear)) {
661+
startParts.push(escapeHtml(item.FirstYearNiaohaoYear) + '');
662+
}
663+
if (item.FirstYear && isValidValue(item.FirstYear)) {
664+
startParts.push('(' + escapeHtml(item.FirstYear) + ')');
665+
}
666+
var startYearText = startParts.length > 0 ? startParts.join(' ') : '未詳';
667+
// 只有当年份不是"未詳"时,才添加年份限定詞
668+
if (startParts.length > 0 && item.FirstYearRange && isValidValue(item.FirstYearRange)) {
669+
startYearText += ' 年份限定詞:' + escapeHtml(item.FirstYearRange);
670+
}
671+
details.push('起始年:' + startYearText);
672+
673+
// 終止年
674+
var endParts = [];
675+
if (item.LastYearNianhao && isValidValue(item.LastYearNianhao)) {
676+
endParts.push(escapeHtml(item.LastYearNianhao));
677+
}
678+
if (item.LastYearNianhaoYear && isValidValue(item.LastYearNianhaoYear)) {
679+
endParts.push(escapeHtml(item.LastYearNianhaoYear) + '');
684680
}
681+
if (item.LastYear && isValidValue(item.LastYear)) {
682+
endParts.push('(' + escapeHtml(item.LastYear) + ')');
683+
}
684+
var endYearText = endParts.length > 0 ? endParts.join(' ') : '未詳';
685+
// 只有当年份不是"未詳"时,才添加年份限定詞
686+
if (endParts.length > 0 && item.LastYearRange && isValidValue(item.LastYearRange)) {
687+
endYearText += ' 年份限定詞:' + escapeHtml(item.LastYearRange);
688+
}
689+
details.push('終止年:' + endYearText);
690+
691+
// 地點
685692
if (item.AddrName) {
686693
details.push('地點:<strong>' + escapeHtml(item.AddrName) + '</strong>');
687694
}
695+
696+
// 出處
688697
if (item.Source) {
689698
var src = '出處:' + escapeHtml(item.Source);
690699
if (item.Pages) {
691700
src += ',頁 ' + escapeHtml(item.Pages);
692701
}
693702
details.push(src);
694703
}
704+
705+
//
695706
if (item.Notes) {
696707
details.push('註:' + escapeHtml(item.Notes));
697708
}
709+
698710
itemHtml += '<div class="small-text mt-2">' + details.join('<br>') + '</div>';
699711
itemHtml += '</div>';
700712
html += itemHtml;
@@ -717,10 +729,10 @@ function renderStatuses(title, items) {
717729
}
718730
text.push(label);
719731
}
720-
if (item.FirstYear) {
732+
if (item.FirstYear && isValidValue(item.FirstYear)) {
721733
text.push('起始年:' + escapeHtml(item.FirstYear));
722734
}
723-
if (item.LastYear) {
735+
if (item.LastYear && isValidValue(item.LastYear)) {
724736
text.push('終止年:' + escapeHtml(item.LastYear));
725737
}
726738
html += '<div class="item-box">' + (text.length ? text.join('') : '<span class="empty">(空)</span>') + '</div>';
@@ -777,7 +789,7 @@ function renderAssociations(title, items) {
777789
if (item.TextTitle) {
778790
base.push('' + escapeHtml(item.TextTitle) + '');
779791
}
780-
if (item.Year) {
792+
if (item.Year && isValidValue(item.Year)) {
781793
base.push('<span class="badge">年份:' + escapeHtml(item.Year) + '</span>');
782794
}
783795
itemHtml += base.join(' ');
@@ -817,7 +829,7 @@ function renderTexts(title, items) {
817829
}
818830
line.push(name);
819831
}
820-
if (item.Year) {
832+
if (item.Year && isValidValue(item.Year)) {
821833
line.push('著作年代:' + escapeHtml(item.Year));
822834
}
823835
if (item.Role) {
@@ -911,7 +923,8 @@ function renderPerson(person) {
911923
html += renderCollection('社會關係', (person.PersonSocialAssociation && person.PersonSocialAssociation.Association) || []);
912924
html += renderCollection('著述', (person.PersonTexts && person.PersonTexts.Text) || []);
913925
914-
html += '<div class="api-info-box"><strong>需要原始 JSON?</strong>請使用 <code>?id=' + escapeHtml(String(personId)) + '&amp;o=json</code></div>';
926+
var jsonUrl = '?id=' + escapeHtml(String(personId)) + '&o=json';
927+
html += '<div class="api-info-box"><strong>需要原始 JSON?</strong>請使用 <a href="' + jsonUrl + '" target="_blank" rel="noopener noreferrer"><code>' + jsonUrl.replace('&', '&amp;') + '</code></a></div>';
915928
916929
return html;
917930
}

0 commit comments

Comments
 (0)