|
@@ -2,6 +2,12 @@ package device |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
"context" |
|
|
"context" |
|
|
|
|
|
"errors" |
|
|
|
|
|
"fmt" |
|
|
|
|
|
"gim/internal/business/comm/utils" |
|
|
|
|
|
"gim/pkg/db" |
|
|
|
|
|
"gim/pkg/gerrors" |
|
|
|
|
|
"gim/pkg/grpclib" |
|
|
"gim/pkg/logger" |
|
|
"gim/pkg/logger" |
|
|
"gim/pkg/pb" |
|
|
"gim/pkg/pb" |
|
|
"gim/pkg/rpc" |
|
|
"gim/pkg/rpc" |
|
@@ -14,6 +20,11 @@ type deviceService struct{} |
|
|
|
|
|
|
|
|
var DeviceService = new(deviceService) |
|
|
var DeviceService = new(deviceService) |
|
|
|
|
|
|
|
|
|
|
|
const ( |
|
|
|
|
|
SignInKey = "%s:gim_sign_in:%s" // 占位符: masterId, key的第一个字母 |
|
|
|
|
|
SignInExpire = 30 * time.Second |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
// Register 注册设备 |
|
|
// Register 注册设备 |
|
|
func (*deviceService) Register(ctx context.Context, device *Device) error { |
|
|
func (*deviceService) Register(ctx context.Context, device *Device) error { |
|
|
err := DeviceDao.Save(device) |
|
|
err := DeviceDao.Save(device) |
|
@@ -26,6 +37,18 @@ func (*deviceService) Register(ctx context.Context, device *Device) error { |
|
|
|
|
|
|
|
|
// SignIn 长连接登录 |
|
|
// SignIn 长连接登录 |
|
|
func (*deviceService) SignIn(ctx context.Context, userId, deviceId int64, token string, connAddr string, clientAddr string) error { |
|
|
func (*deviceService) SignIn(ctx context.Context, userId, deviceId int64, token string, connAddr string, clientAddr string) error { |
|
|
|
|
|
//TODO::限制操作 |
|
|
|
|
|
masterId, _ := grpclib.GetCtxMasterId(ctx) |
|
|
|
|
|
cacheKey := fmt.Sprintf(SignInKey, masterId, utils.Int64ToStr(userId)) |
|
|
|
|
|
if db.RedisUtil.Exists(cacheKey) { |
|
|
|
|
|
return errors.New("限频!!!") |
|
|
|
|
|
} else { |
|
|
|
|
|
err := db.RedisUtil.Set(SignInKey, "already", SignInExpire) |
|
|
|
|
|
if err != nil { |
|
|
|
|
|
return gerrors.WrapError(err) |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
_, err := rpc.GetBusinessIntClient().Auth(ctx, &pb.AuthReq{UserId: userId, DeviceId: deviceId, Token: token}) |
|
|
_, err := rpc.GetBusinessIntClient().Auth(ctx, &pb.AuthReq{UserId: userId, DeviceId: deviceId, Token: token}) |
|
|
if err != nil { |
|
|
if err != nil { |
|
|
return err |
|
|
return err |
|
|