login.go 3.0 KB

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