Commit 1932b376 by tanghuan

H5版本更新的逻辑调整

1 parent bbbffded
...@@ -12,7 +12,6 @@ import 'package:appframe/services/im_service.dart'; ...@@ -12,7 +12,6 @@ import 'package:appframe/services/im_service.dart';
import 'package:appframe/services/local_server_service.dart'; import 'package:appframe/services/local_server_service.dart';
import 'package:appframe/services/player_service.dart'; import 'package:appframe/services/player_service.dart';
import 'package:appframe/services/recorder_service.dart'; import 'package:appframe/services/recorder_service.dart';
import 'package:appframe/utils/zip_util.dart';
import 'package:device_info_plus/device_info_plus.dart'; import 'package:device_info_plus/device_info_plus.dart';
import 'package:dio/dio.dart'; import 'package:dio/dio.dart';
import 'package:equatable/equatable.dart'; import 'package:equatable/equatable.dart';
...@@ -193,29 +192,32 @@ class WebCubit extends Cubit<WebState> { ...@@ -193,29 +192,32 @@ class WebCubit extends Cubit<WebState> {
} }
Future<void> _init() async { Future<void> _init() async {
// 当前使用的H5版本
var curVersion = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey) ?? Constant.h5Version;
try { try {
// 获取版本信息 // 获取版本信息
var versionConfig = await _getVersionConfig(); var versionConfig = await _getVersionConfig();
var correctVersion = versionConfig['version'] as String; var configVersion = versionConfig['version'] as String;
var downloadUrl = versionConfig['zip'] as String; var downloadUrl = versionConfig['zip'] as String;
var force = versionConfig['force'] as String; var force = versionConfig['force'] as String;
// 当前使用的H5版本
var curVersion = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey) ?? Constant.h5Version;
// 版本不一致则需要升级 // 版本不一致则需要升级
if (curVersion != correctVersion) { // 需要强制升级时,一直等待下载完成
// 不需要强制升级时,异步下载,下载完后弹框提示用户进行确认操作
if (curVersion != configVersion) {
if (force == "1") { if (force == "1") {
// 一直等待升级完成 // 一直等待升级完成
// 遮罩界面 // 遮罩界面
emit(state.copyWith(isUpgrading: true)); emit(state.copyWith(isUpgrading: true));
await _upgrade(correctVersion, downloadUrl); await _downloadH5Zip(configVersion, downloadUrl);
// 升级完成后取消遮罩,继续初始化其它数据 _setH5Version(configVersion);
// 下载完成后取消遮罩,继续初始化其它数据
emit(state.copyWith(isUpgrading: false)); emit(state.copyWith(isUpgrading: false));
} else { } else {
// 后台下载,完成后提示用户 // 后台下载,完成后提示用户
_upgrade(correctVersion, downloadUrl).then( _downloadH5Zip(configVersion, downloadUrl).then(
(value) { (value) {
_setH5Version(configVersion);
emit(state.copyWith(suggestUpgrade: true)); emit(state.copyWith(suggestUpgrade: true));
}, },
); );
...@@ -239,8 +241,8 @@ class WebCubit extends Cubit<WebState> { ...@@ -239,8 +241,8 @@ class WebCubit extends Cubit<WebState> {
// 加载H5页面 // 加载H5页面
_loadHtml(); _loadHtml();
// h5 版本号 // 读取 h5 版本号
_readAndSetH5Version(); _readH5ShowVersion();
// 登录IM // 登录IM
_loginIM(); _loginIM();
...@@ -278,7 +280,7 @@ class WebCubit extends Cubit<WebState> { ...@@ -278,7 +280,7 @@ class WebCubit extends Cubit<WebState> {
} }
} }
Future<void> _upgrade(String version, String zipUrl) async { Future<void> _downloadH5Zip(String version, String zipUrl) async {
Dio dio = Dio(); Dio dio = Dio();
try { try {
// 下载zip文件 // 下载zip文件
...@@ -306,18 +308,15 @@ class WebCubit extends Cubit<WebState> { ...@@ -306,18 +308,15 @@ class WebCubit extends Cubit<WebState> {
// 删除临时文件 // 删除临时文件
await tempZipFile.delete(); await tempZipFile.delete();
// 解压zip文件
String targetDir = '$httpDirPath/$version';
var result = await ZipUtil.extractZipFile(saveZipFilePath, targetDir);
if (!result) {
throw Exception('文件解压失败');
}
} finally { } finally {
dio.close(force: true); dio.close(force: true);
} }
} }
void _setH5Version(String version) {
getIt.get<SharedPreferences>().setString(Constant.h5VersionKey, version);
}
Future<void> _startLocalServer() async { Future<void> _startLocalServer() async {
// 启动本地服务器 // 启动本地服务器
_server = await getIt.get<LocalServerService>().startLocalServer(); _server = await getIt.get<LocalServerService>().startLocalServer();
...@@ -362,8 +361,8 @@ class WebCubit extends Cubit<WebState> { ...@@ -362,8 +361,8 @@ class WebCubit extends Cubit<WebState> {
_controller.loadRequest(Uri.parse(serverUrl)); _controller.loadRequest(Uri.parse(serverUrl));
} }
void _readAndSetH5Version() { void _readH5ShowVersion() {
var h5Version = getIt.get<SharedPreferences>().getString(Constant.h5VersionKey) ?? Constant.h5Version; var h5Version = getIt.get<SharedPreferences>().getString(Constant.h5ShowVersionKey) ?? 'unknown';
emit(state.copyWith(h5Version: h5Version)); emit(state.copyWith(h5Version: h5Version));
} }
...@@ -493,7 +492,7 @@ class WebCubit extends Cubit<WebState> { ...@@ -493,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(Constant.h5VersionKey)) { if (!key.startsWith('h5')) {
await sharedPreferences.remove(key); await sharedPreferences.remove(key);
} }
}); });
......
import 'package:appframe/config/evn_config.dart';
class Constant { class Constant {
/// 应用内部 http 服务 /// 应用内部 http 服务
static const int localServerPort = 35982; static const int localServerPort = 35982;
...@@ -25,8 +27,14 @@ class Constant { ...@@ -25,8 +27,14 @@ class Constant {
/// H5的版本号存储的key /// H5的版本号存储的key
static const String h5VersionKey = 'h5_version'; static const String h5VersionKey = 'h5_version';
/// 用于显示的H5版本号存储的key
static const String h5ShowVersionKey = 'h5_show_version';
/// H5版本号配置文件地址 /// H5版本号配置文件地址
static const String configUrl = 'https://bxe-obs.banxiaoer.com/conf/xeapp_conf_dev.json'; static const String configUrl = EnvConfig.env == 'dev'
? 'https://bxe-obs.banxiaoer.com/conf/xeapp_conf_dev.json'
// ? 'http://192.168.2.177/xeapp_conf_dev.json'
: 'https://bxe-obs.banxiaoer.com/conf/xeapp_conf_pro.json';
/// 内部 H5 dist 目录 /// 内部 H5 dist 目录
static const String h5DistDir = 'http_dist_assets'; static const String h5DistDir = 'http_dist_assets';
......
class EnvConfig {
static const String env = String.fromEnvironment('env', defaultValue: 'dev');
static bool isDev() {
return env == 'dev';
}
}
...@@ -60,7 +60,7 @@ class ClearStorageHandler extends MessageHandler { ...@@ -60,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(Constant.h5VersionKey)) { if (!key.startsWith('h5_')) {
await sharedPreferences.remove(key); await sharedPreferences.remove(key);
} }
}); });
......
...@@ -167,18 +167,18 @@ class LocalServerService { ...@@ -167,18 +167,18 @@ class LocalServerService {
} }
// 解压 // 解压
await ZipUtil.extractZipFile(distFilePath, outputDirectory); await ZipUtil.extractZipFile(distFilePath, outputDirectory);
// 版本号 // 用于显示的版本号
await _getAndSetVersion(outputDirectory); await _getAndSetShowVersion(outputDirectory);
} }
// 读取和设置版本号 // 读取和设置用于显示的版本号
Future<void> _getAndSetVersion(String outputDirectory) async { Future<void> _getAndSetShowVersion(String outputDirectory) async {
var versionFile = File('$outputDirectory/version.txt'); var versionFile = File('$outputDirectory/version.txt');
if (await versionFile.exists()) { if (await versionFile.exists()) {
var content = (await versionFile.readAsString()).trim(); var content = (await versionFile.readAsString()).trim();
getIt.get<SharedPreferences>().setString(Constant.h5VersionKey, content); getIt.get<SharedPreferences>().setString(Constant.h5ShowVersionKey, content);
} else { } else {
getIt.get<SharedPreferences>().setString(Constant.h5VersionKey, 'unknown'); getIt.get<SharedPreferences>().setString(Constant.h5ShowVersionKey, 'undefined');
} }
} }
} }
Styling with Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!