feat(my): 新增错题本和我的收藏页面,重构学科标签组件

1.  为我的页面添加错题本和我的收藏跳转路由,指向共用的QuestionListPage
2.  抽离全局学科标签组件,移除class模块内的重复实现
3.  新增用户收藏/错题本接口层和数据模型
4.  完成QuestionListPage分页列表页实现,支持学科筛选和游标分页
5.  调整考试页面卡片布局和图标偏移参数
This commit is contained in:
崔宝新
2026-09-10 17:14:11 +08:00
parent 96ab0221a5
commit 14b8e56bc3
8 changed files with 795 additions and 41 deletions
+66
View File
@@ -0,0 +1,66 @@
import 'package:new_learn/core/api/user_collect/model.dart';
import 'package:new_learn/core/network/dio_client.dart';
/// ============================================================
/// 【user_collect 模块接口】我的收藏 + 错题本 —— 对标 Vue3 端 api/userCollect/index.ts
///
/// getSubjects —— 学科筛选项(顶部 Tab)
/// GET /api/v1/UserCollect/GetUserCollectSubject/{type}
///
/// getCollectList —— 收藏/错题分页列表(游标分页)
/// GET /api/v1/UserCollect/GetUserCollect
/// 参数:type(1收藏/2错题) / subjectId(可选) / pageIndex / pageSize / prevPageId
///
/// 游标分页说明(对齐 Vue useList):
/// 第一页 prevPageId=0,之后每页传上一页最后一条的 id;
/// 切学科/下拉刷新时 prevPageId 重置为 0。
/// ============================================================
class UserCollectApi {
UserCollectApi._();
static final UserCollectApi instance = UserCollectApi._();
/// 学科筛选项([type] 1=我的收藏 2=错题本)
Future<List<CollectTab>> getSubjects(int type) async {
final data = await DioClient.instance.get(
'/api/v1/UserCollect/GetUserCollectSubject/$type',
);
// 兼容裸数组 / {data: [...]} 响应壳
final List<dynamic> list = data is List
? data
: (data is Map<String, dynamic>
? (data['data'] as List<dynamic>? ?? const [])
: const []);
return list
.whereType<Map<String, dynamic>>()
.map(CollectTab.fromJson)
.toList();
}
/// 收藏/错题分页列表
///
/// [type] 1=我的收藏 2=错题本;[subjectId] 不传 = 全部学科;
/// [prevPageId] 游标:首页传 0,翻页传上一页最后一条 id。
Future<UserCollectPage> getCollectList({
required int type,
int? subjectId,
required int pageIndex,
int pageSize = 10,
int prevPageId = 0,
}) async {
final query = <String, dynamic>{
'type': type,
'pageIndex': pageIndex,
'pageSize': pageSize,
'prevPageId': prevPageId,
};
// 对齐 Vue useList:subjectId 为 null 时不带该参数
if (subjectId != null) query['subjectId'] = subjectId;
final data = await DioClient.instance.get(
'/api/v1/UserCollect/GetUserCollect',
query: query,
);
return UserCollectPage.fromJson(data as Map<String, dynamic>);
}
}
+124
View File
@@ -0,0 +1,124 @@
/// ============================================================
/// 【user_collect 模块模型】我的收藏 + 错题本 —— 对标 Vue3 端 api/userCollect/type.ts
///
/// CollectTab 学科筛选 Tab(GetUserCollectSubject 返回项)
/// UserCollectItem 列表项(收藏/错题通用,靠 type 区分)
/// UserCollectPage 分页响应(游标分页:pageIndex + prevPageId)
///
/// 说明:错题本页(myWrong)与收藏页(myCollect)共用同一套接口,
/// type=1 我的收藏 / type=2 错题本。
/// 序列化:手动 fromJson(兼容 {code, message, data: {...}} 响应壳),
/// 不依赖 build_runner。
/// ============================================================
library;
/// 兼容后端 id 字段:雪花 id 后端可能以 String 返回(防 JS 精度丢失),
/// 也可能直接是 num;统一转 int,无法解析返回 null。
int? _toInt(dynamic v) {
if (v == null) return null;
if (v is num) return v.toInt();
if (v is String && v.isNotEmpty) return int.tryParse(v);
return null;
}
/// 学科筛选 Tab
class CollectTab {
const CollectTab({
this.id,
required this.name,
this.remark = '',
this.status = 0,
});
/// 学科 id;null = 客户端插入的"全部"
final int? id;
final String name;
final String remark;
final int status;
factory CollectTab.fromJson(Map<String, dynamic> json) {
return CollectTab(
id: _toInt(json['id']),
name: json['name'] as String? ?? '',
remark: json['remark'] as String? ?? '',
status: _toInt(json['status']) ?? 0,
);
}
}
/// 收藏/错题列表项
class UserCollectItem {
const UserCollectItem({
this.id = 0,
this.questionId = 0,
this.recordQuestionId = 0,
this.questionStem = '',
this.subjectName = '',
this.packName = '',
this.createTime = '',
});
/// 列表明细 id(游标分页 prevPageId 取本页最后一条的 id)
final int id;
/// 题目 id(详情页参数 questionId)
final int questionId;
/// 答题记录 id(详情页参数 recordQuestionId)
final int recordQuestionId;
/// 题干 HTML(RichTextContent 渲染)
final String questionStem;
/// 学科名(SubjectTag 配色用)
final String subjectName;
/// 来源试卷/礼包名
final String packName;
/// 收藏/错题时间字符串
final String createTime;
factory UserCollectItem.fromJson(Map<String, dynamic> json) {
return UserCollectItem(
id: _toInt(json['id']) ?? 0,
questionId: _toInt(json['questionId']) ?? 0,
recordQuestionId: _toInt(json['recordQuestionId']) ?? 0,
questionStem: json['questionStem'] as String? ?? '',
subjectName: json['subjectName'] as String? ?? '',
packName: json['packName'] as String? ?? '',
createTime: json['createTime'] as String? ?? '',
);
}
}
/// 收藏/错题分页响应
class UserCollectPage {
const UserCollectPage({
this.data = const [],
this.total = 0,
this.pageIndex = 1,
this.pageSize = 10,
});
final List<UserCollectItem> data; // 当前页数据
final int total; // 总条数(顶部"X 道错题/收藏题目")
final int pageIndex;
final int pageSize;
/// 兼容业务响应壳 {code, message, data: {...}}:data 存在就取 data
factory UserCollectPage.fromJson(Map<String, dynamic> json) {
final payload = (json['data'] is Map<String, dynamic>)
? json['data'] as Map<String, dynamic>
: json;
return UserCollectPage(
data: (payload['data'] as List<dynamic>? ?? const [])
.whereType<Map<String, dynamic>>()
.map(UserCollectItem.fromJson)
.toList(),
total: _toInt(payload['total']) ?? 0,
pageIndex: _toInt(payload['pageIndex']) ?? 1,
pageSize: _toInt(payload['pageSize']) ?? 10,
);
}
}