diff --git a/.flutter-plugins b/.flutter-plugins new file mode 100644 index 0000000..a698799 --- /dev/null +++ b/.flutter-plugins @@ -0,0 +1,13 @@ +# This is a generated file; do not edit or check into version control. +package_info_plus=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus-1.4.2/ +package_info_plus_linux=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus_linux-1.0.5/ +package_info_plus_macos=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus_macos-1.3.0/ +package_info_plus_web=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus_web-1.0.5/ +package_info_plus_windows=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus_windows-1.0.5/ +url_launcher=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher-6.1.2/ +url_launcher_android=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_android-6.0.17/ +url_launcher_ios=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_ios-6.0.17/ +url_launcher_linux=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_linux-3.0.1/ +url_launcher_macos=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_macos-3.0.1/ +url_launcher_web=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_web-2.0.11/ +url_launcher_windows=/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_windows-3.0.1/ diff --git a/.flutter-plugins-dependencies b/.flutter-plugins-dependencies new file mode 100644 index 0000000..55576a3 --- /dev/null +++ b/.flutter-plugins-dependencies @@ -0,0 +1 @@ +{"info":"This is a generated file; do not edit or check into version control.","plugins":{"ios":[{"name":"package_info_plus","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus-1.4.2/","native_build":true,"dependencies":[]},{"name":"url_launcher_ios","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_ios-6.0.17/","native_build":true,"dependencies":[]}],"android":[{"name":"package_info_plus","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus-1.4.2/","native_build":true,"dependencies":[]},{"name":"url_launcher_android","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_android-6.0.17/","native_build":true,"dependencies":[]}],"macos":[{"name":"package_info_plus_macos","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus_macos-1.3.0/","native_build":true,"dependencies":[]},{"name":"url_launcher_macos","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_macos-3.0.1/","native_build":true,"dependencies":[]}],"linux":[{"name":"package_info_plus_linux","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus_linux-1.0.5/","native_build":false,"dependencies":[]},{"name":"url_launcher_linux","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_linux-3.0.1/","native_build":true,"dependencies":[]}],"windows":[{"name":"package_info_plus_windows","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus_windows-1.0.5/","native_build":false,"dependencies":[]},{"name":"url_launcher_windows","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_windows-3.0.1/","native_build":true,"dependencies":[]}],"web":[{"name":"package_info_plus_web","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/package_info_plus_web-1.0.5/","dependencies":[]},{"name":"url_launcher_web","path":"/Users/zhangmeng/.pub-cache/hosted/pub.dartlang.org/url_launcher_web-2.0.11/","dependencies":[]}]},"dependencyGraph":[{"name":"package_info_plus","dependencies":["package_info_plus_linux","package_info_plus_macos","package_info_plus_windows","package_info_plus_web"]},{"name":"package_info_plus_linux","dependencies":[]},{"name":"package_info_plus_macos","dependencies":[]},{"name":"package_info_plus_web","dependencies":[]},{"name":"package_info_plus_windows","dependencies":[]},{"name":"url_launcher","dependencies":["url_launcher_android","url_launcher_ios","url_launcher_linux","url_launcher_macos","url_launcher_web","url_launcher_windows"]},{"name":"url_launcher_android","dependencies":[]},{"name":"url_launcher_ios","dependencies":[]},{"name":"url_launcher_linux","dependencies":[]},{"name":"url_launcher_macos","dependencies":[]},{"name":"url_launcher_web","dependencies":[]},{"name":"url_launcher_windows","dependencies":[]}],"date_created":"2022-06-02 10:29:21.647080","version":"3.0.0"} \ No newline at end of file diff --git a/lib/aku_app_upgrade.dart b/lib/aku_app_upgrade.dart index 11766cf..8fd4791 100644 --- a/lib/aku_app_upgrade.dart +++ b/lib/aku_app_upgrade.dart @@ -1,7 +1,203 @@ library aku_app_upgrade; -/// A Calculator. -class Calculator { - /// Returns [value] plus 1. - int addOne(int value) => value + 1; +import 'dart:io'; + +import 'package:flutter/foundation.dart'; +import 'package:flutter/material.dart'; +import 'package:package_info_plus/package_info_plus.dart'; +import 'package:dio/dio.dart'; +import 'package:url_launcher/url_launcher_string.dart'; + +enum ForceUpgrade { + force(1), + unForce(2); + + final int typeNum; + + static ForceUpgrade getValue(int value) => + ForceUpgrade.values.firstWhere((element) => element.typeNum == value); + + const ForceUpgrade(this.typeNum); +} + +class AppUpgrade { + static final AppUpgrade _instance = AppUpgrade._(); + + factory AppUpgrade() => _instance; + + AppUpgrade._(); + + Future checkUpgrade( + BuildContext context, { + Function(String)? onError, + Function(Map)? onRequestFail, + Function()? onLaunchFail, + }) async { + WidgetsFlutterBinding.ensureInitialized(); + PackageInfo packageInfo = await PackageInfo.fromPlatform(); + int buildNo = int.parse(packageInfo.buildNumber); + + if (kDebugMode) { + print('当前版本号:${packageInfo.version}${packageInfo.buildNumber}'); + } + Response? response; + try { + response = await Dio().get( + 'http://121.41.26.225:8006/app/version/findNewVersion', + queryParameters: {'buildNo': buildNo}); + } catch (e) { + onError?.call(e.toString()); + return; + } + if (kDebugMode) { + print('查询最新版本结果:${response.data}'); + } + if (response.data['success']) { + AkuAppVersion akuAppVersion = + AkuAppVersion.fromMap(response.data['data']); + if (buildNo < akuAppVersion.buildNo) { + await showDialog( + context: context, + barrierDismissible: akuAppVersion.forceEM != ForceUpgrade.force, + builder: (context) { + return WillPopScope( + onWillPop: () async { + return akuAppVersion.forceEM != ForceUpgrade.force; + }, + child: upgradeDialog(context, + packageInfo: packageInfo, onLaunchFail: onLaunchFail)); + }); + } + } else { + onRequestFail?.call(response.data); + } + } + + Widget upgradeDialog( + BuildContext context, { + required PackageInfo packageInfo, + Function()? onLaunchFail, + }) { + return Center( + child: Material( + child: Container( + width: 200, + height: 200, + decoration: BoxDecoration(borderRadius: BorderRadius.circular(16)), + child: Column( + children: [ + const Text('当前不是最新版本,请升级最新版'), + Row( + children: [ + TextButton( + onPressed: () { + Navigator.pop(context); + }, + child: Text('取消'), + ), + TextButton( + onPressed: () async { + if (Platform.isAndroid) { + var re = await launchUrlString( + 'market://detail?id=${packageInfo.packageName}'); + if (!re && onLaunchFail != null) { + onLaunchFail.call(); + } + } else if (Platform.isIOS) { + launchUrlString( + 'itms-apps://itunes.apple.com/app/${packageInfo.packageName}'); + } else {} + }, + child: Text('去升级'), + ) + ], + ), + ], + ), + ), + ), + ); + } +} + +class AkuAppVersion { + final int id; + final String versionNumber; + final int buildNo; + final int forceUpdate; + final String createDate; + + ForceUpgrade get forceEM => ForceUpgrade.getValue(forceUpdate); + +// + + const AkuAppVersion({ + required this.id, + required this.versionNumber, + required this.buildNo, + required this.forceUpdate, + required this.createDate, + }); + + @override + bool operator ==(Object other) => + identical(this, other) || + (other is AkuAppVersion && + runtimeType == other.runtimeType && + id == other.id && + versionNumber == other.versionNumber && + buildNo == other.buildNo && + forceUpdate == other.forceUpdate && + createDate == other.createDate); + + @override + int get hashCode => + id.hashCode ^ + versionNumber.hashCode ^ + buildNo.hashCode ^ + forceUpdate.hashCode ^ + createDate.hashCode; + + @override + String toString() { + return 'AkuAppVersion{ id: $id, versionNumber: $versionNumber, buildNo: $buildNo, forceUpdate: $forceUpdate, createDate: $createDate,}'; + } + + AkuAppVersion copyWith({ + int? id, + String? versionNumber, + int? buildNo, + int? forceUpdate, + String? createDate, + }) { + return AkuAppVersion( + id: id ?? this.id, + versionNumber: versionNumber ?? this.versionNumber, + buildNo: buildNo ?? this.buildNo, + forceUpdate: forceUpdate ?? this.forceUpdate, + createDate: createDate ?? this.createDate, + ); + } + + Map toMap() { + return { + 'id': id, + 'versionNumber': versionNumber, + 'buildNo': buildNo, + 'forceUpdate': forceUpdate, + 'createDate': createDate, + }; + } + + factory AkuAppVersion.fromMap(Map map) { + return AkuAppVersion( + id: map['id'] as int, + versionNumber: map['versionNumber'] as String, + buildNo: map['buildNo'] as int, + forceUpdate: map['forceUpdate'] as int, + createDate: map['createDate'] as String, + ); + } + +// } diff --git a/pubspec.yaml b/pubspec.yaml index e1102d6..26445f9 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -10,6 +10,9 @@ environment: dependencies: flutter: sdk: flutter + package_info_plus: ^1.4.2 + url_launcher: ^6.1.2 + dio: ^4.0.6 dev_dependencies: flutter_test: diff --git a/test/aku_app_upgrade_test.dart b/test/aku_app_upgrade_test.dart index 8c8f5b4..835f8c7 100644 --- a/test/aku_app_upgrade_test.dart +++ b/test/aku_app_upgrade_test.dart @@ -1,12 +1,11 @@ import 'package:flutter_test/flutter_test.dart'; -import 'package:aku_app_upgrade/aku_app_upgrade.dart'; void main() { test('adds one to input values', () { - final calculator = Calculator(); - expect(calculator.addOne(2), 3); - expect(calculator.addOne(-7), -6); - expect(calculator.addOne(0), 1); + // final calculator = Calculator(); + // expect(calculator.addOne(2), 3); + // expect(calculator.addOne(-7), -6); + // expect(calculator.addOne(0), 1); }); }