help.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package daytask
  2. import (
  3. "app/commons/model/entity"
  4. "app/commons/services"
  5. "github.com/gin-gonic/gin"
  6. )
  7. // HelpCategories 帮助分类列表
  8. func (s *Server) HelpCategories(c *gin.Context) {
  9. ctx := s.FromContext(c)
  10. db := s.DB()
  11. var categories []entity.DtHelpCategory
  12. db.Where("status = ?", 1).
  13. Order("sort ASC, id ASC").
  14. Find(&categories)
  15. ctx.OK(gin.H{
  16. "list": categories,
  17. })
  18. }
  19. // HelpList 帮助文章列表
  20. func (s *Server) HelpList(c *gin.Context) {
  21. ctx := s.FromContext(c)
  22. db := s.DB()
  23. categoryId := ctx.QueryInt64("categoryId", 0)
  24. keyword := ctx.QueryString("keyword", "")
  25. paging := &services.Pagination{
  26. Current: ctx.QueryInt64("current", 1),
  27. Size: ctx.QueryInt64("size", 20),
  28. }
  29. query := db.Model(&entity.DtHelp{}).Where("status = ?", 1)
  30. if categoryId > 0 {
  31. query = query.Where("category_id = ?", categoryId)
  32. }
  33. if keyword != "" {
  34. query = query.Where("title LIKE ? OR content LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
  35. }
  36. query.Count(&paging.Total)
  37. paging.Computer()
  38. type HelpInfo struct {
  39. Id int64 `json:"id"`
  40. CategoryId int64 `json:"categoryId"`
  41. Title string `json:"title"`
  42. ViewCount int `json:"viewCount"`
  43. CreatedAt int64 `json:"createdAt"`
  44. }
  45. helps := make([]*HelpInfo, 0)
  46. query.Select("id, category_id, title, view_count, created_at").
  47. Order("sort ASC, id DESC").
  48. Offset(int(paging.Start)).
  49. Limit(int(paging.Size)).
  50. Scan(&helps)
  51. ctx.OK(gin.H{
  52. "list": helps,
  53. "paging": paging,
  54. })
  55. }
  56. // HelpDetail 帮助文章详情
  57. func (s *Server) HelpDetail(c *gin.Context) {
  58. ctx := s.FromContext(c)
  59. db := s.DB()
  60. type Req struct {
  61. Id int64 `json:"id" form:"id" binding:"required"`
  62. }
  63. var req Req
  64. if err := c.ShouldBind(&req); err != nil {
  65. ctx.Fail("invalid_params")
  66. return
  67. }
  68. var help entity.DtHelp
  69. if err := db.Where("id = ? AND status = ?", req.Id, 1).First(&help).Error; err != nil {
  70. ctx.Fail("help_not_found")
  71. return
  72. }
  73. // 更新浏览次数
  74. db.Model(&entity.DtHelp{}).Where("id = ?", req.Id).
  75. Update("view_count", help.ViewCount+1)
  76. ctx.OK(gin.H{
  77. "id": help.Id,
  78. "categoryId": help.CategoryId,
  79. "title": help.Title,
  80. "content": help.Content,
  81. "viewCount": help.ViewCount + 1,
  82. "createdAt": help.CreatedAt,
  83. })
  84. }
  85. // HelpSearch 帮助搜索
  86. func (s *Server) HelpSearch(c *gin.Context) {
  87. ctx := s.FromContext(c)
  88. db := s.DB()
  89. keyword := ctx.QueryString("keyword", "")
  90. if keyword == "" {
  91. ctx.Fail("keyword_required")
  92. return
  93. }
  94. paging := &services.Pagination{
  95. Current: ctx.QueryInt64("current", 1),
  96. Size: ctx.QueryInt64("size", 20),
  97. }
  98. query := db.Model(&entity.DtHelp{}).
  99. Where("status = ?", 1).
  100. Where("title LIKE ? OR content LIKE ?", "%"+keyword+"%", "%"+keyword+"%")
  101. query.Count(&paging.Total)
  102. paging.Computer()
  103. type HelpInfo struct {
  104. Id int64 `json:"id"`
  105. CategoryId int64 `json:"categoryId"`
  106. Title string `json:"title"`
  107. ViewCount int `json:"viewCount"`
  108. CreatedAt int64 `json:"createdAt"`
  109. }
  110. helps := make([]*HelpInfo, 0)
  111. query.Select("id, category_id, title, view_count, created_at").
  112. Order("view_count DESC, id DESC").
  113. Offset(int(paging.Start)).
  114. Limit(int(paging.Size)).
  115. Scan(&helps)
  116. ctx.OK(gin.H{
  117. "list": helps,
  118. "paging": paging,
  119. })
  120. }