login.go 4.1 KB

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