upgrade_service.dart 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import 'dart:io';
  2. import 'package:dio/dio.dart';
  3. import 'package:install_plugin/install_plugin.dart';
  4. import 'package:lszlgl/config/app_config.dart';
  5. import 'package:path_provider/path_provider.dart';
  6. import 'package:permission_handler/permission_handler.dart';
  7. import '../main.dart';
  8. import '../network/my_api.dart';
  9. import '../router/my_navigator.dart';
  10. import '../widget/upgrade_dialog.dart';
  11. class UpgradeService {
  12. UpgradeService._();
  13. static Future<Response> download(String apkUrl, String apkName, ProgressCallback progress) async {
  14. String savePath = '${await getApkDirectoryPath()}/$apkName';
  15. var rsp = await Dio().download(
  16. apkUrl,
  17. savePath,
  18. onReceiveProgress: (count, total) {
  19. if (total < 0) return;
  20. progress.call(count, total);
  21. },
  22. );
  23. return rsp;
  24. }
  25. /// 获取apk文件夹路径
  26. static Future<String> getApkDirectoryPath() async {
  27. return '${(await getApplicationSupportDirectory()).path}/apk';
  28. }
  29. /// 获取apk文件
  30. static Future<File> getApkFile(String apkName) async {
  31. return File('${await getApkDirectoryPath()}/$apkName');
  32. }
  33. /// 安装APK
  34. static Future<bool> installApk(String path) async {
  35. var status = await Permission.requestInstallPackages.status;
  36. if (!status.isGranted) {
  37. Permission.requestInstallPackages.request();
  38. return false;
  39. }
  40. await InstallPlugin.install(path);
  41. return true;
  42. }
  43. /// 从路径中获取文件名
  44. static String getName(String path) {
  45. var pathSplit = path.split('/');
  46. return pathSplit[pathSplit.length - 1];
  47. }
  48. static bool checking = false;
  49. /// 检查版本更新
  50. static Future<void> checkUpgrade(bool showLoading) async {
  51. if (!Platform.isAndroid) return;
  52. if (checking) return;
  53. checking = true;
  54. if (showLoading) MyNavigator.showLoading(msg: '正在获取版本更新');
  55. try {
  56. // 获取apk下载地址
  57. var rsp = await MyApi.get().getAppDownLoadUrlAndName();
  58. var path = rsp.data?.url;
  59. var contents = rsp.data?.name ?? '';
  60. if (showLoading) MyNavigator.dismissLoading();
  61. if (path != null) {
  62. // 获取线上版本 lszlgl_release_0.0.2_2.apk
  63. List<String> nameSplit = UpgradeService.getName(path).split('_');
  64. int onlineCode = int.parse(nameSplit[nameSplit.length - 1].split('.').first);
  65. int localCode = int.parse(AppConfig.packageInfo.buildNumber);
  66. logger.d('versionUpgrade:localCode:$localCode onlineCode:$onlineCode');
  67. if (localCode >= onlineCode) {
  68. if (showLoading) MyNavigator.showToast('已是最新版本');
  69. } else {
  70. // 版本更新
  71. UpgradeDialog.showDialog(path,contents);
  72. }
  73. }
  74. } catch (e) {
  75. logger.e(e);
  76. if (showLoading) MyNavigator.dismissLoading();
  77. }
  78. checking = false;
  79. }
  80. }