api.dart 3.4 KB

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