HttpManager.dart 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import 'package:dio/dio.dart';
  2. import 'Result.dart';
  3. import 'package:intl/intl.dart';
  4. class HttpManager {
  5. static String baseUrl = "http://49.4.66.233:8201/";
  6. static String token;
  7. static bool debug;
  8. static Dio _createDio() {
  9. Map<String, dynamic> headers = Map();
  10. headers["token"] = token;
  11. BaseOptions options = BaseOptions(baseUrl: baseUrl, headers: headers);
  12. return Dio(options);
  13. }
  14. static Future<Result> post(String url, {Map<String, dynamic> data}) {
  15. return Future(() async {
  16. FormData formData = FormData.from(data ?? {});
  17. Response response = await _createDio().post(url, data: formData);
  18. print(response);
  19. if (response.statusCode != 200) {
  20. return Future.error("httpCode" + response.statusCode.toString());
  21. }
  22. Result result = Result.fromJson(response.data);
  23. return result;
  24. });
  25. }
  26. static Future<Result> get(String url, {Map<String, dynamic> data}) {
  27. return Future(() async {
  28. Response response =
  29. await _createDio().get(url, queryParameters: data ?? {});
  30. if (response.statusCode != 200) {
  31. return Future.error("httpCode" + response.statusCode.toString());
  32. }
  33. Result result = Result.fromJson(response.data);
  34. return result;
  35. });
  36. }
  37. }
  38. String readTimestamp(int timestamp,String _timeType) {
  39. return DateFormat(_timeType).format(DateTime.fromMillisecondsSinceEpoch(timestamp));
  40. }