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