login.go 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. package admin
  2. import (
  3. "Gwen/global"
  4. "Gwen/http/request/admin"
  5. "Gwen/http/response"
  6. adResp "Gwen/http/response/admin"
  7. "Gwen/model"
  8. "Gwen/service"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. )
  12. type Login struct {
  13. }
  14. // Login 登录
  15. // @Tags 登录
  16. // @Summary 登录
  17. // @Description 登录
  18. // @Accept json
  19. // @Produce json
  20. // @Param body body admin.Login true "登录信息"
  21. // @Success 200 {object} response.Response{data=adResp.LoginPayload}
  22. // @Failure 500 {object} response.Response
  23. // @Router /admin/login [post]
  24. // @Security token
  25. func (ct *Login) Login(c *gin.Context) {
  26. f := &admin.Login{}
  27. err := c.ShouldBindJSON(f)
  28. if err != nil {
  29. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
  30. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  31. return
  32. }
  33. errList := global.Validator.ValidStruct(c, f)
  34. if len(errList) > 0 {
  35. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
  36. response.Fail(c, 101, errList[0])
  37. return
  38. }
  39. u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
  40. if u.Id == 0 {
  41. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), c.ClientIP()))
  42. response.Fail(c, 101, response.TranslateMsg(c, "UsernameOrPasswordError"))
  43. return
  44. }
  45. ut := service.AllService.UserService.Login(u, &model.LoginLog{
  46. UserId: u.Id,
  47. Client: "webadmin",
  48. Uuid: "",
  49. Ip: c.ClientIP(),
  50. Type: "account",
  51. Platform: f.Platform,
  52. })
  53. response.Success(c, &adResp.LoginPayload{
  54. Token: ut.Token,
  55. Username: u.Username,
  56. RouteNames: service.AllService.UserService.RouteNames(u),
  57. Nickname: u.Nickname,
  58. })
  59. }
  60. // Logout 登出
  61. // @Tags 登录
  62. // @Summary 登出
  63. // @Description 登出
  64. // @Accept json
  65. // @Produce json
  66. // @Success 200 {object} response.Response
  67. // @Failure 500 {object} response.Response
  68. // @Router /admin/logout [post]
  69. func (ct *Login) Logout(c *gin.Context) {
  70. u := service.AllService.UserService.CurUser(c)
  71. token, ok := c.Get("token")
  72. if ok {
  73. service.AllService.UserService.Logout(u, token.(string))
  74. }
  75. response.Success(c, nil)
  76. }