13cd7a81df
CI / changes (push) Successful in 1m31s
CI / ad (push) Successful in 0s
CI / admin (push) Successful in 0s
CI / aiserver (push) Successful in 0s
CI / analysis (push) Successful in 1s
CI / bff (push) Successful in 0s
CI / chore (push) Successful in 1s
CI / equipment (push) Successful in 0s
CI / express (push) Successful in 0s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 0s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 0s
CI / product (push) Successful in 0s
CI / record (push) Successful in 1s
CI / sale (push) Successful in 0s
CI / store (push) Successful in 0s
CI / support (push) Successful in 0s
CI / task (push) Successful in 0s
CI / user (push) Successful in 26s
CI / water (push) Successful in 0s
CI / wecom (push) Successful in 0s
CI / ws (push) Successful in 0s
88 lines
2.3 KiB
Go
88 lines
2.3 KiB
Go
package logic
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"lone-services/pkg/redis"
|
|
"lone-services/pkg/utils"
|
|
user "lone-services/rpc/user/pb"
|
|
"lone-services/services/user/internal/dao"
|
|
"lone-services/services/user/internal/svc"
|
|
|
|
jsoniter "github.com/json-iterator/go"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
)
|
|
|
|
type LogoutLogic struct {
|
|
ctx context.Context
|
|
svcCtx *svc.ServiceContext
|
|
logx.Logger
|
|
}
|
|
|
|
func NewLogoutLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LogoutLogic {
|
|
return &LogoutLogic{
|
|
ctx: ctx,
|
|
svcCtx: svcCtx,
|
|
Logger: logx.WithContext(ctx),
|
|
}
|
|
}
|
|
|
|
func (l *LogoutLogic) Logout(in *user.LogoutReq) (*user.Response, error) {
|
|
loginUser := utils.GetUserFromCtx(l.ctx)
|
|
if loginUser.ID < utils.NumberOne {
|
|
return failResponse(utils.ErrorNoLoginInfo), nil
|
|
}
|
|
|
|
loginType, typeErr := resolveLoginCacheType(loginUser)
|
|
if typeErr != nil {
|
|
l.Errorf("logout resolve login type: %v", typeErr)
|
|
return failResponse(utils.Fail), nil
|
|
}
|
|
if loginType == utils.StringEmpty {
|
|
return outResponse(utils.ErrorParams, "无法识别登录类型"), nil
|
|
}
|
|
|
|
redisKeysKey := utils.GetLoginKeysKey(loginType, strconv.FormatInt(loginUser.ID, utils.NumberTen))
|
|
oldAuth, oldErr := redis.Client.Get(l.ctx, redisKeysKey).Result()
|
|
if oldErr != nil {
|
|
return failResponse(utils.ErrorNoLoginInfo), nil
|
|
}
|
|
|
|
var loginInfo utils.LoginRedis
|
|
if err := jsoniter.Unmarshal([]byte(oldAuth), &loginInfo); err != nil {
|
|
return failResponse(utils.ErrorNoLoginInfo), nil
|
|
}
|
|
|
|
if len(loginInfo.Token) > utils.NumberOne {
|
|
_ = redis.Client.Del(l.ctx, utils.GetLoginKey(loginType, loginInfo.Token)).Err()
|
|
}
|
|
if len(loginInfo.Refresh) > utils.NumberOne {
|
|
_ = redis.Client.Del(l.ctx, utils.GetLoginRefreshKey(loginType, loginInfo.Refresh)).Err()
|
|
}
|
|
_ = redis.Client.Del(l.ctx, redisKeysKey).Err()
|
|
|
|
return okResponse(nil), nil
|
|
}
|
|
|
|
func resolveLoginCacheType(loginUser utils.UserInfo) (string, error) {
|
|
if loginUser.ClientCode != utils.StringEmpty {
|
|
client, err := loadEnabledClient(loginUser.ClientCode)
|
|
if err != nil {
|
|
return utils.StringEmpty, err
|
|
}
|
|
return loginCacheType(client), nil
|
|
}
|
|
|
|
switch loginUser.Type {
|
|
case dao.TypeSale:
|
|
return utils.LoginTypeSale, nil
|
|
case dao.TypeStore:
|
|
return utils.LoginTypeStore, nil
|
|
case dao.TypeUser:
|
|
return utils.LoginTypeUser, nil
|
|
default:
|
|
return utils.StringEmpty, nil
|
|
}
|
|
}
|