123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- import 'dart:io';
- import 'package:dio/dio.dart';
- import 'package:flutter/material.dart';
- import 'package:install_plugin/install_plugin.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 '../router/my_navigator.dart';
- import '../widget/upgrade_dialog.dart';
- class UpgradeService {
- UpgradeService._();
- static Future<Response> 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<String> getApkDirectoryPath() async {
- return '${(await getApplicationSupportDirectory()).path}/apk';
- }
- /// 获取apk文件
- static Future<File> getApkFile(String apkName) async {
- return File('${await getApkDirectoryPath()}/$apkName');
- }
- /// 安装APK
- static Future<bool> 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<void> 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<String> 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: 24),
- padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 24),
- decoration: const BoxDecoration(
- color: Colors.white,
- borderRadius: BorderRadius.all(Radius.circular(8)),
- ),
- child: Column(
- mainAxisSize: MainAxisSize.min,
- crossAxisAlignment: CrossAxisAlignment.stretch,
- children: [
- const Text(
- '当前版本更新内容:',
- style: TextStyle(fontWeight: FontWeight.bold, fontSize: 16),
- ),
- const SizedBox(height: 12),
- Text(UpgradeService.getContentsString(contents) ?? ''),
- const SizedBox(height: 22),
- MyButton(
- '知道了',
- radius: 8,
- onTap: () {
- MyNavigator.dismiss(tag: 'pop');
- },
- )
- ],
- ),
- );
- },
- );
- }
- } else {
- // 版本更新
- UpgradeDialog.showDialog(path, UpgradeService.getContentsString(contents) ?? '');
- }
- }
- } catch (e) {
- logger.e(e);
- if (showLoading) MyNavigator.dismissLoading();
- }
- checking = false;
- }
- }
|