login.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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: "webadmin",
  51. Uuid: "", //must be empty
  52. Ip: c.ClientIP(),
  53. Type: "account",
  54. Platform: f.Platform,
  55. })
  56. response.Success(c, &adResp.LoginPayload{
  57. Token: ut.Token,
  58. Username: u.Username,
  59. RouteNames: service.AllService.UserService.RouteNames(u),
  60. Nickname: u.Nickname,
  61. })
  62. }
  63. // Logout 登出
  64. // @Tags 登录
  65. // @Summary 登出
  66. // @Description 登出
  67. // @Accept json
  68. // @Produce json
  69. // @Success 200 {object} response.Response
  70. // @Failure 500 {object} response.Response
  71. // @Router /admin/logout [post]
  72. func (ct *Login) Logout(c *gin.Context) {
  73. u := service.AllService.UserService.CurUser(c)
  74. token, ok := c.Get("token")
  75. if ok {
  76. service.AllService.UserService.Logout(u, token.(string))
  77. }
  78. response.Success(c, nil)
  79. }
  80. // LoginOptions
  81. // @Tags 登录
  82. // @Summary 登录选项
  83. // @Description 登录选项
  84. // @Accept json
  85. // @Produce json
  86. // @Success 200 {object} []string
  87. // @Failure 500 {object} response.ErrorResponse
  88. // @Router /admin/login-options [post]
  89. func (ct *Login) LoginOptions(c *gin.Context) {
  90. res := service.AllService.OauthService.List(1, 100, func(tx *gorm.DB) {
  91. tx.Select("op").Order("id")
  92. })
  93. var ops []string
  94. for _, v := range res.Oauths {
  95. ops = append(ops, v.Op)
  96. }
  97. response.Success(c, ops)
  98. }
  99. // OidcAuth
  100. // @Tags Oauth
  101. // @Summary OidcAuth
  102. // @Description OidcAuth
  103. // @Accept json
  104. // @Produce json
  105. // @Router /admin/oidc/auth [post]
  106. func (ct *Login) OidcAuth(c *gin.Context) {
  107. // o := &api.Oauth{}
  108. // o.OidcAuth(c)
  109. f := &apiReq.OidcAuthRequest{}
  110. err := c.ShouldBindJSON(f)
  111. if err != nil {
  112. response.Fail(c, 101, response.TranslateMsg(c, "ParamsError")+err.Error())
  113. return
  114. }
  115. err, code, url := service.AllService.OauthService.BeginAuth(f.Op)
  116. if err != nil {
  117. response.Error(c, response.TranslateMsg(c, err.Error()))
  118. return
  119. }
  120. service.AllService.OauthService.SetOauthCache(code, &service.OauthCacheItem{
  121. Action: service.OauthActionTypeLogin,
  122. Op: f.Op,
  123. Id: f.Id,
  124. DeviceType: "webadmin",
  125. // DeviceOs: ct.Platform(c),
  126. DeviceOs: f.DeviceInfo.Os,
  127. Uuid: f.Uuid,
  128. }, 5*60)
  129. response.Success(c, gin.H{
  130. "code": code,
  131. "url": url,
  132. })
  133. }
  134. // OidcAuthQuery
  135. // @Tags Oauth
  136. // @Summary OidcAuthQuery
  137. // @Description OidcAuthQuery
  138. // @Accept json
  139. // @Produce json
  140. // @Success 200 {object} response.Response{data=adResp.LoginPayload}
  141. // @Failure 500 {object} response.Response
  142. // @Router /admin/oidc/auth-query [get]
  143. func (ct *Login) OidcAuthQuery(c *gin.Context) {
  144. o := &api.Oauth{}
  145. u, ut := o.OidcAuthQueryPre(c)
  146. if ut == nil {
  147. return
  148. }
  149. //fmt.Println("u:", u)
  150. //fmt.Println("ut:", ut)
  151. response.Success(c, &adResp.LoginPayload{
  152. Token: ut.Token,
  153. Username: u.Username,
  154. RouteNames: service.AllService.UserService.RouteNames(u),
  155. Nickname: u.Nickname,
  156. })
  157. }