diff --git a/backend/controller/postapi.go b/backend/controller/postapi.go index c773905..89b2df6 100644 --- a/backend/controller/postapi.go +++ b/backend/controller/postapi.go @@ -147,7 +147,7 @@ func (pr *PostRouter) GetSelfPosts(c *gin.Context) { } // 获取用户自己的帖子 - posts, err := pr.PostService.GetPosts( + posts, total, err := pr.PostService.GetPosts( uin, body.Status, *body.TimeOrder, @@ -161,7 +161,8 @@ func (pr *PostRouter) GetSelfPosts(c *gin.Context) { } pr.Success(c, gin.H{ - "list": posts, + "list": posts, + "total": total, }) } @@ -192,7 +193,7 @@ func (pr *PostRouter) GetPosts(c *gin.Context) { } // 获取稿件列表 - posts, err := pr.PostService.GetPosts( + posts, total, err := pr.PostService.GetPosts( body.Uin, body.Status, *body.TimeOrder, @@ -206,7 +207,8 @@ func (pr *PostRouter) GetPosts(c *gin.Context) { } pr.Success(c, gin.H{ - "list": posts, + "list": posts, + "total": total, }) } diff --git a/backend/database/mongo.go b/backend/database/mongo.go index 301200f..9c3a7e2 100644 --- a/backend/database/mongo.go +++ b/backend/database/mongo.go @@ -281,7 +281,7 @@ func (m *MongoDBManager) GetPosts( status PostStatus, timeOrder int, page, pageSize int, -) ([]PostPO, error) { +) ([]PostPO, int, error) { var posts []PostPO condition := bson.M{} @@ -296,6 +296,16 @@ func (m *MongoDBManager) GetPosts( findOptions := options.Find() findOptions.SetSort(bson.M{"created_at": timeOrder}) + + // 获取符合条件的总数 + totalResult, err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(POST_COLLECTION).CountDocuments(context.TODO(), condition) + + if err != nil { + return nil, 0, err + } + + total := int(totalResult) + findOptions.SetSkip(int64((page - 1) * pageSize)) findOptions.SetLimit(int64(pageSize)) @@ -305,16 +315,16 @@ func (m *MongoDBManager) GetPosts( findOptions, ) if err != nil { - return nil, err + return nil, 0, err } defer cursor.Close(context.Background()) err = cursor.All(context.Background(), &posts) if err != nil { - return nil, err + return nil, 0, err } - return posts, nil + return posts, total, nil } func (m *MongoDBManager) GetPost(id int) (*PostPO, error) { diff --git a/backend/service/post.go b/backend/service/post.go index 4d8dfc9..c8511d8 100644 --- a/backend/service/post.go +++ b/backend/service/post.go @@ -43,7 +43,7 @@ func (ps *PostService) DownloadImage(key string, ioWriter io.Writer) error { func (ps *PostService) PostNew(uuid string, uin int64, text string, images []string, anon bool) (int, error) { // 检查这个用户是否有未过审的帖子 - posts, err := ps.DB.GetPosts(uin, database.POST_STATUS_PENDING_APPROVAL, 1, 1, 1) + posts, _, err := ps.DB.GetPosts(uin, database.POST_STATUS_PENDING_APPROVAL, 1, 1, 1) if err != nil { return -1, err @@ -93,7 +93,7 @@ func (ps *PostService) PostNew(uuid string, uin int64, text string, images []str } // 获取用户的帖子 -func (ps *PostService) GetPosts(uin int64, status database.PostStatus, timeOrder int, page, pageSize int) ([]database.PostPO, error) { +func (ps *PostService) GetPosts(uin int64, status database.PostStatus, timeOrder int, page, pageSize int) ([]database.PostPO, int, error) { return ps.DB.GetPosts(uin, status, timeOrder, page, pageSize) } diff --git a/backend/service/routine/confirm_posted.go b/backend/service/routine/confirm_posted.go index dd3b5b7..d57c81b 100644 --- a/backend/service/routine/confirm_posted.go +++ b/backend/service/routine/confirm_posted.go @@ -12,7 +12,7 @@ func ConfirmPosted(db database.MongoDBManager, msq mq.RedisStreamMQ) { // 取出状态为“队列中”的稿件 // 检查消息队列中HGETALL publish_post_status:post_id 的所有值是否都是1 // 如果是, 则更新稿件状态为“已发布” - inQueuePosts, err := db.GetPosts(-1, database.POST_STATUS_IN_QUEUE, 1, 1, 10) + inQueuePosts, _, err := db.GetPosts(-1, database.POST_STATUS_IN_QUEUE, 1, 1, 10) if err != nil { return } diff --git a/backend/service/routine/schedule_publishing.go b/backend/service/routine/schedule_publishing.go index 1e0e1e2..5c94e8f 100644 --- a/backend/service/routine/schedule_publishing.go +++ b/backend/service/routine/schedule_publishing.go @@ -12,7 +12,7 @@ func SchedulePublishing(db database.MongoDBManager, msq mq.RedisStreamMQ) { // 从数据库中查询出所有待发布的稿件 // 遍历稿件, 发布到消息队列 // 更新稿件状态 - approvedPosts, err := db.GetPosts(-1, database.POST_STATUS_APPROVED, 1, 1, 10) + approvedPosts, _, err := db.GetPosts(-1, database.POST_STATUS_APPROVED, 1, 1, 10) if err != nil { return } diff --git a/frontend/src/pages/world.vue b/frontend/src/pages/world.vue index 34fe03b..58c436e 100644 --- a/frontend/src/pages/world.vue +++ b/frontend/src/pages/world.vue @@ -67,10 +67,13 @@ -
+
+ -
+
@@ -132,9 +135,9 @@ export default { filterForJudge: { "uin": -1, "status": "待审核", // 状态 - "time_order": 1, // 时间排序 + "time_order": -1, // 时间排序 "page": 1, - "page_size": 9999 + "page_size": 10 }, posts: [], filterStatus: ['全部', '待审核', '已通过', '已拒绝', '已取消', '队列中', '已发布', '失败', '待撤回', '已撤回'], @@ -143,6 +146,8 @@ export default { avatarUrl: "", userGroup: "user", judgePosts: [], + judgePages: 1, + judgeCurrentPage: 1, pullLoading: false, } }, @@ -224,6 +229,9 @@ export default { break } } + + filter.page = this.judgeCurrentPage + this.pullLoading = true this.$axios.post('/v1/post/get-posts', filter) .then((response) => { @@ -235,8 +243,6 @@ export default { this.pullLoading = false return } - // reverse - p.reverse() for (let i = 0; i < p.length; i++) { // 2024-04-12T08:19:51.096Z 转成日期,再转成字符串 let date = new Date(p[i].created_at) @@ -248,6 +254,9 @@ export default { } console.log(p) this.judgePosts = p + + // 计算页数 + this.judgePages = Math.ceil(response.data.data.total / this.filterForJudge.page_size) } else { this.toast(response.data.msg) }