login.go 1.8 KB

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