Request.dart 3.4KB

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