login.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package admin
  2. import (
  3. "Gwen/global"
  4. "Gwen/http/controller/api"
  5. "Gwen/http/request/admin"
  6. apiReq "Gwen/http/request/api"
  7. "Gwen/http/response"
  8. adResp "Gwen/http/response/admin"
  9. "Gwen/model"
  10. "Gwen/service"
  11. "fmt"
  12. "github.com/gin-gonic/gin"
  13. )
  14. type Login struct {
  15. }
  16. // Login 登录
  17. // @Tags 登录
  18. // @Summary 登录
  19. // @Description 登录
  20. // @Accept json
  21. // @Produce json
  22. // @Param body body admin.Login true "登录信息"
  23. // @Success 200 {object} response.Response{data=adResp.LoginPayload}
  24. // @Failure 500 {object} response.Response
  25. // @Router /admin/login [post]
  26. // @Security token
  27. func (ct *Login) Login(c *gin.Context) {
  28. f := &admin.Login{}
  29. err := c.ShouldBindJSON(f)
  30. if err != nil {
  31. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
  32. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  33. return
  34. }
  35. errList := global.Validator.ValidStruct(c, f)
  36. if len(errList) > 0 {
  37. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
  38. response.Fail(c, 101, errList[0])
  39. return
  40. }
  41. u := service.AllService.UserService.InfoByUsernamePassword(f.Username, f.Password)
  42. if u.Id == 0 {
  43. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "UsernameOrPasswordError", c.RemoteIP(), c.ClientIP()))
  44. response.Fail(c, 101, response.TranslateMsg(c, "UsernameOrPasswordError"))
  45. return
  46. }
  47. ut := service.AllService.UserService.Login(u, &model.LoginLog{
  48. UserId: u.Id,
  49. Client: model.LoginLogClientWebAdmin,
  50. Uuid: "", //must be empty
  51. Ip: c.ClientIP(),
  52. Type: model.LoginLogTypeAccount,
  53. Platform: f.Platform,
  54. })
  55. responseLoginSuccess(c, u, ut.Token)
  56. }
  57. // Logout 登出
  58. // @Tags 登录
  59. // @Summary 登出
  60. // @Description 登出
  61. // @Accept json
  62. // @Produce json
  63. // @Success 200 {object} response.Response
  64. // @Failure 500 {object} response.Response
  65. // @Router /admin/logout [post]
  66. func (ct *Login) Logout(c *gin.Context) {
  67. u := service.AllService.UserService.CurUser(c)
  68. token, ok := c.Get("token")
  69. if ok {
  70. service.AllService.UserService.Logout(u, token.(string))
  71. }
  72. response.Success(c, nil)
  73. }
  74. // LoginOptions
  75. // @Tags 登录
  76. // @Summary 登录选项
  77. // @Description 登录选项
  78. // @Accept json
  79. // @Produce json
  80. // @Success 200 {object} []string
  81. // @Failure 500 {object} response.ErrorResponse
  82. // @Router /admin/login-options [post]
  83. func (ct *Login) LoginOptions(c *gin.Context) {
  84. ops := service.AllService.OauthService.GetOauthProviders()
  85. response.Success(c, gin.H{
  86. "ops": ops,
  87. "register": global.Config.App.Register,
  88. })
  89. }
  90. // OidcAuth
  91. // @Tags Oauth
  92. // @Summary OidcAuth
  93. // @Description OidcAuth
  94. // @Accept json
  95. // @Produce json
  96. // @Router /admin/oidc/auth [post]
  97. func (ct *Login) OidcAuth(c *gin.Context) {
  98. // o := &api.Oauth{}
  99. // o.OidcAuth(c)
  100. f := &apiReq.OidcAuthRequest{}
  101. err := c.ShouldBindJSON(f)
  102. if err != nil {
  103. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  104. return
  105. }
  106. err, code, url := service.AllService.OauthService.BeginAuth(f.Op)
  107. if err != nil {
  108. response.Error(c, response.TranslateMsg(c, err.Error()))
  109. return
  110. }
  111. service.AllService.OauthService.SetOauthCache(code, &service.OauthCacheItem{
  112. Action: service.OauthActionTypeLogin,
  113. Op: f.Op,
  114. Id: f.Id,
  115. DeviceType: "webadmin",
  116. // DeviceOs: ct.Platform(c),
  117. DeviceOs: f.DeviceInfo.Os,
  118. Uuid: f.Uuid,
  119. }, 5*60)
  120. response.Success(c, gin.H{
  121. "code": code,
  122. "url": url,
  123. })
  124. }
  125. // OidcAuthQuery
  126. // @Tags Oauth
  127. // @Summary OidcAuthQuery
  128. // @Description OidcAuthQuery
  129. // @Accept json
  130. // @Produce json
  131. // @Success 200 {object} response.Response{data=adResp.LoginPayload}
  132. // @Failure 500 {object} response.Response
  133. // @Router /admin/oidc/auth-query [get]
  134. func (ct *Login) OidcAuthQuery(c *gin.Context) {
  135. o := &api.Oauth{}
  136. u, ut := o.OidcAuthQueryPre(c)
  137. if ut == nil {
  138. return
  139. }
  140. responseLoginSuccess(c, u, ut.Token)
  141. }
  142. func responseLoginSuccess(c *gin.Context, u *model.User, token string) {
  143. lp := &adResp.LoginPayload{}
  144. lp.FromUser(u)
  145. lp.Token = token
  146. lp.RouteNames = service.AllService.UserService.RouteNames(u)
  147. response.Success(c, lp)
  148. }