api.dart 3.9 KB

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