HttpManager.dart 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import 'package:dio/dio.dart';
  2. import 'Result.dart';
  3. import 'package:intl/intl.dart';
  4. class HttpManager {
  5. // static String baseUrl = 'http://123.58.240.138:8090/';
  6. static String baseUrl = 'http://192.168.50.132: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. if (response.statusCode != 200) {
  21. result.success = false;
  22. result.error = 'httpCode' + response.statusCode.toString();
  23. return result;
  24. }
  25. result = Result.fromJson(response.data);
  26. return result;
  27. } catch (e) {
  28. result.success = false;
  29. result.error = e.toString();
  30. }
  31. return result;
  32. }
  33. static Future<Result> get(String url, {Map<String, dynamic> data}) async {
  34. Result result = Result.empty();
  35. try {
  36. Response response = await _createDio().get(url, queryParameters: data ?? {});
  37. if (response.statusCode != 200) {
  38. result.success = false;
  39. result.error = 'httpCode' + response.statusCode.toString();
  40. return result;
  41. }
  42. result = Result.fromJson(response.data);
  43. return result;
  44. } catch (e) {
  45. result.success = false;
  46. result.error = e.toString();
  47. }
  48. return result;
  49. }
  50. }
  51. String readTimestamp(int timestamp, String _timeType) {
  52. return DateFormat(_timeType).format(DateTime.fromMillisecondsSinceEpoch(timestamp));
  53. }