Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Feat: 简化搜索结果展示
Browse files Browse the repository at this point in the history
  • Loading branch information
gggxbbb committed May 15, 2022
1 parent a08a667 commit 306aeee
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 76 deletions.
37 changes: 12 additions & 25 deletions templates/search_result.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
<!-- 简介 -->
<hgroup>
<h1>搜索:{{ .Keyword }}</h1>
<h2>分类: {{ .CategoryCount }} | 日记 (标题): {{ .NoteCount }} | 日记 (内容): {{ .NoteWithKeyCount }} |
地点: {{ .LocationCount }}</h2>
<h2>分类: {{ .CategoryCount }} | 日记: {{ .NoteCount }} | 地点: {{ .LocationCount }}</h2>
</hgroup>

<script>
Expand All @@ -27,36 +26,21 @@ <h2>分类: {{ .CategoryCount }} | 日记 (标题): {{ .NoteCount }} | 日记 (
<!-- 日记列表 -->
{{ if gt .NoteCount 0 }}
<div>
<h3>日记 (标题)</h3>
<h3>日记</h3>
<p>仅检索标题及内容</p>
<ul>
{{ range .Notes }}
<li>
<hgroup>
<h4><a href="/notes/{{ .ID }}" class="with_keyword">{{ .Title }}</a></h4>
<h5>{{ .Date}} <span data-tooltip="{{ .Weather }}">{{ .WeatherEmoji }}</span>|<span
<h4>{{ .Date}} <span data-tooltip="{{ .Weather }}">{{ .WeatherEmoji }}</span>|<span
data-tooltip="{{ .Mood}}">{{ .MoodEmoji }}</span> <a
href="/locations/{{ .Location }}">{{ .Location }}</a></h5>
href="/categories/{{ .CategoryID }}">{{ .CategoryName }}</a> | <a
href="/locations/{{ .Location }}">{{ .Location }}</a></h4>
</hgroup>
</li>
{{ end }}
</ul>
</div>
{{ end }}

<!-- 日记关键词列表 -->
{{ if gt .NoteWithKeyCount 0 }}
<div>
<h3>日记 (内容)</h3>
<ul>
{{ range .NotesWithKey }}
<li>
<hgroup>
<h4><a href="/notes/{{ .ID }}">{{ .Title }}</a></h4>
<h5>{{ .Date}} <span data-tooltip="{{ .Weather }}">{{ .WeatherEmoji }}</span>|<span
data-tooltip="{{ .Mood}}">{{ .MoodEmoji }}</span> <a
href="/locations/{{ .Location }}">{{ .Location }}</a></h5>
</hgroup>
<blockquote>... <span class="with_keyword">{{ .KeyContent }}</span> ...</blockquote>
{{ if .KeyContent }}
<blockquote>... <span class="with_keyword">{{ .KeyContent }}</span> ...</blockquote>
{{ end }}
</li>
{{ end }}
</ul>
Expand All @@ -74,6 +58,9 @@ <h3>分类</h3>
<h4><a href="/categories/{{ .ID }}" class="with_keyword">{{ .Name }}</a></h4>
<h5>子分类: {{ .SubcategoryCount }} | 日记: {{ .NoteCount }}</h5>
</hgroup>
{{ if .KeyContent }}
<blockquote><span class="with_keyword">{{ .KeyContent }}</span></blockquote>
{{ end }}
</li>
{{ end }}
</ul>
Expand Down
82 changes: 40 additions & 42 deletions web/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,62 +37,60 @@ func SearchResultPage(c *gin.Context) {
data.Keyword = keyword
data.Source = timenoteData.Source

var NotesResult []simpleNote
var NotesWithKeyResult []simpleNoteWithKey

for _, note := range timenoteData.Notes {
var nData simpleNote
nData.Title = note.Title
nData.ID = strconv.FormatInt(note.ID, 10)
nData.Date = note.GetDateStr()
nData.Weather = note.GetWeatherStr()
nData.WeatherEmoji = note.GetWeatherEmoji()
nData.Mood = note.GetMoodStr()
nData.MoodEmoji = note.GetMoodEmoji()
nData.CategoryID = strconv.FormatInt(note.CategoryID, 10)
nData.CategoryName = func() string {
c, s := timenoteData.FindCategory(note)
if !s {
return ""
} else {
return c.CategoryName
if strings.Contains(note.Title, keyword) || strings.Contains(note.Content, keyword) {
var nData simpleNoteWithKey
nData.Title = note.Title
nData.ID = strconv.FormatInt(note.ID, 10)
nData.Date = note.GetDateStr()
nData.Weather = note.GetWeatherStr()
nData.WeatherEmoji = note.GetWeatherEmoji()
nData.Mood = note.GetMoodStr()
nData.MoodEmoji = note.GetMoodEmoji()
nData.CategoryID = strconv.FormatInt(note.CategoryID, 10)
nData.CategoryName = func() string {
c, s := timenoteData.FindCategory(note)
if !s {
return ""
} else {
return c.CategoryName
}
}()
nData.Location = note.Location
if strings.Contains(note.Content, keyword) {
startIndex := strings.Index(note.Content, keyword)
endIndex := startIndex + len(keyword)
keyStart := startIndex - 50
if keyStart < 0 {
keyStart = 0
}
keyEnd := endIndex + 50
if keyEnd > len(note.Content) {
keyEnd = len(note.Content)
}
nData.KeyContent = note.Content[keyStart:keyEnd]
}
}()
nData.Location = note.Location
if strings.Contains(note.Title, keyword) {
NotesResult = append(NotesResult, nData)
}
if strings.Contains(note.Content, keyword) {
var nDataWithKey = simpleNoteWithKey{simpleNote: nData}
startIndex := strings.Index(note.Content, keyword)
endIndex := startIndex + len(keyword)
keyStart := startIndex - 30
if keyStart < 0 {
keyStart = 0
}
keyEnd := endIndex + 30
if keyEnd > len(note.Content) {
keyEnd = len(note.Content)
}
nDataWithKey.KeyContent = note.Content[keyStart:keyEnd]
NotesWithKeyResult = append(NotesWithKeyResult, nDataWithKey)
NotesWithKeyResult = append(NotesWithKeyResult, nData)
}
}

data.Notes = NotesResult
data.NoteCount = len(NotesResult)
data.NotesWithKey = NotesWithKeyResult
data.NoteWithKeyCount = len(NotesWithKeyResult)
data.Notes = NotesWithKeyResult
data.NoteCount = len(NotesWithKeyResult)

var CategoriesResult []simpleCategory
var CategoriesResult []simpleCategoryWithKey

for _, category := range timenoteData.Categories {
if strings.Contains(category.CategoryName, keyword) {
var cData simpleCategory
if strings.Contains(category.CategoryName, keyword) || strings.Contains(category.CategoryDesc, keyword) {
var cData simpleCategoryWithKey
cData.ID = strconv.FormatInt(category.ID, 10)
cData.Name = category.CategoryName
cData.NoteCount = len(timenoteData.FindSubNote(category))
cData.SubcategoryCount = len(timenoteData.FindSubCategory(category))
if strings.Contains(category.CategoryDesc, keyword) {
cData.KeyContent = category.CategoryDesc
}
CategoriesResult = append(CategoriesResult, cData)
}
}
Expand Down
21 changes: 12 additions & 9 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,21 @@ type simpleNoteWithKey struct {
KeyContent string `json:"key_content"`
}

type simpleCategoryWithKey struct {
simpleCategory
KeyContent string `json:"key_content"`
}

// searchResultPageData 搜索页面数据
type searchResultPageData struct {
basicData
Keyword string `json:"keyword"`
Notes []simpleNote `json:"notes"`
NoteCount int `json:"note_count"`
NotesWithKey []simpleNoteWithKey `json:"notes_with_key"`
NoteWithKeyCount int `json:"note_with_key_count"`
Categories []simpleCategory `json:"categories"`
CategoryCount int `json:"category_count"`
Locations []simpleLocation `json:"locations"`
LocationCount int `json:"location_count"`
Keyword string `json:"keyword"`
Notes []simpleNoteWithKey `json:"notes_with_key"`
NoteCount int `json:"note_with_key_count"`
Categories []simpleCategoryWithKey `json:"categories"`
CategoryCount int `json:"category_count"`
Locations []simpleLocation `json:"locations"`
LocationCount int `json:"location_count"`
}

// simpleError 错误数据
Expand Down

0 comments on commit 306aeee

Please sign in to comment.