41 lines
1.0 KiB
Go
41 lines
1.0 KiB
Go
package login
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
xhttp "bj_power_mes/common/httpx"
|
|
"bj_power_mes/internal/svc"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
// LogoutLogic 退出登��?
|
|
type LogoutLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 退出登录:删除 refresh token,使已签发的 access token 无法续期
|
|
func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic {
|
|
return &LogoutLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *LogoutLogic) Logout() error {
|
|
userId := xhttp.GetUidFromCtx(l.ctx)
|
|
if userId <= 0 {
|
|
l.Logger.Errorf("退出登录失��? 未获取到用户ID")
|
|
return nil // 兜底:不清除也不报错,前端已��?localStorage
|
|
}
|
|
key := fmt.Sprintf("user:%d:refesh_token", userId)
|
|
if err := l.svcCtx.TokenStore.Del(l.ctx, key); err != nil {
|
|
l.Logger.Errorf("退出登��? 删除 refresh token 失败 userId=%d error=%v", userId, err)
|
|
}
|
|
l.Logger.Infof("用户退出登��?userId=%d", userId)
|
|
return nil
|
|
} |