utils.dart 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import 'package:permission_handler/permission_handler.dart';
  2. import 'package:vibration/vibration.dart';
  3. /// 通用工具类
  4. class Utils {
  5. /// 静态常量定义区
  6. static const int DefaultVibrateDuration = 70; // 默认震动时长
  7. static const timeout = const Duration(seconds: 1);
  8. static final RegExp ssidPattern = RegExp(r'^\"(.*)\"$');
  9. static final RegExp cellNoPattern = RegExp(r'^1[3-9]\d{9}$');
  10. static final RegExp numberPattern = RegExp('[0-9]');
  11. static final RegExp namePattern = RegExp(r'[a-z,A-Z,0-9|\s]');
  12. static final RegExp passwordPattern = RegExp(
  13. '[a-z,A-Z,0-9|@|#|\$|%|&|-|+|(|)|.|,|\'|:|;|!|?|/|\||*|"|=|{|}|[|\\]|\\\\|_|`|~|\\^|<|\\>]');
  14. static final RegExp versionPattern = RegExp(r'(\d+)\.*(\d*)\.*(\d*)');
  15. /// 手机号码格式检查
  16. static bool testCellNo(String value) {
  17. if (value.isEmpty) {
  18. return false;
  19. }
  20. return cellNoPattern.hasMatch(value);
  21. }
  22. /// 震动
  23. static Future<Null> vibrate({duration = DefaultVibrateDuration}) async {
  24. bool? hasVibrator = await Vibration.hasVibrator();
  25. if (null != hasVibrator && !hasVibrator) {
  26. return;
  27. }
  28. bool? hasAmplitudeControl = await Vibration.hasAmplitudeControl();
  29. if (null != hasAmplitudeControl && !hasAmplitudeControl) {
  30. Vibration.vibrate(duration: duration, amplitude: 128);
  31. } else {
  32. Vibration.vibrate(duration: duration);
  33. }
  34. }
  35. /// 权限申请
  36. static Future<bool> checkPermission(Permission permission,
  37. {bool init = false}) async {
  38. var permissionName = permission.toString();
  39. PermissionStatus permissionStatus = await permission.status;
  40. if (permissionStatus.isGranted) {
  41. print('$permissionName 已有权限');
  42. return true;
  43. } else {
  44. bool isShow = await permission.shouldShowRequestRationale;
  45. if (init || isShow) {
  46. // 申请权限
  47. Map<Permission, PermissionStatus> statuses =
  48. await [permission].request();
  49. // 申请结果
  50. if (statuses[permission] == PermissionStatus.granted) {
  51. print('$permissionName 权限申请成功');
  52. return true;
  53. } else {
  54. print('$permissionName 权限申请被拒绝 ${statuses[permission]}');
  55. return false;
  56. }
  57. } else {
  58. print('$permissionName 权限已被禁用,请在设置中手动开启重新进入程序');
  59. openAppSettings();
  60. return false;
  61. }
  62. }
  63. }
  64. }
  65. /// 版本比较工具类
  66. class Version {
  67. String origin = ""; // 原版本字符串
  68. int major = -1; // 主版本
  69. int secondary = -1; // 次要版本
  70. int revision = -1; // 修订号
  71. Version(String origin) {
  72. this.origin = "";
  73. this.major = -1;
  74. this.secondary = -1;
  75. this.revision = -1;
  76. if (origin.isEmpty) {
  77. return;
  78. }
  79. origin = origin.trim();
  80. this.origin = origin;
  81. var m = Utils.versionPattern.firstMatch(origin);
  82. if (null == m || m.groupCount < 2) {
  83. return;
  84. }
  85. var str = m.group(1);
  86. var v = int.tryParse(str!);
  87. if (null != v) {
  88. this.major = v;
  89. }
  90. if (m.groupCount > 1) {
  91. str = m.group(2);
  92. v = int.tryParse(str!);
  93. if (null != v) {
  94. this.secondary = v;
  95. }
  96. }
  97. if (m.groupCount > 2) {
  98. str = m.group(3);
  99. v = int.tryParse(str!);
  100. if (null != v) {
  101. this.revision = v;
  102. }
  103. }
  104. }
  105. bool moreThan(Version obj) {
  106. if (obj.major < 0 || this.major < 0) {
  107. return false;
  108. }
  109. if (obj.major != this.major) {
  110. return this.major > obj.major;
  111. }
  112. if (obj.secondary != this.secondary) {
  113. return this.secondary > obj.secondary;
  114. }
  115. return this.revision > obj.revision;
  116. }
  117. }