CommentController.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 len(c.Member.RealName) != 0 {
  37. m.Author = c.Member.RealName
  38. } else {
  39. m.Author = c.Member.Account
  40. }
  41. m.MemberId = c.Member.MemberId
  42. m.IPAddress = c.Ctx.Request.RemoteAddr
  43. m.IPAddress = strings.Split(m.IPAddress, ":")[0]
  44. m.CommentDate = time.Now()
  45. m.Content = content
  46. m.Insert()
  47. var data struct {
  48. DocId int `json:"doc_id"`
  49. }
  50. data.DocId = id
  51. c.JsonResult(0, "ok", data)
  52. }
  53. func (c *CommentController) Index() {
  54. c.Prepare()
  55. c.TplName = "comment/index.tpl"
  56. }
  57. func (c *CommentController) Delete() {
  58. if c.Ctx.Input.IsPost() {
  59. id, _ := c.GetInt("id", 0)
  60. m, err := models.NewComment().Find(id)
  61. if err != nil {
  62. c.JsonResult(1, "评论不存在")
  63. }
  64. doc, err := models.NewDocument().Find(m.DocumentId)
  65. if err != nil {
  66. c.JsonResult(1, "文章不存在")
  67. }
  68. // 判断是否有权限删除
  69. bookRole, _ := models.NewRelationship().FindForRoleId(doc.BookId, c.Member.MemberId)
  70. if m.CanDelete(c.Member.MemberId, bookRole) {
  71. err := m.Delete()
  72. if err != nil {
  73. c.JsonResult(1, "删除错误")
  74. } else {
  75. c.JsonResult(0, "ok")
  76. }
  77. } else {
  78. c.JsonResult(1, "没有权限删除")
  79. }
  80. }
  81. }