login.go 4.4 KB

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