Request.dart 3.4KB

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