| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import 'package:get/get.dart';
- import 'package:grpc/grpc.dart';
- import '../logger.dart';
- import 'app.dart';
- import '../global_var.dart';
- import '../pb.dart' as pb;
- export '../pb.dart';
- typedef SmsType = pb.SmsType;
- class ApiService extends GetxService{
- static ApiService get to => Get.find();
- AppService get app => AppService.to;
- ClientChannel? channel;
- String get _appVersion => app.appVersion;
- Future<ApiService> init()async{
- channel = _newChannel();
- await syncTime();
- return this;
- }
- String? get token {
- final out = app.userProfile.token.val;
- if (out.isEmpty) {
- return null;
- }
- return out;
- }
- set token(String? v) {
- app.userProfile.token.val = v ?? '';
- }
- ClientChannel _newChannel(){
- return ClientChannel(
- GlobalVar.apiHost,
- port: GlobalVar.apiPort,
- options: const ChannelOptions(credentials:
- // ChannelCredentials.secure()
- ChannelCredentials.insecure()
- ),
- );
- }
- pb.ApiToAppClient _newStub({Duration? timeout, ClientChannel? channel}){
- if (this.channel == null) {
- throw Exception('$runtimeType 未初始化');
- }
- final metadata = <String, String>{
- 'source': "${pb.LoginSource.ToApp.value}"
- };
- metadata['version'] = _appVersion;
- if (token != null) {
- metadata['token'] = token!;
- }
- debug("token: $token");
- return pb.ApiToAppClient(channel??this.channel!,
- options: CallOptions(
- metadata: metadata,
- timeout: timeout,
- ));
- }
- pb.ApiToAppClient get stub {
- return _newStub(timeout: 10.seconds);
- }
- // 获取系统时间
- Future<DateTime> serverTime() async {
- final begin = DateTime.now();
- final r = await stub.toGetServerTime(pb.DefaultRequest());
- final cost = DateTime.now().difference(begin);
- final serverNow = DateTime.fromMillisecondsSinceEpoch(
- r.millisecondStamp.toInt(),
- isUtc: true)
- .toLocal();
- return serverNow.add(cost);
- }
- Future<void> syncTime()async{
- try {
- final serverNow = await serverTime();
- app.correctByServerNow(serverNow);
- info('服务器时间:${app.now}');
- } catch(e){
- warn("获取服务器时间失败: ", e);
- }
- }
- // 获取短信验证码
- Future<void> authSendCodeToPhone(String phone, SmsType smsType)async{
- info('authSendCodeToPhone [$phone]');
- await stub.toSendCodeToPhoneV2(pb.ToSendCodeToPhoneRequestV2()
- ..phone= phone
- ..smsType= smsType
- );
- }
- // 场控端_登录
- Future<void> signIn(String userCode, String password, String ip) async {
- final r = await stub.toSignInV2(pb.ToSignInRequestV2()
- ..userCode = userCode
- ..password = password
- ..ip = ip
- );
- token = r.token;
- debug('sign in success: $token');
- }
- // 场控端_登出
- void signOut(){
- stub.toSignOutV2(pb.DefaultRequest());
- token = null;
- }
- Future<Duration> getSmsSendLeftTime(String phone)async{
- final r = await stub.toGetSmsSendLeftTimeV2(pb.GetSmsSendLeftTimeRequest()..phone= phone);
- info('getSmsSendLeftTime: $phone - ${r.second}s');
- return r.second.seconds;
- }
- }
|