links.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package controllers
  2. import (
  3. "cc-officialweb/models"
  4. "cc-officialweb/unity"
  5. "encoding/json"
  6. beego "github.com/beego/beego/v2/server/web"
  7. "github.com/go-playground/validator/v10"
  8. "strconv"
  9. )
  10. type LinksController struct {
  11. beego.Controller
  12. }
  13. // AddLinks 添加友情链接
  14. func (l *LinksController) AddLinks() {
  15. var linksDto models.LinkDto
  16. var link models.Link
  17. err := json.Unmarshal(l.Ctx.Input.RequestBody, &linksDto)
  18. if err != nil {
  19. l.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  20. l.ServeJSON()
  21. return
  22. }
  23. link.Title = linksDto.Title
  24. link.Url = linksDto.Url
  25. link.Sort = linksDto.Sort
  26. _, err = unity.Add(&link)
  27. if err != nil {
  28. l.Data["json"] = &JSON{Code: 101, Msg: "添加失败"}
  29. l.ServeJSON()
  30. return
  31. } else {
  32. l.Data["json"] = &JSON{Code: 200, Msg: "添加成功", Data: nil}
  33. l.ServeJSON()
  34. }
  35. }
  36. // UpdateLinks 更新友情链接
  37. func (l *LinksController) UpdateLinks() {
  38. var linksDto models.LinkDto
  39. var link models.Link
  40. err := json.Unmarshal(l.Ctx.Input.RequestBody, &linksDto)
  41. if err != nil {
  42. l.Data["json"] = &JSON{Code: 101, Msg: "json解析失败"}
  43. l.ServeJSON()
  44. return
  45. }
  46. link.Title = linksDto.Title
  47. link.Url = linksDto.Url
  48. link.Sort = linksDto.Sort
  49. _, err = unity.UpdateById(linksDto.ID, &link)
  50. if err != nil {
  51. l.Data["json"] = &JSON{Code: 102, Msg: err.Error()}
  52. l.ServeJSON()
  53. return
  54. } else {
  55. l.Data["json"] = &JSON{Code: 200, Msg: "修改成功"}
  56. l.ServeJSON()
  57. }
  58. }
  59. // DeleteLinksById 根据id删除产品
  60. func (l *LinksController) DeleteLinksById() {
  61. id := l.GetString("id")
  62. atoi, _ := strconv.Atoi(id)
  63. validate := validator.New()
  64. err2 := validate.Var(atoi, "required")
  65. if err2 != nil {
  66. l.Data["json"] = &JSON{Code: 103, Msg: "id不能为空"}
  67. l.ServeJSON()
  68. return
  69. }
  70. _, err := unity.DeleteById(atoi, models.Link{})
  71. if err != nil {
  72. l.Data["json"] = map[string]interface{}{"code": 101, "msg": "删除失败"}
  73. l.ServeJSON()
  74. return
  75. } else {
  76. l.Data["json"] = map[string]interface{}{"code": 200, "msg": "删除成功"}
  77. l.ServeJSON()
  78. }
  79. }
  80. // GetAllLinks 获取所有产品信息
  81. func (l *LinksController) GetAllLinks() {
  82. var page unity.PageParams
  83. err := json.Unmarshal(l.Ctx.Input.RequestBody, &page)
  84. if err != nil {
  85. l.Data["json"] = &JSON{Code: 101, Msg: "参数错误"}
  86. l.ServeJSON()
  87. return
  88. }
  89. result, total, current, err := unity.Paginate(page, models.Link{})
  90. if err != nil {
  91. l.Data["json"] = &JSON{Code: 101, Msg: "查询失败"}
  92. l.ServeJSON()
  93. return
  94. } else {
  95. l.Data["json"] = &JSON{Code: 200, Msg: "查询成功", Data: &JSONS{
  96. Current: current,
  97. Size: total,
  98. Data: result,
  99. }}
  100. l.ServeJSON()
  101. return
  102. }
  103. }