login.go 3.0 KB

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