login.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package account
  2. import (
  3. "errors"
  4. "mtp20access/global"
  5. "mtp20access/model/account/request"
  6. accountRsp "mtp20access/model/account/response"
  7. commonRequest "mtp20access/model/common/request"
  8. "mtp20access/model/common/response"
  9. accountService "mtp20access/service/account"
  10. "mtp20access/utils"
  11. "github.com/gin-gonic/gin"
  12. "go.uber.org/zap"
  13. )
  14. // Login 账户登录
  15. // @Summary 账户登录
  16. // @accept application/json
  17. // @Produce application/json
  18. // @Param data body request.LoginReq true "登录入参"
  19. // @Success 200 {object} response.Response{data=accountRsp.LoginRsp,msg=string} "返回包括用户信息,token,过期时间"
  20. // @Router /Account/Login [post]
  21. // @Tags 账户服务
  22. func Login(c *gin.Context) {
  23. g := utils.GinUtils{C: c}
  24. r := request.LoginReq{}
  25. g.BindJsonReq(&r)
  26. if loginaccount, token, _, expiresAt, err := accountService.Login(r, g.C.ClientIP()); err == nil {
  27. response.OkWithDetailed(accountRsp.LoginRsp{
  28. LoginID: int(loginaccount.LOGINID),
  29. UserID: int(loginaccount.USERID),
  30. Token: token,
  31. ExpiresAt: expiresAt,
  32. }, "登录成功", g.C)
  33. } else {
  34. global.M2A_LOG.Error(err.Error(), zap.Error(err))
  35. response.FailWithMessage(err.Error(), c)
  36. }
  37. }
  38. // TokenCheck Token校验
  39. // @Summary Token校验
  40. // @Security ApiKeyAuth
  41. // @accept application/json
  42. // @Produce application/json
  43. // @Success 200 {object} response.Response{msg=string} "操作成功"
  44. // @Router /Account/TokenCheck [get]
  45. // @Tags 账户服务
  46. func TokenCheck(c *gin.Context) {
  47. // 获取请求账号信息
  48. s, exists := c.Get("claims")
  49. if !exists {
  50. err := errors.New("获取请求账号信息异常")
  51. response.FailWithMessage(err.Error(), c)
  52. return
  53. }
  54. claims := s.(*commonRequest.CustomClaims)
  55. token := c.Request.Header.Get("x-token")
  56. if err := accountService.RestoreLoginWithToken(claims.LoginID, claims.Group, token); err != nil {
  57. response.FailWithMessage(err.Error(), c)
  58. return
  59. }
  60. response.Ok(c)
  61. }
  62. // Loginout 用户登出请求
  63. // @Summary 用户登出请求
  64. // @Security ApiKeyAuth
  65. // @accept application/json
  66. // @Produce application/json
  67. // @Success 200 {object} response.Response{msg=string} "出参"
  68. // @Router /Account/Loginout [get]
  69. // @Tags 账户服务
  70. func Loginout(c *gin.Context) {
  71. if err := accountService.Logout(c); err != nil {
  72. response.FailWithMessage(err.Error(), c)
  73. return
  74. }
  75. response.OkWithMessage("登出成功", c)
  76. }