newsfeed_news_repository.dart 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import 'package:domain/constants/api_constants.dart';
  2. import 'package:domain/entity/newsfeed_news_entity.dart';
  3. import 'package:domain/entity/server_time.dart';
  4. import 'package:plugin_platform/platform_export.dart';
  5. import 'package:plugin_platform/http/dio_engine.dart';
  6. import 'package:plugin_platform/http/http_result.dart';
  7. import 'package:riverpod_annotation/riverpod_annotation.dart';
  8. import 'package:shared/utils/log_utils.dart';
  9. import 'package:shared/utils/util.dart';
  10. import 'package:flutter_riverpod/flutter_riverpod.dart';
  11. import 'package:plugin_basic/provider/http_provider/http_provider.dart';
  12. part 'newsfeed_news_repository.g.dart';
  13. @Riverpod(keepAlive: true)
  14. NewsFeedNewsRepository newsFeedNewsRepository(Ref ref) {
  15. final dioEngine = ref.watch(dioEngineProvider);
  16. return NewsFeedNewsRepository(dioEngine: dioEngine);
  17. }
  18. /*
  19. * 数据仓库
  20. */
  21. class NewsFeedNewsRepository {
  22. DioEngine dioEngine;
  23. NewsFeedNewsRepository({required this.dioEngine});
  24. // news feed - news 列表
  25. Future<HttpResult<Object>> fetchNewsList(
  26. Map<String, dynamic>? data, {
  27. CancelToken? cancelToken,
  28. }) async {
  29. Map<String, dynamic> params = {};
  30. params = data!;
  31. Map<String, String> headers = {};
  32. headers["Content-Type"] = "application/x-www-form-urlencoded";
  33. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  34. final result = await dioEngine.requestNetResult(
  35. // ApiConstants.apiServerTime, // api 地址
  36. '/api/v1/user/news-feed/feed/news', // api 地址
  37. params: params,
  38. headers: headers,
  39. method: HttpMethod.GET,
  40. isShowLoadingDialog: false, //是否展示默认的Loading弹窗
  41. networkDebounce: true, //是否防抖防止重复请求
  42. cancelToken: cancelToken,
  43. );
  44. //根据返回的结果,封装原始数据为Bean/Entity对象
  45. if (result.isSuccess) {
  46. //重新赋值data或list
  47. final json = result.getDataJson();
  48. var data = NewsfeedNewsEntity.fromJson(json!);
  49. //重新赋值data或list
  50. return result.convert(data: data);
  51. }
  52. return result.convert();
  53. }
  54. // news feed -news 点赞/取消点赞
  55. Future<HttpResult<Object>> fetchLikeClick(
  56. Map<String, dynamic>? data, {
  57. CancelToken? cancelToken,
  58. }) async {
  59. Map<String, dynamic> params = {};
  60. params = data!;
  61. Map<String, String> headers = {};
  62. headers["Content-Type"] = "application/x-www-form-urlencoded";
  63. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  64. final result = await dioEngine.requestNetResult(
  65. // ApiConstants.apiServerTime, // api 地址
  66. '/api/v1/user/news-feed/like/click', // api 地址
  67. params: params,
  68. headers: headers,
  69. method: HttpMethod.POST,
  70. isShowLoadingDialog: true, //是否展示默认的Loading弹窗
  71. networkDebounce: true, //是否防抖防止重复请求
  72. cancelToken: cancelToken,
  73. );
  74. //根据返回的结果,封装原始数据为Bean/Entity对象
  75. if (result.isSuccess) {
  76. //重新赋值data或list
  77. final boolData = result.getDataDynamic();
  78. // var data = NewsfeedNewsEntity.fromJson(json!);
  79. //重新赋值data或list
  80. return result.convert(data: boolData);
  81. }
  82. return result.convert();
  83. }
  84. }