Commit 2fd34d99 by tanghuan

1.APP唤醒时,检测APP的版本号;2.设置界面增加打开下载界面的按钮。

1 parent bd06b441
......@@ -18,10 +18,10 @@ import 'package:equatable/equatable.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:fluwx/fluwx.dart';
import 'package:path_provider/path_provider.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:uuid/uuid.dart';
import 'package:volume_controller/volume_controller.dart';
import 'package:webview_flutter/webview_flutter.dart';
......@@ -34,6 +34,7 @@ class WebState extends Equatable {
final bool loaded;
final bool isUpgrading;
final bool suggestUpgrade;
final bool suggestAppUpgrade;
final String title;
final int titleColor;
......@@ -75,6 +76,7 @@ class WebState extends Equatable {
this.loaded = false,
this.isUpgrading = false,
this.suggestUpgrade = false,
this.suggestAppUpgrade = false,
this.title = '',
this.titleColor = 0xFFFFFFFF,
this.bgColor = 0xFF7691FA,
......@@ -102,6 +104,7 @@ class WebState extends Equatable {
bool? loaded,
bool? isUpgrading,
bool? suggestUpgrade,
bool? suggestAppUpgrade,
String? title,
int? titleColor,
int? bgColor,
......@@ -129,6 +132,7 @@ class WebState extends Equatable {
loaded: loaded ?? this.loaded,
isUpgrading: isUpgrading ?? this.isUpgrading,
suggestUpgrade: suggestUpgrade ?? this.suggestUpgrade,
suggestAppUpgrade: suggestAppUpgrade ?? this.suggestAppUpgrade,
title: title ?? this.title,
titleColor: titleColor ?? this.titleColor,
bgColor: bgColor ?? this.bgColor,
......@@ -158,6 +162,7 @@ class WebState extends Equatable {
loaded,
isUpgrading,
suggestUpgrade,
suggestAppUpgrade,
title,
titleColor,
bgColor,
......@@ -278,10 +283,18 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
String version = response.data['version'] as String;
String force = response.data['force'] as String;
String zip = response.data['zip'] as String;
String appversionCheck = response.data['appversionCheck'] as String? ?? '0';
String appversionAndroid = response.data['appversionAndroid'] as String? ?? '';
String appversionIos = response.data['appversionIos'] as String? ?? '';
return {
'version': version,
'force': force,
'zip': '$zip$version.zip',
'appversionCheck': appversionCheck,
'appversionAndroid': appversionAndroid,
'appversionIos': appversionIos,
};
} finally {
dio.close(force: true);
......@@ -609,34 +622,198 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
///
/// 升级提示
///
void suggestUpgrade(BuildContext ctx) {
showDialog(
context: ctx,
Future<void> suggestUpgrade(BuildContext context) async {
// showDialog(
// context: ctx,
// barrierDismissible: false,
// builder: (BuildContext context) {
// return AlertDialog(
// title: Text('温馨提示'),
// content: Text('资源已更新为最新版本,是否现在进行加载?'),
// actions: <Widget>[
// TextButton(
// child: Text('取消'),
// onPressed: () {
// Navigator.of(context).pop();
// emit(state.copyWith(suggestUpgrade: false));
// },
// ),
// TextButton(
// child: Text('确定'),
// onPressed: () {
// Navigator.of(context).pop();
// emit(state.copyWith(suggestUpgrade: false));
// router.go('/reload');
// },
// ),
// ],
// );
// },
// );
final result = await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
title: Text('温馨提示'),
content: Text('资源已更新为最新版本,是否现在进行加载?'),
actions: <Widget>[
TextButton(
child: Text('取消'),
onPressed: () {
Navigator.of(context).pop();
emit(state.copyWith(suggestUpgrade: false));
},
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(5),
),
TextButton(
child: Text('确定'),
onPressed: () {
Navigator.of(context).pop();
emit(state.copyWith(suggestUpgrade: false));
router.go('/reload');
},
),
title: Text(
'资源包更新',
style: TextStyle(
fontSize: 17,
color: Color(0xFF000000),
// fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
content: Text.rich(
TextSpan(
children: [
TextSpan(
text: '资源更新包已准备就绪,是否立即加载?',
style: TextStyle(color: Color(0xFF666666), fontSize: 14),
),
],
),
),
actions: [
Table(
children: [
TableRow(
children: [
TableCell(
child: TextButton(
onPressed: () {
Navigator.of(context).pop();
},
style: TextButton.styleFrom(
foregroundColor: Color(0xFF666666),
textStyle: TextStyle(fontSize: 17),
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.zero,
),
),
child: Text('稍后再说'),
),
),
TableCell(
child: TextButton(
onPressed: () {
Navigator.of(context).pop('OK');
},
style: TextButton.styleFrom(
foregroundColor: Color(0xFF7691FA),
textStyle: TextStyle(fontSize: 17),
minimumSize: Size.fromHeight(40),
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.zero,
),
),
child: Text('立即加载'),
),
),
],
),
],
),
],
);
},
);
emit(state.copyWith(suggestUpgrade: false));
if (result != null) {
router.go('/reload');
}
}
Future<void> suggestAppUpdate(BuildContext context) async {
final result = await showDialog(
context: context,
barrierDismissible: false,
builder: (BuildContext context) {
return AlertDialog(
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(
Radius.circular(5),
),
),
title: Text(
'发现新版本',
style: TextStyle(
fontSize: 17,
color: Color(0xFF000000),
// fontWeight: FontWeight.bold,
),
textAlign: TextAlign.center,
),
content: Text.rich(
TextSpan(
children: [
TextSpan(
text: '已有新版 APP 可供下载,建议您立即下载更新以获得更好体验。',
style: TextStyle(color: Color(0xFF666666), fontSize: 14),
),
],
),
),
actions: [
Table(
children: [
TableRow(
children: [
TableCell(
child: TextButton(
onPressed: () {
Navigator.of(context).pop();
},
style: TextButton.styleFrom(
foregroundColor: Color(0xFF666666),
textStyle: TextStyle(fontSize: 17),
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.zero,
),
),
child: Text('稍后再说'),
),
),
TableCell(
child: TextButton(
onPressed: () {
Navigator.of(context).pop('OK');
},
style: TextButton.styleFrom(
foregroundColor: Color(0xFF7691FA),
textStyle: TextStyle(fontSize: 17),
minimumSize: Size.fromHeight(40),
padding: EdgeInsets.zero,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.zero,
),
),
child: Text('打开下载页面'),
),
),
],
),
],
),
],
);
},
);
emit(state.copyWith(suggestAppUpgrade: false));
if (result != null) {
final uri = Uri.parse('https://www.banxe.cn/apps/pro.html');
if (await canLaunchUrl(uri)) {
await launchUrl(uri, mode: LaunchMode.externalApplication);
}
}
}
///
......@@ -1138,6 +1315,9 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
/// H5资源已下载标志(本次生命周期内)
bool _hasDownloadedH5 = false;
/// APP版本检测上次执行时间(2小时内只执行一次)
DateTime? _lastAppVersionCheckTime;
///
/// 从后台恢复时进行版本检测
/// 参照 _init() 方法中的版本检测逻辑
......@@ -1154,17 +1334,39 @@ class WebCubit extends Cubit<WebState> with WidgetsBindingObserver {
var versionConfig = await _getVersionConfig();
var configVersion = versionConfig['version'] as String;
var downloadUrl = versionConfig['zip'] as String;
var appversionCheck = versionConfig['appversionCheck'] as String;
var appversionAndroid = versionConfig['appversionAndroid'] as String;
var appversionIos = versionConfig['appversionIos'] as String;
if (appversionCheck == '1') {
final now = DateTime.now();
if (_lastAppVersionCheckTime == null || now.difference(_lastAppVersionCheckTime!).inHours >= 2) {
if ((Platform.isAndroid && appversionAndroid != '' && appversionAndroid != Constant.appVersion) ||
(Platform.isIOS && appversionIos != '' && appversionIos != Constant.appVersion)) {
emit(state.copyWith(suggestAppUpgrade: true));
}
_lastAppVersionCheckTime = now;
}
}
if (Constant.needUpgrade && curVersion != configVersion) {
await _downloadH5Zip(configVersion, downloadUrl);
_setH5Version(configVersion);
_hasDownloadedH5 = true;
emit(state.copyWith(suggestUpgrade: true));
// 闭包中提前捕获变量,确保回调执行时读取的是当前值
final capturedConfigVersion = configVersion;
_downloadH5Zip(capturedConfigVersion, downloadUrl).then((_) {
_setH5Version(capturedConfigVersion);
_hasDownloadedH5 = true;
emit(state.copyWith(suggestUpgrade: true));
}).catchError((e) {
debugPrint('后台H5资源下载失败 $e');
}).whenComplete(() {
_isCheckingVersionOfH5 = false;
});
} else {
// 不需要下载时,立即重置标志
_isCheckingVersionOfH5 = false;
}
} catch (e) {
debugPrint('后台H5版本检测失败 $e');
} finally {
_isCheckingVersionOfH5 = false;
}
}
......
......@@ -6,6 +6,7 @@ import 'package:appframe/config/env_config.dart';
import 'package:appframe/config/routes.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:url_launcher/url_launcher.dart';
class SettingPage extends StatelessWidget {
const SettingPage({super.key});
......@@ -158,6 +159,31 @@ class SettingPage extends StatelessWidget {
),
),
const SizedBox(height: 8),
Card(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 2),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(6),
),
child: Column(
children: [
ListTile(
leading: const Icon(Icons.download, size: 20),
title: const Text('下载与更新', style: TextStyle(fontSize: 14)),
onTap: () async {
final uri = Uri.parse('https://www.banxe.cn/apps/pro.html');
if (await canLaunchUrl(uri)) {
await launchUrl(uri, mode: LaunchMode.externalApplication);
}
},
contentPadding: const EdgeInsets.symmetric(horizontal: 16, vertical: 4),
trailing: const Icon(Icons.arrow_forward_ios, size: 14),
dense: true,
visualDensity: VisualDensity.compact,
),
],
),
),
const SizedBox(height: 8),
...customerService,
Card(
margin: const EdgeInsets.symmetric(horizontal: 16, vertical: 2),
......
......@@ -148,6 +148,8 @@ class WebPage extends StatelessWidget {
context.read<WebCubit>().chooseImage(context);
} else if (state.chooseVideoCmdFlag) {
context.read<WebCubit>().chooseVideo(context);
} else if (state.suggestAppUpgrade) {
context.read<WebCubit>().suggestAppUpdate(context);
}
},
),
......
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!