HttpManager.dart 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import 'package:dio/dio.dart';
  2. import 'Result.dart';
  3. import 'package:intl/intl.dart';
  4. class HttpManager {
  5. // static String baseUrl = 'http://117.81.233.142:8206/';
  6. // static String baseUrl='http://192.168.50.15:8080/';
  7. static String baseUrl = 'http://202.79.174.56:8206/';
  8. static String token;
  9. static bool debug;
  10. static Dio _createDio() {
  11. Map<String, dynamic> headers = <String, dynamic>{};
  12. headers['token'] = token;
  13. BaseOptions options = BaseOptions(baseUrl: baseUrl, headers: headers);
  14. return Dio(options);
  15. }
  16. static Future<Result> post(String url, {Map<String, dynamic> data}) async {
  17. Result result = Result.empty();
  18. try {
  19. FormData formData = FormData.from(data ?? {});
  20. Response response = await _createDio().post(url, data: formData);
  21. print(response);
  22. if (response.statusCode != 200) {
  23. result.success = false;
  24. result.error = 'httpCode' + response.statusCode.toString();
  25. return result;
  26. }
  27. result = Result.fromJson(response.data);
  28. return result;
  29. } catch (e) {
  30. result.success = false;
  31. result.error = e.toString();
  32. }
  33. return result;
  34. }
  35. static Future<Result> get(String url, {Map<String, dynamic> data}) async {
  36. Result result = Result.empty();
  37. try {
  38. Response response = await _createDio().get(url, queryParameters: data ?? {});
  39. if (response.statusCode != 200) {
  40. result.success = false;
  41. result.error = 'httpCode' + response.statusCode.toString();
  42. return result;
  43. }
  44. result = Result.fromJson(response.data);
  45. return result;
  46. } catch (e) {
  47. result.success = false;
  48. result.error = e.toString();
  49. }
  50. return result;
  51. }
  52. }
  53. String readTimestamp(int timestamp, String _timeType) {
  54. return DateFormat(_timeType).format(DateTime.fromMillisecondsSinceEpoch(timestamp));
  55. }