api.dart 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import 'dart:async';
  2. import 'dart:typed_data';
  3. import 'package:app_business/app_config.dart';
  4. import 'package:app_business/generated/base.pb.dart' as pb;
  5. import 'package:app_business/generated/google/protobuf/timestamp.pb.dart' as pb;
  6. import 'package:app_business/generated/track_offical.pbgrpc.dart' as pb;
  7. import 'package:app_business/service/app.dart';
  8. import 'package:common_pub/model.dart';
  9. import 'package:grpc/grpc.dart';
  10. import 'package:track_common/model/map_info.dart';
  11. import 'package:track_common/track_common.dart';
  12. import 'abase.dart';
  13. export 'package:app_business/generated/google/protobuf/timestamp.pb.dart';
  14. export 'package:app_business/generated/track_offical.pbgrpc.dart'
  15. show ToMatchRegusterAddRequest, ToMatchRegusterListRequest;
  16. typedef SmsType = pb.SmsType;
  17. typedef IdRequest = pb.IdRequest;
  18. extension _ImageExt on pb.NetImage {
  19. NetImage toModel() {
  20. return NetImage(url, md5);
  21. }
  22. }
  23. extension DateTimeExt on DateTime? {
  24. pb.Timestamp toPb() {
  25. if (this != null) {
  26. return pb.Timestamp.fromDateTime(this!.toUtc());
  27. } else {
  28. return pb.Timestamp();
  29. }
  30. }
  31. }
  32. class ApiService extends IService {
  33. ClientChannel? channel;
  34. @override
  35. Future<void> init() async {
  36. channel = _newChannel();
  37. }
  38. ClientChannel _newChannel() {
  39. return ClientChannel(
  40. AppConfig.apiHost,
  41. port: AppConfig.apiPort,
  42. options: const ChannelOptions(credentials: ChannelCredentials.secure()
  43. // ChannelCredentials.insecure()
  44. ),
  45. );
  46. }
  47. pb.ApiToClient get stub {
  48. return _newStub(timeout: 10.seconds);
  49. }
  50. pb.ApiToClient _newStub({Duration? timeout, ClientChannel? channel}) {
  51. if (this.channel == null) {
  52. throw Exception('$runtimeType 未初始化');
  53. }
  54. final metadata = <String, String>{};
  55. metadata['version'] = _appVersion;
  56. if (token != null) {
  57. metadata['token'] = token!;
  58. }
  59. // debug("token: $token");
  60. return pb.ApiToClient(channel ?? this.channel!,
  61. options: CallOptions(
  62. metadata: metadata,
  63. timeout: timeout,
  64. ));
  65. }
  66. String get _appVersion => Get.find<AppService>().appVersion;
  67. set token(String? v) {
  68. Get.find<AppService>().token.val = v ?? '';
  69. }
  70. String? get token {
  71. final app = Get.find<AppService>();
  72. final token = app.token.val;
  73. if (token.isEmpty) {
  74. return null;
  75. }
  76. return token;
  77. }
  78. // 获取短信验证码
  79. Future<void> authSendCodeToPhone(String phone, SmsType smsType) async {
  80. info('authSendCodeToPhone [$phone]');
  81. await stub.toSendCodeToPhoneV2(pb.ToSendCodeToPhoneRequestV2()
  82. ..phone = phone
  83. ..smsType = smsType);
  84. }
  85. // 场控端_登录
  86. Future<void> signIn(String userCode, String password, String ip) async {
  87. final r = await stub.toSignInV2(pb.ToSignInRequestV2()
  88. ..userCode = userCode
  89. ..password = password
  90. ..ip = ip);
  91. token = r.token;
  92. debug('sign in success: $token');
  93. }
  94. // 场控端_登出
  95. void signOut() {
  96. stub.toSignOutV2(pb.DefaultRequest());
  97. token = null;
  98. }
  99. Future<Duration> getSmsSendLeftTime(String phone) async {
  100. final r = await stub
  101. .toGetSmsSendLeftTimeV2(pb.GetSmsSendLeftTimeRequest()..phone = phone);
  102. info('getSmsSendLeftTime: $phone - ${r.second}s');
  103. return r.second.seconds;
  104. }
  105. Future<List<MapInfo>> getMapList(int limit, int offset) async {
  106. final r = await stub.toMapListV2(pb.MapListRequestV2()
  107. ..limit = limit
  108. ..offset = offset);
  109. return r.list
  110. .map((e) => MapInfo(e.mapId, e.name, e.distance.meter, e.description,
  111. e.mapScaleNumber, e.image.toModel()))
  112. .toList();
  113. }
  114. Future<BinReader> getBinReaderByMd5(Uint8List md5) async {
  115. final stream =
  116. stub.toGetBinaryByMd5(pb.ToGetBinaryByMd5Request()..md5 = md5);
  117. final controller = StreamController<List<int>>();
  118. controller.onCancel = () {
  119. stream.cancel();
  120. };
  121. Future<void> rcv() async {
  122. try {
  123. await for (final one in stream) {
  124. controller.add(one.data);
  125. }
  126. } finally {
  127. controller.close();
  128. stream.cancel();
  129. }
  130. }
  131. rcv();
  132. stream.headers.then((value) => debug(value));
  133. final headers = await stream.headers;
  134. final lenStr = headers['all-length']!;
  135. final length = int.parse(lenStr);
  136. final nonce = headers['nonce']!;
  137. final ext = headers['ext']!;
  138. return BinReader(
  139. data: controller.stream, length: length, ext: ext, nonce: nonce);
  140. }
  141. }