deviceauth_controller.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import 'dart:async';
  2. import 'package:get/get.dart';
  3. class DeviceauthController extends GetxController {
  4. // 用户类型:true为VIP用户,false为免费用户
  5. final isPremium = true.obs;
  6. // 授权码相关
  7. final authCode = '673999'.obs;
  8. final countdownSeconds = 899.obs; // 15分钟 = 900秒,从899开始倒计时
  9. Timer? _countdownTimer;
  10. // 输入码相关
  11. final inputCode = ''.obs;
  12. final isCodeComplete = false.obs;
  13. final isAuthorizing = false.obs;
  14. // 设备管理
  15. final currentDevices = <DeviceInfo>[].obs;
  16. final maxDevices = 4;
  17. // 授权状态
  18. final authorizationStatus = AuthorizationStatus.waiting.obs;
  19. @override
  20. void onInit() {
  21. super.onInit();
  22. _initializeDevices();
  23. _startCountdown();
  24. }
  25. @override
  26. void onClose() {
  27. _countdownTimer?.cancel();
  28. super.onClose();
  29. }
  30. /// 初始化设备列表
  31. void _initializeDevices() {
  32. currentDevices.add(
  33. DeviceInfo(
  34. id: '1',
  35. name: 'Current Device',
  36. type: DeviceTypeEnum.ios,
  37. date: '2022/01/25 13:45',
  38. isCurrent: true,
  39. ),
  40. );
  41. }
  42. /// 开始倒计时
  43. void _startCountdown() {
  44. _countdownTimer = Timer.periodic(const Duration(seconds: 1), (timer) {
  45. if (countdownSeconds.value > 0) {
  46. countdownSeconds.value--;
  47. } else {
  48. // 倒计时结束,刷新授权码
  49. _refreshAuthCode();
  50. countdownSeconds.value = 899;
  51. }
  52. });
  53. }
  54. /// 刷新授权码
  55. void _refreshAuthCode() {
  56. // 生成新的6位随机码
  57. final random = DateTime.now().millisecondsSinceEpoch;
  58. authCode.value = (random % 900000 + 100000).toString();
  59. }
  60. /// 设置输入码(由pinput调用)
  61. void setInputCode(String code) {
  62. inputCode.value = code;
  63. isCodeComplete.value = code.length == 6;
  64. }
  65. /// 提交授权码
  66. void submitAuthCode() {
  67. if (inputCode.value.length == 6) {
  68. isAuthorizing.value = true;
  69. authorizationStatus.value = AuthorizationStatus.configuring;
  70. // 模拟授权过程
  71. Timer(const Duration(seconds: 2), () {
  72. isAuthorizing.value = false;
  73. authorizationStatus.value = AuthorizationStatus.success;
  74. // 添加新设备
  75. currentDevices.add(
  76. DeviceInfo(
  77. id: '2',
  78. name: 'Android devices',
  79. type: DeviceTypeEnum.android,
  80. uid: 'UID 123-456-789-101',
  81. isCurrent: false,
  82. ),
  83. );
  84. // 清空输入码
  85. inputCode.value = '';
  86. isCodeComplete.value = false;
  87. });
  88. }
  89. }
  90. /// 移除设备
  91. void removeDevice(String deviceId) {
  92. currentDevices.removeWhere((device) => device.id == deviceId);
  93. }
  94. /// 复制授权码
  95. void copyAuthCode() {
  96. // TODO: 实现复制到剪贴板
  97. Get.snackbar('已复制', '授权码已复制到剪贴板');
  98. }
  99. /// 获取倒计时显示文本
  100. String get countdownText {
  101. final minutes = countdownSeconds.value ~/ 60;
  102. final seconds = countdownSeconds.value % 60;
  103. return '${minutes.toString().padLeft(2, '0')}:${seconds.toString().padLeft(2, '0')}';
  104. }
  105. /// 获取当前设备数量
  106. int get currentDeviceCount => currentDevices.length;
  107. /// 获取设备数量显示文本
  108. String get deviceCountText => '$currentDeviceCount/$maxDevices';
  109. }
  110. /// 设备信息类
  111. class DeviceInfo {
  112. final String id;
  113. final String name;
  114. final DeviceTypeEnum type;
  115. final String? uid;
  116. final String? date;
  117. final bool isCurrent;
  118. DeviceInfo({
  119. required this.id,
  120. required this.name,
  121. required this.type,
  122. this.uid,
  123. this.date,
  124. this.isCurrent = false,
  125. });
  126. }
  127. /// 设备类型枚举
  128. enum DeviceTypeEnum { ios, android, windows, mac }
  129. /// 授权状态枚举
  130. enum AuthorizationStatus { waiting, configuring, success, failed }