HttpManager.dart 1.5 KB

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