Commit 7e9add2c authored by tongzifang's avatar tongzifang

add dio

parent 047a6a5e
...@@ -7,10 +7,12 @@ import Foundation ...@@ -7,10 +7,12 @@ import Foundation
import base_data_channel import base_data_channel
import path_provider_foundation import path_provider_foundation
import shared_preferences_foundation
import sqflite import sqflite
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
BaseDataChannelPlugin.register(with: registry.registrar(forPlugin: "BaseDataChannelPlugin")) BaseDataChannelPlugin.register(with: registry.registrar(forPlugin: "BaseDataChannelPlugin"))
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin")) PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin")) SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
} }
...@@ -248,6 +248,14 @@ packages: ...@@ -248,6 +248,14 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "4.0.2" version: "4.0.2"
http_proxy:
dependency: transitive
description:
name: http_proxy
sha256: "7d5bc7ad1b0c6d0cfb5da97c5bfe302082f93d32cf5c67d484d1a4085b3ffa58"
url: "https://pub.flutter-io.cn"
source: hosted
version: "1.2.1"
image_picker: image_picker:
dependency: transitive dependency: transitive
description: description:
...@@ -488,6 +496,62 @@ packages: ...@@ -488,6 +496,62 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "0.27.7" version: "0.27.7"
shared_preferences:
dependency: transitive
description:
name: shared_preferences
sha256: "858aaa72d8f61637d64e776aca82e1c67e6d9ee07979123c5d17115031c1b13b"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
shared_preferences_android:
dependency: transitive
description:
name: shared_preferences_android
sha256: "8304d8a1f7d21a429f91dee552792249362b68a331ac5c3c1caf370f658873f6"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: cf2a42fb20148502022861f71698db12d937c7459345a1bdaa88fc91a91b3603
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.2.0"
shared_preferences_linux:
dependency: transitive
description:
name: shared_preferences_linux
sha256: "9d387433ca65717bbf1be88f4d5bb18f10508917a8fa2fb02e0fd0d7479a9afa"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.2.0"
shared_preferences_platform_interface:
dependency: transitive
description:
name: shared_preferences_platform_interface
sha256: fb5cf25c0235df2d0640ac1b1174f6466bd311f621574997ac59018a6664548d
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.2.0"
shared_preferences_web:
dependency: transitive
description:
name: shared_preferences_web
sha256: "74083203a8eae241e0de4a0d597dbedab3b8fef5563f33cf3c12d7e93c655ca5"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.1.0"
shared_preferences_windows:
dependency: transitive
description:
name: shared_preferences_windows
sha256: "5e588e2efef56916a3b229c3bfe81e6a525665a454519ca51dbcc4236a274173"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.2.0"
sky_engine: sky_engine:
dependency: transitive dependency: transitive
description: flutter description: flutter
...@@ -501,6 +565,14 @@ packages: ...@@ -501,6 +565,14 @@ packages:
url: "https://pub.flutter-io.cn" url: "https://pub.flutter-io.cn"
source: hosted source: hosted
version: "1.9.1" version: "1.9.1"
sp_util:
dependency: transitive
description:
name: sp_util
sha256: "9da43dce5de79c17a787d0626bf01538d63090ca32521200d22a232171c495dc"
url: "https://pub.flutter-io.cn"
source: hosted
version: "2.0.3"
sqflite: sqflite:
dependency: transitive dependency: transitive
description: description:
......
class BaseEntity {
int? code;
dynamic data;
String? message;
bool? status ;
BaseEntity({this.code, this.data, this.message, this.status});
BaseEntity.fromJson(Map<String, dynamic> json) {
code = json['code'];
data = json['data'];
message = json['message'];
status = json['status'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['code'] = this.code;
data['data'] = this.data;
data['message'] = this.message;
data['status'] = this.status;
return data;
}
}
\ No newline at end of file
import 'dart:io';
import 'package:base_data_channel/http/qmai_host.dart';
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:http_proxy/http_proxy.dart';
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
import 'package:sp_util/sp_util.dart';
import 'baseurl_interceptor.dart';
class BaseDio {
BaseDio._();
static BaseDio? _instance;
static BaseDio? getInstance() {
_instance ??= BaseDio._();
return _instance;
}
Dio getDio() {
final Dio dio = Dio();
dio.options = BaseOptions(
receiveTimeout: Duration(seconds: 5),
connectTimeout:Duration(seconds: 5),
sendTimeout:Duration(seconds: 5),
baseUrl: QmaiHost.getBaseUrl()); // 设置超时时间等 ...
dio.interceptors.add(HeaderInterceptor()); // 添加拦截器,如 token之类,需要全局使用的参数
dio.interceptors.add(PrettyDioLogger(
// 添加日志格式化工具类
requestHeader: true,
requestBody: true,
responseBody: true,
responseHeader: false,
compact: false,
));
checkForCharlesProxy(dio);
// (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
//
// client.badCertificateCallback =
// (X509Certificate cert, String host, int port) {
// return Platform.isAndroid;
// };
// client.findProxy = (uri) {
// //proxy all request to localhost:8888
// // return "PROXY 192.168.100.179:8888";
// return "PROXY 192.168.20.64:8888";
// // return "PROXY 192.168.10.223:8888";
// };
// // 你也可以自己创建一个新的HttpClient实例返回。
// // return new HttpClient(SecurityContext);
// };
return dio;
}
void checkForCharlesProxy(Dio dio) async {
if (SpUtil.getString('build_type') != 'release') {
HttpProxy httpProxy = await HttpProxy.createHttpProxy();
var proxyHost = httpProxy.host;
var proxyPort = httpProxy.port;
if (proxyHost == null) return;
(dio.httpClientAdapter as IOHttpClientAdapter).onHttpClientCreate =
(client) {
client.findProxy = (uri) => "PROXY $proxyHost:$proxyPort";
client.badCertificateCallback =
(X509Certificate cert, String host, int port) => Platform.isAndroid;
};
}
}
}
import 'package:base_data_channel/http/qmai_host.dart';
import 'package:dio/dio.dart';
class HeaderInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
options.baseUrl = QmaiHost.getBaseUrl();
super.onRequest(options, handler);
}
}
import 'package:base_data_channel/http/qmai_host.dart';
import 'package:dio/dio.dart';
import 'package:sp_util/sp_util.dart';
class HeaderInterceptor extends Interceptor {
@override
void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
var userToken = SpUtil.getString('user_token');
var userTokenNew = SpUtil.getString('user_token_new');
Map<String, dynamic> dictionary = {
'Accept': '*/*; v=1.0',
'Accept-Language':
'zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2',
'QM-STORE-AUTH': 'undefined2',
'X-CSRF-TOKEN': '{{csrf_token()}}',
'Connection': 'close',
'Referer': QmaiHost.getBasicUrl(),
'Cookie': 'qm_account_token=$userToken',
'content-type': 'application/json',
'User-Agent': 'wechatdevtools appservice port/62739',
'Qm-From-Type': userToken,
'Authorization': 'Bearer $userToken',
'Qm-Account-Token': userToken,
'Qm-seller-Token': userTokenNew,
'Qm-From': 'android',
};
options.headers.addAll(dictionary);
super.onRequest(options, handler);
}
}
import 'package:sp_util/sp_util.dart';
class QmaiHost {
//base copy原生逻辑
static String getBaseUrl() {
String? buildType = SpUtil.getString('build_type');
String? organ = SpUtil.getString('organ');
if (buildType == 'debug'||buildType == 'dev') {
return 'http://inapi.zmcms.cn/$organ/';
} else if (buildType == 'shop') {
return 'http://inapi.qimai.shop/$organ/';
} else if (buildType == 'beta') {
return 'http://inapi.qmai.co/$organ/';
} else
return 'https://inapi.qmai.cn/$organ/';
}
//basic copy原生逻辑
static String getBasicUrl() {
String? buildType = SpUtil.getString('build_type');
if (buildType == 'debug'||buildType == 'dev') {
return 'http://qmapp.dev.zmcms.cn/';
} else if (buildType == 'shop') {
return 'http://qmapp.qimai.shop/';
} else if (buildType == 'beta') {
return 'http://qmapp.qmai.co/';
} else
return 'http://sellerapp.qmai.cn/';
}
}
...@@ -32,13 +32,15 @@ dependencies: ...@@ -32,13 +32,15 @@ dependencies:
flutter_web_plugins: flutter_web_plugins:
sdk: flutter sdk: flutter
plugin_platform_interface: ^2.1.4 plugin_platform_interface: ^2.1.4
dio: ^5.1.1
retrofit: ^4.0.1
pretty_dio_logger: ^1.3.1 pretty_dio_logger: ^1.3.1
pigeon: ^9.2.3 pigeon: ^9.2.3
image_picker: ^0.8.7+1 image_picker: ^0.8.7+1
dotted_border: ^2.0.0+3 dotted_border: ^2.0.0+3
cached_network_image: ^3.2.3 cached_network_image: ^3.2.3
http_proxy: ^1.2.1
sp_util: ^2.0.3
dio: ^5.1.1
retrofit: ^4.0.1
flutter_boost: flutter_boost:
git: git:
url: 'https://github.com/alibaba/flutter_boost.git' url: 'https://github.com/alibaba/flutter_boost.git'
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment