情景推荐

This commit is contained in:
Developer
2026-04-09 02:23:27 +08:00
parent cd1f9dd17a
commit 73036a8856
23 changed files with 3501 additions and 138 deletions

View File

@@ -1,15 +1,14 @@
import 'dart:convert';
import 'dart:math' show Random;
import 'dart:io' as io;
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/foundation.dart' show kIsWeb;
import 'package:get/get.dart';
import '../../constants/app_constants.dart';
import '../../controllers/history_controller.dart';
import '../../controllers/shared_preferences_storage_controller.dart';
import '../../controllers/settings/is_platform.dart';
import '../isweb/wakelock_service.dart';
import '../../views/profile/guide/tongji.dart';
import 'theme_controller.dart';
@@ -217,7 +216,7 @@ class ProfileController extends GetxController with WidgetsBindingObserver {
Future<void> toggleScreenWake(bool enable) async {
final themeController = Get.find<ThemeController>();
// Web 平台不支持 wakelock_plus
if (kIsWeb) {
if (PlatformUtils.isWeb) {
Get.snackbar(
'提示',
'Web 平台不支持屏幕常亮功能',
@@ -227,9 +226,9 @@ class ProfileController extends GetxController with WidgetsBindingObserver {
}
try {
// 使用 io.Platform 检测平台
final String osName = io.Platform.operatingSystem;
final String osVersion = io.Platform.operatingSystemVersion;
// 获取平台信息
final String osName = PlatformUtils.operatingSystem;
final String osVersion = PlatformUtils.operatingSystemVersion;
print('Current platform: $osName, version: $osVersion');
if (enable) {

View File

@@ -0,0 +1,150 @@
/// 今日诗词 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<String?> _getToken() async {
final prefs = await SharedPreferences.getInstance();
return prefs.getString(_tokenKey);
}
/// 保存 token
Future<void> _saveToken(String token) async {
final prefs = await SharedPreferences.getInstance();
await prefs.setString(_tokenKey, token);
}
/// 生成 token
Future<String> 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<Map<String, dynamic>> 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<String, dynamic>;
}
throw Exception('获取诗词失败');
} catch (e) {
print('获取诗词异常: $e');
throw Exception('获取诗词异常: $e');
}
}
/// 清除缓存的 token
Future<void> clearToken() async {
final prefs = await SharedPreferences.getInstance();
await prefs.remove(_tokenKey);
}
/// 获取用户信息IP、地区、天气等
Future<Map<String, dynamic>> 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<String, dynamic>;
}
throw Exception('获取用户信息失败');
} catch (e) {
print('获取用户信息异常: $e');
throw Exception('获取用户信息异常: $e');
}
}
}