CommentController.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package controllers
  2. import (
  3. "strings"
  4. "time"
  5. "github.com/mindoc-org/mindoc/conf"
  6. "github.com/mindoc-org/mindoc/models"
  7. "github.com/mindoc-org/mindoc/utils/pagination"
  8. )
  9. type CommentController struct {
  10. BaseController
  11. }
  12. func (c *CommentController) Lists() {
  13. docid, _ := c.GetInt("docid", 0)
  14. pageIndex, _ := c.GetInt("page", 1)
  15. // 获取评论、分页
  16. comments, count, pageIndex := models.NewComment().QueryCommentByDocumentId(docid, pageIndex, conf.PageSize, c.Member)
  17. page := pagination.PageUtil(int(count), pageIndex, conf.PageSize, comments)
  18. var data struct {
  19. DocId int `json:"doc_id"`
  20. Page pagination.Page `json:"page"`
  21. }
  22. data.DocId = docid
  23. data.Page = page
  24. c.JsonResult(0, "ok", data)
  25. return
  26. }
  27. func (c *CommentController) Create() {
  28. content := c.GetString("content")
  29. id, _ := c.GetInt("doc_id")
  30. _, err := models.NewDocument().Find(id)
  31. if err != nil {
  32. c.JsonResult(1, "文章不存在")
  33. }
  34. m := models.NewComment()
  35. m.DocumentId = id
  36. if c.Member == nil {
  37. c.JsonResult(1, "请先登录,再评论")
  38. }
  39. if len(c.Member.RealName) != 0 {
  40. m.Author = c.Member.RealName
  41. } else {
  42. m.Author = c.Member.Account
  43. }
  44. m.MemberId = c.Member.MemberId
  45. m.IPAddress = c.Ctx.Request.RemoteAddr
  46. m.IPAddress = strings.Split(m.IPAddress, ":")[0]
  47. m.CommentDate = time.Now()
  48. m.Content = content
  49. m.Insert()
  50. var data struct {
  51. DocId int `json:"doc_id"`
  52. }
  53. data.DocId = id
  54. c.JsonResult(0, "ok", data)
  55. }
  56. func (c *CommentController) Index() {
  57. c.Prepare()
  58. c.TplName = "comment/index.tpl"
  59. }
  60. func (c *CommentController) Delete() {
  61. if c.Ctx.Input.IsPost() {
  62. id, _ := c.GetInt("id", 0)
  63. m, err := models.NewComment().Find(id)
  64. if err != nil {
  65. c.JsonResult(1, "评论不存在")
  66. }
  67. doc, err := models.NewDocument().Find(m.DocumentId)
  68. if err != nil {
  69. c.JsonResult(1, "文章不存在")
  70. }
  71. // 判断是否有权限删除
  72. bookRole, _ := models.NewRelationship().FindForRoleId(doc.BookId, c.Member.MemberId)
  73. if m.CanDelete(c.Member.MemberId, bookRole) {
  74. err := m.Delete()
  75. if err != nil {
  76. c.JsonResult(1, "删除错误")
  77. } else {
  78. c.JsonResult(0, "ok")
  79. }
  80. } else {
  81. c.JsonResult(1, "没有权限删除")
  82. }
  83. }
  84. }