/// 今日诗词 API 服务 /// /// 创建时间:2026-04-08 /// 作用:调用今日诗词 API 获取每日诗词 /// 最后更新:2026-04-08 - 初始创建 import 'package:dio/dio.dart'; import 'package:shared_preferences/shared_preferences.dart'; import '../models/scenario/jinrishici_sdk_config.dart'; class JinrishiciService { static final JinrishiciService _instance = JinrishiciService._internal(); factory JinrishiciService() => _instance; JinrishiciService._internal(); final Dio _dio = Dio(BaseOptions( connectTimeout: const Duration(seconds: 10), receiveTimeout: const Duration(seconds: 10), )); static const String _tokenKey = 'jinrishici_token'; /// 获取 token Future _getToken() async { final prefs = await SharedPreferences.getInstance(); return prefs.getString(_tokenKey); } /// 保存 token Future _saveToken(String token) async { final prefs = await SharedPreferences.getInstance(); await prefs.setString(_tokenKey, token); } /// 生成 token Future generateToken() async { try { print('正在请求 token: ${JinrishiciSdkConfig.tokenUrl}'); final response = await _dio.get(JinrishiciSdkConfig.tokenUrl); print('Token 响应状态码: ${response.statusCode}'); print('Token 响应数据: ${response.data}'); if (response.statusCode == 200 && response.data != null) { // 根据官方文档,token 在 data 字段中 String? token; if (response.data is Map) { token = response.data['data'] as String?; } if (token != null && token.isNotEmpty) { await _saveToken(token); print('Token 获取成功: $token'); return token; } } throw Exception('获取 token 失败: 响应数据格式不正确'); } catch (e) { print('获取 token 异常: $e'); throw Exception('获取 token 异常: $e'); } } /// 获取今日诗词 Future> getTodayPoetry() async { try { // 获取或生成 token String? token = await _getToken(); if (token == null || token.isEmpty) { try { token = await generateToken(); } catch (e) { print('Token 获取失败,尝试不带 token 请求: $e'); token = null; } } // 使用 token 获取诗词 print('正在请求诗词: ${JinrishiciSdkConfig.sentenceUrl}'); final response = await _dio.get( JinrishiciSdkConfig.sentenceUrl, options: token != null ? Options( headers: { 'X-User-Token': token, }, ) : null, ); print('诗词响应状态码: ${response.statusCode}'); print('诗词响应数据: ${response.data}'); if (response.statusCode == 200 && response.data != null) { return response.data as Map; } throw Exception('获取诗词失败'); } catch (e) { print('获取诗词异常: $e'); throw Exception('获取诗词异常: $e'); } } /// 清除缓存的 token Future clearToken() async { final prefs = await SharedPreferences.getInstance(); await prefs.remove(_tokenKey); } /// 获取用户信息(IP、地区、天气等) Future> getUserInfo() async { try { // 获取或生成 token String? token = await _getToken(); if (token == null || token.isEmpty) { try { token = await generateToken(); } catch (e) { print('Token 获取失败,尝试不带 token 请求: $e'); token = null; } } // 调用 info 接口 print('正在请求用户信息: https://v2.jinrishici.com/info'); final response = await _dio.get( 'https://v2.jinrishici.com/info', options: token != null ? Options( headers: { 'X-User-Token': token, }, ) : null, ); print('用户信息响应状态码: ${response.statusCode}'); print('用户信息响应数据: ${response.data}'); if (response.statusCode == 200 && response.data != null) { return response.data as Map; } throw Exception('获取用户信息失败'); } catch (e) { print('获取用户信息异常: $e'); throw Exception('获取用户信息异常: $e'); } } }