import 'dart:io'; import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:install_plugin/install_plugin.dart'; import 'package:lszlgl/base/base_lifecycle_state.dart'; import 'package:lszlgl/config/app_config.dart'; import 'package:lszlgl/widget/button.dart'; import 'package:path_provider/path_provider.dart'; import 'package:permission_handler/permission_handler.dart'; import '../main.dart'; import '../network/my_api.dart'; import '../widget/upgrade_dialog.dart'; class UpgradeService { UpgradeService._(); static Future download(String apkUrl, String apkName, ProgressCallback progress) async { String savePath = '${await getApkDirectoryPath()}/$apkName'; var rsp = await Dio().download( apkUrl, savePath, onReceiveProgress: (count, total) { if (total < 0) return; progress.call(count, total); }, ); return rsp; } /// 获取apk文件夹路径 static Future getApkDirectoryPath() async { return '${(await getApplicationSupportDirectory()).path}/apk'; } /// 获取apk文件 static Future getApkFile(String apkName) async { return File('${await getApkDirectoryPath()}/$apkName'); } /// 安装APK static Future installApk(String path) async { var status = await Permission.requestInstallPackages.status; if (!status.isGranted) { Permission.requestInstallPackages.request(); return false; } await InstallPlugin.install(path); return true; } /// 从路径中获取文件名 static String getName(String path) { var pathSplit = path.split('/'); return pathSplit[pathSplit.length - 1]; } static String? getContentsString(String? v) { if (v != null && v.contains('*')) { v = v.replaceAll('*', '\n'); } return v; } static bool checking = false; /// 检查版本更新 static Future checkUpgrade(bool showLoading, {bool pop = false}) async { if (!Platform.isAndroid) return; if (checking) return; checking = true; if (showLoading) MyNavigator.showLoading(msg: '正在获取版本更新'); try { // 获取apk下载地址 var rsp = await MyApi.get().getAppDownLoadUrlAndName(); var path = rsp.data?.url; var contents = rsp.data?.name ?? ''; if (showLoading) MyNavigator.dismissLoading(); if (path != null) { // 获取线上版本 lszlgl_release_0.0.2_2.apk List nameSplit = UpgradeService.getName(path).split('_'); int onlineCode = int.parse(nameSplit[nameSplit.length - 1].split('.').first); int localCode = int.parse(AppConfig.packageInfo.buildNumber); logger.d('versionUpgrade:localCode:$localCode onlineCode:$onlineCode'); if (localCode >= onlineCode) { if (showLoading && !pop) { MyNavigator.showToast('已是最新版本'); } if (showLoading && pop) { MyNavigator.showDialog( tag: 'pop', builder: (ctx) { return Container( margin: const EdgeInsets.symmetric(horizontal: 12), padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 24), decoration: const BoxDecoration( color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(8)), ), child: Column( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Image.asset( imgAppSetIcon, width: 80, height: 80, ), const SizedBox(height: 12), const Align( child: Text( '国粮质检', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 18), )), const SizedBox(height: 2), Align( child: Text( '版本 ${AppConfig.packageInfo.version}', style: const TextStyle(fontSize: 15,color: Colors.grey), ), ), const SizedBox(height: 32), const Text( '当前版本更新内容:', style: TextStyle(fontWeight: FontWeight.bold, fontSize: 14), ), const SizedBox(height: 10), Text(UpgradeService.getContentsString(contents) ?? ''), const SizedBox(height: 22), MyButton( '知道了', radius: 8, gradient: const LinearGradient(colors: [Color(0xFF3BD2E5), Color(0xFF247AF8)]), onTap: () { MyNavigator.dismiss(tag: 'pop'); }, ) ], ), ); }, ); } } else { // 版本更新 UpgradeDialog.showDialog(path, UpgradeService.getContentsString(contents) ?? ''); } } } catch (e) { logger.e(e); if (showLoading) MyNavigator.dismissLoading(); } checking = false; } }