route_controller.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package settings
  2. import (
  3. "errors"
  4. "nginx-ui/server/base"
  5. "nginx-ui/server/models"
  6. "nginx-ui/server/utils"
  7. )
  8. type RouteController struct {
  9. base.Controller
  10. }
  11. // List 查看全部路由
  12. func (c *RouteController) List() {
  13. current := c.RequiredUser()
  14. if current == nil {
  15. return
  16. }
  17. listVo := RouteListVo{}
  18. if !c.ReadBody(&listVo) {
  19. return
  20. }
  21. if listVo.Uid == -1 {
  22. listVo.Uid = current.Id
  23. }
  24. list, err := Route.GetList(listVo)
  25. if err != nil {
  26. c.ErrorJson(err)
  27. return
  28. }
  29. c.SetData(list).Json()
  30. }
  31. // Save 新增或者修改用户
  32. func (c *RouteController) Save() {
  33. current := c.RequiredUser()
  34. if current == nil {
  35. return
  36. }
  37. route := models.SettingRoute{}
  38. if !c.ReadBody(&route) {
  39. return
  40. }
  41. if route.Uid == -1 {
  42. route.Uid = current.Id
  43. if route.Id == "" {
  44. route.Id = utils.NextIdStr()
  45. }
  46. }
  47. if route.Id == "" {
  48. c.ErrorJson(errors.New("ID不能为空"))
  49. return
  50. }
  51. resp, err := Route.Save(&route)
  52. if err != nil {
  53. c.ErrorJson(err)
  54. return
  55. }
  56. c.SetData(resp).Json()
  57. }
  58. func (c *RouteController) Delete() {
  59. current := c.RequiredUser()
  60. if current == nil {
  61. return
  62. }
  63. route := models.SettingRoute{}
  64. if !c.ReadBody(&route) {
  65. return
  66. }
  67. err := Route.Delete(route.Id)
  68. if err != nil {
  69. c.ErrorJson(err)
  70. return
  71. }
  72. c.Json()
  73. }