api.dart 3.9 KB

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