123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- import 'dart:convert';
-
- import 'package:dio/dio.dart';
- import 'package:fluttertoast/fluttertoast.dart';
- import 'package:get/get.dart';
- import 'package:get_storage/get_storage.dart';
-
- class Response {
- late int code;
- String? message;
- dynamic data;
-
- Response({
- required this.code,
- this.message,
- this.data,
- });
-
- factory Response.fromJson(dynamic str) =>
- Response.fromMap(jsonDecode(str) as Map<String, dynamic>);
-
- factory Response.fromMap(Map<String, dynamic> resp) => Response(
- code: resp['code'],
- message: resp['message'],
- data: resp['data'],
- );
- }
-
- final client = GetPlatform.isAndroid ? 'android' : 'ios';
-
- Dio createRequest() {
- const isProd = bool.fromEnvironment('dart.vm.product');
- // const host = isProd ? 'https://machine.njyunzhi.com' : 'http://192.168.89.147:7080';
- const host = 'https://machine.njyunzhi.com';
- final baseUrl = '$host/api/$client/farmer';
-
- var options = BaseOptions(
- baseUrl: baseUrl,
- connectTimeout: 5000,
- receiveTimeout: 3000,
- );
-
- var dio = Dio(options);
- GetStorage box = GetStorage();
- dio.interceptors.add(InterceptorsWrapper(onRequest: (options, handler) {
- // Do something before request is sent
- options.headers['Authorization'] = GetStorage().read("token");
- print('options+$options');
- return handler.next(options); //continue
- // If you want to resolve the request with some custom data,
- // you can resolve a `Response` object eg: `handler.resolve(response)`.
- // If you want to reject the request with a error message,
- // you can reject a `DioError` object eg: `handler.reject(dioError)`
- }, onResponse: (response, handler) {
- var resp = response.data as Map<String, dynamic>;
- if(resp['code']==1001 || resp['code']==1002){
- Fluttertoast.showToast(msg: '登录过期请重新登录!');
- box.remove('token');
- Get.toNamed('/login?isBack=true');
- return handler.next(response);
- }
- if (resp['code'] == 1000) {
- response.data = resp['data'];
-
- try {
- var data = response.data as Map<String, dynamic>;
- if (null != data['token']) {
- GetStorage().write('token', data['token']); //取数据
- }
- } catch (e) {
- //
- }
-
- return handler.next(response);
- } else {
- DioError error = DioError(
- requestOptions: response.requestOptions,
- error: response.data,
- response: response);
- return handler.reject(error);
- }
- }, onError: (DioError e, handler) {
- // Do something with response error
- return handler.next(e); //continue
- // If you want to resolve the request with some custom data,
- // you can resolve a `Response` object eg: `handler.resolve(response)`.
- }));
-
- return dio;
- }
-
- var instance = createRequest();
-
- Future request(String path,
- {data,
- Map<String, dynamic>? queryParameters,
- CancelToken? cancelToken,
- Options? options,
- ProgressCallback? onSendProgress,
- ProgressCallback? onReceiveProgress}) async {
- var resp = await instance.request(path,
- data: data,
- queryParameters: queryParameters,
- cancelToken: cancelToken,
- options: options);
-
- if (resp.data.runtimeType == String) {
- String dt = resp.data as String;
- dt.trim();
- if (dt.startsWith('{') && dt.endsWith('}')) {
- return jsonDecode(dt);
- }
- return dt;
- }
-
- return resp.data;
- }
|