logoutLogic.go 847 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Code scaffolded by goctl. Safe to edit.
  2. // goctl 1.10.0
  3. package auth
  4. import (
  5. "context"
  6. "perms-system-server/internal/middleware"
  7. "perms-system-server/internal/response"
  8. "perms-system-server/internal/svc"
  9. "github.com/zeromicro/go-zero/core/logx"
  10. )
  11. type LogoutLogic struct {
  12. logx.Logger
  13. ctx context.Context
  14. svcCtx *svc.ServiceContext
  15. }
  16. func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic {
  17. return &LogoutLogic{
  18. Logger: logx.WithContext(ctx),
  19. ctx: ctx,
  20. svcCtx: svcCtx,
  21. }
  22. }
  23. func (l *LogoutLogic) Logout() error {
  24. userId := middleware.GetUserId(l.ctx)
  25. if userId == 0 {
  26. return response.ErrUnauthorized("未登录")
  27. }
  28. if _, err := l.svcCtx.SysUserModel.IncrementTokenVersion(l.ctx, userId); err != nil {
  29. return err
  30. }
  31. l.svcCtx.UserDetailsLoader.Clean(l.ctx, userId)
  32. return nil
  33. }