add ws 链接确认
CI / changes (push) Successful in 1m43s
CI / ad (push) Successful in 1s
CI / admin (push) Successful in 0s
CI / bff (push) Successful in 37s
CI / chore (push) Successful in 1s
CI / equipment (push) Successful in 27s
CI / express (push) Successful in 0s
CI / factory (push) Successful in 0s
CI / message (push) Successful in 0s
CI / order (push) Successful in 1s
CI / payment (push) Successful in 0s
CI / percutaneous (push) Successful in 36s
CI / product (push) Successful in 0s
CI / record (push) Successful in 0s
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 1s
CI / water (push) Successful in 0s
CI / wecom (push) Successful in 1s

This commit is contained in:
2026-10-23 13:20:18 +08:00
parent 0346297803
commit 38cffc2f2a
11 changed files with 182 additions and 27 deletions
@@ -11,6 +11,11 @@ import (
jsoniter "github.com/json-iterator/go"
)
const (
WsTypeApp = "app"
WsTypeOperation = "operation"
)
type BaseLogic struct {
}
@@ -30,17 +30,21 @@ func NewGetWsCodeLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetWsCo
}
func (l *GetWsCodeLogic) GetWsCode(in *percutaneous.GetWsCodeRequest) (*percutaneous.Response, error) {
adminInfo := utils.GetUserFromCtx(l.ctx)
if adminInfo.ID < utils.NumberOne {
return l.fail(utils.ErrorNoLoginInfo)
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 := "app"
if in.Type == utils.NumberOne {
Type = "operation"
Type := WsTypeApp
if in.Type == utils.NumberTwo {
Type = WsTypeOperation
}
key := utils.MD5Encrypt(fmt.Sprintf("%d%s%s", adminInfo.ID, dao.WsCode, Type))
key := utils.MD5Encrypt(fmt.Sprintf("%d%s%s", id, dao.WsCode, Type))
redisKey := pkgws.RedisKeyQRSession + key
payload, err := jsoniter.MarshalToString(pkgws.Session{
@@ -40,8 +40,8 @@ func (l *SetPositionLogic) SetPosition(in *percutaneous.SetPositionRequest) (*pe
data := in.Position
if err := pkgws.Publish(l.ctx, in.SessionId, pkgws.Success(data)); err != nil {
l.Errorf("qrcode scan publish: %v", err)
l.Errorf("set position publish: %v", err)
}
return &percutaneous.Response{}, nil
return l.ok(utils.NumberOne)
}
@@ -0,0 +1,77 @@
package logic
import (
"context"
"fmt"
"lone-services/pkg/redis"
"lone-services/pkg/utils"
pkgws "lone-services/pkg/ws"
"lone-services/services/percutaneous/internal/dao"
"lone-services/rpc/percutaneous/pb"
"lone-services/services/percutaneous/internal/svc"
"github.com/zeromicro/go-zero/core/logx"
)
type WsClientStatusLogic struct {
ctx context.Context
svcCtx *svc.ServiceContext
logx.Logger
BaseLogic
}
func NewWsClientStatusLogic(ctx context.Context, svcCtx *svc.ServiceContext) *WsClientStatusLogic {
return &WsClientStatusLogic{
ctx: ctx,
svcCtx: svcCtx,
Logger: logx.WithContext(ctx),
}
}
func (l *WsClientStatusLogic) WsClientStatus(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
//1为web,2为设备
if in.Type == utils.NumberOne {
Type = WsTypeOperation
}
key := utils.MD5Encrypt(fmt.Sprintf("%d%s%s", id, dao.WsCode, Type))
redisKey := pkgws.RedisKeyQRSession + key
val, err := redis.Client.Get(l.ctx, redisKey).Result()
if err != nil && err.Error() != redis.Nil.Error() {
return l.fail(utils.Fail)
}
if len(val) > utils.NumberTen {
data := "ok"
keys := []string{WsTypeApp, WsTypeOperation}
for _, k := range keys {
key = utils.MD5Encrypt(fmt.Sprintf("%d%s%s", id, dao.WsCode, k))
redisKey = pkgws.RedisKeyQRSession + key
if err = pkgws.Publish(l.ctx, redisKey, pkgws.Success(data)); err != nil {
l.Errorf("ws client publish: %v", err)
return l.fail(utils.Fail)
}
redis.Client.Del(l.ctx, redisKey).Result()
}
return l.ok(utils.NumberOne)
}
if err := redis.Client.Set(l.ctx, redisKey, redisKey, pkgws.SessionTTL).Err(); err != nil {
return l.fail(utils.Fail)
}
return l.ok(utils.NumberOne)
}
@@ -64,6 +64,11 @@ func (s *PercutaneousServer) GetWsCode(ctx context.Context, in *percutaneous.Get
return l.GetWsCode(in)
}
func (s *PercutaneousServer) WsClientStatus(ctx context.Context, in *percutaneous.GetWsCodeRequest) (*percutaneous.Response, error) {
l := logic.NewWsClientStatusLogic(ctx, s.svcCtx)
return l.WsClientStatus(in)
}
// 添加用户
func (s *PercutaneousServer) WsAddUserStream(stream percutaneous.Percutaneous_WsAddUserStreamServer) error {
l := logic.NewWsAddUserStreamLogic(stream.Context(), s.svcCtx)
@@ -38,6 +38,7 @@ type (
// 使用指南
SetPosition(ctx context.Context, in *SetPositionRequest, opts ...grpc.CallOption) (*Response, error)
GetWsCode(ctx context.Context, in *GetWsCodeRequest, opts ...grpc.CallOption) (*Response, error)
WsClientStatus(ctx context.Context, in *GetWsCodeRequest, opts ...grpc.CallOption) (*Response, error)
// 添加用户
WsAddUserStream(ctx context.Context, opts ...grpc.CallOption) (percutaneous.Percutaneous_WsAddUserStreamClient, error)
}
@@ -94,6 +95,11 @@ func (m *defaultPercutaneous) GetWsCode(ctx context.Context, in *GetWsCodeReques
return client.GetWsCode(ctx, in, opts...)
}
func (m *defaultPercutaneous) WsClientStatus(ctx context.Context, in *GetWsCodeRequest, opts ...grpc.CallOption) (*Response, error) {
client := percutaneous.NewPercutaneousClient(m.cli.Conn())
return client.WsClientStatus(ctx, in, opts...)
}
// 添加用户
func (m *defaultPercutaneous) WsAddUserStream(ctx context.Context, opts ...grpc.CallOption) (percutaneous.Percutaneous_WsAddUserStreamClient, error) {
client := percutaneous.NewPercutaneousClient(m.cli.Conn())