controller.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package user
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "github.com/astaxie/beego/logs"
  6. "nginx-ui/server/base"
  7. "nginx-ui/server/models"
  8. "nginx-ui/server/vo"
  9. )
  10. type Controller struct {
  11. base.Controller
  12. service *UserService
  13. }
  14. func NewUserController() *Controller {
  15. return &Controller{
  16. service: NewUserService(),
  17. }
  18. }
  19. // Login 登录
  20. func (c *Controller) Login() {
  21. var user models.User
  22. err := json.Unmarshal(c.Ctx.Input.RequestBody, &user)
  23. if err != nil {
  24. logs.Error(err, string(c.Ctx.Input.RequestBody))
  25. c.ErrorJson(err)
  26. return
  27. }
  28. resp := c.service.Login(&user)
  29. if resp.Success() {
  30. c.SetSession("user", user)
  31. }
  32. c.PostJson(resp)
  33. }
  34. // Logout 退出登录
  35. func (c *Controller) Logout() {
  36. user := c.RequiredUser()
  37. if user != nil {
  38. c.DelSession("user")
  39. }
  40. c.Json()
  41. }
  42. func (c *Controller) User() {
  43. user := c.RequiredUser()
  44. if user == nil {
  45. return
  46. }
  47. c.SetData(user).Json()
  48. }
  49. // Register 用户注册
  50. func (c *Controller) Register() {
  51. resp := c.service.SignUp(c.Ctx.Input.RequestBody)
  52. c.PostJson(resp)
  53. }
  54. // Users 获取全部用户信息
  55. func (c *Controller) Users() {
  56. user := c.RequiredUser()
  57. if user == nil {
  58. return
  59. }
  60. req := vo.PageReq{}
  61. if !c.ReadBody(&req) {
  62. return
  63. }
  64. resp, err := c.service.Users(&req)
  65. if err != nil {
  66. logs.Warn("Users get fail: %v", err)
  67. c.ErrorJson(err)
  68. return
  69. }
  70. c.SetData(resp).Json()
  71. }
  72. // Update 获取全部用户信息
  73. func (c *Controller) Update() {
  74. user := c.RequiredUser()
  75. if user == nil {
  76. return
  77. }
  78. req := models.User{}
  79. if !c.ReadBody(&req) {
  80. return
  81. }
  82. resp, err := c.service.Update(&req)
  83. if err != nil {
  84. logs.Warn("Users get fail: %v", err)
  85. c.ErrorJson(err)
  86. return
  87. }
  88. c.SetData(resp).Json()
  89. }
  90. // Update 获取全部用户信息
  91. func (c *Controller) UpdateUserInfo() {
  92. user := c.RequiredUser()
  93. if user == nil {
  94. return
  95. }
  96. req := models.User{}
  97. if !c.ReadBody(&req) {
  98. return
  99. }
  100. if req.Id != user.Id {
  101. c.ErrorJson(errors.New("更新失败,无操作权限!"))
  102. return
  103. }
  104. resp, err := c.service.Update(&req)
  105. if err != nil {
  106. logs.Warn("Users get fail: %v", err)
  107. c.ErrorJson(err)
  108. return
  109. }
  110. c.SetSession("user", *resp)
  111. c.SetData(resp).Json()
  112. }
  113. // UpdatePassword Update 获取全部用户信息
  114. func (c *Controller) UpdatePassword() {
  115. user := c.RequiredUser()
  116. if user == nil {
  117. return
  118. }
  119. req := vo.UserUpdatePassword{}
  120. if !c.ReadBody(&req) {
  121. return
  122. }
  123. req.Id = user.Id
  124. err := c.service.UpdatePassword(&req)
  125. if err != nil {
  126. logs.Warn("Users get fail: %v", err)
  127. c.ErrorJson(err)
  128. return
  129. }
  130. c.SetMsg("密码更新成功").Json()
  131. }
  132. // GetDetail 获取全部用户信息
  133. func (c *Controller) GetDetail() {
  134. user := c.RequiredUser()
  135. if user == nil {
  136. return
  137. }
  138. id, err := c.GetIntQuery("id")
  139. if err != nil {
  140. logs.Warn("Users get fail: %v", err)
  141. c.ErrorJson(err)
  142. return
  143. }
  144. query, err := c.service.GetDetail(id)
  145. if err != nil {
  146. logs.Warn("Users get fail: %v", err)
  147. c.ErrorJson(err)
  148. return
  149. }
  150. c.SetData(query).Json()
  151. }