1
0

CommentController.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package controllers
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/astaxie/beego"
  6. "github.com/mindoc-org/mindoc/conf"
  7. "github.com/mindoc-org/mindoc/models"
  8. "github.com/mindoc-org/mindoc/utils/pagination"
  9. )
  10. type CommentController struct {
  11. BaseController
  12. }
  13. func (c *CommentController) Lists() {
  14. docid, _ := c.GetInt("docid", 0)
  15. pageIndex, _ := c.GetInt("page", 1)
  16. beego.Info("CommentController.Lists", docid, pageIndex)
  17. // 获取评论、分页
  18. comments, count := models.NewComment().QueryCommentByDocumentId(docid, pageIndex, conf.PageSize)
  19. page := pagination.PageUtil(int(count), pageIndex, conf.PageSize, comments)
  20. beego.Info("docid=", docid, "Page", page)
  21. var data struct {
  22. DocId int `json:"doc_id"`
  23. Page pagination.Page `json:"page"`
  24. }
  25. data.DocId = docid
  26. data.Page = page
  27. c.JsonResult(0, "ok", data)
  28. return
  29. }
  30. func (c *CommentController) Create() {
  31. content := c.GetString("content")
  32. id, _ := c.GetInt("doc_id")
  33. m := models.NewComment()
  34. m.DocumentId = id
  35. if len(c.Member.RealName) != 0 {
  36. m.Author = c.Member.RealName
  37. } else {
  38. m.Author = c.Member.Account
  39. }
  40. m.MemberId = c.Member.MemberId
  41. m.IPAddress = c.Ctx.Request.RemoteAddr
  42. m.IPAddress = strings.Split(m.IPAddress, ":")[0]
  43. m.CommentDate = time.Now()
  44. m.Content = content
  45. beego.Info(m)
  46. m.Insert()
  47. c.JsonResult(0, "ok")
  48. }
  49. func (c *CommentController) Index() {
  50. c.Prepare()
  51. c.TplName = "comment/index.tpl"
  52. }