upgrade_service.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import 'dart:io';
  2. import 'package:dio/dio.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:install_plugin/install_plugin.dart';
  5. import 'package:lszlgl/config/app_config.dart';
  6. import 'package:lszlgl/widget/button.dart';
  7. import 'package:path_provider/path_provider.dart';
  8. import 'package:permission_handler/permission_handler.dart';
  9. import '../main.dart';
  10. import '../network/my_api.dart';
  11. import '../router/my_navigator.dart';
  12. import '../widget/upgrade_dialog.dart';
  13. class UpgradeService {
  14. UpgradeService._();
  15. static Future<Response> download(String apkUrl, String apkName, ProgressCallback progress) async {
  16. String savePath = '${await getApkDirectoryPath()}/$apkName';
  17. var rsp = await Dio().download(
  18. apkUrl,
  19. savePath,
  20. onReceiveProgress: (count, total) {
  21. if (total < 0) return;
  22. progress.call(count, total);
  23. },
  24. );
  25. return rsp;
  26. }
  27. /// 获取apk文件夹路径
  28. static Future<String> getApkDirectoryPath() async {
  29. return '${(await getApplicationSupportDirectory()).path}/apk';
  30. }
  31. /// 获取apk文件
  32. static Future<File> getApkFile(String apkName) async {
  33. return File('${await getApkDirectoryPath()}/$apkName');
  34. }
  35. /// 安装APK
  36. static Future<bool> installApk(String path) async {
  37. var status = await Permission.requestInstallPackages.status;
  38. if (!status.isGranted) {
  39. Permission.requestInstallPackages.request();
  40. return false;
  41. }
  42. await InstallPlugin.install(path);
  43. return true;
  44. }
  45. /// 从路径中获取文件名
  46. static String getName(String path) {
  47. var pathSplit = path.split('/');
  48. return pathSplit[pathSplit.length - 1];
  49. }
  50. static String? getContentsString(String? v) {
  51. if (v != null && v.contains('*')) {
  52. v = v.replaceAll('*', '\n');
  53. }
  54. return v;
  55. }
  56. static bool checking = false;
  57. /// 检查版本更新
  58. static Future<void> checkUpgrade(bool showLoading, {bool pop = false}) async {
  59. if (!Platform.isAndroid) return;
  60. if (checking) return;
  61. checking = true;
  62. if (showLoading) MyNavigator.showLoading(msg: '正在获取版本更新');
  63. try {
  64. // 获取apk下载地址
  65. var rsp = await MyApi.get().getAppDownLoadUrlAndName();
  66. var path = rsp.data?.url;
  67. var contents = rsp.data?.name ?? '';
  68. if (showLoading) MyNavigator.dismissLoading();
  69. if (path != null) {
  70. // 获取线上版本 lszlgl_release_0.0.2_2.apk
  71. List<String> nameSplit = UpgradeService.getName(path).split('_');
  72. int onlineCode = int.parse(nameSplit[nameSplit.length - 1].split('.').first);
  73. int localCode = int.parse(AppConfig.packageInfo.buildNumber);
  74. logger.d('versionUpgrade:localCode:$localCode onlineCode:$onlineCode');
  75. if (localCode >= onlineCode) {
  76. if (showLoading && !pop) {
  77. MyNavigator.showToast('已是最新版本');
  78. }
  79. if (showLoading && pop) {
  80. MyNavigator.showDialog(
  81. tag: 'pop',
  82. builder: (ctx) {
  83. return Container(
  84. margin: const EdgeInsets.symmetric(horizontal: 24),
  85. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
  86. decoration: const BoxDecoration(
  87. color: Colors.white,
  88. borderRadius: BorderRadius.all(Radius.circular(8)),
  89. ),
  90. child: Column(
  91. mainAxisSize: MainAxisSize.min,
  92. crossAxisAlignment: CrossAxisAlignment.stretch,
  93. children: [
  94. const Text(
  95. '当前版本更新内容:',
  96. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
  97. ),
  98. const SizedBox(height: 12),
  99. Text(UpgradeService.getContentsString(contents) ?? ''),
  100. const SizedBox(height: 22),
  101. MyButton(
  102. '知道了',
  103. radius: 8,
  104. onTap: () {
  105. MyNavigator.dismiss(tag: 'pop');
  106. },
  107. )
  108. ],
  109. ),
  110. );
  111. },
  112. );
  113. }
  114. } else {
  115. // 版本更新
  116. UpgradeDialog.showDialog(path, UpgradeService.getContentsString(contents) ?? '');
  117. }
  118. }
  119. } catch (e) {
  120. logger.e(e);
  121. if (showLoading) MyNavigator.dismissLoading();
  122. }
  123. checking = false;
  124. }
  125. }