func_logout.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package admin
  2. import (
  3. "net/http"
  4. "github.com/xinliangnote/go-gin-api/configs"
  5. "github.com/xinliangnote/go-gin-api/internal/code"
  6. "github.com/xinliangnote/go-gin-api/internal/pkg/core"
  7. "github.com/xinliangnote/go-gin-api/internal/repository/redis"
  8. "github.com/xinliangnote/go-gin-api/pkg/errors"
  9. )
  10. type logoutResponse struct {
  11. Username string `json:"username"` // 用户账号
  12. }
  13. // Logout 管理员登出
  14. // @Summary 管理员登出
  15. // @Description 管理员登出
  16. // @Tags API.admin
  17. // @Accept application/x-www-form-urlencoded
  18. // @Produce json
  19. // @Success 200 {object} logoutResponse
  20. // @Failure 400 {object} code.Failure
  21. // @Router /api/admin/logout [post]
  22. // @Security LoginToken
  23. func (h *handler) Logout() core.HandlerFunc {
  24. return func(c core.Context) {
  25. res := new(logoutResponse)
  26. res.Username = c.SessionUserInfo().UserName
  27. if !h.cache.Del(configs.RedisKeyPrefixLoginUser+c.GetHeader(configs.HeaderLoginToken), redis.WithTrace(c.Trace())) {
  28. c.AbortWithError(core.Error(
  29. http.StatusBadRequest,
  30. code.AdminLogOutError,
  31. code.Text(code.AdminLogOutError)).WithError(errors.New("cache del err")),
  32. )
  33. return
  34. }
  35. c.Payload(res)
  36. }
  37. }