feat:资源备份
This commit is contained in:
1 parent
e191cad606
commit
a92732310e
6 files changed
+124
-47
No files matched your search
@@ -43,14 +43,22 @@ abstract class AppDatabase extends FloorDatabase {
|
||||
SettingDao get getSettingDao;
|
||||
|
||||
static Future<AppDatabase> getDataBase() async {
|
||||
final dbPath = await AppFileTool.getDatabasePath();
|
||||
return await $FloorAppDatabase.databaseBuilder("cash_register.db").build();
|
||||
final dataBase = await $FloorAppDatabase
|
||||
.databaseBuilder(Constants.databaseName)
|
||||
.addCallback(
|
||||
Callback(
|
||||
onCreate: (database, version) async {
|
||||
logger.i('数据库已创建,版本: $version');
|
||||
},
|
||||
onOpen: (database) async {
|
||||
logger.i('数据库已打开');
|
||||
},
|
||||
onUpgrade: (database, startVersion, endVersion) {
|
||||
logger.i('数据库已升级,版本: $startVersion -> $endVersion');
|
||||
},
|
||||
),
|
||||
)
|
||||
.addMigrations([]).build();
|
||||
return dataBase;
|
||||
}
|
||||
|
||||
// static Future<AppDatabase> getDataBase() async {
|
||||
// final dbPath = await AppFileTool.getDatabasePath();
|
||||
// return await $FloorAppDatabase
|
||||
// .databaseBuilder("$dbPath/cash_register.db")
|
||||
// .addMigrations([]).build();
|
||||
// }
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -17,5 +17,9 @@ class Constants {
|
||||
/// 静态资源 url
|
||||
static const ossUrl = apiUrl;
|
||||
|
||||
/// ttl android 插件方法管道
|
||||
static const localTTSPlugsMethodChannel = 'local/tts';
|
||||
|
||||
/// 数据库名字
|
||||
static const databaseName = 'cash_register.db';
|
||||
}
|
||||
@@ -199,6 +199,29 @@ class SettingController extends GetxController {
|
||||
}
|
||||
}
|
||||
|
||||
// 备份sql
|
||||
void backupsSql() async {
|
||||
EasyLoading.show(
|
||||
status: '备份中...',
|
||||
maskType: EasyLoadingMaskType.black,
|
||||
);
|
||||
try {
|
||||
final bool = await AppFileTool.backupSqlFile();
|
||||
if (!bool) throw Error();
|
||||
EasyLoading.dismiss();
|
||||
Toast.success(msg: "备份中sql成功");
|
||||
EasyLoading.dismiss();
|
||||
} catch (e) {
|
||||
Toast.error(msg: "备份中sql失败");
|
||||
EasyLoading.dismiss();
|
||||
}
|
||||
}
|
||||
|
||||
// 备份日志
|
||||
void backupsLog() async {
|
||||
Toast.success(msg: "当前日志为实时备份");
|
||||
}
|
||||
|
||||
Future<void> getSetting() async {
|
||||
sysSetting.value = await AppSettingTool.getSysSetting();
|
||||
userSetting.value = await AppSettingTool.getUserSetting();
|
||||
|
||||
@@ -1,37 +1,38 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:forui/forui.dart';
|
||||
|
||||
import 'package:ducafe_ui_core/ducafe_ui_core.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
import 'package:pj_supermarket_cloud/common/index.dart';
|
||||
|
||||
import '../index.dart';
|
||||
|
||||
Widget settingBackup(BuildContext context) {
|
||||
return FutureBuilder(
|
||||
future: AppSettingTool.getSysSetting(),
|
||||
builder: (context, snapshot) {
|
||||
if (!snapshot.hasData) {
|
||||
return const SizedBox.shrink();
|
||||
}
|
||||
final sys = snapshot.data!;
|
||||
return <Widget>[
|
||||
FTile(
|
||||
prefixIcon: appPrefixIcon(context, FAssets.icons.user),
|
||||
suffixIcon: appSuffixIcon(context, FAssets.icons.files),
|
||||
title: appPrefixTitle(context, "备份路径"),
|
||||
details: appSuffixTitle(context, sys.externalPath),
|
||||
onPress: () => KeywordTool.copy(sys.sysType.cnStr),
|
||||
),
|
||||
FTile(
|
||||
prefixIcon: appPrefixIcon(context, FAssets.icons.user),
|
||||
suffixIcon: appSuffixIcon(context, FAssets.icons.folderSync),
|
||||
title: appPrefixTitle(context, "sql备份"),
|
||||
onPress: () => KeywordTool.copy(sys.sysType.cnStr),
|
||||
),
|
||||
FTile(
|
||||
prefixIcon: appPrefixIcon(context, FAssets.icons.user),
|
||||
suffixIcon: appSuffixIcon(context, FAssets.icons.folderSync),
|
||||
title: appPrefixTitle(context, "log备份"),
|
||||
onPress: () => KeywordTool.copy(sys.sysType.cnStr),
|
||||
),
|
||||
].toColumnSpace(space: 20.h);
|
||||
});
|
||||
return GetBuilder<SettingController>(builder: (controller) {
|
||||
return Obx(() {
|
||||
final sys = controller.sysSetting.value;
|
||||
if (sys == null) return const SizedBox.shrink();
|
||||
return <Widget>[
|
||||
FTile(
|
||||
prefixIcon: appPrefixIcon(context, FAssets.icons.user),
|
||||
suffixIcon: appSuffixIcon(context, FAssets.icons.files),
|
||||
title: appPrefixTitle(context, "备份路径"),
|
||||
details: appSuffixTitle(context, sys.externalPath),
|
||||
onPress: () => KeywordTool.copy(sys.sysType.cnStr),
|
||||
),
|
||||
FTile(
|
||||
prefixIcon: appPrefixIcon(context, FAssets.icons.user),
|
||||
suffixIcon: appSuffixIcon(context, FAssets.icons.folderSync),
|
||||
title: appPrefixTitle(context, "sql备份"),
|
||||
onPress: () => controller.backupsSql(),
|
||||
),
|
||||
FTile(
|
||||
prefixIcon: appPrefixIcon(context, FAssets.icons.user),
|
||||
suffixIcon: appSuffixIcon(context, FAssets.icons.folderSync),
|
||||
title: appPrefixTitle(context, "log备份"),
|
||||
onPress: () => controller.backupsLog(),
|
||||
),
|
||||
].toColumnSpace(space: 20.h);
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user