import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; import 'package:flutter_app/http/config.dart'; import 'package:bot_toast/bot_toast.dart'; class Http { Dio _dio; Http() { this._dio = new Dio(new BaseOptions( baseUrl: config.BASE_URL, connectTimeout: 3000, receiveTimeout: 3000, )); this._dio.interceptors.add(InterceptorsWrapper( onRequest:(RequestOptions options){ /// 请求发送前拦截,对 queryParameters 参数进行RSA公钥加密,放到token中发送给后端 /// 后端私钥解密,查询数据,然后通过私钥加密数据再返回给前端 ///options.headers = options.queryParameters; ///print(options.headers); return options; }, onResponse:(Response response) { /// 响应拦截器,将数据返回给前端之前先使用公钥解密后端的数据,然后将解密后明文给前端 // 在返回响应数据之前做一些预处理 return response; }, onError: (DioError e) { return e; } )); } /*** * get 请求 * url 请求地址 * params 请求参数 */ get (String url, Object params) async { Response response; try { response = await this._dio.get(url, queryParameters: params); return response.data; } on DioError catch (e) { if (CancelToken.isCancel(e)) { BotToast.showSimpleNotification( title: 'GET请求取消', subTitle: e.message, hideCloseButton: true ); } BotToast.showSimpleNotification( title: 'GET请求错误', subTitle: e.message, hideCloseButton: true ); print(e.message); } } /*** * post 请求 * url 请求地址 * params 请求参数 */ post (String url, Object params) async { Response response; try { response = await this._dio.post(url, data: params); return response.data; } on DioError catch (e) { if (CancelToken.isCancel(e)) { BotToast.showSimpleNotification( title: 'POST请求取消', subTitle: e.message, hideCloseButton: true ); } BotToast.showSimpleNotification( title: 'POST请求错误', subTitle: e.message, hideCloseButton: true ); } } }