blog_classification.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package system
  2. import (
  3. "gfast/app/service/cache_service"
  4. "gfast/library/response"
  5. "gfast/plugin/blog/model/blog_classification"
  6. "gfast/plugin/blog/service/blog_service"
  7. "github.com/gogf/gf/errors/gerror"
  8. "github.com/gogf/gf/frame/g"
  9. "github.com/gogf/gf/net/ghttp"
  10. "github.com/gogf/gf/util/gconv"
  11. "github.com/gogf/gf/util/gvalid"
  12. )
  13. // 简单博客管理-分类管理
  14. type BlogClassification struct{}
  15. func (c *BlogClassification) Add(r *ghttp.Request) {
  16. if r.Method == "POST" {
  17. var req *blog_classification.AddReq
  18. // 通过Parse方法解析获取参数
  19. err := r.Parse(&req)
  20. if err != nil {
  21. response.FailJson(true, r, err.(*gvalid.Error).FirstString())
  22. }
  23. // 调用service中的添加函数添加广告位
  24. err = blog_service.AddClassificationSave(req)
  25. if err != nil {
  26. response.FailJson(true, r, err.Error())
  27. }
  28. cache_service.New().RemoveByTag(cache_service.AdminBlogTag)
  29. response.SusJson(true, r, "添加成功!")
  30. }
  31. //获取上级分类(频道)
  32. menus, err := blog_service.GetMenuListChannel()
  33. if err != nil {
  34. response.FailJson(true, r, err.Error())
  35. }
  36. res := g.Map{
  37. "parentList": menus,
  38. }
  39. response.SusJson(true, r, "添加栏目", res)
  40. }
  41. func (c *BlogClassification) Delete(r *ghttp.Request) {
  42. // 从页面获取要删除记录的 ID int切片
  43. ids := r.GetInts("classificationId")
  44. if len(ids) == 0 {
  45. response.FailJson(true, r, "ID获取失败,删除失败")
  46. }
  47. err := blog_service.DeleteClassificationByIds(ids)
  48. if err != nil {
  49. response.FailJson(true, r, err.Error())
  50. }
  51. cache_service.New().RemoveByTag(cache_service.AdminBlogTag)
  52. response.SusJson(true, r, "删除成功")
  53. }
  54. func (c *BlogClassification) Edit(r *ghttp.Request) {
  55. // 如果是post提交的请求就执行修改操作
  56. if r.Method == "POST" {
  57. var editReq *blog_classification.EditReq
  58. // 通过Parse方法解析获取参数
  59. err := r.Parse(&editReq)
  60. if err != nil {
  61. response.FailJson(true, r, err.(*gvalid.Error).FirstString())
  62. }
  63. err = blog_service.EditClassificationSave(editReq)
  64. if err != nil {
  65. response.FailJson(true, r, err.Error())
  66. }
  67. cache_service.New().RemoveByTag(cache_service.AdminBlogTag)
  68. response.SusJson(true, r, "修改参数成功")
  69. }
  70. // 不是post提交的请求就到修改页面后查询出要修改的记录
  71. id := r.GetInt("classificationId")
  72. params, err := blog_service.GetClassificationByID(int64(id))
  73. if err != nil {
  74. response.FailJson(true, r, err.Error())
  75. }
  76. //获取上级分类(频道)
  77. menus, err := blog_service.GetMenuListChannel()
  78. if err != nil {
  79. response.FailJson(true, r, err.Error())
  80. }
  81. res := g.Map{
  82. "params": params,
  83. "parentList": menus,
  84. }
  85. response.SusJson(true, r, "ok", res)
  86. }
  87. func (c *BlogClassification) List(r *ghttp.Request) {
  88. // 定义一个结构体存储请求参数
  89. var req *blog_classification.SelectPageReq
  90. // 获取参数
  91. err := r.Parse(&req)
  92. if err != nil {
  93. response.FailJson(true, r, err.Error())
  94. }
  95. total, page, list, err := blog_service.SelectClassificationListByPage(req)
  96. if err != nil {
  97. response.FailJson(true, r, err.Error())
  98. }
  99. result := g.Map{
  100. "currentPage": page,
  101. "total": total,
  102. "list": list,
  103. }
  104. response.SusJson(true, r, "日志分类列表", result)
  105. }
  106. func (c *BlogClassification) Sort(r *ghttp.Request) {
  107. sorts := r.Get("sorts")
  108. s := gconv.Map(sorts)
  109. if s == nil {
  110. response.FailJson(true, r, "获取记录id、序号失败")
  111. }
  112. var err error
  113. for k, v := range s {
  114. _, err = blog_classification.Model.Where("classification_id=?", k).Data("classification_sort", v).Update()
  115. if err != nil {
  116. response.FailJson(true, r, err.Error())
  117. }
  118. }
  119. cache_service.New().RemoveByTag(cache_service.AdminBlogTag)
  120. response.SusJson(true, r, "排序成功")
  121. }
  122. // 查询所有分类名称和对应id
  123. func (c *BlogClassification) Type(r *ghttp.Request) {
  124. res, err := blog_classification.Model.FindAll()
  125. if err != nil {
  126. g.Log().Error(err)
  127. err = gerror.New("查询所有分类名称失败!")
  128. response.FailJson(true, r, err.Error())
  129. }
  130. response.SusJson(true, r, "分类名称列表", res)
  131. }