Skip to content

Commit

Permalink
增加随机功能
Browse files Browse the repository at this point in the history
  • Loading branch information
jianyun8023 committed Aug 26, 2024
1 parent 6d58697 commit 7271309
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 29 deletions.
2 changes: 1 addition & 1 deletion app/calibre-pages/src/components/BookCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const props = defineProps({
},
more_info: {
type: Boolean,
required: true
default: false
},
proxy_image: {
type: Boolean,
Expand Down
6 changes: 3 additions & 3 deletions app/calibre-pages/src/views/BatchMeta.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<template>
<div class="flex justify-center mb-8">
<el-row>
<el-col :span="1">
<el-col :span="1" :xs="4">
<el-text>查询:</el-text>
</el-col>
<el-col :span="2">
<el-col :span="2" :xs="18">
<el-select
v-model="filterType"
placeholder="类型"
Expand All @@ -16,7 +16,7 @@
<el-option label="ISBN" value="isbn"/>
</el-select>
</el-col>
<el-col :span="8" :offset="1">
<el-col :span="8" :offset="1" :xs="21">

<el-autocomplete
v-model="keyword"
Expand Down
43 changes: 28 additions & 15 deletions app/calibre-pages/src/views/Home.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,22 @@
</el-row>

<el-row>
<h2>文学</h2>
<el-row :gutter="20">
<!-- Repeat the book item structure here -->
<el-row class="full-width-row">
<h2>便便看看</h2>
<el-button link class="text-right" @click="randomSomeBooks">
换换
<el-icon>
<Refresh/>
</el-icon>
</el-button>
</el-row>
</el-row>
<el-row>
<h2>社会文化</h2>
<el-row :gutter="20">
<!-- Repeat the book item structure here -->
<el-col v-for="book in randomBooks" :key="book.id" :span="6" :lg="6" :sm="12" :xs="24">
<BookCard :book="book"/>
</el-col>
</el-row>
</el-row>

<el-row>
<el-row class="full-width-row">
<el-col :span="24" class="full-width-row">
Expand All @@ -52,13 +57,13 @@

<el-col :span="24" class="col-top">
<el-pagination justify="center"
size="small"
background
layout="prev, pager, next"
:total="allPublishers.length"
:page-size="publisherPage"
class="mt-4"
@change="handleCurrentChange"
size="small"
background
layout="prev, pager, next"
:total="allPublishers.length"
:page-size="publisherPage"
class="mt-4"
@change="handleCurrentChange"
/>
</el-col>
</el-row>
Expand Down Expand Up @@ -87,6 +92,7 @@ export default {
data() {
return {
recentBooks: [] as Book[],
randomBooks: [] as Book[],
publishers: [] as string[],
allPublishers: [] as string[],
publisherPage: 8,
Expand All @@ -95,19 +101,25 @@ export default {
created() {
this.fetchRecentBooks()
this.fetchPublishers()
this.randomSomeBooks()
},
methods: {
async fetchRecentBooks() {
const response = await fetch('/api/recently?limit=12')
const books = await response.json()
this.recentBooks = books.hits
this.recentBooks = books.data
},
async fetchPublishers() {
const response = await fetch('/api/publisher')
const publishers = await response.json()
this.allPublishers = publishers.data
this.publishers = publishers.data.slice(0, this.publisherPage)
},
async randomSomeBooks() {
const response = await fetch('/api/random?limit=12')
const books = await response.json()
this.randomBooks = books.data
},
searchByPublisher(publisher: string) {
this.$router.push({
path: '/search',
Expand Down Expand Up @@ -139,6 +151,7 @@ export default {
.col-top {
margin-top: 20px;
}
.col-bottom {
margin-bottom: 20px;
}
Expand Down
62 changes: 52 additions & 10 deletions internal/calibre/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io"
"io/fs"
"io/ioutil"
"math/rand"
"net/http"
"os"
"path"
Expand Down Expand Up @@ -48,6 +49,7 @@ func (c *Api) SetupRouter(r *gin.Engine) {
base.GET("/publisher", c.listPublisher)
// 最近更新Recently
base.GET("/recently", c.recently)
base.GET("/random", c.random)
base.POST("/index/update", c.updateIndex)
base.POST("/index/switch", c.switchIndex)
}
Expand Down Expand Up @@ -493,7 +495,7 @@ func (c *Api) recently(r *gin.Context) {
Offset: int64(offset),
}

c.client.Index(c.config.Search.Index).Search("", &searchRequest)
c.currentIndex().Search("", &searchRequest)

search, err := c.currentIndex().Search("", &searchRequest)
if err != nil {
Expand All @@ -519,15 +521,55 @@ func (c *Api) recently(r *gin.Context) {
}

r.JSON(http.StatusOK, gin.H{
"totalHits": search.TotalHits,
"totalPages": search.TotalPages,
"hitsPerPage": search.HitsPerPage,
"estimatedTotalHits": search.EstimatedTotalHits,
"offset": search.Offset,
"limit": search.Limit,
"processingTimeMs": search.ProcessingTimeMs,
"query": search.Query,
"hits": &books,
"data": &books,
"code": 200,
})
}

func (c *Api) random(r *gin.Context) {
limit, err := strconv.Atoi(r.DefaultQuery("limit", "10"))
if err != nil {
r.JSON(http.StatusBadRequest, gin.H{"error": "Invalid limit"})
return
}

RandomInt := func(min, max int) int {
return min + rand.Intn(max-min)
}
offset := RandomInt(0, 1000)

// 使用随机排序
searchRequest := meilisearch.SearchRequest{
Limit: int64(limit),
Offset: int64(offset),
}

c.currentIndex().Search("", &searchRequest)
search, err := c.currentIndex().Search("", &searchRequest)
if err != nil {
r.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}

books := make([]Book, len(search.Hits))
for i := range search.Hits {
tmp := search.Hits[i].(map[string]interface{})
jsonb, err := json.Marshal(tmp)
if err != nil {
r.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
book := Book{}
if err := json.Unmarshal(jsonb, &book); err != nil {
r.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
return
}
books[i] = book
}

r.JSON(http.StatusOK, gin.H{
"data": &books,
"code": 200,
})
}

Expand Down

0 comments on commit 7271309

Please sign in to comment.