login.go 4.2 KB

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