1. Config Configures the root address of the URL to be requested and the environment

config.dart

import 'env.dart'; Class Config {// various configurations String APP_ID; String SECURITY_KEY; String API_DOMAIN; // Intranet environment static final String _NEIBU_APP_ID = "381"; static final String _NEIBU_SECURITY_KEY = "aaaaaaaaaaaaaaaaaaaaaaaaa"; static final String _NEIBU_API_DOMAIN = "https://neibu.koo.com"; Static final String _PRODUCT_APP_ID = "180"; static final String _PRODUCT_SECURITY_KEY = "aaaaaaaaaaaaaaaaaaaaaaaa"; static final String _PRODUCT_API_DOMAIN = "https://neibu.koo.com"; void setEnv(Env env){ switch (env) { case Env.NEIBU: APP_ID=_NEIBU_APP_ID; SECURITY_KEY=_NEIBU_SECURITY_KEY; API_DOMAIN=_NEIBU_API_DOMAIN; break; case Env.PRODUCT: APP_ID=_PRODUCT_APP_ID; SECURITY_KEY=_PRODUCT_SECURITY_KEY; API_DOMAIN=_PRODUCT_API_DOMAIN; break; default: APP_ID=_PRODUCT_APP_ID; SECURITY_KEY=_PRODUCT_SECURITY_KEY; API_DOMAIN=_PRODUCT_API_DOMAIN; break; }} static final Config _instance= config._internal (); Config._internal(); Factory config.getInstance () => _instance; factory config.getInstance () => _instance; }Copy the code

2. Environment enumeration

env.dart

enum Env {
  NEIBU,
  PRODUCT
}
Copy the code

3. Parameter encryption processing class

encrypt_params.dart

// import 'dart:convert'; // import 'dart:ffi'; // For FFI // import 'dart:io'; // For Platform.isX // import 'package:ffi/ffi.dart'; // // import '.. /config.dart'; // final DynamicLibrary nativeLib = Platform.isAndroid // ? DynamicLibrary.open("libsome.so") // : DynamicLibrary.open("some.framework/some"); // // typedef reverse_func = Pointer<Utf8> Function(Pointer<Utf8> str, Pointer<Utf8> str2); // typedef Reverse = Pointer<Utf8> Function(Pointer<Utf8> str, Pointer<Utf8> str2); // // final reversePointer = nativeLib.lookup<NativeFunction<reverse_func>>('reverse'); // final reverse = reversePointer.asFunction<Reverse>(); // // class FlutterNoCppSrc { // static String _getNetParams(String params){ // Pointer<Utf8> paramsUtf8 = Utf8.toUtf8(params); // Pointer<Utf8> security = Utf8.toUtf8(Config.security_KEY); // Pointer<Utf8> result = reverse(paramsUtf8,security); // print(result.toString()); // final reversedMessage = Utf8.fromUtf8(result); // return reversedMessage; // } // static getRequestMap(Map<dynamic, dynamic> requestMaps){ // requestMaps['app_id'] = Config.appId; // String params = _getNetParams(json.encode(requestMaps)); // requestMaps['sign'] = params; // } // } import 'dart:ffi'; // For FFI import 'dart:io'; // For Platform.isX import 'package:ffi/ffi.dart'; import '.. /config.dart'; final DynamicLibrary nativeLib = Platform.isAndroid ? DynamicLibrary.open("libsome.so") : DynamicLibrary.open("some.framework/some"); final int Function(int x, int y) nativeAdd = nativeLib .lookup<NativeFunction<Int32 Function(Int32, Int32)>>("native_add") .asFunction(); typedef reverse_func = Pointer<Utf8> Function(Pointer<Utf8> str, Pointer<Utf8> str2); typedef Reverse = Pointer<Utf8> Function(Pointer<Utf8> str, Pointer<Utf8> str2); final reversePointer = nativeLib.lookup<NativeFunction<reverse_func>>('reverse'); final reverse = reversePointer.asFunction<Reverse>(); class FlutterNoCppSrc { // static const MethodChannel _channel = // const MethodChannel('flutter_no_cpp_src'); // // static Future<String> get platformVersion async { // final String version = await _channel.invokeMethod('getPlatformVersion'); // return version; // } static int addWithCpp(int x, int y) { return nativeAdd(x, y); } static String getNetParams(String params){ // print("getNetParams Begin:"+params); Pointer<Utf8> paramsUtf8 = Utf8.toUtf8(params); Pointer<Utf8> security = Utf8.toUtf8(Config.getInstance().SECURITY_KEY); Pointer<Utf8> result = reverse(paramsUtf8,security); final reversedMessage = Utf8.fromUtf8(result); // print("result:"+reversedMessage); return reversedMessage; }}Copy the code

A simple network request manager

http_manager.dart

import 'dart:convert'; import 'dart:io'; import 'package:dio/dio.dart'; import 'package:dio/adapter.dart'; import 'package:kooielts/base/base_resp.dart'; import 'package:kooielts/base/utils.dart'; import '.. /config.dart'; import 'encrypt_params.dart'; class HttpManager { Dio _dio; BaseOptions _options; Future post<T>(String path,Map params) async{ params["app_id"]=Config.getInstance().APP_ID; print("path:$path"); print("params$params"); params["validation"]=FlutterNoCppSrc.getNetParams(json.encode(params)); var response =await _dio.post(path, data:params); if (response.statusCode == HttpStatus.ok){ return response.data["obj"]; } return new Future.error(new DioError(request: response.request,response: response,type: DioErrorType.RESPONSE,error: "Request failed ")); } static BaseOptions _getDefOptions() { return BaseOptions( connectTimeout: 5000, receiveTimeout: 100000, headers: Utils.getHeader(), baseUrl: Config.getInstance().API_DOMAIN, contentType:ContentType.parse("application/x-www-form-urlencoded").toString(), ); } // static HttpManager _instance= httpManager._internal (); Httpmanager._internal (){_options=_getDefOptions(); httpManager._internal (){_options=_getDefOptions(); _dio=Dio(_options); // Set the proxy for local Charles to capture packets (_dio.httpClientAdapter as DefaultHttpClientAdapter). OnHttpClientCreate = (HttpClient client) { Client.findproxy = (uri) {return "PROXY 10.155.33.250:8888"; }; client.badCertificateCallback = (X509Certificate cert, String host, int port) { return true; }; }; } // Factory httpManager.getInstance () => _instance; }Copy the code

5. Network request invocation

In initState ()

HttpManager.getInstance().post<Map<String,dynamic>>(Api.launchInfo, map).then((value){
      // var config = jsonDecode(value);
      // SpUtil.getInstance().then((value) => value.)
      setState(() {
        info = LaunchInfo.fromJson(value);
        _adUrl=info.infos.first.imageurl;
      });
    });
Copy the code

Where info is the data that we end up with