543 lines
18 KiB
Dart
543 lines
18 KiB
Dart
import 'package:pj_supermarket_cloud/common/utils/app/file_access.dart';
|
|
import 'package:pj_supermarket_cloud/common/utils/app/package_download.dart';
|
|
import 'dart:async';
|
|
import 'dart:io';
|
|
import 'dart:math' as math;
|
|
|
|
import 'package:background_downloader/background_downloader.dart';
|
|
import 'package:common_utils/common_utils.dart';
|
|
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_easyloading/flutter_easyloading.dart';
|
|
import 'package:flutter_html/flutter_html.dart';
|
|
import 'package:forui/forui.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:package_info_plus/package_info_plus.dart';
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
|
|
import 'package:pj_supermarket_cloud/common/index.dart';
|
|
|
|
import 'setting_components.dart';
|
|
|
|
/// 版本管理页面控制器,负责终端平台精确查询、当前版本识别和人工下载。
|
|
class VersionManageController extends GetxController {
|
|
/// 版本页诊断日志使用的稳定模块标签。
|
|
static const String _tag = '版本管理 Controller';
|
|
|
|
/// 当前服务端分页页码,从 1 开始。
|
|
final currentPage = 1.obs;
|
|
|
|
/// 服务端返回的版本总条数。
|
|
final total = 0.obs;
|
|
|
|
/// 紧凑列表固定每页展示 8 条。
|
|
final pageSize = 8.obs;
|
|
|
|
/// 当前页中与本机 App 编码、终端平台精确匹配的已发布版本。
|
|
final versionList = <ServiceAppVersion>[].obs;
|
|
|
|
/// 当前是否正在读取版本列表。
|
|
final isLoading = false.obs;
|
|
|
|
/// 列表加载失败时展示的可恢复错误摘要。
|
|
final errorMessage = ''.obs;
|
|
|
|
/// 当前安装 APK 的内部版本码,用于标记“当前版本”并禁止误判升级。
|
|
final currentVersionCode = 0.obs;
|
|
|
|
/// 初始化当前安装版本后加载第一页数据。
|
|
@override
|
|
void onInit() {
|
|
super.onInit();
|
|
unawaited(_initialize());
|
|
}
|
|
|
|
/// 先读取本机 Build,再请求终端平台版本列表,避免首帧标识跳变。
|
|
Future<void> _initialize() async {
|
|
final package = await PackageInfo.fromPlatform();
|
|
currentVersionCode.value = int.tryParse(package.buildNumber) ?? 0;
|
|
await getVersionList();
|
|
}
|
|
|
|
/// 切换页码或刷新当前页。
|
|
Future<void> doPage({int? page}) async {
|
|
if (page != null) currentPage.value = page;
|
|
await getVersionList(showLoading: true);
|
|
}
|
|
|
|
/// 根据本机 SysType 传递精确平台编码,只读取服务端返回的已发布版本。
|
|
Future<void> getVersionList({bool showLoading = false}) async {
|
|
isLoading.value = true;
|
|
errorMessage.value = '';
|
|
if (showLoading) {
|
|
EasyLoading.show(status: '查询中...', maskType: EasyLoadingMaskType.black);
|
|
}
|
|
try {
|
|
final setting = await AppSettingTool.getSysSetting();
|
|
final data = await AppApi.openCardList({
|
|
'appUniqueCode': Constants.deviceUniqueCode,
|
|
'platform': AppPlatformEnum.terminalCode(setting.sysType),
|
|
'current': currentPage.value,
|
|
'size': pageSize.value,
|
|
});
|
|
versionList.assignAll(data?.records ?? const []);
|
|
total.value = data?.total ?? 0;
|
|
} catch (error, stack) {
|
|
versionList.clear();
|
|
errorMessage.value = '版本列表加载失败,请检查网络后重试';
|
|
logger.e('|$_tag|获取版本列表失败error:|$error|stack:|$stack|');
|
|
} finally {
|
|
isLoading.value = false;
|
|
if (showLoading) EasyLoading.dismiss();
|
|
}
|
|
}
|
|
|
|
/// 打开右侧版本详情抽屉,列表本身不承载更新说明和下载操作。
|
|
Future<void> openDetails(
|
|
BuildContext context,
|
|
ServiceAppVersion item,
|
|
) async {
|
|
await _showVersionDrawer(
|
|
context,
|
|
item: item,
|
|
currentVersionCode: currentVersionCode.value,
|
|
onDownload: () => doDownload(item),
|
|
);
|
|
}
|
|
|
|
/// 人工下载继续保存到系统下载目录,不参与 Root 自动安装流程。
|
|
Future<void> doDownload(ServiceAppVersion version) async {
|
|
final url = version.downloadUrl ?? '';
|
|
if (url.isEmpty) {
|
|
Toast.error(msg: '当前版本没有可用下载地址');
|
|
return;
|
|
}
|
|
final downUrl = FileAccess.packageUrl(url);
|
|
if (!Platform.isAndroid) {
|
|
EasyLoading.showInfo('当前平台不支持系统下载');
|
|
return;
|
|
}
|
|
EasyLoading.show(status: '下载中...', maskType: EasyLoadingMaskType.black);
|
|
try {
|
|
var granted = await Permission.storage.request().isGranted;
|
|
if (!granted && await Permission.manageExternalStorage.isDenied) {
|
|
granted = await Permission.manageExternalStorage.request().isGranted;
|
|
}
|
|
if (!granted) {
|
|
EasyLoading.showError('存储权限未授权,无法下载');
|
|
return;
|
|
}
|
|
final ext = AppFileTool.getFileExtension(downUrl);
|
|
final useExt = ObjectUtil.isEmpty(ext) ? '.apk' : ext;
|
|
final fileName = 'pj_xfj_${DateTime.now().millisecondsSinceEpoch}$useExt';
|
|
final task = DownloadTask(
|
|
url: downUrl,
|
|
filename: fileName,
|
|
updates: Updates.statusAndProgress,
|
|
baseDirectory: BaseDirectory.applicationDocuments,
|
|
);
|
|
final result = await downloadPackageWithRefresh(
|
|
task: task,
|
|
refreshUrl: () => AppApi.refreshPackageUrl(version),
|
|
download: (currentTask) => FileDownloader().download(
|
|
currentTask,
|
|
onProgress: (progress) => EasyLoading.showProgress(
|
|
progress,
|
|
status: '(${(progress * 100).toStringAsFixed(0)}%) 下载中...',
|
|
),
|
|
onStatus: (status) => logger.d('|$_tag|下载状态:$status'),
|
|
),
|
|
);
|
|
if (result.status != TaskStatus.complete) {
|
|
EasyLoading.showError('下载失败');
|
|
return;
|
|
}
|
|
final movedPath = await FileDownloader().moveToSharedStorage(
|
|
task,
|
|
SharedStorage.downloads,
|
|
mimeType: useExt == '.apk'
|
|
? 'application/vnd.android.package-archive'
|
|
: 'application/octet-stream',
|
|
);
|
|
if (ObjectUtil.isEmpty(movedPath)) {
|
|
EasyLoading.showError('移动到下载目录失败');
|
|
return;
|
|
}
|
|
logger.i('|$_tag|人工下载完成:$movedPath');
|
|
EasyLoading.showSuccess('下载完成');
|
|
} catch (error) {
|
|
logger.e('|$_tag|下载失败type:|${error.runtimeType}|');
|
|
EasyLoading.showError('下载失败');
|
|
} finally {
|
|
EasyLoading.dismiss();
|
|
}
|
|
}
|
|
}
|
|
|
|
/// 构建每页 8 条的紧凑版本列表,点击行后从右侧展开完整详情。
|
|
Widget settingVersionSetting(BuildContext context) {
|
|
final controller = Get.isRegistered<VersionManageController>()
|
|
? Get.find<VersionManageController>()
|
|
: Get.put(VersionManageController());
|
|
return <Widget>[
|
|
SettingActionButton(
|
|
label: '刷新版本列表',
|
|
icon: FAssets.icons.refreshCw,
|
|
primary: true,
|
|
onTap: controller.doPage,
|
|
).alignRight(),
|
|
Obx(() {
|
|
if (controller.isLoading.value) {
|
|
return const Center(child: CircularProgressIndicator());
|
|
}
|
|
if (controller.errorMessage.isNotEmpty) {
|
|
return SettingEmpty(message: controller.errorMessage.value);
|
|
}
|
|
if (controller.versionList.isEmpty) {
|
|
return const SettingEmpty(message: '暂无已发布版本');
|
|
}
|
|
return SettingGroup(
|
|
title: '已发布版本',
|
|
child: List.generate(
|
|
controller.versionList.length,
|
|
(index) {
|
|
final item = controller.versionList[index];
|
|
return _VersionRow(
|
|
item: item,
|
|
latest: controller.currentPage.value == 1 && index == 0,
|
|
current: item.versionCode == controller.currentVersionCode.value,
|
|
onTap: () => controller.openDetails(context, item),
|
|
);
|
|
},
|
|
growable: false,
|
|
).toColumn(
|
|
separator: const Divider(
|
|
height: 1,
|
|
color: XfjDesignTokens.line,
|
|
),
|
|
),
|
|
);
|
|
}).expanded(),
|
|
Obx(
|
|
() => appPage(
|
|
context,
|
|
pageNo: controller.currentPage.value,
|
|
pageSize: controller.pageSize.value,
|
|
total: controller.total.value,
|
|
onPageChanged: (pageNo) => controller.doPage(page: pageNo),
|
|
),
|
|
),
|
|
].toColumnSpace(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
space: XfjDesignTokens.cardGap.h,
|
|
);
|
|
}
|
|
|
|
/// 单条版本只展示版本名、Build、平台、发布状态和当前/最新标识。
|
|
class _VersionRow extends StatelessWidget {
|
|
/// 创建一个具有稳定高度和完整点击热区的版本行。
|
|
const _VersionRow({
|
|
required this.item,
|
|
required this.latest,
|
|
required this.current,
|
|
required this.onTap,
|
|
});
|
|
|
|
/// 服务端版本记录。
|
|
final ServiceAppVersion item;
|
|
|
|
/// 是否为第一页版本码最高记录。
|
|
final bool latest;
|
|
|
|
/// 是否与当前安装 Build 相同。
|
|
final bool current;
|
|
|
|
/// 打开右侧详情抽屉的回调。
|
|
final VoidCallback onTap;
|
|
|
|
/// 构建固定列宽的紧凑扫描行。
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = FTheme.of(context);
|
|
final platform =
|
|
AppPlatformEnum.values.fromCode(item.platform ?? '')?.title ?? '未知平台';
|
|
final status =
|
|
AppVersionStatusEnum.values.fromCode(item.status ?? '')?.title ??
|
|
'未知状态';
|
|
return InkWell(
|
|
onTap: onTap,
|
|
child: <Widget>[
|
|
<Widget>[
|
|
Text(
|
|
item.versionName ?? '--',
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: theme.typography.base.copyWith(
|
|
color: XfjDesignTokens.ink,
|
|
fontWeight: FontWeight.w800,
|
|
),
|
|
),
|
|
Text(
|
|
'Build ${item.versionCode ?? '--'}',
|
|
style: theme.typography.sm.copyWith(
|
|
color: XfjDesignTokens.muted,
|
|
fontFeatures: const [FontFeature.tabularFigures()],
|
|
),
|
|
),
|
|
]
|
|
.toColumnSpace(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
space: 4.h,
|
|
)
|
|
.expanded(flex: 3),
|
|
Text(platform, style: theme.typography.sm).expanded(flex: 2),
|
|
_versionBadge(status, XfjDesignTokens.success),
|
|
<Widget>[
|
|
if (current) _versionBadge('当前', XfjDesignTokens.primary),
|
|
if (latest) _versionBadge('最新', XfjDesignTokens.warning),
|
|
if (!current && !latest)
|
|
Text('--',
|
|
style:
|
|
theme.typography.sm.copyWith(color: XfjDesignTokens.muted)),
|
|
]
|
|
.toRowSpace(mainAxisSize: MainAxisSize.min, space: 8.w)
|
|
.expanded(flex: 2),
|
|
FIcon(
|
|
FAssets.icons.chevronRight,
|
|
color: XfjDesignTokens.muted,
|
|
size: 20.sp,
|
|
),
|
|
]
|
|
.toRowSpace(space: 18.w)
|
|
.padding(horizontal: 20.w, vertical: 10.h)
|
|
.constrained(minHeight: 68.h),
|
|
);
|
|
}
|
|
}
|
|
|
|
/// 构建同时带文字与语义色的版本状态标签,避免只依靠颜色表达状态。
|
|
Widget _versionBadge(String label, Color color) => Text(
|
|
label,
|
|
maxLines: 1,
|
|
style: TextStyle(
|
|
color: color,
|
|
fontSize: 13.sp,
|
|
fontWeight: FontWeight.w700,
|
|
),
|
|
).padding(horizontal: 10.w, vertical: 5.h).decorated(
|
|
color: color.withValues(alpha: 0.1),
|
|
borderRadius: BorderRadius.circular(XfjDesignTokens.controlRadius),
|
|
);
|
|
|
|
/// 从右侧展示完整版本信息与静态 HTML 更新内容,遮罩点击和关闭图标均可退出。
|
|
Future<void> _showVersionDrawer(
|
|
BuildContext context, {
|
|
required ServiceAppVersion item,
|
|
required int currentVersionCode,
|
|
required VoidCallback onDownload,
|
|
}) async {
|
|
await showGeneralDialog<void>(
|
|
context: context,
|
|
barrierDismissible: true,
|
|
barrierLabel: '关闭版本详情',
|
|
barrierColor: Colors.black.withValues(alpha: 0.45),
|
|
transitionDuration: const Duration(milliseconds: 220),
|
|
pageBuilder: (dialogContext, _, __) {
|
|
final width = math.min(MediaQuery.sizeOf(context).width * 0.52, 960.w);
|
|
return Align(
|
|
alignment: Alignment.centerRight,
|
|
child: Material(
|
|
color: XfjDesignTokens.surface,
|
|
child: SafeArea(
|
|
child: _VersionDetail(
|
|
item: item,
|
|
currentVersionCode: currentVersionCode,
|
|
onClose: () => Navigator.of(dialogContext).pop(),
|
|
onDownload: onDownload,
|
|
).width(width),
|
|
),
|
|
),
|
|
);
|
|
},
|
|
transitionBuilder: (_, animation, __, child) => SlideTransition(
|
|
position: Tween<Offset>(
|
|
begin: const Offset(1, 0),
|
|
end: Offset.zero,
|
|
).animate(CurvedAnimation(parent: animation, curve: Curves.easeOutCubic)),
|
|
child: child,
|
|
),
|
|
);
|
|
}
|
|
|
|
/// 右侧抽屉内容,详情与 HTML 使用单一滚动区域,底部下载操作保持可见。
|
|
class _VersionDetail extends StatelessWidget {
|
|
/// 创建版本详情内容。
|
|
const _VersionDetail({
|
|
required this.item,
|
|
required this.currentVersionCode,
|
|
required this.onClose,
|
|
required this.onDownload,
|
|
});
|
|
|
|
/// 服务端版本记录。
|
|
final ServiceAppVersion item;
|
|
|
|
/// 当前安装版本码,用于详情状态说明。
|
|
final int currentVersionCode;
|
|
|
|
/// 关闭抽屉回调。
|
|
final VoidCallback onClose;
|
|
|
|
/// 人工下载 APK 回调。
|
|
final VoidCallback onDownload;
|
|
|
|
/// 构建标题、元数据、静态 HTML 与底部操作。
|
|
@override
|
|
Widget build(BuildContext context) {
|
|
final theme = FTheme.of(context);
|
|
final platform =
|
|
AppPlatformEnum.values.fromCode(item.platform ?? '')?.title ?? '未知平台';
|
|
final status =
|
|
AppVersionStatusEnum.values.fromCode(item.status ?? '')?.title ??
|
|
'未知状态';
|
|
return <Widget>[
|
|
<Widget>[
|
|
<Widget>[
|
|
Text(
|
|
'版本 ${item.versionName ?? '--'}',
|
|
maxLines: 1,
|
|
overflow: TextOverflow.ellipsis,
|
|
style: theme.typography.xl2.copyWith(
|
|
color: XfjDesignTokens.ink,
|
|
fontWeight: FontWeight.w800,
|
|
),
|
|
),
|
|
Text(
|
|
'Build ${item.versionCode ?? '--'}',
|
|
style: theme.typography.sm.copyWith(color: XfjDesignTokens.muted),
|
|
),
|
|
]
|
|
.toColumnSpace(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
space: 4.h,
|
|
)
|
|
.expanded(),
|
|
IconButton(
|
|
tooltip: '关闭版本详情',
|
|
onPressed: onClose,
|
|
icon: FIcon(FAssets.icons.x, size: 22.sp),
|
|
),
|
|
]
|
|
.toRowSpace(space: 12.w)
|
|
.padding(horizontal: 24.w, vertical: 18.h)
|
|
.decorated(
|
|
border:
|
|
const Border(bottom: BorderSide(color: XfjDesignTokens.line)),
|
|
),
|
|
<Widget>[
|
|
_detailLine(context, '平台', platform),
|
|
_detailLine(context, '状态', status),
|
|
_detailLine(context, 'App 编码', item.appUniqueCode ?? '--'),
|
|
_detailLine(context, '强制更新', item.isForce == true ? '是' : '否'),
|
|
_detailLine(context, '静默更新', item.isSilent == true ? '是' : '否'),
|
|
_detailLine(
|
|
context,
|
|
'本机状态',
|
|
item.versionCode == currentVersionCode ? '当前版本' : '非当前版本',
|
|
),
|
|
Text(
|
|
'更新内容',
|
|
style: theme.typography.lg.copyWith(
|
|
color: XfjDesignTokens.ink,
|
|
fontWeight: FontWeight.w800,
|
|
),
|
|
).paddingOnly(top: 16.h, bottom: 6.h),
|
|
Html(
|
|
data: ObjectUtil.isEmpty(item.updateContent)
|
|
? '<p>暂无更新内容</p>'
|
|
: item.updateContent!,
|
|
shrinkWrap: true,
|
|
// 只允许静态文本排版标签;a 标签保留文字但回调不执行任何外链跳转。
|
|
onlyRenderTheseTags: const {
|
|
'html',
|
|
'body',
|
|
'div',
|
|
'span',
|
|
'p',
|
|
'br',
|
|
'ul',
|
|
'ol',
|
|
'li',
|
|
'strong',
|
|
'b',
|
|
'em',
|
|
'i',
|
|
'h1',
|
|
'h2',
|
|
'h3',
|
|
'h4',
|
|
'h5',
|
|
'h6',
|
|
'blockquote',
|
|
'a',
|
|
},
|
|
doNotRenderTheseTags: const {
|
|
'script',
|
|
'iframe',
|
|
'object',
|
|
'embed',
|
|
'form',
|
|
'input',
|
|
'button',
|
|
},
|
|
onLinkTap: (_, __, ___) {},
|
|
style: {
|
|
'body': Style(
|
|
color: XfjDesignTokens.ink,
|
|
fontSize: FontSize(15.sp),
|
|
lineHeight: const LineHeight(1.6),
|
|
margin: Margins.zero,
|
|
),
|
|
'p': Style(margin: Margins.only(bottom: 10.h)),
|
|
'li': Style(margin: Margins.only(bottom: 6.h)),
|
|
},
|
|
),
|
|
]
|
|
.toColumn(crossAxisAlignment: CrossAxisAlignment.stretch)
|
|
.padding(horizontal: 24.w, vertical: 18.h)
|
|
.scrollable()
|
|
.expanded(),
|
|
SettingActionButton(
|
|
label: '下载 APK',
|
|
icon: FAssets.icons.arrowDownToLine,
|
|
primary: true,
|
|
enabled: ObjectUtil.isNotEmpty(item.downloadUrl),
|
|
onTap: onDownload,
|
|
).alignRight().padding(horizontal: 24.w, vertical: 16.h).decorated(
|
|
border: const Border(top: BorderSide(color: XfjDesignTokens.line)),
|
|
),
|
|
].toColumn(crossAxisAlignment: CrossAxisAlignment.stretch);
|
|
}
|
|
}
|
|
|
|
/// 构建抽屉元数据键值行,长值允许换行且不会挤压标签。
|
|
Widget _detailLine(BuildContext context, String label, String value) {
|
|
final theme = FTheme.of(context);
|
|
return <Widget>[
|
|
Text(
|
|
label,
|
|
style: theme.typography.sm.copyWith(color: XfjDesignTokens.muted),
|
|
).width(130.w),
|
|
Text(
|
|
value,
|
|
textAlign: TextAlign.right,
|
|
style: theme.typography.base.copyWith(
|
|
color: XfjDesignTokens.ink,
|
|
fontWeight: FontWeight.w600,
|
|
),
|
|
).expanded(),
|
|
]
|
|
.toRowSpace(crossAxisAlignment: CrossAxisAlignment.start, space: 16.w)
|
|
.padding(vertical: 8.h);
|
|
}
|