-
-
Notifications
You must be signed in to change notification settings - Fork 281
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
354 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package token | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/suyuan32/simple-admin-core/api/internal/logic/token" | ||
"github.com/suyuan32/simple-admin-core/api/internal/svc" | ||
"github.com/suyuan32/simple-admin-core/api/internal/types" | ||
"github.com/zeromicro/go-zero/rest/httpx" | ||
) | ||
|
||
// swagger:route POST /token/logout token logout | ||
// Force logging out by user UUID | 根据UUID强制用户退出 | ||
// Parameters: | ||
// + name: body | ||
// require: true | ||
// in: body | ||
// type: UUIDReq | ||
// Responses: | ||
// 200: SimpleMsg | ||
// 401: SimpleMsg | ||
// 500: SimpleMsg | ||
|
||
func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
var req types.UUIDReq | ||
if err := httpx.Parse(r, &req); err != nil { | ||
httpx.Error(w, err) | ||
return | ||
} | ||
|
||
l := token.NewLogoutLogic(r.Context(), svcCtx) | ||
resp, err := l.Logout(&req) | ||
if err != nil { | ||
httpx.Error(w, err) | ||
} else { | ||
httpx.OkJson(w, resp) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package user | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/zeromicro/go-zero/rest/httpx" | ||
|
||
"github.com/suyuan32/simple-admin-core/api/internal/logic/user" | ||
"github.com/suyuan32/simple-admin-core/api/internal/svc" | ||
) | ||
|
||
// swagger:route GET /user/logout user logout | ||
// Log out | 退出登陆 | ||
// Responses: | ||
// 200: SimpleMsg | ||
// 401: SimpleMsg | ||
// 500: SimpleMsg | ||
|
||
func LogoutHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
l := user.NewLogoutLogic(r.Context(), svcCtx) | ||
resp, err := l.Logout() | ||
if err != nil { | ||
httpx.Error(w, err) | ||
} else { | ||
httpx.OkJson(w, resp) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package token | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/suyuan32/simple-admin-core/api/internal/svc" | ||
"github.com/suyuan32/simple-admin-core/api/internal/types" | ||
"github.com/suyuan32/simple-admin-core/rpc/types/core" | ||
|
||
"github.com/zeromicro/go-zero/core/logx" | ||
) | ||
|
||
type LogoutLogic struct { | ||
logx.Logger | ||
ctx context.Context | ||
svcCtx *svc.ServiceContext | ||
} | ||
|
||
func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic { | ||
return &LogoutLogic{ | ||
Logger: logx.WithContext(ctx), | ||
ctx: ctx, | ||
svcCtx: svcCtx, | ||
} | ||
} | ||
|
||
func (l *LogoutLogic) Logout(req *types.UUIDReq) (resp *types.SimpleMsg, err error) { | ||
result, err := l.svcCtx.CoreRpc.BlockUserAllToken(context.Background(), &core.UUIDReq{UUID: req.UUID}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &types.SimpleMsg{Msg: result.Msg}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package user | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/suyuan32/simple-admin-core/api/internal/svc" | ||
"github.com/suyuan32/simple-admin-core/api/internal/types" | ||
"github.com/suyuan32/simple-admin-core/rpc/types/core" | ||
|
||
"github.com/zeromicro/go-zero/core/logx" | ||
) | ||
|
||
type LogoutLogic struct { | ||
logx.Logger | ||
ctx context.Context | ||
svcCtx *svc.ServiceContext | ||
} | ||
|
||
func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic { | ||
return &LogoutLogic{ | ||
Logger: logx.WithContext(ctx), | ||
ctx: ctx, | ||
svcCtx: svcCtx, | ||
} | ||
} | ||
|
||
func (l *LogoutLogic) Logout() (resp *types.SimpleMsg, err error) { | ||
result, err := l.svcCtx.CoreRpc.BlockUserAllToken(context.Background(), | ||
&core.UUIDReq{UUID: l.ctx.Value("userId").(string)}) | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &types.SimpleMsg{Msg: result.Msg}, nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package logic | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"time" | ||
|
||
"github.com/zeromicro/go-zero/core/errorx" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
"gorm.io/gorm" | ||
|
||
"github.com/suyuan32/simple-admin-core/common/logmessage" | ||
"github.com/suyuan32/simple-admin-core/rpc/internal/model" | ||
"github.com/suyuan32/simple-admin-core/rpc/internal/svc" | ||
"github.com/suyuan32/simple-admin-core/rpc/types/core" | ||
|
||
"github.com/zeromicro/go-zero/core/logx" | ||
) | ||
|
||
type BlockUserAllTokenLogic struct { | ||
ctx context.Context | ||
svcCtx *svc.ServiceContext | ||
logx.Logger | ||
} | ||
|
||
func NewBlockUserAllTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BlockUserAllTokenLogic { | ||
return &BlockUserAllTokenLogic{ | ||
ctx: ctx, | ||
svcCtx: svcCtx, | ||
Logger: logx.WithContext(ctx), | ||
} | ||
} | ||
|
||
func (l *BlockUserAllTokenLogic) BlockUserAllToken(in *core.UUIDReq) (*core.BaseResp, error) { | ||
result := l.svcCtx.DB.Model(&model.Token{}).Where("uuid = ?", in.UUID).Update("status", 0) | ||
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) { | ||
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", result.Error.Error())) | ||
return nil, status.Error(codes.Internal, errorx.DatabaseError) | ||
} | ||
|
||
var tokens []model.Token | ||
tokenData := l.svcCtx.DB.Where("uuid = ?", in.UUID).Where("status = ?", 0). | ||
Where("expire_at > ?", time.Now()).Find(&tokens) | ||
|
||
if tokenData.Error != nil && !errors.Is(tokenData.Error, gorm.ErrRecordNotFound) { | ||
logx.Errorw(logmessage.DatabaseError, logx.Field("Detail", result.Error.Error())) | ||
return nil, status.Error(codes.Internal, errorx.DatabaseError) | ||
} | ||
|
||
for _, v := range tokens { | ||
err := l.svcCtx.Redis.Set("token_"+v.Token, "1") | ||
if err != nil { | ||
logx.Errorw(logmessage.RedisError, logx.Field("Detail", err.Error())) | ||
return nil, status.Error(codes.Internal, errorx.RedisError) | ||
} | ||
} | ||
|
||
return &core.BaseResp{Msg: errorx.UpdateSuccess}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.