Skip to content

Commit

Permalink
完善导入功能
Browse files Browse the repository at this point in the history
  • Loading branch information
clssw1004 committed Dec 13, 2024
1 parent 6f2e265 commit b9f42c0
Show file tree
Hide file tree
Showing 12 changed files with 346 additions and 239 deletions.
2 changes: 1 addition & 1 deletion lib/data/data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ abstract class DataSource {
Future<BatchDeleteResult> batchDeleteAccountItems(List<String> itemIds);

/// 导入数据
Future<void> importData({
Future<Map<String, dynamic>> importData({
required String accountBookId,
required String dataSource,
required PlatformFile file,
Expand Down
10 changes: 7 additions & 3 deletions lib/data/http/http_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class HttpClient {
void setToken(String token) {
_token = token;
print('HttpClient setToken: $token');

_dio.options.headers = {
..._dio.options.headers,
'Authorization': 'Bearer $token',
Expand All @@ -78,14 +78,18 @@ class HttpClient {
try {
print('Request path: $path');
print('Request data: $data');
print('Request options: ${options?.headers}');
final contentType =
options?.headers?['Content-Type'] ?? 'application/json';
print('Request options: $contentType');
final response = await _dio.request<Map<String, dynamic>>(
path,
queryParameters: queryParameters,
data: data,
options: options ?? Options(
options: Options(
method: method.value,
headers: {
'Content-Type': 'application/json',
'Content-Type': contentType,
if (_token != null) 'Authorization': 'Bearer $_token',
},
),
Expand Down
35 changes: 26 additions & 9 deletions lib/data/http/http_data_source.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'api_endpoints.dart';
import 'http_client.dart';
import '../../constants/storage_keys.dart';
import 'package:intl/intl.dart';
import 'package:http_parser/http_parser.dart';

class HttpDataSource implements DataSource {
final HttpClient _httpClient;
Expand Down Expand Up @@ -497,26 +498,42 @@ class HttpDataSource implements DataSource {
}

@override
Future<void> importData({
Future<Map<String, dynamic>> importData({
required String accountBookId,
required String dataSource,
required PlatformFile file,
}) async {
try {
final formData = FormData.fromMap({
'accountBookId': accountBookId,
'dataSource': dataSource,
'file': await MultipartFile.fromFile(
file.path!,
filename: file.name,
final formData = FormData();
formData.fields.addAll([
MapEntry('accountBookId', accountBookId),
MapEntry('dataSource', dataSource),
]);
formData.files.add(
MapEntry(
'file',
await MultipartFile.fromFile(
file.path!,
filename: file.name,
contentType: MediaType('text', 'csv'),
),
),
});
);

await _httpClient.request<void>(
final response = await _httpClient.request<Map<String, dynamic>>(
path: '/api/import',
method: HttpMethod.post,
data: formData,
options: Options(
headers: {
'Content-Type': 'multipart/form-data',
},
responseType: ResponseType.json,
sendTimeout: const Duration(minutes: 2),
receiveTimeout: const Duration(minutes: 2),
),
);
return response;
} on DioException catch (e) {
throw _handleDioError(e);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/data/sqlite/sqlite_data_source.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'package:file_picker/src/platform_file.dart';

import '../../models/account_item_request.dart';
import '../../models/server_status.dart';
import '../../services/api_service.dart';
Expand Down Expand Up @@ -524,10 +526,10 @@ class SqliteDataSource implements DataSource {
}

@override
Future<void> importData(
Future<Map<String, dynamic>> importData(
{required String accountBookId,
required String dataSource,
required file}) {
required PlatformFile file}) {
// TODO: implement importData
throw UnimplementedError();
}
Expand Down
12 changes: 12 additions & 0 deletions lib/generated/app_localizations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1931,6 +1931,18 @@ abstract class AppLocalizations {
/// In en, this message translates to:
/// **'Select Account Book'**
String get selectBook;

/// No description provided for @importSuccessCount.
///
/// In en, this message translates to:
/// **'Successfully imported {count} records'**
String importSuccessCount(String count);

/// No description provided for @importErrors.
///
/// In en, this message translates to:
/// **'The following errors occurred during import:'**
String get importErrors;
}

class _AppLocalizationsDelegate extends LocalizationsDelegate<AppLocalizations> {
Expand Down
8 changes: 8 additions & 0 deletions lib/generated/app_localizations_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -974,4 +974,12 @@ class AppLocalizationsEn extends AppLocalizations {

@override
String get selectBook => 'Select Account Book';

@override
String importSuccessCount(String count) {
return 'Successfully imported $count records';
}

@override
String get importErrors => 'The following errors occurred during import:';
}
14 changes: 11 additions & 3 deletions lib/generated/app_localizations_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ class AppLocalizationsZh extends AppLocalizations {
String get accountName => '账户名称';

@override
String get accountRemark => '账户备���';
String get accountRemark => '账户备注';

@override
String get currentBalance => '当前余额';
Expand Down Expand Up @@ -502,7 +502,7 @@ class AppLocalizationsZh extends AppLocalizations {
}

@override
String get permViewBook => '查看账本';
String get permViewBook => '查看���本';

@override
String get permEditBook => '编辑账本';
Expand Down Expand Up @@ -901,7 +901,7 @@ class AppLocalizationsZh extends AppLocalizations {
String get serverCheckFailed => '服务器检测失败';

@override
String get serverCheckFailedMessage => '无法连接到服务器,请检查网络连接后重试。';
String get serverCheckFailedMessage => '无法连接到服务��,请检查网络连接后重试。';

@override
String get backToLogin => '返回登录';
Expand Down Expand Up @@ -974,6 +974,14 @@ class AppLocalizationsZh extends AppLocalizations {

@override
String get selectBook => '选择账本';

@override
String importSuccessCount(String count) {
return '成功导入 $count 条记录';
}

@override
String get importErrors => '导入过程中出现以下错误:';
}

/// The translations for Chinese, using the Han script (`zh_Hant`).
Expand Down
11 changes: 10 additions & 1 deletion lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -350,5 +350,14 @@
"selectFile": "Select File",
"importFieldsRequired": "Please fill in all required fields",
"importSuccess": "Import successful",
"selectBook": "Select Account Book"
"selectBook": "Select Account Book",
"importSuccessCount": "Successfully imported {count} records",
"@importSuccessCount": {
"placeholders": {
"count": {
"type": "String"
}
}
},
"importErrors": "The following errors occurred during import:"
}
17 changes: 13 additions & 4 deletions lib/l10n/app_zh.arb
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@
"unknown": "未知",
"basicInfo": "基本信息",
"accountName": "账户名称",
"accountRemark": "账户备���",
"accountRemark": "账户备注",
"currentBalance": "当前余额",
"pleaseInputAccountName": "请输入账户名称",
"fundIncome": "收入",
Expand Down Expand Up @@ -169,7 +169,7 @@
"memberAlreadyExists": "该用户已经是成员",
"addMemberSuccess": "成员添加成功",
"addMemberFailed": "添加成员失败:{error}",
"permViewBook": "查看账本",
"permViewBook": "查看���本",
"permEditBook": "编辑账本",
"permDeleteBook": "删除账本",
"permViewItem": "查看账目",
Expand Down Expand Up @@ -304,7 +304,7 @@
},
"checkingServerStatus": "正在检测服务器状态...",
"serverCheckFailed": "服务器检测失败",
"serverCheckFailedMessage": "无法连接到服务器,请检查网络连接后重试。",
"serverCheckFailedMessage": "无法连接到服务��,请检查网络连接后重试。",
"backToLogin": "返回登录",
"retry": "重试",
"about": "关于",
Expand All @@ -328,5 +328,14 @@
"selectFile": "选择文件",
"importFieldsRequired": "请填写所有必填字段",
"importSuccess": "导入成功",
"selectBook": "选择账本"
"selectBook": "选择账本",
"importSuccessCount": "成功导入 {count} 条记录",
"@importSuccessCount": {
"placeholders": {
"count": {
"type": "String"
}
}
},
"importErrors": "导入过程中出现以下错误:"
}
Loading

0 comments on commit b9f42c0

Please sign in to comment.