demo_repository.dart 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import 'package:domain/entity/server_time.dart';
  2. import 'package:plugin_platform/platform_export.dart';
  3. import 'package:plugin_platform/http/dio_engine.dart';
  4. import 'package:plugin_platform/http/http_result.dart';
  5. import 'package:riverpod_annotation/riverpod_annotation.dart';
  6. import 'package:shared/utils/util.dart';
  7. import '../constants/api_constants.dart';
  8. import 'package:flutter_riverpod/flutter_riverpod.dart';
  9. import 'package:plugin_basic/provider/http_provider/http_provider.dart';
  10. part 'demo_repository.g.dart';
  11. @Riverpod(keepAlive: true)
  12. DemoRepository demoRepository(Ref ref) {
  13. final dioEngine = ref.watch(dioEngineProvider);
  14. return DemoRepository(dioEngine: dioEngine);
  15. }
  16. /*
  17. * 测试用的数据仓库
  18. */
  19. class DemoRepository {
  20. DioEngine dioEngine;
  21. DemoRepository({required this.dioEngine});
  22. Future<HttpResult<ServerTime>> fetchServerTime(
  23. String? type, {
  24. CancelToken? cancelToken,
  25. }) async {
  26. Map<String, String> params = {};
  27. if (!Utils.isEmpty(type)) {
  28. params["type"] = type!;
  29. }
  30. Map<String, String> headers = {};
  31. headers["Content-Type"] = "application/x-www-form-urlencoded";
  32. headers["Accept"] = "application/x.yyjobs-api.v1+json";
  33. final result = await dioEngine.requestNetResult(
  34. ApiConstants.apiServerTime,
  35. params: params,
  36. headers: headers,
  37. method: HttpMethod.GET,
  38. isShowLoadingDialog: true, //是否展示默认的Loading弹窗
  39. networkDebounce: true, //是否防抖防止重复请求
  40. cancelToken: cancelToken,
  41. );
  42. //根据返回的结果,封装原始数据为Bean/Entity对象
  43. if (result.isSuccess) {
  44. //重新赋值data或list
  45. final json = result.getDataJson();
  46. var data = ServerTime.fromJson(json!);
  47. //重新赋值data或list
  48. return result.convert<ServerTime>(data: data);
  49. }
  50. return result.convert();
  51. }
  52. }