login.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 = model.LoginLogClientWeb
  51. }
  52. ut := service.AllService.UserService.Login(u, &model.LoginLog{
  53. UserId: u.Id,
  54. Client: f.DeviceInfo.Type,
  55. DeviceId: f.Id,
  56. Uuid: f.Uuid,
  57. Ip: c.ClientIP(),
  58. Type: model.LoginLogTypeAccount,
  59. Platform: f.DeviceInfo.Os,
  60. })
  61. c.JSON(http.StatusOK, apiResp.LoginRes{
  62. AccessToken: ut.Token,
  63. Type: "access_token",
  64. User: *(&apiResp.UserPayload{}).FromUser(u),
  65. })
  66. }
  67. // LoginOptions
  68. // @Tags 登录
  69. // @Summary 登录选项
  70. // @Description 登录选项
  71. // @Accept json
  72. // @Produce json
  73. // @Success 200 {object} []string
  74. // @Failure 500 {object} response.ErrorResponse
  75. // @Router /login-options [get]
  76. func (l *Login) LoginOptions(c *gin.Context) {
  77. ops := service.AllService.OauthService.GetOauthProviders()
  78. if global.Config.App.WebSso {
  79. ops = append(ops, model.OauthTypeWebauth)
  80. }
  81. var oidcItems []map[string]string
  82. for _, v := range ops {
  83. oidcItems = append(oidcItems, map[string]string{"name": v})
  84. }
  85. common, err := json.Marshal(oidcItems)
  86. if err != nil {
  87. response.Error(c, response.TranslateMsg(c, "SystemError")+err.Error())
  88. return
  89. }
  90. var res []string
  91. res = append(res, "common-oidc/"+string(common))
  92. for _, v := range ops {
  93. res = append(res, "oidc/"+v)
  94. }
  95. c.JSON(http.StatusOK, res)
  96. }
  97. // Logout
  98. // @Tags 登录
  99. // @Summary 登出
  100. // @Description 登出
  101. // @Accept json
  102. // @Produce json
  103. // @Success 200 {string} string
  104. // @Failure 500 {object} response.ErrorResponse
  105. // @Router /logout [post]
  106. func (l *Login) Logout(c *gin.Context) {
  107. u := service.AllService.UserService.CurUser(c)
  108. token, ok := c.Get("token")
  109. if ok {
  110. service.AllService.UserService.Logout(u, token.(string))
  111. }
  112. c.JSON(http.StatusOK, nil)
  113. }