upgrade_service.dart 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/base/base_lifecycle_state.dart';
  6. import 'package:lszlgl/config/app_config.dart';
  7. import 'package:lszlgl/widget/button.dart';
  8. import 'package:path_provider/path_provider.dart';
  9. import 'package:permission_handler/permission_handler.dart';
  10. import '../main.dart';
  11. import '../network/my_api.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: 12),
  85. padding: const EdgeInsets.symmetric(horizontal: 20, 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. Image.asset(
  95. imgAppSetIcon,
  96. width: 80,
  97. height: 80,
  98. ),
  99. const SizedBox(height: 12),
  100. const Align(
  101. child: Text(
  102. '国粮质检',
  103. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18),
  104. )),
  105. const SizedBox(height: 2),
  106. Align(
  107. child: Text(
  108. '版本 ${AppConfig.packageInfo.version}',
  109. style: const TextStyle(fontSize: 15,color: Colors.grey),
  110. ),
  111. ),
  112. const SizedBox(height: 32),
  113. const Text(
  114. '当前版本更新内容:',
  115. style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14),
  116. ),
  117. const SizedBox(height: 10),
  118. Text(UpgradeService.getContentsString(contents) ?? ''),
  119. const SizedBox(height: 22),
  120. MyButton(
  121. '知道了',
  122. radius: 8,
  123. gradient:
  124. const LinearGradient(colors: [Color(0xFF3BD2E5), Color(0xFF247AF8)]),
  125. onTap: () {
  126. MyNavigator.dismiss(tag: 'pop');
  127. },
  128. )
  129. ],
  130. ),
  131. );
  132. },
  133. );
  134. }
  135. } else {
  136. // 版本更新
  137. UpgradeDialog.showDialog(path, UpgradeService.getContentsString(contents) ?? '');
  138. }
  139. }
  140. } catch (e) {
  141. logger.e(e);
  142. if (showLoading) MyNavigator.dismissLoading();
  143. }
  144. checking = false;
  145. }
  146. }