o2_utils.dart 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:device_info_plus/device_info_plus.dart';
  4. import 'package:file_picker/file_picker.dart';
  5. import 'package:flutter/material.dart';
  6. import 'package:flutter/services.dart';
  7. import 'package:get/get.dart';
  8. import 'package:image_picker/image_picker.dart';
  9. import 'package:permission_handler/permission_handler.dart';
  10. import 'package:local_auth/local_auth.dart';
  11. import 'package:local_auth_android/local_auth_android.dart';
  12. import 'package:local_auth_ios/local_auth_ios.dart';
  13. import '../../environment_config.dart';
  14. import '../api/index.dart';
  15. import '../routers/index.dart';
  16. import '../services/index.dart';
  17. import '../widgets/index.dart';
  18. import 'loading.dart';
  19. import 'log_util.dart';
  20. import 'o2_api_manager.dart';
  21. import 'webscoket_util.dart';
  22. typedef OnFilePicker = void Function(List<String?> paths);
  23. class O2Utils {
  24. static Future<void> logout({bool hasToken = true}) async {
  25. Loading.show();
  26. // websocket 断开
  27. O2WebsocketClient().stop();
  28. if (GetPlatform.isMobile && hasToken) {
  29. await _unbindDeviceForJpush();
  30. }
  31. // 登出
  32. await OrgAuthenticationService.to.logout();
  33. // 清除保存的用户信息
  34. await O2ApiManager.instance.cleanUser();
  35. // 关闭极速打卡
  36. FastCheckInService.instance.stop();
  37. Loading.dismiss();
  38. var routePath = Get.currentRoute;
  39. if (EnvironmentConfig.o2AppIsDemo && O2OARoutes.demoLogin != routePath) {
  40. Get.offAllNamed(O2OARoutes.demoLogin);
  41. } else if (O2OARoutes.login != routePath) {
  42. Get.offAllNamed(O2OARoutes.login);
  43. }
  44. }
  45. static Future<void> _unbindDeviceForJpush() async {
  46. var deviceId = O2ApiManager.instance.deviceID;
  47. if (deviceId.isNotEmpty) {
  48. var result = await JpushAssembleControlService.to.unBindDevice(deviceId);
  49. if (result == null) {
  50. OLogger.e('设备号解除绑定失败!!!');
  51. }
  52. } else {
  53. OLogger.e('没有获取到极光推送的设备号!!!!!');
  54. }
  55. }
  56. static Map<String, dynamic> parseStringToJson(String jsonString) {
  57. try {
  58. final Map<String, dynamic> parsedJson = json.decode(jsonString);
  59. return parsedJson;
  60. } catch (e) {
  61. // JSON解析异常处理
  62. OLogger.e('解析JSON时出现异常: $e json: $jsonString');
  63. return {};
  64. }
  65. }
  66. ///
  67. ///判断是否手机号码
  68. ///中国的11位手机号码 和 香港澳门的 手机号码
  69. static bool isMobilePhoneNumber(String phoneNumber) {
  70. if (phoneNumber.isEmpty) {
  71. return false;
  72. }
  73. return GetUtils.hasMatch(phoneNumber, r'^(1)\d{10}$') ||
  74. GetUtils.hasMatch(phoneNumber, r'^((\+00)?(852|853)\d{8})$');
  75. }
  76. /// 判断存储权限
  77. ///
  78. static Future<bool> storagePermission() async {
  79. if (Platform.isAndroid) {
  80. final DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();
  81. AndroidDeviceInfo info = await deviceInfoPlugin.androidInfo;
  82. final sdk = info.version.sdkInt;
  83. if (sdk >= 33) {
  84. // Android sdk 33 以后没有storage权限了
  85. return true;
  86. }
  87. }
  88. var status = await Permission.storage.status;
  89. if (status == PermissionStatus.granted) {
  90. return true;
  91. } else {
  92. if (Platform.isAndroid) {
  93. O2OverlayEntryDialog.instance.openAlertDialog(
  94. 'common_permission_storage_request_title'.tr,
  95. 'common_permission_storage_request_content'.tr);
  96. }
  97. status = await Permission.storage.request();
  98. if (Platform.isAndroid) {
  99. O2OverlayEntryDialog.instance.closeDiloag();
  100. }
  101. if (status == PermissionStatus.granted) {
  102. return true;
  103. }
  104. }
  105. return false;
  106. }
  107. /// 摄像头权限判断
  108. static Future<bool> cameraPermission() async {
  109. var status = await Permission.camera.status;
  110. if (status == PermissionStatus.granted) {
  111. return true;
  112. } else {
  113. if (Platform.isAndroid) {
  114. O2OverlayEntryDialog.instance.openAlertDialog(
  115. 'common_permission_camera_request_title'.tr,
  116. 'common_permission_camera_request_content'.tr);
  117. }
  118. status = await Permission.camera.request();
  119. if (Platform.isAndroid) {
  120. O2OverlayEntryDialog.instance.closeDiloag();
  121. }
  122. if (status == PermissionStatus.granted) {
  123. return true;
  124. }
  125. }
  126. return false;
  127. }
  128. /// 定位权限判断
  129. static Future<bool> locationPermission() async {
  130. var status = await Permission.location.status;
  131. if (status == PermissionStatus.granted) {
  132. return true;
  133. } else {
  134. if (Platform.isAndroid) {
  135. O2OverlayEntryDialog.instance.openAlertDialog(
  136. 'common_permission_location_request_title'.tr,
  137. 'common_permission_location_request_content'.tr);
  138. }
  139. status = await Permission.location.request();
  140. if (Platform.isAndroid) {
  141. O2OverlayEntryDialog.instance.closeDiloag();
  142. }
  143. if (status == PermissionStatus.granted) {
  144. return true;
  145. }
  146. }
  147. return false;
  148. }
  149. /// 麦克风权限判断
  150. static Future<bool> microphonePermission() async {
  151. var status = await Permission.microphone.status;
  152. if (status == PermissionStatus.granted) {
  153. return true;
  154. } else {
  155. if (Platform.isAndroid) {
  156. O2OverlayEntryDialog.instance.openAlertDialog(
  157. 'common_permission_microphone_request_title'.tr,
  158. 'common_permission_microphone_request_content'.tr);
  159. }
  160. status = await Permission.microphone.request();
  161. if (Platform.isAndroid) {
  162. O2OverlayEntryDialog.instance.closeDiloag();
  163. }
  164. if (status == PermissionStatus.granted) {
  165. return true;
  166. }
  167. }
  168. return false;
  169. }
  170. /// 文件选择器
  171. /// @param callback 返回选择的文件结果
  172. static void pickerFileOrImage(OnFilePicker callback,
  173. {allowMultiple = false}) {
  174. // ios 可以打开相册
  175. final context = Get.context;
  176. if (context == null) {
  177. callback([]);
  178. return;
  179. }
  180. List<Widget> menus = [];
  181. if (Platform.isIOS) {
  182. menus = [
  183. ListTile(
  184. onTap: () {
  185. Navigator.pop(context);
  186. _takePhoto(callback);
  187. },
  188. title: Align(
  189. alignment: Alignment.center,
  190. child: Text('take_photo'.tr,
  191. style: Theme.of(context).textTheme.bodyMedium),
  192. ),
  193. ),
  194. const Divider(height: 1),
  195. ListTile(
  196. onTap: () {
  197. Navigator.pop(context);
  198. _pickImage(callback);
  199. },
  200. title: Align(
  201. alignment: Alignment.center,
  202. child:
  203. Text('album'.tr, style: Theme.of(context).textTheme.bodyMedium),
  204. ),
  205. ),
  206. const Divider(height: 1),
  207. ListTile(
  208. onTap: () {
  209. Navigator.pop(context);
  210. _pickFiles(callback, allowMultiple);
  211. },
  212. title: Align(
  213. alignment: Alignment.center,
  214. child: Text('pick_file'.tr,
  215. style: Theme.of(context).textTheme.bodyMedium),
  216. ),
  217. ),
  218. ];
  219. } else {
  220. menus = [
  221. ListTile(
  222. onTap: () {
  223. Navigator.pop(context);
  224. _takePhoto(callback);
  225. },
  226. title: Align(
  227. alignment: Alignment.center,
  228. child: Text('take_photo'.tr,
  229. style: Theme.of(context).textTheme.bodyMedium),
  230. ),
  231. ),
  232. const Divider(height: 1),
  233. ListTile(
  234. onTap: () {
  235. Navigator.pop(context);
  236. _pickFiles(callback, allowMultiple);
  237. },
  238. title: Align(
  239. alignment: Alignment.center,
  240. child: Text('pick_file'.tr,
  241. style: Theme.of(context).textTheme.bodyMedium),
  242. ),
  243. ),
  244. ];
  245. }
  246. O2UI.showBottomSheetWithCancel(context, menus);
  247. }
  248. /// 直接拍照
  249. static void _takePhoto(OnFilePicker callback) async {
  250. if (!await O2Utils.cameraPermission()) {
  251. callback([]);
  252. return;
  253. }
  254. XFile? file = await ImagePicker().pickImage(source: ImageSource.camera);
  255. if (file != null) {
  256. callback([file.path]);
  257. } else {
  258. callback([]);
  259. }
  260. }
  261. /// 相册
  262. static void _pickImage(OnFilePicker callback) async {
  263. XFile? file = await ImagePicker().pickImage(source: ImageSource.gallery);
  264. if (file != null) {
  265. callback([file.path]);
  266. } else {
  267. callback([]);
  268. }
  269. }
  270. /// 文件选择
  271. static void _pickFiles(OnFilePicker callback, bool allowMultiple) async {
  272. if (!await O2Utils.storagePermission()) {
  273. callback([]);
  274. return;
  275. }
  276. FilePickerResult? result =
  277. await FilePicker.platform.pickFiles(allowMultiple: allowMultiple);
  278. if (result != null && result.paths.isNotEmpty) {
  279. callback(result.paths);
  280. } else {
  281. callback([]);
  282. }
  283. }
  284. static Future<bool> bioAuth() async {
  285. OLogger.d('点击了认证诶');
  286. try {
  287. final auth = LocalAuthentication();
  288. final didAuthenticate = await auth.authenticate(
  289. localizedReason: 'account_safe_local_auth_open_app'.tr,
  290. authMessages: [
  291. AndroidAuthMessages(
  292. biometricRequiredTitle: 'account_safe_local_auth_biometric_required'.tr,
  293. goToSettingsDescription: 'account_safe_local_auth_android_description'.tr,
  294. cancelButton: 'cancel'.tr,
  295. goToSettingsButton: 'account_safe_local_auth_go_to_setting'.tr,
  296. biometricNotRecognized: 'account_safe_local_auth_not_recognized'.tr,
  297. biometricHint: 'account_safe_local_auth_android_bio_hint'.tr,
  298. signInTitle: 'account_safe_local_auth_android_bio_sign_in_title'.tr
  299. ),
  300. IOSAuthMessages(
  301. goToSettingsButton: 'account_safe_local_auth_go_to_setting'.tr,
  302. goToSettingsDescription: 'account_safe_local_auth_ios_description'.tr,
  303. cancelButton: 'cancel'.tr,
  304. )
  305. ],
  306. options: const AuthenticationOptions(biometricOnly: false));
  307. return didAuthenticate;
  308. } on PlatformException catch (e) {
  309. OLogger.e('认证失败', e);
  310. }
  311. return false;
  312. }
  313. }