Request.dart 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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(requestOptions: response.requestOptions, error: response.data, response: response);
  53. return handler.reject(error);
  54. }
  55. }, onError: (DioError e, handler) {
  56. // Do something with response error
  57. return handler.next(e); //continue
  58. // If you want to resolve the request with some custom data,
  59. // you can resolve a `Response` object eg: `handler.resolve(response)`.
  60. }));
  61. return dio;
  62. }
  63. var instance = createRequest();
  64. Future request(String path,
  65. {data,
  66. Map<String, dynamic>? queryParameters,
  67. CancelToken? cancelToken,
  68. Options? options,
  69. ProgressCallback? onSendProgress,
  70. ProgressCallback? onReceiveProgress}) async {
  71. var resp = await instance.request(path,
  72. data: data,
  73. queryParameters: queryParameters,
  74. cancelToken: cancelToken,
  75. options: options);
  76. if (resp.data.runtimeType == String) {
  77. String dt = resp.data as String;
  78. dt.trim();
  79. if (dt.startsWith('{') && dt.endsWith('}')) {
  80. return jsonDecode(dt);
  81. }
  82. return dt;
  83. }
  84. return resp.data;
  85. }