login_controller.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package controllers
  2. import (
  3. "github.com/kataras/iris/v12/mvc"
  4. "xps/service"
  5. )
  6. type LoginController struct {
  7. Base
  8. Service service.LoginService
  9. }
  10. func (c *LoginController) BeforeActivation(b mvc.BeforeActivation) {
  11. b.Handle("POST", "/login", "Login" )
  12. b.Handle("GET", "/logout", "Logout" )
  13. b.Handle("GET", "/test", "GetTest" )
  14. b.Handle("GET", "/hello", "GetHello" )
  15. }
  16. func (c *LoginController) Login() *JsonResult {
  17. username := c.Ctx.FormValue("username")
  18. password := c.Ctx.FormValue("password")
  19. userNameErr := validate.Var(username, "required,min=4,max=32")
  20. if userNameErr != nil {
  21. return ResultErr("用户名格式错误", nil)
  22. }
  23. pwdErr := validate.Var(password, "required,min=4,max=20")
  24. if pwdErr != nil {
  25. return ResultErr("密码格式错误", nil)
  26. }
  27. bRet, msg, data := c.Service.CheckLogin(username, password)
  28. if !bRet {
  29. return ResultErr(msg, data)
  30. } else {
  31. return ResultOk(msg, data)
  32. }
  33. }
  34. func (c *LoginController) Logout() *JsonResult {
  35. userId := c.GetCurOcId()
  36. if userId > 0 {
  37. _ = c.Service.Logout(userId)
  38. }
  39. return ResultOk("登出成功", nil)
  40. }
  41. func (c *LoginController) GetTest() mvc.Response {
  42. return mvc.Response{
  43. ContentType: "text/html",
  44. Text: "<h1>Test</h1>",
  45. }
  46. }
  47. func (c *LoginController) GetHello() interface{} {
  48. return map[string]string{"message": "hello iris"}
  49. }