login.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. oauthOks = append(oauthOks, model.OauthTypeWebauth)
  86. var oidcItems []map[string]string
  87. for _, v := range oauthOks {
  88. oidcItems = append(oidcItems, map[string]string{"name": v})
  89. }
  90. common, err := json.Marshal(oidcItems)
  91. if err != nil {
  92. response.Error(c, response.TranslateMsg(c, "SystemError")+err.Error())
  93. return
  94. }
  95. var res []string
  96. res = append(res, "common-oidc/"+string(common))
  97. for _, v := range oauthOks {
  98. res = append(res, "oidc/"+v)
  99. }
  100. c.JSON(http.StatusOK, res)
  101. }
  102. // Logout
  103. // @Tags 登录
  104. // @Summary 登出
  105. // @Description 登出
  106. // @Accept json
  107. // @Produce json
  108. // @Success 200 {string} string
  109. // @Failure 500 {object} response.ErrorResponse
  110. // @Router /logout [post]
  111. func (l *Login) Logout(c *gin.Context) {
  112. u := service.AllService.UserService.CurUser(c)
  113. token, ok := c.Get("token")
  114. if ok {
  115. service.AllService.UserService.Logout(u, token.(string))
  116. }
  117. c.JSON(http.StatusOK, nil)
  118. }