diff --git a/lib/api/api_music_return.dart b/lib/api/api_music_return.dart index 1905401..4d8458b 100644 --- a/lib/api/api_music_return.dart +++ b/lib/api/api_music_return.dart @@ -41,9 +41,10 @@ class commentMusic { Response response = await dio.post( _postComment, data: { - 'musicId': musicId, - 'password': content, - 'Authorization': Authorization + 'content': content, + 'MusicId': musicId, + 'Authorization':Authorization + }, options: Options(headers:{'Authorization':Authorization,'Content-Type':'application/json;charset=UTF-8'}) ); @@ -59,21 +60,26 @@ class getCommentApi { required String musicId, required String pageNo, required String pageSize, - required String Authorization + required String Authorization, }) async { Response response = await dio.get( _postComment, - queryParameters: {'musicId':musicId,'pageNo':pageNo,'pageSize':pageSize}, - options: Options(headers:{'Authorization':Authorization,'Content-Type':'application/json;charset=UTF-8'}) + queryParameters: { + 'musicId': musicId, + 'pageNo': pageNo, + 'pageSize': pageSize, + }, + options: Options(headers: { + 'Authorization': Authorization, + 'Content-Type': 'application/json;charset=UTF-8', + }), ); print(response.data); return GetCommentBean.formMap(response.data); } - } - diff --git a/lib/api/api_release.dart b/lib/api/api_release.dart index 87392b4..d5b736d 100644 --- a/lib/api/api_release.dart +++ b/lib/api/api_release.dart @@ -35,8 +35,7 @@ class ReleaseApi { queryParameters: {'singerName':singerName,'name':name,'introduce':introduce}, data: formData, options: Options(headers:{'Authorization':Authorization, - 'Content-Type':'multipart/form-data;boundary=--------------------------741231434897925203274596', - 'Content-Length':'2858240'}) + }) ); print(response.data); return UniversalBean.formMap(response.data); diff --git a/lib/models/getComment_bean.dart b/lib/models/getComment_bean.dart index d084d1d..11fadfa 100644 --- a/lib/models/getComment_bean.dart +++ b/lib/models/getComment_bean.dart @@ -1,26 +1,30 @@ - class GetCommentBean { int? code; String? msg; int? total; List? rows; - GetCommentBean.formMap(Map map) { - code = map['code']; - msg = map['msg']; - total = map['total']; - - List? rowList = map['rows']; - if (rowList == null) return; - - rows = rowList - .map((item) => CommentBean._formMap(item)) - .toList(); + GetCommentBean.formMap(Map map) { + code = map['code'] as int?; + msg = map['msg'] as String?; + + // 首先获取 data 字段,然后在 data 中查找 total 和 rows + Map? data = map['data']; + if (data != null) { + total = data['total'] as int?; + + List? rowList = data['rows']; + if (rowList != null) { + rows = rowList.map((item) => CommentBean.formMap(item as Map)).toList(); + } else { + rows = []; // 如果 rows 为空,初始化为空列表 + } + } else { + rows = []; // 如果 data 为空,初始化为空列表 + } } } - - class CommentBean { int? id; String? content; @@ -28,11 +32,11 @@ class CommentBean { String? username; String? avatar; - CommentBean._formMap(Map map) { - id = map['id']; - content = map['content']; - time = map['time']; - username = map['username']; - avatar = map['avatar']; + CommentBean.formMap(Map map) { + id = map['id'] as int?; + content = map['content'] as String? ?? 'No content'; + time = map['time'] as String? ?? 'Unknown time'; + username = map['username'] as String? ?? 'Anonymous'; + avatar = map['avatar'] as String? ?? 'Default avatar'; } -} +} \ No newline at end of file diff --git a/lib/view/commend_view.dart b/lib/view/commend_view.dart index 48fb130..36ef0c5 100644 --- a/lib/view/commend_view.dart +++ b/lib/view/commend_view.dart @@ -8,6 +8,8 @@ import 'package:music_player_miao/widget/text_field.dart'; class CommentView extends StatefulWidget { + + @override _CommentViewState createState() => _CommentViewState(); @@ -36,29 +38,33 @@ class _CommentViewState extends State { Future _fetchSonglistData() async { try { GetCommentBean bean1 = await getCommentApi().getComment( - musicId: '1', - pageNo: '0', - pageSize: '10', - Authorization: AppData().currentToken); - setState(() { - comments = bean1.rows!.map((rows) => rows.content!).toList(); - commentTimes = bean1.rows!.map((rows) => rows.time!).toList(); - commentHeader = bean1.rows!.map((rows) => rows.avatar!).toList(); - commentName = bean1.rows!.map((rows) => rows.username!).toList(); - playlistCount = comments.length; - }); + musicId: '1', + pageNo: '0', + pageSize: '10', + Authorization: AppData().currentToken, + ); - } catch (error) { - if (error != null) { - print('Response data: $error'); - } else { - print('Error fetching songlist data: $error'); + // 检查 rows 是否为空 + if (bean1.rows == null) { + print('No comments found'); + return; } + + setState(() { + comments = bean1.rows!.map((rows) => rows.content ?? 'No content').toList(); + commentTimes = bean1.rows!.map((rows) => rows.time ?? 'Unknown time').toList(); + commentHeader = bean1.rows!.map((rows) => rows.avatar ?? 'Default avatar').toList(); + commentName = bean1.rows!.map((rows) => rows.username ?? 'Anonymous').toList(); + playlistCount = comments.length; + }); + } catch (error) { + print('Error fetching songlist data: $error'); } } + @override Widget build(BuildContext context) { return Scaffold( @@ -164,17 +170,22 @@ class _CommentViewState extends State { ), IconButton( onPressed: () { - // setState(() { - // ascendingOrder = !ascendingOrder; - // comments.sort((a, b) { - // int compare = ascendingOrder - // ? commentTimes.indexOf(a).compareTo( - // commentTimes.indexOf(b)) - // : commentTimes.indexOf(b).compareTo( - // commentTimes.indexOf(a)); - // return compare; - // }); - // }); + setState(() { + ascendingOrder = !ascendingOrder; + + // 使用时间排序索引列表 + List sortedIndexes = List.generate(comments.length, (i) => i); + sortedIndexes.sort((a, b) { + DateTime timeA = DateTime.parse(commentTimes[a]); + DateTime timeB = DateTime.parse(commentTimes[b]); + return ascendingOrder ? timeA.compareTo(timeB) : timeB.compareTo(timeA); + }); + + comments = [for (var i in sortedIndexes) comments[i]]; + commentTimes = [for (var i in sortedIndexes) commentTimes[i]]; + commentHeader = [for (var i in sortedIndexes) commentHeader[i]]; + commentName = [for (var i in sortedIndexes) commentName[i]]; + }); }, icon: Image.asset( ascendingOrder @@ -202,7 +213,7 @@ class _CommentViewState extends State { Row( children: [ CircleAvatar( - backgroundImage: NetworkImage(commentHeader[index]) + backgroundImage: NetworkImage(commentHeader[index]) ), const SizedBox(width: 10,), Column( @@ -284,21 +295,35 @@ class _CommentViewState extends State { ); } - void submitComment() { - String comment = commentController.text; - if (comment.isNotEmpty) { - DateTime currentTime = DateTime.now(); - String formattedTime = "${currentTime.year}/${currentTime - .month}/${currentTime.day} ${currentTime.hour}:${currentTime.minute}"; + void submitComment() async { + String comment = commentController.text.trim(); + if (comment.isEmpty) { + print('Comment cannot be empty'); + Get.snackbar('错误', '评论不能为空'); + return; + } - setState(() { - comments.add(comment); - commentTimes.add(formattedTime); - commentName.add(AppData().currentUsername); - commentHeader.add(AppData().currentAvatar); + print('Submitting comment with content: $comment'); + + try { + UniversalBean bean = await commentMusic().comment( + musicId: widget.initialSongIndex, + content: comment, + Authorization: AppData().currentToken, + ); + + // 处理响应结果 + if (bean.code == 200) { + print('Comment submitted successfully'); commentController.clear(); - playlistCount++; - }); + _fetchSonglistData(); // 刷新评论列表 + } else { + print('Failed to submit comment: ${bean.msg}'); + Get.snackbar('错误', bean.msg ?? '评论提交失败'); + } + } catch (error) { + print('Error submitting comment: $error'); } } -} + +} \ No newline at end of file diff --git a/lib/view/music_view.dart b/lib/view/music_view.dart index 9916679..b1bb803 100644 --- a/lib/view/music_view.dart +++ b/lib/view/music_view.dart @@ -7,7 +7,11 @@ import '../../view_model/home_view_model.dart'; import '../api/api_music_likes.dart'; import '../api/api_collection.dart'; import '../common/download_manager.dart'; +import '../api/api_music_list.dart'; +import '../api/api_collection.dart'; import '../common_widget/Song_widegt.dart'; +import '../models/getMusicList_bean.dart'; +import '../view/commend_view.dart'; import '../models/universal_bean.dart'; class MusicView extends StatefulWidget { @@ -408,7 +412,7 @@ class _MusicViewState extends State { ), ), IconButton( - onPressed: () async{ + onPressed: () async { setState(() { collectionsnot = !collectionsnot; collection[currentSongIndex] = !collection[currentSongIndex]; @@ -436,7 +440,12 @@ class _MusicViewState extends State { ), IconButton( onPressed: () { - Image.asset("assets/img/music_good.png"); + Navigator.push( + context, + MaterialPageRoute( + builder: (context) => CommentView(initialSongIndex: widget.initialSongIndex), + ), + ); }, icon: Image.asset( "assets/img/music_commend_un.png",