feat:资源备份
This commit is contained in:
1 parent
e191cad606
commit
a92732310e
6 files changed
+124
-47
No files matched your search
@@ -1,9 +1,9 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:common_utils/common_utils.dart';
|
||||
import 'package:external_path/external_path.dart';
|
||||
import 'package:pj_supermarket_cloud/common/index.dart';
|
||||
|
||||
import 'package:pj_supermarket_cloud/common/utils/index.dart';
|
||||
import 'package:pj_supermarket_cloud/common/index.dart';
|
||||
|
||||
abstract class AppFileTool {
|
||||
static const _tag = "app文件工具";
|
||||
@@ -40,16 +40,29 @@ abstract class AppFileTool {
|
||||
|
||||
/// 创建数据库文件地址
|
||||
static Future<String?> getDatabasePath() async {
|
||||
final backupDir = await getExternalPath(
|
||||
backupExternalPath: false,
|
||||
);
|
||||
return _checkAndcreateDir("$backupDir/$_backupDatabaseDir");
|
||||
final appSetting = await AppSettingTool.getSysSetting();
|
||||
return appSetting.sqlAddress;
|
||||
}
|
||||
|
||||
/// 保存数据库文件地址
|
||||
static Future<void> setDatabasePath(String sqlAddress) async {
|
||||
final appSetting = await AppSettingTool.getSysSetting();
|
||||
appSetting.sqlAddress = sqlAddress;
|
||||
await AppSettingTool.setSysSetting(appSetting);
|
||||
}
|
||||
|
||||
/// 创建数据库文件备份地址
|
||||
static Future<String?> getDatabaseBackupPath() async {
|
||||
final backupDir = await getExternalPath();
|
||||
return _checkAndcreateDir("$backupDir/$_baseBackupDir/$_backupDatabaseDir");
|
||||
String backupDir = await getExternalPath();
|
||||
String dir = "$backupDir/$_baseBackupDir/$_backupDatabaseDir";
|
||||
DateTime now = DateTime.now();
|
||||
int year = now.year;
|
||||
int month = now.month;
|
||||
int day = now.day;
|
||||
dir = "$dir/$year-$month";
|
||||
dir = await _checkAndcreateDir(backupDir);
|
||||
dir = "$dir/$day.db";
|
||||
return dir;
|
||||
}
|
||||
|
||||
/// 创建日志地址
|
||||
@@ -83,4 +96,27 @@ abstract class AppFileTool {
|
||||
final backupDir = await getExternalPath();
|
||||
return _checkAndcreateDir("$backupDir/$_baseBackupDir/$_backupFaceDir");
|
||||
}
|
||||
|
||||
// 备份数据库
|
||||
static Future<bool> backupSqlFile() async {
|
||||
final sourcePath = await getDatabasePath();
|
||||
if (ObjectUtil.isEmpty(sourcePath)) {
|
||||
logger.e("|$_tag|备份sql文件失败,无效的sourcePath路径path:|$sourcePath|");
|
||||
return false;
|
||||
}
|
||||
String? backupPath = await getDatabaseBackupPath();
|
||||
if (ObjectUtil.isEmpty(backupPath)) {
|
||||
logger.e("|$_tag|备份sql文件失败,无效的backupPath路径path:|$backupPath|");
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
await File("$sourcePath/${Constants.databaseName}").copy(backupPath!);
|
||||
return true;
|
||||
} catch (e, stack) {
|
||||
logger.e("|$_tag|备份sql文件失败,意外的错误error|$e|stack|$stack|");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// 备份日志
|
||||
}
|
||||
@@ -2,6 +2,7 @@ import 'dart:async';
|
||||
import 'package:event_bus/event_bus.dart';
|
||||
|
||||
import 'package:get/get.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
import 'package:pj_supermarket_cloud/common/index.dart';
|
||||
|
||||
@@ -49,6 +50,10 @@ class AppControl extends GetxController {
|
||||
serialCardManage = SerialCardManage();
|
||||
srialSteelyardManage = SerialSteelyardManage();
|
||||
AppNetworkTool.listenNetwork();
|
||||
await AppFileTool.setDatabasePath(
|
||||
await getDatabasesPath(),
|
||||
);
|
||||
await AppFileTool.backupSqlFile();
|
||||
Get.offAllNamed(RouteNames.login);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user