-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
update_token_logic.go
63 lines (54 loc) · 1.94 KB
/
update_token_logic.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package token
import (
"context"
"github.com/suyuan32/simple-admin-common/config"
"time"
"github.com/suyuan32/simple-admin-common/enum/common"
"github.com/suyuan32/simple-admin-common/i18n"
"github.com/suyuan32/simple-admin-common/msg/logmsg"
"github.com/suyuan32/simple-admin-common/utils/pointy"
"github.com/suyuan32/simple-admin-common/utils/uuidx"
"github.com/zeromicro/go-zero/core/errorx"
"github.com/suyuan32/simple-admin-core/rpc/internal/svc"
"github.com/suyuan32/simple-admin-core/rpc/internal/utils/dberrorhandler"
"github.com/suyuan32/simple-admin-core/rpc/types/core"
"github.com/zeromicro/go-zero/core/logx"
)
type UpdateTokenLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
}
func NewUpdateTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateTokenLogic {
return &UpdateTokenLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *UpdateTokenLogic) UpdateToken(in *core.TokenInfo) (*core.BaseResp, error) {
token, err := l.svcCtx.DB.Token.UpdateOneID(uuidx.ParseUUIDString(*in.Id)).
SetNotNilStatus(pointy.GetStatusPointer(in.Status)).
SetNotNilSource(in.Source).
Save(l.ctx)
if err != nil {
return nil, dberrorhandler.DefaultEntError(l.Logger, err, in)
}
if uint8(*in.Status) == common.StatusBanned {
expiredTime := token.ExpiredAt.Sub(time.Now())
if expiredTime > 0 {
err = l.svcCtx.Redis.Set(l.ctx, config.RedisTokenPrefix+token.Token, "1", expiredTime).Err()
if err != nil {
logx.Errorw(logmsg.RedisError, logx.Field("detail", err.Error()))
return nil, errorx.NewInternalError(i18n.RedisError)
}
}
} else if uint8(*in.Status) == common.StatusNormal {
err := l.svcCtx.Redis.Del(l.ctx, config.RedisTokenPrefix+token.Token).Err()
if err != nil {
logx.Errorw(logmsg.RedisError, logx.Field("detail", err.Error()))
return nil, errorx.NewInternalError(i18n.RedisError)
}
}
return &core.BaseResp{Msg: i18n.UpdateSuccess}, nil
}