Commit d0f20c55 by tanghuan

H5的版本号获取和设置方式调整

1 parent 4381b3c8
...@@ -87,8 +87,8 @@ class WebState extends Equatable { ...@@ -87,8 +87,8 @@ class WebState extends Equatable {
this.chooseImageCmdMessage = '', this.chooseImageCmdMessage = '',
this.chooseVideoCmdFlag = false, this.chooseVideoCmdFlag = false,
this.chooseVideoCmdMessage = '', this.chooseVideoCmdMessage = '',
String? h5Version, this.h5Version = '',
}) : this.h5Version = h5Version ?? getIt.get<SharedPreferences>().getString("h5_version") ?? Constant.h5Version; });
WebState copyWith({ WebState copyWith({
int? selectedIndex, int? selectedIndex,
...@@ -201,7 +201,7 @@ class WebCubit extends Cubit<WebState> { ...@@ -201,7 +201,7 @@ class WebCubit extends Cubit<WebState> {
var force = versionConfig['force'] as String; var force = versionConfig['force'] as String;
// 当前使用的H5版本 // 当前使用的H5版本
var curVersion = getIt.get<SharedPreferences>().getString('h5_version') ?? Constant.h5Version; var curVersion = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey) ?? Constant.h5Version;
// 版本不一致则需要升级 // 版本不一致则需要升级
if (curVersion != correctVersion) { if (curVersion != correctVersion) {
...@@ -211,8 +211,7 @@ class WebCubit extends Cubit<WebState> { ...@@ -211,8 +211,7 @@ class WebCubit extends Cubit<WebState> {
emit(state.copyWith(isUpgrading: true)); emit(state.copyWith(isUpgrading: true));
await _upgrade(correctVersion, downloadUrl); await _upgrade(correctVersion, downloadUrl);
// 升级完成后取消遮罩,继续初始化其它数据 // 升级完成后取消遮罩,继续初始化其它数据
// 同时设置h5版本号 emit(state.copyWith(isUpgrading: false));
emit(state.copyWith(isUpgrading: false, h5Version: correctVersion));
} else { } else {
// 后台下载,完成后提示用户 // 后台下载,完成后提示用户
_upgrade(correctVersion, downloadUrl).then( _upgrade(correctVersion, downloadUrl).then(
...@@ -239,6 +238,9 @@ class WebCubit extends Cubit<WebState> { ...@@ -239,6 +238,9 @@ class WebCubit extends Cubit<WebState> {
// 加载H5页面 // 加载H5页面
_loadHtml(); _loadHtml();
// h5 版本号
_readAndSetH5Version();
// 登录IM // 登录IM
_loginIM(); _loginIM();
...@@ -310,8 +312,6 @@ class WebCubit extends Cubit<WebState> { ...@@ -310,8 +312,6 @@ class WebCubit extends Cubit<WebState> {
throw Exception('文件解压失败'); throw Exception('文件解压失败');
} }
var sharedPreferences = await SharedPreferences.getInstance();
await sharedPreferences.setString('h5_version', version);
} finally { } finally {
dio.close(force: true); dio.close(force: true);
} }
...@@ -361,6 +361,11 @@ class WebCubit extends Cubit<WebState> { ...@@ -361,6 +361,11 @@ class WebCubit extends Cubit<WebState> {
_controller.loadRequest(Uri.parse(serverUrl)); _controller.loadRequest(Uri.parse(serverUrl));
} }
void _readAndSetH5Version() {
var h5Version = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey) ?? Constant.h5Version;
emit(state.copyWith(h5Version: h5Version));
}
Future<void> _loginIM() async { Future<void> _loginIM() async {
if (Constant.needIM) { if (Constant.needIM) {
var imService = getIt.get<ImService>(); var imService = getIt.get<ImService>();
...@@ -487,7 +492,7 @@ class WebCubit extends Cubit<WebState> { ...@@ -487,7 +492,7 @@ class WebCubit extends Cubit<WebState> {
// 1 清理非 h5_version 的缓存 // 1 清理非 h5_version 的缓存
var sharedPreferences = getIt.get<SharedPreferences>(); var sharedPreferences = getIt.get<SharedPreferences>();
sharedPreferences.getKeys().forEach((key) async { sharedPreferences.getKeys().forEach((key) async {
if (!key.startsWith('h5_version')) { if (!key.startsWith(Constant.h5VersionKey)) {
await sharedPreferences.remove(key); await sharedPreferences.remove(key);
} }
}); });
...@@ -496,7 +501,7 @@ class WebCubit extends Cubit<WebState> { ...@@ -496,7 +501,7 @@ class WebCubit extends Cubit<WebState> {
var dir = await getApplicationSupportDirectory(); var dir = await getApplicationSupportDirectory();
var httpDir = Directory('${dir.path}/${Constant.h5DistDir}'); var httpDir = Directory('${dir.path}/${Constant.h5DistDir}');
if (httpDir.existsSync()) { if (httpDir.existsSync()) {
var version = sharedPreferences.getString('h5_version') ?? Constant.h5Version; var version = sharedPreferences.getString(Constant.h5VersionKey) ?? Constant.h5Version;
await for (final FileSystemEntity entity in httpDir.list()) { await for (final FileSystemEntity entity in httpDir.list()) {
if (entity is Directory) { if (entity is Directory) {
...@@ -577,9 +582,7 @@ class WebCubit extends Cubit<WebState> { ...@@ -577,9 +582,7 @@ class WebCubit extends Cubit<WebState> {
onPressed: () { onPressed: () {
Navigator.of(context).pop(); Navigator.of(context).pop();
emit(state.copyWith(suggestUpgrade: false)); emit(state.copyWith(suggestUpgrade: false));
getIt.get<LocalServerService>().resetHttpDirectory(); router.go('/reload');
_controller.reload();
// _loadHtml();
}, },
), ),
], ],
......
...@@ -19,9 +19,12 @@ class Constant { ...@@ -19,9 +19,12 @@ class Constant {
/// app 版本号规则 /// app 版本号规则
static const String appVersion = '1.0.2512021'; static const String appVersion = '1.0.2512021';
// h5的起始终最低版本号规则 /// H5的起始终最低版本号规则
static const String h5Version = '1.0.0'; static const String h5Version = '1.0.0';
/// H5的版本号存储的key
static const String h5VersionKey = 'h5_version';
/// H5版本号配置文件地址 /// H5版本号配置文件地址
static const String configUrl = 'https://bxe-obs.banxiaoer.com/conf/xeapp_conf_dev.json'; static const String configUrl = 'https://bxe-obs.banxiaoer.com/conf/xeapp_conf_dev.json';
......
...@@ -3,6 +3,7 @@ import 'package:appframe/ui/pages/im_page.dart'; ...@@ -3,6 +3,7 @@ import 'package:appframe/ui/pages/im_page.dart';
import 'package:appframe/ui/pages/link_page.dart'; import 'package:appframe/ui/pages/link_page.dart';
import 'package:appframe/ui/pages/login_main_page.dart'; import 'package:appframe/ui/pages/login_main_page.dart';
import 'package:appframe/ui/pages/login_phone_page.dart'; import 'package:appframe/ui/pages/login_phone_page.dart';
import 'package:appframe/ui/pages/reload_page.dart';
import 'package:appframe/ui/pages/scan_code_page.dart'; import 'package:appframe/ui/pages/scan_code_page.dart';
import 'package:appframe/ui/pages/web_page.dart'; import 'package:appframe/ui/pages/web_page.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
...@@ -53,5 +54,11 @@ final GoRouter router = GoRouter( ...@@ -53,5 +54,11 @@ final GoRouter router = GoRouter(
return const ImPage(); return const ImPage();
}, },
), ),
GoRoute(
path: '/reload',
builder: (BuildContext context, GoRouterState state) {
return const ReloadPage();
},
),
], ],
); );
import 'package:appframe/config/constant.dart';
import 'package:appframe/config/locator.dart'; import 'package:appframe/config/locator.dart';
import 'package:appframe/services/dispatcher.dart'; import 'package:appframe/services/dispatcher.dart';
import 'package:shared_preferences/shared_preferences.dart'; import 'package:shared_preferences/shared_preferences.dart';
...@@ -59,7 +60,7 @@ class ClearStorageHandler extends MessageHandler { ...@@ -59,7 +60,7 @@ class ClearStorageHandler extends MessageHandler {
Future<dynamic> handleMessage(dynamic params) async { Future<dynamic> handleMessage(dynamic params) async {
var sharedPreferences = getIt.get<SharedPreferences>(); var sharedPreferences = getIt.get<SharedPreferences>();
sharedPreferences.getKeys().forEach((key) async { sharedPreferences.getKeys().forEach((key) async {
if (!key.startsWith('h5_version')) { if (!key.startsWith(Constant.h5VersionKey)) {
await sharedPreferences.remove(key); await sharedPreferences.remove(key);
} }
}); });
......
...@@ -44,10 +44,6 @@ class LocalServerService { ...@@ -44,10 +44,6 @@ class LocalServerService {
return server; return server;
} }
void resetHttpDirectory() {
_httpDirectory = null;
}
// 目录下的文件 // 目录下的文件
Future<void> _serveTempFile(HttpRequest request, String requestPath) async { Future<void> _serveTempFile(HttpRequest request, String requestPath) async {
try { try {
...@@ -149,7 +145,7 @@ class LocalServerService { ...@@ -149,7 +145,7 @@ class LocalServerService {
} }
Future<void> _initHttpDirectory() async { Future<void> _initHttpDirectory() async {
var version = getIt.get<SharedPreferences>().getString('h5_version') ?? Constant.h5Version; var version = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey) ?? Constant.h5Version;
var direct = await getApplicationSupportDirectory(); var direct = await getApplicationSupportDirectory();
_httpDirectory = '${direct.path}/${Constant.h5DistDir}/$version'; _httpDirectory = '${direct.path}/${Constant.h5DistDir}/$version';
} }
...@@ -163,12 +159,26 @@ class LocalServerService { ...@@ -163,12 +159,26 @@ class LocalServerService {
// } // }
// 判断H5打包文件是否存在,不存在则从assets中解压 // 判断H5打包文件是否存在,不存在则从assets中解压
var version = getIt.get<SharedPreferences>().getString('h5_version') ?? Constant.h5Version; var version = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey) ?? Constant.h5Version;
var dir = await getApplicationSupportDirectory(); var dir = await getApplicationSupportDirectory();
var distFilePath = '${dir.path}/${Constant.h5DistDir}/$version.zip'; var distFilePath = '${dir.path}/${Constant.h5DistDir}/$version.zip';
if (!File(distFilePath).existsSync()) { if (!File(distFilePath).existsSync()) {
distFilePath = 'assets/dist.zip'; distFilePath = 'assets/dist.zip';
} }
// 解压
await ZipUtil.extractZipFile(distFilePath, outputDirectory); await ZipUtil.extractZipFile(distFilePath, outputDirectory);
// 版本号
await _getAndSetVersion(outputDirectory);
}
// 读取和设置版本号
Future<void> _getAndSetVersion(String outputDirectory) async {
var versionFile = File('$outputDirectory/version.txt');
if (await versionFile.exists()) {
var content = (await versionFile.readAsString()).trim();
getIt.get<SharedPreferences>().setString(Constant.h5VersionKey, content);
} else {
getIt.get<SharedPreferences>().setString(Constant.h5VersionKey, 'unknown');
}
} }
} }
import 'package:appframe/config/routes.dart';
import 'package:flutter/material.dart';
///
/// 用于重新加载的中间路由
///
class ReloadPage extends StatefulWidget {
const ReloadPage({super.key});
@override
State<ReloadPage> createState() => _ReloadPageState();
}
class _ReloadPageState extends State<ReloadPage> {
@override
void initState() {
super.initState();
WidgetsBinding.instance.addPostFrameCallback((_) {
// 界面显示完成后执行的操作
_performPostDisplayOperations();
});
}
void _performPostDisplayOperations() {
router.go('/web');
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const CircularProgressIndicator(),
const SizedBox(height: 16),
Text('加载中...'),
],
),
),
);
}
}
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!