Files
lone-services/services/percutaneous/internal/logic/getWsCodeLogic.go
T
2026-10-28 16:10:10 +08:00

62 lines
1.4 KiB
Go

package logic
import (
"context"
"fmt"
"lone-services/pkg/redis"
"lone-services/pkg/utils"
pkgws "lone-services/pkg/ws"
percutaneous "lone-services/rpc/percutaneous/pb"
"lone-services/services/percutaneous/internal/dao"
"lone-services/services/percutaneous/internal/svc"
jsoniter "github.com/json-iterator/go"
"github.com/zeromicro/go-zero/core/logx"
)
type GetWsCodeLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
BaseLogic
}
func NewGetWsCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWsCodeLogic {
return &GetWsCodeLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *GetWsCodeLogic) GetWsCode(in *percutaneous.GetWsCodeRequest) (*percutaneous.Response, error) {
id := in.UserId
if id < utils.NumberOne {
adminInfo := utils.GetUserFromCtx(l.ctx)
if adminInfo.ID < utils.NumberOne {
return l.fail(utils.ErrorNoLoginInfo)
}
id = adminInfo.ID
}
Type := WsTypeApp
if in.Type == utils.NumberTwo {
Type = WsTypeOperation
}
key := utils.MD5Encrypt(fmt.Sprintf("%d%s%s", id, dao.WsCode, Type))
redisKey := pkgws.RedisKeyQRSession + key
payload, err := jsoniter.MarshalToString(pkgws.Session{
Status: pkgws.StatusUsePosition,
ClientCode: key,
})
if err != nil {
return l.fail(utils.Fail)
}
if err := redis.Client.Set(l.ctx, redisKey, payload, pkgws.SessionTTL).Err(); err != nil {
return l.fail(utils.Fail)
}
return l.ok(key)
}