Request.dart 3.0KB

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