重构
This commit is contained in:
@@ -6,7 +6,6 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class HttpClient {
|
||||
static const String _baseUrl = 'https://yy.vogov.cn/api/';
|
||||
@@ -89,7 +88,7 @@ class HttpClient {
|
||||
final options = Options(
|
||||
method: method,
|
||||
headers: headers != null
|
||||
? {..._options.headers!, ...headers}
|
||||
? {..._options.headers, ...headers}
|
||||
: _options.headers,
|
||||
);
|
||||
|
||||
@@ -165,7 +164,7 @@ class HttpClient {
|
||||
final options = Options(
|
||||
method: method,
|
||||
headers: headers != null
|
||||
? {..._options.headers!, ...headers}
|
||||
? {..._options.headers, ...headers}
|
||||
: _options.headers,
|
||||
);
|
||||
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
/// 最新变化: 新增 search.php 搜索接口(与 API_DOCUMENTATION.md 一致)
|
||||
|
||||
import 'http_client.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
class PoetryApi {
|
||||
static const String _endpoint = 'pms.php';
|
||||
|
||||
/// 全文搜索(见 lib/services/API_DOCUMENTATION.md 第二节)
|
||||
static const String _searchEndpoint = 'searchs.php';
|
||||
|
||||
@@ -17,26 +17,29 @@ class PoetryApi {
|
||||
String? tag,
|
||||
}) async {
|
||||
final queryParams = <String, dynamic>{};
|
||||
|
||||
|
||||
if (dynasty != null && dynasty.isNotEmpty) {
|
||||
queryParams['dyn'] = dynasty;
|
||||
}
|
||||
|
||||
|
||||
if (tag != null && tag.isNotEmpty) {
|
||||
queryParams['tag'] = tag;
|
||||
}
|
||||
|
||||
final response = await HttpClient.get(_endpoint, queryParameters: queryParams);
|
||||
|
||||
final response = await HttpClient.get(
|
||||
_endpoint,
|
||||
queryParameters: queryParams,
|
||||
);
|
||||
|
||||
if (!response.isSuccess) {
|
||||
throw HttpException('获取诗词失败1: ${response.message}');
|
||||
}
|
||||
|
||||
|
||||
final jsonData = response.jsonData;
|
||||
if (jsonData['code'] != 0) {
|
||||
throw HttpException(jsonData['msg'] ?? '获取诗词失败2');
|
||||
}
|
||||
|
||||
|
||||
return PoetryResponse.fromJson(jsonData);
|
||||
}
|
||||
|
||||
@@ -46,16 +49,16 @@ class PoetryApi {
|
||||
_endpoint,
|
||||
queryParameters: {'id': id.toString()},
|
||||
);
|
||||
|
||||
|
||||
if (!response.isSuccess) {
|
||||
throw HttpException('获取诗词失败3: ${response.message}');
|
||||
}
|
||||
|
||||
|
||||
final jsonData = response.jsonData;
|
||||
if (jsonData['code'] != 0) {
|
||||
throw HttpException(jsonData['msg'] ?? '获取诗词失败4');
|
||||
}
|
||||
|
||||
|
||||
return PoetryResponse.fromJson(jsonData);
|
||||
}
|
||||
|
||||
@@ -68,16 +71,16 @@ class PoetryApi {
|
||||
'like': '', // 无值参数
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
if (!response.isSuccess) {
|
||||
throw HttpException('点赞失败: ${response.message}');
|
||||
}
|
||||
|
||||
|
||||
final jsonData = response.jsonData;
|
||||
if (jsonData['code'] != 0) {
|
||||
throw HttpException(jsonData['msg'] ?? '点赞失败');
|
||||
}
|
||||
|
||||
|
||||
return PoetryResponse.fromJson(jsonData);
|
||||
}
|
||||
|
||||
@@ -87,16 +90,16 @@ class PoetryApi {
|
||||
_endpoint,
|
||||
queryParameters: {'lid': lid.toString()},
|
||||
);
|
||||
|
||||
|
||||
if (!response.isSuccess) {
|
||||
throw HttpException('点赞失败: ${response.message}');
|
||||
}
|
||||
|
||||
|
||||
final jsonData = response.jsonData;
|
||||
if (jsonData['code'] != 0) {
|
||||
throw HttpException(jsonData['msg'] ?? '点赞失败');
|
||||
}
|
||||
|
||||
|
||||
return PoetryResponse.fromJson(jsonData);
|
||||
}
|
||||
|
||||
@@ -111,7 +114,10 @@ class PoetryApi {
|
||||
}
|
||||
|
||||
/// 按朝代和标签获取诗词
|
||||
static Future<PoetryResponse> getPoetryByDynastyAndTag(String dynasty, String tag) async {
|
||||
static Future<PoetryResponse> getPoetryByDynastyAndTag(
|
||||
String dynasty,
|
||||
String tag,
|
||||
) async {
|
||||
return getRandomPoetry(dynasty: dynasty, tag: tag);
|
||||
}
|
||||
|
||||
@@ -167,9 +173,12 @@ class PoetryApi {
|
||||
|
||||
// 从 pagination 中获取分页信息
|
||||
final pagination = raw['pagination'] as Map<String, dynamic>? ?? {};
|
||||
final totalCount = int.tryParse(pagination['total_count']?.toString() ?? '0') ?? 0;
|
||||
final currentPage = int.tryParse(pagination['current_page']?.toString() ?? '$page') ?? page;
|
||||
final pageSize = int.tryParse(pagination['page_size']?.toString() ?? '$limit') ?? limit;
|
||||
final totalCount =
|
||||
int.tryParse(pagination['total_count']?.toString() ?? '0') ?? 0;
|
||||
final currentPage =
|
||||
int.tryParse(pagination['current_page']?.toString() ?? '$page') ?? page;
|
||||
final pageSize =
|
||||
int.tryParse(pagination['page_size']?.toString() ?? '$limit') ?? limit;
|
||||
|
||||
return SearchPoetryResult(
|
||||
total: totalCount,
|
||||
@@ -203,11 +212,7 @@ class PoetryResponse {
|
||||
final String message;
|
||||
final PoetryData? data;
|
||||
|
||||
PoetryResponse({
|
||||
required this.code,
|
||||
required this.message,
|
||||
this.data,
|
||||
});
|
||||
PoetryResponse({required this.code, required this.message, this.data});
|
||||
|
||||
factory PoetryResponse.fromJson(Map<String, dynamic> json) {
|
||||
return PoetryResponse(
|
||||
@@ -218,18 +223,14 @@ class PoetryResponse {
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'code': code,
|
||||
'msg': message,
|
||||
'data': data?.toJson(),
|
||||
};
|
||||
return {'code': code, 'msg': message, 'data': data?.toJson()};
|
||||
}
|
||||
}
|
||||
|
||||
/// 诗词数据模型
|
||||
class PoetryData {
|
||||
final int id;
|
||||
final String name;// 精选诗句
|
||||
final String name; // 精选诗句
|
||||
final String alias; // 朝代
|
||||
final String keywords; // 标签
|
||||
final String introduce; // 译文/介绍
|
||||
@@ -323,7 +324,11 @@ class PoetryData {
|
||||
/// 获取标签列表
|
||||
List<String> get keywordList {
|
||||
if (keywords.isEmpty) return [];
|
||||
return keywords.split(',').map((k) => k.trim()).where((k) => k.isNotEmpty).toList();
|
||||
return keywords
|
||||
.split(',')
|
||||
.map((k) => k.trim())
|
||||
.where((k) => k.isNotEmpty)
|
||||
.toList();
|
||||
}
|
||||
|
||||
/// 生成星级显示
|
||||
|
||||
@@ -8,7 +8,6 @@ import 'dart:io' as io show Platform;
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio_cookie_manager/dio_cookie_manager.dart';
|
||||
import 'package:cookie_jar/cookie_jar.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:platform_info/platform_info.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user