request.dart 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:dio/dio.dart';
  4. import 'package:get_it/get_it.dart';
  5. import 'package:sport/bean/friend_list.dart';
  6. import 'package:sport/services/api/resp.dart';
  7. Future<String?> mergeAny(List<RequestOptions> options) async {
  8. var dio = GetIt.I<Dio>();
  9. Map<String, String> params = {};
  10. for (var i = 0; i < options.length; i++) {
  11. var o = options[i];
  12. params["groups[$i][url]"] = o.uri.toString();
  13. params["groups[$i][method]"] = o.method;
  14. }
  15. Response response = await dio.post<String>("/merge/any", data: params, options: Options(contentType: "application/x-www-form-urlencoded"));
  16. if (response.statusCode == HttpStatus.ok) {
  17. return response.data;
  18. }
  19. return null;
  20. }
  21. Future<List<int>> mergeMessageCount() async {
  22. List<int> result = [0, 0, 0];
  23. var dio = GetIt.I<Dio>();
  24. var options = dio.options.copyWith(baseUrl: "");
  25. String? jsonStr = await mergeAny([
  26. Options(
  27. method: "GET",
  28. ).compose(options, "/notice/count", queryParameters: {"type": "like", "read": 0, "isMsg": 1}),
  29. Options(
  30. method: "GET",
  31. ).compose(options, "/notice/count", queryParameters: {"type": "comment", "read": 0, "isMsg": 1}),
  32. Options(
  33. method: "GET",
  34. ).compose(options, "/forum/fansList", queryParameters: {"limit": 1})
  35. ]);
  36. if (jsonStr?.isNotEmpty == true) {
  37. List<dynamic> jsonResult = json.decode(jsonStr!);
  38. result[0] = RespData<int>.fromJson(jsonResult[0]).data ?? 0;
  39. result[1] = RespData<int>.fromJson(jsonResult[1]).data ?? 0;
  40. result[2] = RespData<FriendList>.fromJson(jsonResult[2]).data?.noIgnoreCount ?? 0;
  41. }
  42. return result;
  43. }