global_view_model.dart 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import 'dart:async';
  2. import 'package:eitc_erm_dental_flutter/entity/version_info.dart';
  3. import 'package:eitc_erm_dental_flutter/funcs.dart';
  4. import 'package:eitc_erm_dental_flutter/http/api_service.dart';
  5. import 'package:eitc_erm_dental_flutter/http/http.dart';
  6. import 'package:flutter/foundation.dart';
  7. import 'package:flutter/services.dart';
  8. import 'package:riverpod_annotation/riverpod_annotation.dart';
  9. part 'global_view_model.g.dart';
  10. ///连接状态
  11. @riverpod
  12. class DeviceConnectStatus extends _$DeviceConnectStatus {
  13. int _connectCode = -1;
  14. Timer? _timer;
  15. @override
  16. bool build(MethodChannel videoChannel) {
  17. return _connectCode == 1;
  18. }
  19. ///开始检查
  20. void startCheck() {
  21. if (_timer != null) {
  22. return;
  23. }
  24. logd("开始检查连接");
  25. _timer = Timer.periodic(const Duration(seconds: 1, milliseconds: 500), (_) {
  26. _getConnectStatus();
  27. });
  28. //一开始就获取一次
  29. _getConnectStatus();
  30. }
  31. ///停止检查
  32. void stopCheck() {
  33. logd("停止检查连接");
  34. _timer?.cancel();
  35. _timer = null;
  36. }
  37. ///读取连接状态
  38. void _getConnectStatus() async {
  39. int code = await videoChannel.invokeMethod("getConnectStatus");
  40. if (_connectCode != code) {
  41. _connectCode = code;
  42. state = _connectCode == 1;
  43. }
  44. }
  45. }
  46. ///验证码
  47. @riverpod
  48. class Captcha extends _$Captcha {
  49. @override
  50. String build() {
  51. return "";
  52. }
  53. ///发送验证码
  54. Future<bool> sendCaptcha(String mobile) async {
  55. try {
  56. await Http.instance
  57. .request(ApiService(Http.instance.dio).sendCaptchaCode(mobile));
  58. } catch (e) {
  59. loge("发送验证码异常", error: e);
  60. return SynchronousFuture(false);
  61. }
  62. return SynchronousFuture(true);
  63. }
  64. ///检查验证码
  65. Future<bool> checkCaptcha(String captcha, String mobile) async {
  66. try {
  67. await Http.instance.request(
  68. ApiService(Http.instance.dio).checkCaptchaCode(captcha, mobile));
  69. } catch (e) {
  70. loge("检查验证码异常", error: e);
  71. return SynchronousFuture(false);
  72. }
  73. return SynchronousFuture(true);
  74. }
  75. }
  76. @riverpod
  77. class CheckNewVersion extends _$CheckNewVersion {
  78. @override
  79. String build() {
  80. return "";
  81. }
  82. ///检查新版本
  83. Future<VersionInfo?> checkNewVersion(String type) async {
  84. try {
  85. return Http.instance
  86. .request(ApiService(Http.instance.dio).getVersion(type));
  87. } catch (e) {
  88. loge("检查版本异常", error: e);
  89. }
  90. return null;
  91. }
  92. }