diff --git a/backend/controller/accapi.go b/backend/controller/accapi.go index 839842c..fb1a791 100644 --- a/backend/controller/accapi.go +++ b/backend/controller/accapi.go @@ -73,16 +73,30 @@ func (ar *AccountRouter) LoginAccount(c *gin.Context) { domain := c.Request.Header.Get("Origin") // set-cookie - http.SetCookie(c.Writer, &http.Cookie{ - Name: "access-token", - Value: token, - Path: "/", - Domain: domain, - Secure: false, - SameSite: http.SameSiteNoneMode, - HttpOnly: true, - MaxAge: 3600, - }) + if gin.Mode() == gin.DebugMode { + http.SetCookie(c.Writer, &http.Cookie{ + Name: "access-token", + Value: token, + Path: "/", + Domain: domain, + Secure: true, + SameSite: http.SameSiteNoneMode, + HttpOnly: true, + MaxAge: 3600, + }) + } else { + // 正式环境用strict模式 + http.SetCookie(c.Writer, &http.Cookie{ + Name: "access-token", + Value: token, + Path: "/", + Domain: domain, + Secure: true, + SameSite: http.SameSiteStrictMode, + HttpOnly: true, + MaxAge: 3600, + }) + } ar.Success(c, gin.H{ "token": token, diff --git a/backend/controller/postapi.go b/backend/controller/postapi.go index 73ee8c3..00a4243 100644 --- a/backend/controller/postapi.go +++ b/backend/controller/postapi.go @@ -2,6 +2,7 @@ package controller import ( "bytes" + "strconv" "github.com/RockChinQ/Campux/backend/service" "github.com/gin-gonic/gin" @@ -26,6 +27,7 @@ func NewPostRouter(rg *gin.RouterGroup, ps service.PostService) *PostRouter { group.GET("/download-image/:key", pr.DownloadImage) group.POST("/get-self-posts", pr.GetSelfPosts) group.POST("/get-posts", pr.GetPosts) + group.GET("/get-post-info/:id", pr.GetPostInfo) return pr } @@ -183,3 +185,32 @@ func (pr *PostRouter) GetPosts(c *gin.Context) { "list": posts, }) } + +func (pr *PostRouter) GetPostInfo(c *gin.Context) { + _, err := pr.GetUin(c) + + if err != nil { + pr.StatusCode(c, 401, err.Error()) + return + } + + id := c.Param("id") + + idInt, err := strconv.Atoi(id) + + if err != nil { + pr.Fail(c, 1, err.Error()) + return + } + + post, err := pr.PostService.GetPost(idInt) + + if err != nil { + pr.Fail(c, 1, err.Error()) + return + } + + pr.Success(c, gin.H{ + "post": post, + }) +} diff --git a/backend/database/mongo.go b/backend/database/mongo.go index 5650e3d..1ec008c 100644 --- a/backend/database/mongo.go +++ b/backend/database/mongo.go @@ -147,3 +147,19 @@ func (m *MongoDBManager) GetPosts( return posts, nil } + +func (m *MongoDBManager) GetPost(id int) (*PostPO, error) { + var post PostPO + err := m.Client.Database(viper.GetString("database.mongo.db")).Collection(POST_COLLECTION).FindOne( + context.TODO(), + bson.M{"id": id}, + ).Decode(&post) + if err != nil { + if err == mongo.ErrNoDocuments { + return nil, nil + } else { + return nil, err + } + } + return &post, nil +} diff --git a/backend/service/post.go b/backend/service/post.go index f73b5a1..f42aeb6 100644 --- a/backend/service/post.go +++ b/backend/service/post.go @@ -83,4 +83,9 @@ 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) { return ps.DB.GetPosts(uin, status, timeOrder, page, pageSize) -} \ No newline at end of file +} + +// 获取单个稿件信息 +func (ps *PostService) GetPost(id int) (*database.PostPO, error) { + return ps.DB.GetPost(id) +}