feat: API 통합을 위한 기초 인프라 구축
- 네트워크 레이어 구현 (Dio 기반 ApiClient) - 환경별 설정 관리 시스템 구축 - 의존성 주입 설정 (GetIt) - API 엔드포인트 상수 정의 - 인터셉터 구현 (Auth, Error, Logging) - 프로젝트 아키텍처 개선 (core, data, di 디렉토리 구조) - API 통합 계획서 및 요구사항 문서 작성 - 필요 패키지 추가 (dio, flutter_secure_storage, get_it 등)
This commit is contained in:
65
lib/core/config/environment.dart
Normal file
65
lib/core/config/environment.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
||||
|
||||
/// 환경 설정 관리 클래스
|
||||
class Environment {
|
||||
static const String dev = 'development';
|
||||
static const String prod = 'production';
|
||||
|
||||
static late String _environment;
|
||||
|
||||
/// 현재 환경
|
||||
static String get current => _environment;
|
||||
|
||||
/// 개발 환경 여부
|
||||
static bool get isDevelopment => _environment == dev;
|
||||
|
||||
/// 프로덕션 환경 여부
|
||||
static bool get isProduction => _environment == prod;
|
||||
|
||||
/// API 베이스 URL
|
||||
static String get apiBaseUrl {
|
||||
return dotenv.env['API_BASE_URL'] ?? 'http://localhost:8080/api/v1';
|
||||
}
|
||||
|
||||
/// API 타임아웃 (밀리초)
|
||||
static int get apiTimeout {
|
||||
final timeoutStr = dotenv.env['API_TIMEOUT'] ?? '30000';
|
||||
return int.tryParse(timeoutStr) ?? 30000;
|
||||
}
|
||||
|
||||
/// 로깅 활성화 여부
|
||||
static bool get enableLogging {
|
||||
final loggingStr = dotenv.env['ENABLE_LOGGING'] ?? 'false';
|
||||
return loggingStr.toLowerCase() == 'true';
|
||||
}
|
||||
|
||||
/// 환경 초기화
|
||||
static Future<void> initialize([String? environment]) async {
|
||||
_environment = environment ??
|
||||
const String.fromEnvironment('ENVIRONMENT', defaultValue: dev);
|
||||
|
||||
final envFile = _getEnvFile();
|
||||
await dotenv.load(fileName: envFile);
|
||||
}
|
||||
|
||||
/// 환경별 파일 경로 반환
|
||||
static String _getEnvFile() {
|
||||
switch (_environment) {
|
||||
case prod:
|
||||
return '.env.production';
|
||||
case dev:
|
||||
default:
|
||||
return '.env.development';
|
||||
}
|
||||
}
|
||||
|
||||
/// 환경 변수 가져오기
|
||||
static String? get(String key) {
|
||||
return dotenv.env[key];
|
||||
}
|
||||
|
||||
/// 환경 변수 존재 여부 확인
|
||||
static bool has(String key) {
|
||||
return dotenv.env.containsKey(key);
|
||||
}
|
||||
}
|
||||
77
lib/core/constants/api_endpoints.dart
Normal file
77
lib/core/constants/api_endpoints.dart
Normal file
@@ -0,0 +1,77 @@
|
||||
/// API 엔드포인트 상수 정의
|
||||
class ApiEndpoints {
|
||||
// 인증
|
||||
static const String login = '/auth/login';
|
||||
static const String logout = '/auth/logout';
|
||||
static const String refresh = '/auth/refresh';
|
||||
static const String me = '/me';
|
||||
|
||||
// 장비 관리
|
||||
static const String equipment = '/equipment';
|
||||
static const String equipmentSearch = '/equipment/search';
|
||||
static const String equipmentIn = '/equipment/in';
|
||||
static const String equipmentOut = '/equipment/out';
|
||||
static const String equipmentBatchOut = '/equipment/batch-out';
|
||||
static const String equipmentManufacturers = '/equipment/manufacturers';
|
||||
static const String equipmentNames = '/equipment/names';
|
||||
static const String equipmentHistory = '/equipment/history';
|
||||
static const String equipmentRentals = '/equipment/rentals';
|
||||
static const String equipmentRepairs = '/equipment/repairs';
|
||||
static const String equipmentDisposals = '/equipment/disposals';
|
||||
|
||||
// 회사 관리
|
||||
static const String companies = '/companies';
|
||||
static const String companiesSearch = '/companies/search';
|
||||
static const String companiesNames = '/companies/names';
|
||||
static const String companiesCheckDuplicate = '/companies/check-duplicate';
|
||||
static const String companiesWithBranches = '/companies/with-branches';
|
||||
static const String companiesBranches = '/companies/{id}/branches';
|
||||
|
||||
// 사용자 관리
|
||||
static const String users = '/users';
|
||||
static const String usersSearch = '/users/search';
|
||||
static const String usersChangePassword = '/users/{id}/change-password';
|
||||
static const String usersStatus = '/users/{id}/status';
|
||||
|
||||
// 라이선스 관리
|
||||
static const String licenses = '/licenses';
|
||||
static const String licensesExpiring = '/licenses/expiring';
|
||||
static const String licensesAssign = '/licenses/{id}/assign';
|
||||
static const String licensesUnassign = '/licenses/{id}/unassign';
|
||||
|
||||
// 창고 위치 관리
|
||||
static const String warehouseLocations = '/warehouse-locations';
|
||||
static const String warehouseLocationsSearch = '/warehouse-locations/search';
|
||||
static const String warehouseEquipment = '/warehouse-locations/{id}/equipment';
|
||||
static const String warehouseCapacity = '/warehouse-locations/{id}/capacity';
|
||||
|
||||
// 파일 관리
|
||||
static const String filesUpload = '/files/upload';
|
||||
static const String filesDownload = '/files/{id}';
|
||||
|
||||
// 보고서
|
||||
static const String reports = '/reports';
|
||||
static const String reportsPdf = '/reports/{type}/pdf';
|
||||
static const String reportsExcel = '/reports/{type}/excel';
|
||||
|
||||
// 대시보드 및 통계
|
||||
static const String overviewStats = '/overview/stats';
|
||||
static const String overviewRecentActivities = '/overview/recent-activities';
|
||||
static const String overviewEquipmentStatus = '/overview/equipment-status';
|
||||
static const String overviewLicenseExpiry = '/overview/license-expiry';
|
||||
|
||||
// 대량 처리
|
||||
static const String bulkUpload = '/bulk/upload';
|
||||
static const String bulkUpdate = '/bulk/update';
|
||||
|
||||
// 감사 로그
|
||||
static const String auditLogs = '/audit-logs';
|
||||
|
||||
// 백업
|
||||
static const String backupCreate = '/backup/create';
|
||||
static const String backupRestore = '/backup/restore';
|
||||
|
||||
// 검색 및 조회
|
||||
static const String lookups = '/lookups';
|
||||
static const String categories = '/lookups/categories';
|
||||
}
|
||||
75
lib/core/constants/app_constants.dart
Normal file
75
lib/core/constants/app_constants.dart
Normal file
@@ -0,0 +1,75 @@
|
||||
/// 앱 전역 상수 정의
|
||||
class AppConstants {
|
||||
// API 관련
|
||||
static const int defaultPageSize = 20;
|
||||
static const int maxPageSize = 100;
|
||||
static const Duration cacheTimeout = Duration(minutes: 5);
|
||||
|
||||
// 토큰 키
|
||||
static const String accessTokenKey = 'access_token';
|
||||
static const String refreshTokenKey = 'refresh_token';
|
||||
static const String tokenTypeKey = 'token_type';
|
||||
static const String expiresInKey = 'expires_in';
|
||||
|
||||
// 사용자 권한 매핑
|
||||
static const Map<String, String> flutterToBackendRole = {
|
||||
'S': 'admin', // Super user
|
||||
'M': 'manager', // Manager
|
||||
'U': 'staff', // User
|
||||
'V': 'viewer', // Viewer
|
||||
};
|
||||
|
||||
static const Map<String, String> backendToFlutterRole = {
|
||||
'admin': 'S',
|
||||
'manager': 'M',
|
||||
'staff': 'U',
|
||||
'viewer': 'V',
|
||||
};
|
||||
|
||||
// 장비 상태
|
||||
static const Map<String, String> equipmentStatus = {
|
||||
'available': '사용가능',
|
||||
'in_use': '사용중',
|
||||
'maintenance': '유지보수',
|
||||
'disposed': '폐기',
|
||||
'rented': '대여중',
|
||||
};
|
||||
|
||||
// 정렬 옵션
|
||||
static const Map<String, String> sortOptions = {
|
||||
'created_at': '생성일',
|
||||
'updated_at': '수정일',
|
||||
'name': '이름',
|
||||
'status': '상태',
|
||||
};
|
||||
|
||||
// 날짜 형식
|
||||
static const String dateFormat = 'yyyy-MM-dd';
|
||||
static const String dateTimeFormat = 'yyyy-MM-dd HH:mm:ss';
|
||||
|
||||
// 파일 업로드
|
||||
static const int maxFileSize = 10 * 1024 * 1024; // 10MB
|
||||
static const List<String> allowedFileExtensions = [
|
||||
'jpg', 'jpeg', 'png', 'pdf', 'doc', 'docx', 'xls', 'xlsx'
|
||||
];
|
||||
|
||||
// 에러 메시지
|
||||
static const String networkError = '네트워크 연결을 확인해주세요.';
|
||||
static const String timeoutError = '요청 시간이 초과되었습니다.';
|
||||
static const String unauthorizedError = '인증이 필요합니다.';
|
||||
static const String serverError = '서버 오류가 발생했습니다.';
|
||||
static const String unknownError = '알 수 없는 오류가 발생했습니다.';
|
||||
|
||||
// 정규식 패턴
|
||||
static final RegExp emailRegex = RegExp(
|
||||
r'^[a-zA-Z0-9.]+@[a-zA-Z0-9]+\.[a-zA-Z]+',
|
||||
);
|
||||
|
||||
static final RegExp phoneRegex = RegExp(
|
||||
r'^01[0-9]{1}-?[0-9]{4}-?[0-9]{4}$',
|
||||
);
|
||||
|
||||
static final RegExp businessNumberRegex = RegExp(
|
||||
r'^[0-9]{3}-?[0-9]{2}-?[0-9]{5}$',
|
||||
);
|
||||
}
|
||||
117
lib/core/errors/exceptions.dart
Normal file
117
lib/core/errors/exceptions.dart
Normal file
@@ -0,0 +1,117 @@
|
||||
/// 커스텀 예외 클래스들 정의
|
||||
|
||||
/// 서버 예외
|
||||
class ServerException implements Exception {
|
||||
final String message;
|
||||
final int? statusCode;
|
||||
final Map<String, dynamic>? errors;
|
||||
|
||||
ServerException({
|
||||
required this.message,
|
||||
this.statusCode,
|
||||
this.errors,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() => 'ServerException: $message (code: $statusCode)';
|
||||
}
|
||||
|
||||
/// 캐시 예외
|
||||
class CacheException implements Exception {
|
||||
final String message;
|
||||
|
||||
CacheException({required this.message});
|
||||
|
||||
@override
|
||||
String toString() => 'CacheException: $message';
|
||||
}
|
||||
|
||||
/// 네트워크 예외
|
||||
class NetworkException implements Exception {
|
||||
final String message;
|
||||
|
||||
NetworkException({required this.message});
|
||||
|
||||
@override
|
||||
String toString() => 'NetworkException: $message';
|
||||
}
|
||||
|
||||
/// 인증 예외
|
||||
class UnauthorizedException implements Exception {
|
||||
final String message;
|
||||
|
||||
UnauthorizedException({required this.message});
|
||||
|
||||
@override
|
||||
String toString() => 'UnauthorizedException: $message';
|
||||
}
|
||||
|
||||
/// 유효성 검사 예외
|
||||
class ValidationException implements Exception {
|
||||
final String message;
|
||||
final Map<String, List<String>>? fieldErrors;
|
||||
|
||||
ValidationException({
|
||||
required this.message,
|
||||
this.fieldErrors,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() => 'ValidationException: $message';
|
||||
}
|
||||
|
||||
/// 권한 부족 예외
|
||||
class ForbiddenException implements Exception {
|
||||
final String message;
|
||||
|
||||
ForbiddenException({required this.message});
|
||||
|
||||
@override
|
||||
String toString() => 'ForbiddenException: $message';
|
||||
}
|
||||
|
||||
/// 리소스 찾을 수 없음 예외
|
||||
class NotFoundException implements Exception {
|
||||
final String message;
|
||||
final String? resourceType;
|
||||
final String? resourceId;
|
||||
|
||||
NotFoundException({
|
||||
required this.message,
|
||||
this.resourceType,
|
||||
this.resourceId,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() => 'NotFoundException: $message';
|
||||
}
|
||||
|
||||
/// 중복 리소스 예외
|
||||
class DuplicateException implements Exception {
|
||||
final String message;
|
||||
final String? field;
|
||||
final String? value;
|
||||
|
||||
DuplicateException({
|
||||
required this.message,
|
||||
this.field,
|
||||
this.value,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() => 'DuplicateException: $message';
|
||||
}
|
||||
|
||||
/// 비즈니스 로직 예외
|
||||
class BusinessException implements Exception {
|
||||
final String message;
|
||||
final String? code;
|
||||
|
||||
BusinessException({
|
||||
required this.message,
|
||||
this.code,
|
||||
});
|
||||
|
||||
@override
|
||||
String toString() => 'BusinessException: $message (code: $code)';
|
||||
}
|
||||
117
lib/core/errors/failures.dart
Normal file
117
lib/core/errors/failures.dart
Normal file
@@ -0,0 +1,117 @@
|
||||
import 'package:dartz/dartz.dart';
|
||||
|
||||
/// 실패 처리를 위한 추상 클래스
|
||||
abstract class Failure {
|
||||
final String message;
|
||||
final String? code;
|
||||
|
||||
const Failure({
|
||||
required this.message,
|
||||
this.code,
|
||||
});
|
||||
|
||||
@override
|
||||
bool operator ==(Object other) =>
|
||||
identical(this, other) ||
|
||||
other is Failure &&
|
||||
runtimeType == other.runtimeType &&
|
||||
message == other.message &&
|
||||
code == other.code;
|
||||
|
||||
@override
|
||||
int get hashCode => message.hashCode ^ code.hashCode;
|
||||
}
|
||||
|
||||
/// 서버 실패
|
||||
class ServerFailure extends Failure {
|
||||
final int? statusCode;
|
||||
final Map<String, dynamic>? errors;
|
||||
|
||||
const ServerFailure({
|
||||
required String message,
|
||||
String? code,
|
||||
this.statusCode,
|
||||
this.errors,
|
||||
}) : super(message: message, code: code);
|
||||
}
|
||||
|
||||
/// 캐시 실패
|
||||
class CacheFailure extends Failure {
|
||||
const CacheFailure({
|
||||
required String message,
|
||||
String? code,
|
||||
}) : super(message: message, code: code);
|
||||
}
|
||||
|
||||
/// 네트워크 실패
|
||||
class NetworkFailure extends Failure {
|
||||
const NetworkFailure({
|
||||
required String message,
|
||||
String? code,
|
||||
}) : super(message: message, code: code);
|
||||
}
|
||||
|
||||
/// 인증 실패
|
||||
class AuthenticationFailure extends Failure {
|
||||
const AuthenticationFailure({
|
||||
required String message,
|
||||
String? code,
|
||||
}) : super(message: message, code: code);
|
||||
}
|
||||
|
||||
/// 권한 실패
|
||||
class AuthorizationFailure extends Failure {
|
||||
const AuthorizationFailure({
|
||||
required String message,
|
||||
String? code,
|
||||
}) : super(message: message, code: code);
|
||||
}
|
||||
|
||||
/// 유효성 검사 실패
|
||||
class ValidationFailure extends Failure {
|
||||
final Map<String, List<String>>? fieldErrors;
|
||||
|
||||
const ValidationFailure({
|
||||
required String message,
|
||||
String? code,
|
||||
this.fieldErrors,
|
||||
}) : super(message: message, code: code);
|
||||
}
|
||||
|
||||
/// 리소스 찾을 수 없음 실패
|
||||
class NotFoundFailure extends Failure {
|
||||
final String? resourceType;
|
||||
final String? resourceId;
|
||||
|
||||
const NotFoundFailure({
|
||||
required String message,
|
||||
String? code,
|
||||
this.resourceType,
|
||||
this.resourceId,
|
||||
}) : super(message: message, code: code);
|
||||
}
|
||||
|
||||
/// 중복 리소스 실패
|
||||
class DuplicateFailure extends Failure {
|
||||
final String? field;
|
||||
final String? value;
|
||||
|
||||
const DuplicateFailure({
|
||||
required String message,
|
||||
String? code,
|
||||
this.field,
|
||||
this.value,
|
||||
}) : super(message: message, code: code);
|
||||
}
|
||||
|
||||
/// 비즈니스 로직 실패
|
||||
class BusinessFailure extends Failure {
|
||||
const BusinessFailure({
|
||||
required String message,
|
||||
String? code,
|
||||
}) : super(message: message, code: code);
|
||||
}
|
||||
|
||||
/// 타입 정의
|
||||
typedef FutureEither<T> = Future<Either<Failure, T>>;
|
||||
typedef FutureVoid = FutureEither<void>;
|
||||
146
lib/core/utils/formatters.dart
Normal file
146
lib/core/utils/formatters.dart
Normal file
@@ -0,0 +1,146 @@
|
||||
import 'package:intl/intl.dart';
|
||||
import '../constants/app_constants.dart';
|
||||
|
||||
/// 데이터 포맷터 유틸리티 클래스
|
||||
class Formatters {
|
||||
/// 날짜 포맷
|
||||
static String formatDate(DateTime date) {
|
||||
return DateFormat(AppConstants.dateFormat).format(date);
|
||||
}
|
||||
|
||||
/// 날짜+시간 포맷
|
||||
static String formatDateTime(DateTime dateTime) {
|
||||
return DateFormat(AppConstants.dateTimeFormat).format(dateTime);
|
||||
}
|
||||
|
||||
/// 상대적 시간 포맷 (예: 3분 전, 2시간 전)
|
||||
static String formatRelativeTime(DateTime dateTime) {
|
||||
final now = DateTime.now();
|
||||
final difference = now.difference(dateTime);
|
||||
|
||||
if (difference.inDays > 365) {
|
||||
return '${(difference.inDays / 365).floor()}년 전';
|
||||
} else if (difference.inDays > 30) {
|
||||
return '${(difference.inDays / 30).floor()}개월 전';
|
||||
} else if (difference.inDays > 0) {
|
||||
return '${difference.inDays}일 전';
|
||||
} else if (difference.inHours > 0) {
|
||||
return '${difference.inHours}시간 전';
|
||||
} else if (difference.inMinutes > 0) {
|
||||
return '${difference.inMinutes}분 전';
|
||||
} else {
|
||||
return '방금 전';
|
||||
}
|
||||
}
|
||||
|
||||
/// 숫자 포맷 (천 단위 구분)
|
||||
static String formatNumber(int number) {
|
||||
return NumberFormat('#,###').format(number);
|
||||
}
|
||||
|
||||
/// 통화 포맷
|
||||
static String formatCurrency(int amount) {
|
||||
return NumberFormat.currency(
|
||||
locale: 'ko_KR',
|
||||
symbol: '₩',
|
||||
decimalDigits: 0,
|
||||
).format(amount);
|
||||
}
|
||||
|
||||
/// 전화번호 포맷
|
||||
static String formatPhone(String phone) {
|
||||
final cleaned = phone.replaceAll(RegExp(r'[^0-9]'), '');
|
||||
|
||||
if (cleaned.length == 11) {
|
||||
return '${cleaned.substring(0, 3)}-${cleaned.substring(3, 7)}-${cleaned.substring(7)}';
|
||||
} else if (cleaned.length == 10) {
|
||||
if (cleaned.startsWith('02')) {
|
||||
return '${cleaned.substring(0, 2)}-${cleaned.substring(2, 6)}-${cleaned.substring(6)}';
|
||||
} else {
|
||||
return '${cleaned.substring(0, 3)}-${cleaned.substring(3, 6)}-${cleaned.substring(6)}';
|
||||
}
|
||||
}
|
||||
|
||||
return phone;
|
||||
}
|
||||
|
||||
/// 사업자번호 포맷
|
||||
static String formatBusinessNumber(String businessNumber) {
|
||||
final cleaned = businessNumber.replaceAll(RegExp(r'[^0-9]'), '');
|
||||
|
||||
if (cleaned.length == 10) {
|
||||
return '${cleaned.substring(0, 3)}-${cleaned.substring(3, 5)}-${cleaned.substring(5)}';
|
||||
}
|
||||
|
||||
return businessNumber;
|
||||
}
|
||||
|
||||
/// 파일 크기 포맷
|
||||
static String formatFileSize(int bytes) {
|
||||
if (bytes < 1024) {
|
||||
return '$bytes B';
|
||||
} else if (bytes < 1024 * 1024) {
|
||||
return '${(bytes / 1024).toStringAsFixed(1)} KB';
|
||||
} else if (bytes < 1024 * 1024 * 1024) {
|
||||
return '${(bytes / (1024 * 1024)).toStringAsFixed(1)} MB';
|
||||
} else {
|
||||
return '${(bytes / (1024 * 1024 * 1024)).toStringAsFixed(1)} GB';
|
||||
}
|
||||
}
|
||||
|
||||
/// 백분율 포맷
|
||||
static String formatPercentage(double value, {int decimals = 0}) {
|
||||
return '${(value * 100).toStringAsFixed(decimals)}%';
|
||||
}
|
||||
|
||||
/// 기간 포맷 (일수)
|
||||
static String formatDuration(int days) {
|
||||
if (days >= 365) {
|
||||
final years = days ~/ 365;
|
||||
final remainingDays = days % 365;
|
||||
if (remainingDays > 0) {
|
||||
return '$years년 $remainingDays일';
|
||||
}
|
||||
return '$years년';
|
||||
} else if (days >= 30) {
|
||||
final months = days ~/ 30;
|
||||
final remainingDays = days % 30;
|
||||
if (remainingDays > 0) {
|
||||
return '$months개월 $remainingDays일';
|
||||
}
|
||||
return '$months개월';
|
||||
} else {
|
||||
return '$days일';
|
||||
}
|
||||
}
|
||||
|
||||
/// 상태 텍스트 변환
|
||||
static String formatStatus(String status) {
|
||||
return AppConstants.equipmentStatus[status] ?? status;
|
||||
}
|
||||
|
||||
/// 역할 텍스트 변환
|
||||
static String formatRole(String role) {
|
||||
switch (role) {
|
||||
case 'S':
|
||||
case 'admin':
|
||||
return '관리자';
|
||||
case 'M':
|
||||
case 'manager':
|
||||
return '매니저';
|
||||
case 'U':
|
||||
case 'staff':
|
||||
return '직원';
|
||||
case 'V':
|
||||
case 'viewer':
|
||||
return '열람자';
|
||||
default:
|
||||
return role;
|
||||
}
|
||||
}
|
||||
|
||||
/// null 값 처리
|
||||
static String formatNullable(String? value, {String defaultValue = '-'}) {
|
||||
return value?.isEmpty ?? true ? defaultValue : value!;
|
||||
}
|
||||
}
|
||||
154
lib/core/utils/validators.dart
Normal file
154
lib/core/utils/validators.dart
Normal file
@@ -0,0 +1,154 @@
|
||||
import '../constants/app_constants.dart';
|
||||
|
||||
/// 유효성 검사 유틸리티 클래스
|
||||
class Validators {
|
||||
/// 이메일 유효성 검사
|
||||
static String? validateEmail(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '이메일을 입력해주세요.';
|
||||
}
|
||||
|
||||
if (!AppConstants.emailRegex.hasMatch(value)) {
|
||||
return '올바른 이메일 형식이 아닙니다.';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 비밀번호 유효성 검사
|
||||
static String? validatePassword(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '비밀번호를 입력해주세요.';
|
||||
}
|
||||
|
||||
if (value.length < 8) {
|
||||
return '비밀번호는 8자 이상이어야 합니다.';
|
||||
}
|
||||
|
||||
if (!value.contains(RegExp(r'[0-9]'))) {
|
||||
return '비밀번호는 숫자를 포함해야 합니다.';
|
||||
}
|
||||
|
||||
if (!value.contains(RegExp(r'[a-zA-Z]'))) {
|
||||
return '비밀번호는 영문자를 포함해야 합니다.';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 필수 입력 검사
|
||||
static String? validateRequired(String? value, String fieldName) {
|
||||
if (value == null || value.trim().isEmpty) {
|
||||
return '$fieldName을(를) 입력해주세요.';
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 전화번호 유효성 검사
|
||||
static String? validatePhone(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '전화번호를 입력해주세요.';
|
||||
}
|
||||
|
||||
final cleanedValue = value.replaceAll('-', '');
|
||||
if (!AppConstants.phoneRegex.hasMatch(cleanedValue)) {
|
||||
return '올바른 전화번호 형식이 아닙니다.';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 사업자번호 유효성 검사
|
||||
static String? validateBusinessNumber(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '사업자번호를 입력해주세요.';
|
||||
}
|
||||
|
||||
final cleanedValue = value.replaceAll('-', '');
|
||||
if (!AppConstants.businessNumberRegex.hasMatch(cleanedValue)) {
|
||||
return '올바른 사업자번호 형식이 아닙니다.';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 숫자 유효성 검사
|
||||
static String? validateNumber(String? value, {
|
||||
required String fieldName,
|
||||
int? min,
|
||||
int? max,
|
||||
}) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return '$fieldName을(를) 입력해주세요.';
|
||||
}
|
||||
|
||||
final number = int.tryParse(value);
|
||||
if (number == null) {
|
||||
return '숫자만 입력 가능합니다.';
|
||||
}
|
||||
|
||||
if (min != null && number < min) {
|
||||
return '$fieldName은(는) $min 이상이어야 합니다.';
|
||||
}
|
||||
|
||||
if (max != null && number > max) {
|
||||
return '$fieldName은(는) $max 이하여야 합니다.';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 날짜 범위 유효성 검사
|
||||
static String? validateDateRange(
|
||||
DateTime? startDate,
|
||||
DateTime? endDate,
|
||||
) {
|
||||
if (startDate == null || endDate == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (startDate.isAfter(endDate)) {
|
||||
return '시작일은 종료일보다 이전이어야 합니다.';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 파일 확장자 유효성 검사
|
||||
static String? validateFileExtension(String fileName) {
|
||||
final extension = fileName.split('.').last.toLowerCase();
|
||||
|
||||
if (!AppConstants.allowedFileExtensions.contains(extension)) {
|
||||
return '허용되지 않는 파일 형식입니다. (${AppConstants.allowedFileExtensions.join(', ')})';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 파일 크기 유효성 검사
|
||||
static String? validateFileSize(int sizeInBytes) {
|
||||
if (sizeInBytes > AppConstants.maxFileSize) {
|
||||
final maxSizeMB = AppConstants.maxFileSize / (1024 * 1024);
|
||||
return '파일 크기는 ${maxSizeMB}MB를 초과할 수 없습니다.';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// 시리얼 번호 유효성 검사
|
||||
static String? validateSerialNumber(String? value) {
|
||||
if (value == null || value.isEmpty) {
|
||||
return null; // 시리얼 번호는 선택사항
|
||||
}
|
||||
|
||||
if (value.length < 5) {
|
||||
return '시리얼 번호는 5자 이상이어야 합니다.';
|
||||
}
|
||||
|
||||
if (!RegExp(r'^[A-Za-z0-9-]+$').hasMatch(value)) {
|
||||
return '시리얼 번호는 영문, 숫자, 하이픈(-)만 사용 가능합니다.';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
212
lib/data/datasources/remote/api_client.dart
Normal file
212
lib/data/datasources/remote/api_client.dart
Normal file
@@ -0,0 +1,212 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import '../../../core/config/environment.dart';
|
||||
import 'interceptors/auth_interceptor.dart';
|
||||
import 'interceptors/error_interceptor.dart';
|
||||
import 'interceptors/logging_interceptor.dart';
|
||||
|
||||
/// API 클라이언트 클래스
|
||||
class ApiClient {
|
||||
late final Dio _dio;
|
||||
|
||||
static final ApiClient _instance = ApiClient._internal();
|
||||
|
||||
factory ApiClient() => _instance;
|
||||
|
||||
ApiClient._internal() {
|
||||
_dio = Dio(_baseOptions);
|
||||
_setupInterceptors();
|
||||
}
|
||||
|
||||
/// Dio 인스턴스 getter
|
||||
Dio get dio => _dio;
|
||||
|
||||
/// 기본 옵션 설정
|
||||
BaseOptions get _baseOptions => BaseOptions(
|
||||
baseUrl: Environment.apiBaseUrl,
|
||||
connectTimeout: Duration(milliseconds: Environment.apiTimeout),
|
||||
receiveTimeout: Duration(milliseconds: Environment.apiTimeout),
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Accept': 'application/json',
|
||||
},
|
||||
validateStatus: (status) {
|
||||
return status != null && status < 500;
|
||||
},
|
||||
);
|
||||
|
||||
/// 인터셉터 설정
|
||||
void _setupInterceptors() {
|
||||
_dio.interceptors.clear();
|
||||
|
||||
// 인증 인터셉터
|
||||
_dio.interceptors.add(AuthInterceptor());
|
||||
|
||||
// 에러 처리 인터셉터
|
||||
_dio.interceptors.add(ErrorInterceptor());
|
||||
|
||||
// 로깅 인터셉터 (개발 환경에서만)
|
||||
if (Environment.enableLogging && kDebugMode) {
|
||||
_dio.interceptors.add(LoggingInterceptor());
|
||||
}
|
||||
}
|
||||
|
||||
/// 토큰 업데이트
|
||||
void updateAuthToken(String token) {
|
||||
_dio.options.headers['Authorization'] = 'Bearer $token';
|
||||
}
|
||||
|
||||
/// 토큰 제거
|
||||
void removeAuthToken() {
|
||||
_dio.options.headers.remove('Authorization');
|
||||
}
|
||||
|
||||
/// GET 요청
|
||||
Future<Response<T>> get<T>(
|
||||
String path, {
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) {
|
||||
return _dio.get<T>(
|
||||
path,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onReceiveProgress: onReceiveProgress,
|
||||
);
|
||||
}
|
||||
|
||||
/// POST 요청
|
||||
Future<Response<T>> post<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) {
|
||||
return _dio.post<T>(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onSendProgress: onSendProgress,
|
||||
onReceiveProgress: onReceiveProgress,
|
||||
);
|
||||
}
|
||||
|
||||
/// PUT 요청
|
||||
Future<Response<T>> put<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) {
|
||||
return _dio.put<T>(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onSendProgress: onSendProgress,
|
||||
onReceiveProgress: onReceiveProgress,
|
||||
);
|
||||
}
|
||||
|
||||
/// PATCH 요청
|
||||
Future<Response<T>> patch<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
ProgressCallback? onSendProgress,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
}) {
|
||||
return _dio.patch<T>(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
onSendProgress: onSendProgress,
|
||||
onReceiveProgress: onReceiveProgress,
|
||||
);
|
||||
}
|
||||
|
||||
/// DELETE 요청
|
||||
Future<Response<T>> delete<T>(
|
||||
String path, {
|
||||
dynamic data,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
Options? options,
|
||||
CancelToken? cancelToken,
|
||||
}) {
|
||||
return _dio.delete<T>(
|
||||
path,
|
||||
data: data,
|
||||
queryParameters: queryParameters,
|
||||
options: options,
|
||||
cancelToken: cancelToken,
|
||||
);
|
||||
}
|
||||
|
||||
/// 파일 업로드
|
||||
Future<Response<T>> uploadFile<T>(
|
||||
String path, {
|
||||
required String filePath,
|
||||
required String fileFieldName,
|
||||
Map<String, dynamic>? additionalData,
|
||||
ProgressCallback? onSendProgress,
|
||||
CancelToken? cancelToken,
|
||||
}) async {
|
||||
final fileName = filePath.split('/').last;
|
||||
final formData = FormData.fromMap({
|
||||
fileFieldName: await MultipartFile.fromFile(
|
||||
filePath,
|
||||
filename: fileName,
|
||||
),
|
||||
...?additionalData,
|
||||
});
|
||||
|
||||
return _dio.post<T>(
|
||||
path,
|
||||
data: formData,
|
||||
options: Options(
|
||||
headers: {
|
||||
'Content-Type': 'multipart/form-data',
|
||||
},
|
||||
),
|
||||
onSendProgress: onSendProgress,
|
||||
cancelToken: cancelToken,
|
||||
);
|
||||
}
|
||||
|
||||
/// 파일 다운로드
|
||||
Future<Response> downloadFile(
|
||||
String path, {
|
||||
required String savePath,
|
||||
ProgressCallback? onReceiveProgress,
|
||||
CancelToken? cancelToken,
|
||||
Map<String, dynamic>? queryParameters,
|
||||
}) {
|
||||
return _dio.download(
|
||||
path,
|
||||
savePath,
|
||||
queryParameters: queryParameters,
|
||||
onReceiveProgress: onReceiveProgress,
|
||||
cancelToken: cancelToken,
|
||||
options: Options(
|
||||
responseType: ResponseType.bytes,
|
||||
followRedirects: false,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
122
lib/data/datasources/remote/interceptors/auth_interceptor.dart
Normal file
122
lib/data/datasources/remote/interceptors/auth_interceptor.dart
Normal file
@@ -0,0 +1,122 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import '../../../../core/constants/app_constants.dart';
|
||||
import '../../../../core/constants/api_endpoints.dart';
|
||||
|
||||
/// 인증 인터셉터
|
||||
class AuthInterceptor extends Interceptor {
|
||||
final _storage = const FlutterSecureStorage();
|
||||
|
||||
@override
|
||||
void onRequest(
|
||||
RequestOptions options,
|
||||
RequestInterceptorHandler handler,
|
||||
) async {
|
||||
// 로그인, 토큰 갱신 요청은 토큰 없이 진행
|
||||
if (_isAuthEndpoint(options.path)) {
|
||||
handler.next(options);
|
||||
return;
|
||||
}
|
||||
|
||||
// 저장된 액세스 토큰 가져오기
|
||||
final accessToken = await _storage.read(key: AppConstants.accessTokenKey);
|
||||
|
||||
if (accessToken != null) {
|
||||
options.headers['Authorization'] = 'Bearer $accessToken';
|
||||
}
|
||||
|
||||
handler.next(options);
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(
|
||||
DioException err,
|
||||
ErrorInterceptorHandler handler,
|
||||
) async {
|
||||
// 401 Unauthorized 에러 처리
|
||||
if (err.response?.statusCode == 401) {
|
||||
// 토큰 갱신 시도
|
||||
final refreshSuccess = await _refreshToken();
|
||||
|
||||
if (refreshSuccess) {
|
||||
// 새로운 토큰으로 원래 요청 재시도
|
||||
try {
|
||||
final newAccessToken = await _storage.read(key: AppConstants.accessTokenKey);
|
||||
|
||||
if (newAccessToken != null) {
|
||||
err.requestOptions.headers['Authorization'] = 'Bearer $newAccessToken';
|
||||
|
||||
final response = await Dio().fetch(err.requestOptions);
|
||||
handler.resolve(response);
|
||||
return;
|
||||
}
|
||||
} catch (e) {
|
||||
// 재시도 실패
|
||||
handler.next(err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// 토큰 갱신 실패 시 로그인 화면으로 이동
|
||||
await _clearTokens();
|
||||
// TODO: Navigate to login screen
|
||||
}
|
||||
|
||||
handler.next(err);
|
||||
}
|
||||
|
||||
/// 토큰 갱신
|
||||
Future<bool> _refreshToken() async {
|
||||
try {
|
||||
final refreshToken = await _storage.read(key: AppConstants.refreshTokenKey);
|
||||
|
||||
if (refreshToken == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final dio = Dio();
|
||||
final response = await dio.post(
|
||||
'${dio.options.baseUrl}${ApiEndpoints.refresh}',
|
||||
data: {
|
||||
'refresh_token': refreshToken,
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200 && response.data != null) {
|
||||
final data = response.data;
|
||||
|
||||
// 새로운 토큰 저장
|
||||
await _storage.write(
|
||||
key: AppConstants.accessTokenKey,
|
||||
value: data['access_token'],
|
||||
);
|
||||
|
||||
if (data['refresh_token'] != null) {
|
||||
await _storage.write(
|
||||
key: AppConstants.refreshTokenKey,
|
||||
value: data['refresh_token'],
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// 토큰 삭제
|
||||
Future<void> _clearTokens() async {
|
||||
await _storage.delete(key: AppConstants.accessTokenKey);
|
||||
await _storage.delete(key: AppConstants.refreshTokenKey);
|
||||
}
|
||||
|
||||
/// 인증 관련 엔드포인트 확인
|
||||
bool _isAuthEndpoint(String path) {
|
||||
return path.contains(ApiEndpoints.login) ||
|
||||
path.contains(ApiEndpoints.refresh) ||
|
||||
path.contains(ApiEndpoints.logout);
|
||||
}
|
||||
}
|
||||
253
lib/data/datasources/remote/interceptors/error_interceptor.dart
Normal file
253
lib/data/datasources/remote/interceptors/error_interceptor.dart
Normal file
@@ -0,0 +1,253 @@
|
||||
import 'dart:io';
|
||||
import 'package:dio/dio.dart';
|
||||
import '../../../../core/errors/exceptions.dart';
|
||||
import '../../../../core/constants/app_constants.dart';
|
||||
|
||||
/// 에러 처리 인터셉터
|
||||
class ErrorInterceptor extends Interceptor {
|
||||
@override
|
||||
void onError(
|
||||
DioException err,
|
||||
ErrorInterceptorHandler handler,
|
||||
) {
|
||||
// 에러 타입에 따른 처리
|
||||
switch (err.type) {
|
||||
case DioExceptionType.connectionTimeout:
|
||||
case DioExceptionType.sendTimeout:
|
||||
case DioExceptionType.receiveTimeout:
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: NetworkException(
|
||||
message: AppConstants.timeoutError,
|
||||
),
|
||||
type: err.type,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
case DioExceptionType.connectionError:
|
||||
if (err.error is SocketException) {
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: NetworkException(
|
||||
message: AppConstants.networkError,
|
||||
),
|
||||
type: err.type,
|
||||
),
|
||||
);
|
||||
} else {
|
||||
handler.reject(err);
|
||||
}
|
||||
break;
|
||||
|
||||
case DioExceptionType.badResponse:
|
||||
_handleBadResponse(err, handler);
|
||||
break;
|
||||
|
||||
case DioExceptionType.cancel:
|
||||
handler.reject(err);
|
||||
break;
|
||||
|
||||
case DioExceptionType.badCertificate:
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: NetworkException(
|
||||
message: '보안 인증서 오류가 발생했습니다.',
|
||||
),
|
||||
type: err.type,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
case DioExceptionType.unknown:
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: ServerException(
|
||||
message: AppConstants.unknownError,
|
||||
),
|
||||
type: err.type,
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// 잘못된 응답 처리
|
||||
void _handleBadResponse(
|
||||
DioException err,
|
||||
ErrorInterceptorHandler handler,
|
||||
) {
|
||||
final statusCode = err.response?.statusCode;
|
||||
final data = err.response?.data;
|
||||
|
||||
String message = AppConstants.serverError;
|
||||
Map<String, dynamic>? errors;
|
||||
|
||||
// API 응답에서 에러 메시지 추출
|
||||
if (data != null) {
|
||||
if (data is Map) {
|
||||
message = data['message'] ?? data['error'] ?? message;
|
||||
errors = data['errors'] as Map<String, dynamic>?;
|
||||
} else if (data is String) {
|
||||
message = data;
|
||||
}
|
||||
}
|
||||
|
||||
// 상태 코드별 예외 처리
|
||||
switch (statusCode) {
|
||||
case 400:
|
||||
// Bad Request - 유효성 검사 실패
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: ValidationException(
|
||||
message: message,
|
||||
fieldErrors: _parseFieldErrors(errors),
|
||||
),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
case 401:
|
||||
// Unauthorized - 인증 실패
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: UnauthorizedException(
|
||||
message: AppConstants.unauthorizedError,
|
||||
),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
case 403:
|
||||
// Forbidden - 권한 부족
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: ForbiddenException(
|
||||
message: '해당 작업을 수행할 권한이 없습니다.',
|
||||
),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
case 404:
|
||||
// Not Found - 리소스 없음
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: NotFoundException(
|
||||
message: message,
|
||||
),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
case 409:
|
||||
// Conflict - 중복 리소스
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: DuplicateException(
|
||||
message: message,
|
||||
),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
case 422:
|
||||
// Unprocessable Entity - 비즈니스 로직 오류
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: BusinessException(
|
||||
message: message,
|
||||
code: data?['code'],
|
||||
),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
case 429:
|
||||
// Too Many Requests
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: NetworkException(
|
||||
message: '너무 많은 요청을 보냈습니다. 잠시 후 다시 시도해주세요.',
|
||||
),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
case 500:
|
||||
case 502:
|
||||
case 503:
|
||||
case 504:
|
||||
// Server Error
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: ServerException(
|
||||
message: AppConstants.serverError,
|
||||
statusCode: statusCode,
|
||||
),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
),
|
||||
);
|
||||
break;
|
||||
|
||||
default:
|
||||
handler.reject(
|
||||
DioException(
|
||||
requestOptions: err.requestOptions,
|
||||
error: ServerException(
|
||||
message: message,
|
||||
statusCode: statusCode,
|
||||
errors: errors,
|
||||
),
|
||||
type: err.type,
|
||||
response: err.response,
|
||||
),
|
||||
);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/// 필드 에러 파싱
|
||||
Map<String, List<String>>? _parseFieldErrors(Map<String, dynamic>? errors) {
|
||||
if (errors == null) return null;
|
||||
|
||||
final fieldErrors = <String, List<String>>{};
|
||||
|
||||
errors.forEach((key, value) {
|
||||
if (value is List) {
|
||||
fieldErrors[key] = value.map((e) => e.toString()).toList();
|
||||
} else if (value is String) {
|
||||
fieldErrors[key] = [value];
|
||||
}
|
||||
});
|
||||
|
||||
return fieldErrors.isNotEmpty ? fieldErrors : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
import 'dart:convert';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// 로깅 인터셉터
|
||||
class LoggingInterceptor extends Interceptor {
|
||||
@override
|
||||
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
|
||||
debugPrint('╔════════════════════════════════════════════════════════════');
|
||||
debugPrint('║ REQUEST');
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ ${options.method} ${options.uri}');
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ Headers:');
|
||||
options.headers.forEach((key, value) {
|
||||
// 민감한 정보 마스킹
|
||||
if (key.toLowerCase() == 'authorization') {
|
||||
debugPrint('║ $key: ${_maskToken(value.toString())}');
|
||||
} else {
|
||||
debugPrint('║ $key: $value');
|
||||
}
|
||||
});
|
||||
|
||||
if (options.queryParameters.isNotEmpty) {
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ Query Parameters:');
|
||||
options.queryParameters.forEach((key, value) {
|
||||
debugPrint('║ $key: $value');
|
||||
});
|
||||
}
|
||||
|
||||
if (options.data != null) {
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ Request Body:');
|
||||
try {
|
||||
final formattedData = _formatJson(options.data);
|
||||
formattedData.split('\n').forEach((line) {
|
||||
debugPrint('║ $line');
|
||||
});
|
||||
} catch (e) {
|
||||
debugPrint('║ ${options.data}');
|
||||
}
|
||||
}
|
||||
|
||||
debugPrint('╚════════════════════════════════════════════════════════════');
|
||||
|
||||
handler.next(options);
|
||||
}
|
||||
|
||||
@override
|
||||
void onResponse(Response response, ResponseInterceptorHandler handler) {
|
||||
final requestTime = response.requestOptions.extra['requestTime'] as DateTime?;
|
||||
final responseTime = DateTime.now();
|
||||
final duration = requestTime != null
|
||||
? responseTime.difference(requestTime).inMilliseconds
|
||||
: null;
|
||||
|
||||
debugPrint('╔════════════════════════════════════════════════════════════');
|
||||
debugPrint('║ RESPONSE');
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ ${response.requestOptions.method} ${response.requestOptions.uri}');
|
||||
debugPrint('║ Status: ${response.statusCode} ${response.statusMessage}');
|
||||
if (duration != null) {
|
||||
debugPrint('║ Duration: ${duration}ms');
|
||||
}
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ Headers:');
|
||||
response.headers.forEach((key, values) {
|
||||
debugPrint('║ $key: ${values.join(', ')}');
|
||||
});
|
||||
|
||||
if (response.data != null) {
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ Response Body:');
|
||||
try {
|
||||
final formattedData = _formatJson(response.data);
|
||||
final lines = formattedData.split('\n');
|
||||
// 너무 긴 응답은 일부만 출력
|
||||
if (lines.length > 50) {
|
||||
lines.take(25).forEach((line) {
|
||||
debugPrint('║ $line');
|
||||
});
|
||||
debugPrint('║ ... (${lines.length - 50} lines omitted) ...');
|
||||
lines.skip(lines.length - 25).forEach((line) {
|
||||
debugPrint('║ $line');
|
||||
});
|
||||
} else {
|
||||
lines.forEach((line) {
|
||||
debugPrint('║ $line');
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('║ ${response.data}');
|
||||
}
|
||||
}
|
||||
|
||||
debugPrint('╚════════════════════════════════════════════════════════════');
|
||||
|
||||
handler.next(response);
|
||||
}
|
||||
|
||||
@override
|
||||
void onError(DioException err, ErrorInterceptorHandler handler) {
|
||||
debugPrint('╔════════════════════════════════════════════════════════════');
|
||||
debugPrint('║ ERROR');
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ ${err.requestOptions.method} ${err.requestOptions.uri}');
|
||||
debugPrint('║ Error Type: ${err.type}');
|
||||
debugPrint('║ Error Message: ${err.message}');
|
||||
|
||||
if (err.response != null) {
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ Status: ${err.response!.statusCode} ${err.response!.statusMessage}');
|
||||
|
||||
if (err.response!.data != null) {
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ Error Response:');
|
||||
try {
|
||||
final formattedData = _formatJson(err.response!.data);
|
||||
formattedData.split('\n').forEach((line) {
|
||||
debugPrint('║ $line');
|
||||
});
|
||||
} catch (e) {
|
||||
debugPrint('║ ${err.response!.data}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (err.error != null) {
|
||||
debugPrint('╟────────────────────────────────────────────────────────────');
|
||||
debugPrint('║ Original Error: ${err.error}');
|
||||
}
|
||||
|
||||
debugPrint('╚════════════════════════════════════════════════════════════');
|
||||
|
||||
handler.next(err);
|
||||
}
|
||||
|
||||
/// JSON 포맷팅
|
||||
String _formatJson(dynamic data) {
|
||||
try {
|
||||
if (data is String) {
|
||||
final parsed = json.decode(data);
|
||||
return const JsonEncoder.withIndent(' ').convert(parsed);
|
||||
} else {
|
||||
return const JsonEncoder.withIndent(' ').convert(data);
|
||||
}
|
||||
} catch (e) {
|
||||
return data.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/// 토큰 마스킹
|
||||
String _maskToken(String token) {
|
||||
if (token.length <= 20) {
|
||||
return '***MASKED***';
|
||||
}
|
||||
|
||||
// Bearer 프리픽스 처리
|
||||
if (token.startsWith('Bearer ')) {
|
||||
final actualToken = token.substring(7);
|
||||
if (actualToken.length > 20) {
|
||||
return 'Bearer ${actualToken.substring(0, 10)}...${actualToken.substring(actualToken.length - 10)}';
|
||||
}
|
||||
return 'Bearer ***MASKED***';
|
||||
}
|
||||
|
||||
return '${token.substring(0, 10)}...${token.substring(token.length - 10)}';
|
||||
}
|
||||
}
|
||||
|
||||
/// 요청 시간 측정을 위한 확장
|
||||
extension RequestOptionsExtension on RequestOptions {
|
||||
void setRequestTime() {
|
||||
extra['requestTime'] = DateTime.now();
|
||||
}
|
||||
}
|
||||
65
lib/di/injection_container.dart
Normal file
65
lib/di/injection_container.dart
Normal file
@@ -0,0 +1,65 @@
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
||||
import 'package:get_it/get_it.dart';
|
||||
import '../core/config/environment.dart';
|
||||
import '../data/datasources/remote/api_client.dart';
|
||||
|
||||
/// GetIt 인스턴스
|
||||
final getIt = GetIt.instance;
|
||||
|
||||
/// 의존성 주입 설정
|
||||
Future<void> setupDependencies() async {
|
||||
// 환경 초기화
|
||||
await Environment.initialize();
|
||||
|
||||
// 외부 라이브러리
|
||||
getIt.registerLazySingleton(() => Dio());
|
||||
getIt.registerLazySingleton(() => const FlutterSecureStorage());
|
||||
|
||||
// API 클라이언트
|
||||
getIt.registerLazySingleton(() => ApiClient());
|
||||
|
||||
// 데이터소스
|
||||
// TODO: Remote datasources will be registered here
|
||||
|
||||
// 리포지토리
|
||||
// TODO: Repositories will be registered here
|
||||
|
||||
// 유스케이스
|
||||
// TODO: Use cases will be registered here
|
||||
|
||||
// 컨트롤러/프로바이더
|
||||
// TODO: Controllers will be registered here
|
||||
}
|
||||
|
||||
/// 의존성 리셋 (테스트용)
|
||||
Future<void> resetDependencies() async {
|
||||
await getIt.reset();
|
||||
}
|
||||
|
||||
/// 특정 타입의 의존성 가져오기
|
||||
T inject<T extends Object>() => getIt.get<T>();
|
||||
|
||||
/// 특정 타입의 의존성이 등록되어 있는지 확인
|
||||
bool isRegistered<T extends Object>() => getIt.isRegistered<T>();
|
||||
|
||||
/// 의존성 등록 헬퍼 함수들
|
||||
extension GetItHelpers on GetIt {
|
||||
/// 싱글톤 등록 헬퍼
|
||||
void registerSingletonIfNotRegistered<T extends Object>(
|
||||
T Function() factory,
|
||||
) {
|
||||
if (!isRegistered<T>()) {
|
||||
registerLazySingleton<T>(factory);
|
||||
}
|
||||
}
|
||||
|
||||
/// 팩토리 등록 헬퍼
|
||||
void registerFactoryIfNotRegistered<T extends Object>(
|
||||
T Function() factory,
|
||||
) {
|
||||
if (!isRegistered<T>()) {
|
||||
registerFactory<T>(factory);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,15 @@ import 'package:superport/screens/warehouse_location/warehouse_location_form.dar
|
||||
import 'package:superport/utils/constants.dart';
|
||||
import 'package:flutter_localizations/flutter_localizations.dart';
|
||||
import 'package:superport/screens/login/login_screen.dart';
|
||||
import 'package:superport/di/injection_container.dart' as di;
|
||||
|
||||
void main() {
|
||||
void main() async {
|
||||
// Flutter 바인딩 초기화
|
||||
WidgetsFlutterBinding.ensureInitialized();
|
||||
|
||||
// 의존성 주입 설정
|
||||
await di.setupDependencies();
|
||||
|
||||
// MockDataService는 싱글톤으로 자동 초기화됨
|
||||
runApp(const SuperportApp());
|
||||
}
|
||||
|
||||
@@ -199,7 +199,7 @@ class ShadcnTheme {
|
||||
titleTextStyle: headingH4,
|
||||
iconTheme: const IconThemeData(color: foreground),
|
||||
),
|
||||
cardTheme: CardTheme(
|
||||
cardTheme: CardThemeData(
|
||||
color: card,
|
||||
elevation: 0,
|
||||
shape: RoundedRectangleBorder(
|
||||
|
||||
@@ -56,7 +56,7 @@ class AppThemeTailwind {
|
||||
),
|
||||
|
||||
// 카드 테마
|
||||
cardTheme: CardTheme(
|
||||
cardTheme: CardThemeData(
|
||||
color: Colors.white,
|
||||
elevation: 1,
|
||||
shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(8)),
|
||||
|
||||
Reference in New Issue
Block a user