Request.dart 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. var options = BaseOptions(
  25. baseUrl: 'https://machine.njyunzhi.com/api/' + client + '/farmer',
  26. connectTimeout: 5000,
  27. receiveTimeout: 3000,
  28. );
  29. var dio = Dio(options);
  30. dio.interceptors.add(InterceptorsWrapper(onRequest: (options, handler) {
  31. // Do something before request is sent
  32. options.headers['Authorization'] = GetStorage().read("token");
  33. return handler.next(options); //continue
  34. // If you want to resolve the request with some custom data,
  35. // you can resolve a `Response` object eg: `handler.resolve(response)`.
  36. // If you want to reject the request with a error message,
  37. // you can reject a `DioError` object eg: `handler.reject(dioError)`
  38. }, onResponse: (response, handler) {
  39. var resp = response.data as Map<String, dynamic>;
  40. if (resp['code'] == 1000) {
  41. response.data = resp['data'];
  42. try {
  43. var data = response.data as Map<String, dynamic>;
  44. if (null != data['token']) {
  45. GetStorage().write('token', data['token']); //取数据
  46. }
  47. } catch (e) {
  48. //
  49. }
  50. return handler.next(response);
  51. } else {
  52. DioError error = DioError(
  53. requestOptions: response.requestOptions,
  54. error: response.data,
  55. response: response);
  56. return handler.reject(error);
  57. }
  58. }, onError: (DioError e, handler) {
  59. // Do something with response error
  60. return handler.next(e); //continue
  61. // If you want to resolve the request with some custom data,
  62. // you can resolve a `Response` object eg: `handler.resolve(response)`.
  63. }));
  64. return dio;
  65. }
  66. var instance = createRequest();
  67. Future request(String path,
  68. {data,
  69. Map<String, dynamic>? queryParameters,
  70. CancelToken? cancelToken,
  71. Options? options,
  72. ProgressCallback? onSendProgress,
  73. ProgressCallback? onReceiveProgress}) async {
  74. var resp = await instance.request(path,
  75. data: data,
  76. queryParameters: queryParameters,
  77. cancelToken: cancelToken,
  78. options: options);
  79. if (resp.data.runtimeType == String) {
  80. String dt = resp.data as String;
  81. dt.trim();
  82. if (dt.startsWith('{') && dt.endsWith('}')) {
  83. return jsonDecode(dt);
  84. }
  85. return dt;
  86. }
  87. return resp.data;
  88. }