HttpManager.dart 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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.226:8080/';
  7. static String token;
  8. static bool debug;
  9. static Dio _createDio() {
  10. Map<String, dynamic> headers = <String, dynamic>{};
  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}) async {
  16. Result result = Result.empty();
  17. try {
  18. FormData formData = FormData.from(data ?? {});
  19. Response response = await _createDio().post(url, data: formData);
  20. print(response);
  21. if (response.statusCode != 200) {
  22. result.success = false;
  23. result.error = 'httpCode' + response.statusCode.toString();
  24. return result;
  25. }
  26. result = Result.fromJson(response.data);
  27. return result;
  28. } catch (e) {
  29. result.success = false;
  30. result.error = e.toString();
  31. }
  32. return result;
  33. }
  34. static Future<Result> get(String url, {Map<String, dynamic> data}) async {
  35. Result result = Result.empty();
  36. try {
  37. Response response = await _createDio().get(url, queryParameters: data ?? {});
  38. if (response.statusCode != 200) {
  39. result.success = false;
  40. result.error = 'httpCode' + response.statusCode.toString();
  41. return result;
  42. }
  43. result = Result.fromJson(response.data);
  44. return result;
  45. } catch (e) {
  46. result.success = false;
  47. result.error = e.toString();
  48. }
  49. return result;
  50. }
  51. }
  52. String readTimestamp(int timestamp, String _timeType) {
  53. return DateFormat(_timeType).format(DateTime.fromMillisecondsSinceEpoch(timestamp));
  54. }