login.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. package api
  2. import (
  3. "Gwen/global"
  4. "Gwen/http/request/api"
  5. "Gwen/http/response"
  6. apiResp "Gwen/http/response/api"
  7. "Gwen/model"
  8. "Gwen/service"
  9. "encoding/json"
  10. "fmt"
  11. "github.com/gin-gonic/gin"
  12. "net/http"
  13. )
  14. type Login struct {
  15. }
  16. // Login 登录
  17. // @Tags 登录
  18. // @Summary 登录
  19. // @Description 登录
  20. // @Accept json
  21. // @Produce json
  22. // @Param body body api.LoginForm true "登录表单"
  23. // @Success 200 {object} apiResp.LoginRes
  24. // @Failure 500 {object} response.ErrorResponse
  25. // @Router /login [post]
  26. func (l *Login) Login(c *gin.Context) {
  27. f := &api.LoginForm{}
  28. err := c.ShouldBindJSON(f)
  29. //fmt.Println(f)
  30. if err != nil {
  31. global.Logger.Warn(fmt.Sprintf("Login Fail: %s %s %s", "ParamsError", c.RemoteIP(), c.ClientIP()))
  32. response.Error(c, 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.Error(c, 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.Error(c, response.TranslateMsg(c, "UsernameOrPasswordError"))
  45. return
  46. }
  47. //根据refer判断是webclient还是app
  48. ref := c.GetHeader("referer")
  49. if ref != "" {
  50. f.DeviceInfo.Type = "webclient"
  51. }
  52. ut := service.AllService.UserService.Login(u, &model.LoginLog{
  53. UserId: u.Id,
  54. Client: f.DeviceInfo.Type,
  55. Uuid: f.Uuid,
  56. Ip: c.ClientIP(),
  57. Type: model.LoginLogTypeAccount,
  58. Platform: f.DeviceInfo.Os,
  59. })
  60. c.JSON(http.StatusOK, apiResp.LoginRes{
  61. AccessToken: ut.Token,
  62. Type: "access_token",
  63. User: *(&apiResp.UserPayload{}).FromUser(u),
  64. })
  65. }
  66. // LoginOptions
  67. // @Tags 登录
  68. // @Summary 登录选项
  69. // @Description 登录选项
  70. // @Accept json
  71. // @Produce json
  72. // @Success 200 {object} []string
  73. // @Failure 500 {object} response.ErrorResponse
  74. // @Router /login-options [post]
  75. func (l *Login) LoginOptions(c *gin.Context) {
  76. oauthOks := []string{}
  77. err, _ := service.AllService.OauthService.GetOauthConfig(model.OauthTypeGithub)
  78. if err == nil {
  79. oauthOks = append(oauthOks, model.OauthTypeGithub)
  80. }
  81. err, _ = service.AllService.OauthService.GetOauthConfig(model.OauthTypeGoogle)
  82. if err == nil {
  83. oauthOks = append(oauthOks, model.OauthTypeGoogle)
  84. }
  85. err, _ = service.AllService.OauthService.GetOauthConfig(model.OauthTypeOidc)
  86. if err == nil {
  87. oauthOks = append(oauthOks, model.OauthTypeOidc)
  88. }
  89. oauthOks = append(oauthOks, model.OauthTypeWebauth)
  90. var oidcItems []map[string]string
  91. for _, v := range oauthOks {
  92. oidcItems = append(oidcItems, map[string]string{"name": v})
  93. }
  94. common, err := json.Marshal(oidcItems)
  95. if err != nil {
  96. response.Error(c, response.TranslateMsg(c, "SystemError")+err.Error())
  97. return
  98. }
  99. var res []string
  100. res = append(res, "common-oidc/"+string(common))
  101. for _, v := range oauthOks {
  102. res = append(res, "oidc/"+v)
  103. }
  104. c.JSON(http.StatusOK, res)
  105. }
  106. // Logout
  107. // @Tags 登录
  108. // @Summary 登出
  109. // @Description 登出
  110. // @Accept json
  111. // @Produce json
  112. // @Success 200 {string} string
  113. // @Failure 500 {object} response.ErrorResponse
  114. // @Router /logout [post]
  115. func (l *Login) Logout(c *gin.Context) {
  116. u := service.AllService.UserService.CurUser(c)
  117. token, ok := c.Get("token")
  118. if ok {
  119. service.AllService.UserService.Logout(u, token.(string))
  120. }
  121. c.JSON(http.StatusOK, nil)
  122. }