You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
85 lines
1.9 KiB
85 lines
1.9 KiB
package user
|
|
|
|
import (
|
|
"backend/usercenter/api/internal/svc"
|
|
"backend/usercenter/api/internal/types"
|
|
"context"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
// github.com/spf13/cast // 类型转换
|
|
// github.com/golang-module/carbon/v2 // 日期时间处理
|
|
// github.com/jinzhu/copier/v2 // 结构体复制
|
|
// orm 目录下的 orm 包
|
|
)
|
|
|
|
type UserLogoutLogic struct {
|
|
logx.Logger
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
}
|
|
|
|
// 用户登出
|
|
func NewUserLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserLogoutLogic {
|
|
return &UserLogoutLogic{
|
|
Logger: logx.WithContext(ctx),
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
}
|
|
}
|
|
|
|
func (l *UserLogoutLogic) UserLogout(req *types.LogoutRequest) (resp *types.UsersResponse, err error) {
|
|
// 获取请求头里的token
|
|
// 从 context 中获取请求头里的 token
|
|
token := l.ctx.Value("token").(string)
|
|
fmt.Println(token)
|
|
if token == "" {
|
|
return &types.UsersResponse{
|
|
Success: false,
|
|
Message: "未获取到token",
|
|
Data: nil,
|
|
Code: 200,
|
|
}, nil
|
|
}
|
|
re := regexp.MustCompile(`(?i)^bearer\s*`)
|
|
token = re.ReplaceAllString(strings.TrimSpace(token), "")
|
|
fmt.Println(token)
|
|
// 查找token是否存在
|
|
exists, err := l.svcCtx.RedisClient.Exists(l.ctx, token).Result()
|
|
if err != nil {
|
|
return &types.UsersResponse{
|
|
Success: false,
|
|
Message: "登出失败",
|
|
Data: err.Error(),
|
|
Code: 200,
|
|
}, nil
|
|
}
|
|
if exists == 0 {
|
|
return &types.UsersResponse{
|
|
Success: false,
|
|
Message: "token不存在",
|
|
Data: nil,
|
|
Code: 200,
|
|
}, nil
|
|
}
|
|
|
|
// 删除token
|
|
err = l.svcCtx.RedisClient.Del(l.ctx, token).Err()
|
|
if err != nil {
|
|
return &types.UsersResponse{
|
|
Success: false,
|
|
Message: "登出失败",
|
|
Data: err.Error(),
|
|
Code: 200,
|
|
}, nil
|
|
}
|
|
|
|
return &types.UsersResponse{
|
|
Success: true,
|
|
Message: "登出成功",
|
|
Data: nil,
|
|
Code: 200,
|
|
}, nil
|
|
}
|
|
|