You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.8 KiB
57 lines
1.8 KiB
import 'package:flutter_dotenv/flutter_dotenv.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:get_storage/get_storage.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:logger/logger.dart';
|
|
import 'package:flutter/services.dart';
|
|
|
|
/// 配置服务,用于管理应用配置和环境变量
|
|
class ConfigService extends GetxService {
|
|
final Logger _logger = Logger();
|
|
late final SharedPreferences _prefs;
|
|
|
|
// 创建一个 MethodChannel 用于与原生代码通信
|
|
static const MethodChannel _channel = MethodChannel('com.example.deep_voice/config');
|
|
|
|
// 初始化服务
|
|
Future<ConfigService> init() async {
|
|
try {
|
|
_prefs = await SharedPreferences.getInstance();
|
|
|
|
// 从 .env 读取 Azure 语音服务配置
|
|
final azureSpeechKey = dotenv.env['AZURE_ASR_SUBSCRIPTION_KEY'];
|
|
final azureSpeechRegion = dotenv.env['AZURE_ASR_SERVICE_REGION'];
|
|
|
|
// 从 .env 读取火山 AI API Key
|
|
final volcanoAiApiKey = dotenv.env['VOLCANO_AI_API_KEY'];
|
|
|
|
|
|
// 通过 MethodChannel 向原生代码传递配置值
|
|
try {
|
|
await _channel.invokeMethod('setConfig', {
|
|
'azure_speech_key': azureSpeechKey ?? '',
|
|
'azure_speech_region': azureSpeechRegion ?? '',
|
|
'volcano_ai_api_key': volcanoAiApiKey ?? '',
|
|
});
|
|
_logger.i('已通过 MethodChannel 向原生代码传递配置值');
|
|
} catch (e) {
|
|
_logger.e('通过 MethodChannel 传递配置值失败: $e');
|
|
}
|
|
|
|
return this;
|
|
} catch (e) {
|
|
_logger.e('初始化配置服务失败: $e');
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
// 获取配置值
|
|
String? getString(String key) {
|
|
return _prefs.getString(key);
|
|
}
|
|
|
|
// 设置配置值
|
|
Future<bool> setString(String key, String value) {
|
|
return _prefs.setString(key, value);
|
|
}
|
|
}
|