golang-im聊天
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

86 lines
2.0 KiB

  1. package app
  2. import (
  3. "context"
  4. devicedomain "gim/internal/logic/domain/device"
  5. "gim/pkg/gerrors"
  6. "gim/pkg/pb"
  7. )
  8. type deviceApp struct{}
  9. var DeviceApp = new(deviceApp)
  10. // Register 注册设备
  11. func (*deviceApp) Register(ctx context.Context, in *pb.RegisterDeviceReq) (int64, error) {
  12. device := devicedomain.Device{
  13. Type: in.Type,
  14. Brand: in.Brand,
  15. Model: in.Model,
  16. SystemVersion: in.SystemVersion,
  17. SDKVersion: in.SdkVersion,
  18. }
  19. // 判断设备信息是否合法
  20. if !device.IsLegal() {
  21. return 0, gerrors.ErrBadRequest
  22. }
  23. err := devicedomain.DeviceRepo.Save(&device)
  24. if err != nil {
  25. return 0, err
  26. }
  27. return device.Id, nil
  28. }
  29. // SignIn 登录
  30. func (*deviceApp) SignIn(ctx context.Context, userId, deviceId int64, token string, connAddr string, clientAddr string) error {
  31. return devicedomain.DeviceService.SignIn(ctx, userId, deviceId, token, connAddr, clientAddr)
  32. }
  33. // Offline 设备离线
  34. func (*deviceApp) Offline(ctx context.Context, deviceId int64, clientAddr string) error {
  35. device, err := devicedomain.DeviceRepo.Get(deviceId)
  36. if err != nil {
  37. return err
  38. }
  39. if device == nil {
  40. return nil
  41. }
  42. if device.ClientAddr != clientAddr {
  43. return nil
  44. }
  45. device.Status = devicedomain.DeviceOffLine
  46. err = devicedomain.DeviceRepo.Save(device)
  47. if err != nil {
  48. return err
  49. }
  50. return nil
  51. }
  52. // ListOnlineByUserId 获取用户所有在线设备
  53. func (*deviceApp) ListOnlineByUserId(ctx context.Context, userId int64) ([]*pb.Device, error) {
  54. return devicedomain.DeviceService.ListOnlineByUserId(ctx, userId)
  55. }
  56. // GetDevice 获取设备信息
  57. func (*deviceApp) GetDevice(ctx context.Context, deviceId int64) (*pb.Device, error) {
  58. device, err := devicedomain.DeviceRepo.Get(deviceId)
  59. if err != nil {
  60. return nil, err
  61. }
  62. if device == nil {
  63. return nil, gerrors.ErrDeviceNotExist
  64. }
  65. return device.ToProto(), err
  66. }
  67. // ServerStop connect服务停止
  68. func (*deviceApp) ServerStop(ctx context.Context, connAddr string) error {
  69. return devicedomain.DeviceService.ServerStop(ctx, connAddr)
  70. }