feat:参照研听的基础工程

This commit is contained in:
jingyun
2026-06-18 15:02:28 +08:00
commit 364fc837b3
367 changed files with 16495 additions and 0 deletions
+20
View File
@@ -0,0 +1,20 @@
import 'package:dio/dio.dart';
String? kratosReason(Object e) {
if (e is! DioException) return null;
final body = e.response?.data;
if (body is Map && body['reason'] is String) return body['reason'] as String;
return null;
}
String kratosDisplayMessage(Object e, {required String fallback}) {
if (kratosReason(e) == 'INTERNAL_ERROR') return fallback;
if (e is DioException) {
final body = e.response?.data;
if (body is Map && body['message'] is String) {
final m = body['message'] as String;
if (m.isNotEmpty) return m;
}
}
return fallback;
}
+28
View File
@@ -0,0 +1,28 @@
import 'package:dio/dio.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import '../services/device_header_service.dart';
const _kJwtKey = 'jwt_token';
const _storage = FlutterSecureStorage();
Future<void> saveJwt(String token) =>
_storage.write(key: _kJwtKey, value: token);
Future<void> clearJwt() => _storage.delete(key: _kJwtKey);
Future<String?> loadJwt() => _storage.read(key: _kJwtKey);
class AuthInterceptor extends Interceptor {
@override
Future<void> onRequest(
RequestOptions options,
RequestInterceptorHandler handler,
) async {
await DeviceHeaderService.to.ensureInitialized();
options.headers.addAll(DeviceHeaderService.to.commonHeaders());
options.headers.addAll(DeviceHeaderService.to.platformIdentityHeaders());
final jwt = await loadJwt();
if (jwt != null && jwt.isNotEmpty) {
options.headers['Authorization'] = 'Bearer $jwt';
}
handler.next(options);
}
}
+41
View File
@@ -0,0 +1,41 @@
import 'package:dio/dio.dart';
import 'package:fancy_dio_inspector/fancy_dio_inspector.dart';
import 'package:flutter/foundation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import '../config/app_config.dart';
import 'auth_interceptor.dart';
import 'kratos_error_interceptor.dart';
part 'dio_provider.g.dart';
const _protobufContentType = 'application/x-protobuf';
@Riverpod(keepAlive: true)
AppConfig appConfig(Ref ref) => AppConfig.fromEnvironment();
@Riverpod(keepAlive: true)
Dio dio(Ref ref) {
final config = ref.watch(appConfigProvider);
final d = Dio(
BaseOptions(
baseUrl: config.apiBaseUrl,
connectTimeout: const Duration(seconds: 15),
receiveTimeout: const Duration(seconds: 30),
responseType: ResponseType.bytes,
contentType: _protobufContentType,
headers: const {
'Accept': _protobufContentType,
'Content-Type': _protobufContentType,
},
),
);
d.interceptors.add(AuthInterceptor());
d.interceptors.add(KratosErrorInterceptor());
if (!kReleaseMode) {
d.interceptors.add(LogInterceptor(requestBody: false, responseBody: false));
}
d.interceptors.add(FancyDioInterceptor());
return d;
}
@@ -0,0 +1,8 @@
import 'package:dio/dio.dart';
class KratosErrorInterceptor extends Interceptor {
@override
void onError(DioException err, ErrorInterceptorHandler handler) {
handler.next(err);
}
}