api.dart 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import 'package:get/get.dart';
  2. import 'package:grpc/grpc.dart';
  3. import '../logger.dart';
  4. import 'app.dart';
  5. import '../global_var.dart';
  6. import '../pb.dart' as pb;
  7. export '../pb.dart';
  8. typedef SmsType = pb.SmsType;
  9. class ApiService extends GetxService{
  10. static ApiService get to => Get.find();
  11. AppService get app => AppService.to;
  12. ClientChannel? channel;
  13. String get _appVersion => app.appVersion;
  14. Future<ApiService> init()async{
  15. channel = _newChannel();
  16. await syncTime();
  17. return this;
  18. }
  19. String? get token {
  20. final out = app.userProfile.token.val;
  21. if (out.isEmpty) {
  22. return null;
  23. }
  24. return out;
  25. }
  26. set token(String? v) {
  27. app.userProfile.token.val = v ?? '';
  28. }
  29. ClientChannel _newChannel(){
  30. return ClientChannel(
  31. GlobalVar.apiHost,
  32. port: GlobalVar.apiPort,
  33. options: const ChannelOptions(credentials:
  34. // ChannelCredentials.secure()
  35. ChannelCredentials.insecure()
  36. ),
  37. );
  38. }
  39. pb.ApiToAppClient _newStub({Duration? timeout, ClientChannel? channel}){
  40. if (this.channel == null) {
  41. throw Exception('$runtimeType 未初始化');
  42. }
  43. final metadata = <String, String>{
  44. 'source': "${pb.LoginSource.ToApp.value}"
  45. };
  46. metadata['version'] = _appVersion;
  47. if (token != null) {
  48. metadata['token'] = token!;
  49. }
  50. debug("token: $token");
  51. return pb.ApiToAppClient(channel??this.channel!,
  52. options: CallOptions(
  53. metadata: metadata,
  54. timeout: timeout,
  55. ));
  56. }
  57. pb.ApiToAppClient get stub {
  58. return _newStub(timeout: 10.seconds);
  59. }
  60. // 获取系统时间
  61. Future<DateTime> serverTime() async {
  62. final begin = DateTime.now();
  63. final r = await stub.toGetServerTime(pb.DefaultRequest());
  64. final cost = DateTime.now().difference(begin);
  65. final serverNow = DateTime.fromMillisecondsSinceEpoch(
  66. r.millisecondStamp.toInt(),
  67. isUtc: true)
  68. .toLocal();
  69. return serverNow.add(cost);
  70. }
  71. Future<void> syncTime()async{
  72. try {
  73. final serverNow = await serverTime();
  74. app.correctByServerNow(serverNow);
  75. info('服务器时间:${app.now}');
  76. } catch(e){
  77. warn("获取服务器时间失败: ", e);
  78. }
  79. }
  80. // 获取短信验证码
  81. Future<void> authSendCodeToPhone(String phone, SmsType smsType)async{
  82. info('authSendCodeToPhone [$phone]');
  83. await stub.toSendCodeToPhoneV2(pb.ToSendCodeToPhoneRequestV2()
  84. ..phone= phone
  85. ..smsType= smsType
  86. );
  87. }
  88. // 场控端_登录
  89. Future<void> signIn(String userCode, String password, String ip) async {
  90. final r = await stub.toSignInV2(pb.ToSignInRequestV2()
  91. ..userCode = userCode
  92. ..password = password
  93. ..ip = ip
  94. );
  95. token = r.token;
  96. debug('sign in success: $token');
  97. }
  98. // 场控端_登出
  99. void signOut(){
  100. stub.toSignOutV2(pb.DefaultRequest());
  101. token = null;
  102. }
  103. Future<Duration> getSmsSendLeftTime(String phone)async{
  104. final r = await stub.toGetSmsSendLeftTimeV2(pb.GetSmsSendLeftTimeRequest()..phone= phone);
  105. info('getSmsSendLeftTime: $phone - ${r.second}s');
  106. return r.second.seconds;
  107. }
  108. }