Request.dart 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'dart:convert';
  2. import 'package:dio/dio.dart';
  3. import 'package:get/get.dart';
  4. import 'package:get_storage/get_storage.dart';
  5. class Response {
  6. late int code;
  7. String? message;
  8. dynamic data;
  9. Response({
  10. required this.code,
  11. this.message,
  12. this.data,
  13. });
  14. factory Response.fromJson(dynamic str) =>
  15. Response.fromMap(jsonDecode(str) as Map<String, dynamic>);
  16. factory Response.fromMap(Map<String, dynamic> resp) => Response(
  17. code: resp['code'],
  18. message: resp['message'],
  19. data: resp['data'],
  20. );
  21. }
  22. final client = GetPlatform.isAndroid ? 'android' : 'ios';
  23. Dio createRequest() {
  24. const isProd = bool.fromEnvironment('dart.vm.product');
  25. const host = isProd ? 'https://machine.njyunzhi.com' : 'http://192.168.89.147:7080';
  26. final baseUrl = '$host/api/$client/farmer';
  27. var options = BaseOptions(
  28. baseUrl: baseUrl,
  29. connectTimeout: 5000,
  30. receiveTimeout: 3000,
  31. );
  32. var dio = Dio(options);
  33. dio.interceptors.add(InterceptorsWrapper(onRequest: (options, handler) {
  34. // Do something before request is sent
  35. options.headers['Authorization'] = GetStorage().read("token");
  36. print('options+$options');
  37. return handler.next(options); //continue
  38. // If you want to resolve the request with some custom data,
  39. // you can resolve a `Response` object eg: `handler.resolve(response)`.
  40. // If you want to reject the request with a error message,
  41. // you can reject a `DioError` object eg: `handler.reject(dioError)`
  42. }, onResponse: (response, handler) {
  43. var resp = response.data as Map<String, dynamic>;
  44. if (resp['code'] == 1000) {
  45. response.data = resp['data'];
  46. try {
  47. var data = response.data as Map<String, dynamic>;
  48. if (null != data['token']) {
  49. GetStorage().write('token', data['token']); //取数据
  50. }
  51. } catch (e) {
  52. //
  53. }
  54. return handler.next(response);
  55. } else {
  56. DioError error = DioError(
  57. requestOptions: response.requestOptions,
  58. error: response.data,
  59. response: response);
  60. return handler.reject(error);
  61. }
  62. }, onError: (DioError e, handler) {
  63. // Do something with response error
  64. return handler.next(e); //continue
  65. // If you want to resolve the request with some custom data,
  66. // you can resolve a `Response` object eg: `handler.resolve(response)`.
  67. }));
  68. return dio;
  69. }
  70. var instance = createRequest();
  71. Future request(String path,
  72. {data,
  73. Map<String, dynamic>? queryParameters,
  74. CancelToken? cancelToken,
  75. Options? options,
  76. ProgressCallback? onSendProgress,
  77. ProgressCallback? onReceiveProgress}) async {
  78. var resp = await instance.request(path,
  79. data: data,
  80. queryParameters: queryParameters,
  81. cancelToken: cancelToken,
  82. options: options);
  83. if (resp.data.runtimeType == String) {
  84. String dt = resp.data as String;
  85. dt.trim();
  86. if (dt.startsWith('{') && dt.endsWith('}')) {
  87. return jsonDecode(dt);
  88. }
  89. return dt;
  90. }
  91. return resp.data;
  92. }