user.go 910 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package controllers
  2. import (
  3. "encoding/json"
  4. "github.com/astaxie/beego/logs"
  5. "nginx-ui/server/models"
  6. "nginx-ui/server/service"
  7. )
  8. type UserController struct {
  9. BaseController
  10. service *service.UserService
  11. }
  12. func NewUserController() *UserController {
  13. return &UserController{
  14. service: service.NewUserService(),
  15. }
  16. }
  17. // Login 登录
  18. func (c *UserController) Login() {
  19. var user models.User
  20. err := json.Unmarshal(c.Ctx.Input.RequestBody, &user)
  21. if err != nil {
  22. logs.Error(err, string(c.Ctx.Input.RequestBody))
  23. c.ErrorJson(err)
  24. return
  25. }
  26. resp := c.service.Login(&user)
  27. if resp.Success() {
  28. c.SetSession("user", user)
  29. }
  30. c.postJson(resp)
  31. }
  32. func (c *UserController) User() {
  33. user := c.RequiredUser()
  34. if user == nil {
  35. return
  36. }
  37. c.setData(user).json()
  38. }
  39. // Register 用户注册
  40. func (c *UserController) Register() {
  41. resp := c.service.SignUp(c.Ctx.Input.RequestBody)
  42. c.postJson(resp)
  43. }