From 8a46f8f6e93b91c60c552d4c886af24a6fc76ce5 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Tue, 12 Jan 2021 15:50:18 +0800 Subject: [PATCH] add netutil --- lib/constants/api.dart | 9 +++ lib/main.dart | 2 + lib/pages/personal/personal_page.dart | 9 ++- lib/routers/router_init.dart | 2 +- lib/service/net.dart | 1 + lib/utils/developer_util.dart | 8 +++ lib/utils/net_util.dart | 84 +++++++++++++++++++++++++++ 7 files changed, 109 insertions(+), 6 deletions(-) create mode 100644 lib/constants/api.dart create mode 100644 lib/utils/developer_util.dart create mode 100644 lib/utils/net_util.dart diff --git a/lib/constants/api.dart b/lib/constants/api.dart new file mode 100644 index 00000000..86c173b4 --- /dev/null +++ b/lib/constants/api.dart @@ -0,0 +1,9 @@ +class API { + static const String host = 'http://192.168.31.129:9001/app'; + static const int networkTimeOut = 10000; + static _Login login = _Login(); +} + +class _Login { + String get a => ''; +} diff --git a/lib/main.dart b/lib/main.dart index fbd43304..aea24f93 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,6 @@ import 'package:akuCommunity/pages/tab_navigator.dart'; import 'package:akuCommunity/provider/user_provider.dart'; +import 'package:akuCommunity/utils/developer_util.dart'; import 'package:amap_map_fluttify/amap_map_fluttify.dart'; import 'package:ani_route/ani_route.dart'; import 'package:bot_toast/bot_toast.dart'; @@ -18,6 +19,7 @@ void main() { WidgetsFlutterBinding.ensureInitialized(); ARoute.init(true); AmapLocation.instance.init(iosKey: 'ios key'); + DeveloperUtil.setDev(true); runApp(MyApp()); } diff --git a/lib/pages/personal/personal_page.dart b/lib/pages/personal/personal_page.dart index 19851df5..c88735e1 100644 --- a/lib/pages/personal/personal_page.dart +++ b/lib/pages/personal/personal_page.dart @@ -1,5 +1,7 @@ import 'package:akuCommunity/pages/sign/sign_in_page.dart'; import 'package:akuCommunity/provider/user_provider.dart'; +import 'package:akuCommunity/service/net.dart'; +import 'package:akuCommunity/utils/net_util.dart'; import 'package:ani_route/ani_route.dart'; import 'package:flutter/material.dart'; import 'package:flutter/cupertino.dart'; @@ -23,13 +25,11 @@ class PersonalIndex extends StatefulWidget { } class _PersonalIndexState extends State - with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin { - @override - bool get wantKeepAlive => true; - + with SingleTickerProviderStateMixin { @override void initState() { super.initState(); + NetUtil().get('test'); } SliverAppBar _sliverAppBar(double height) { @@ -171,7 +171,6 @@ class _PersonalIndexState extends State @override Widget build(BuildContext context) { - super.build(context); double _statusHeight = MediaQuery.of(context).padding.top; return Scaffold( body: CustomScrollView( diff --git a/lib/routers/router_init.dart b/lib/routers/router_init.dart index 5e4c4939..31f412a3 100644 --- a/lib/routers/router_init.dart +++ b/lib/routers/router_init.dart @@ -14,4 +14,4 @@ class RouterInit { ); }); } -} \ No newline at end of file +} diff --git a/lib/service/net.dart b/lib/service/net.dart index 93938758..e782f8b0 100644 --- a/lib/service/net.dart +++ b/lib/service/net.dart @@ -36,6 +36,7 @@ class Net { (client) { client.badCertificateCallback = (X509Certificate cert, String host, int port) { + //TODO return true; }; }; diff --git a/lib/utils/developer_util.dart b/lib/utils/developer_util.dart new file mode 100644 index 00000000..a1e3c47f --- /dev/null +++ b/lib/utils/developer_util.dart @@ -0,0 +1,8 @@ +class DeveloperUtil { + static bool _dev = false; + static setDev(bool state) { + _dev = state; + } + + static bool get dev => _dev; +} diff --git a/lib/utils/net_util.dart b/lib/utils/net_util.dart new file mode 100644 index 00000000..ab3acfd9 --- /dev/null +++ b/lib/utils/net_util.dart @@ -0,0 +1,84 @@ +import 'dart:convert'; + +import 'package:bot_toast/bot_toast.dart'; +import 'package:dio/dio.dart'; + +import 'package:akuCommunity/constants/api.dart'; + +class NetUtil { + Dio _dio; + static final NetUtil _netUtil = NetUtil._internal(); + + factory NetUtil() => _netUtil; + + NetUtil._internal() { + BaseOptions options = BaseOptions( + baseUrl: API.host, + connectTimeout: API.networkTimeOut, + receiveTimeout: API.networkTimeOut, + sendTimeout: API.networkTimeOut, + headers: {}, + ); + if (_dio == null) _dio = Dio(options); + } + + Future get( + String path, { + Map params, + }) async { + try { + Response res = await _dio.get(path, queryParameters: params); + return BaseModel.fromJson(res.data); + } on DioError catch (e) { + _parseErr(e); + } + return BaseModel.err(); + } + + _parseErr(DioError err) { + _makeToast(String message) { + BotToast.showText(text: '$message\_${err.response.statusCode}'); + } + + switch (err.type) { + case DioErrorType.CONNECT_TIMEOUT: + case DioErrorType.SEND_TIMEOUT: + case DioErrorType.RECEIVE_TIMEOUT: + _makeToast('连接超时'); + break; + case DioErrorType.RESPONSE: + _makeToast('服务器出错'); + break; + case DioErrorType.CANCEL: + break; + case DioErrorType.DEFAULT: + _makeToast('未知错误'); + break; + } + } +} + +class BaseModel { + int code; + String message; + dynamic data; + BaseModel({ + this.code, + this.message, + this.data, + }); + + BaseModel.err({this.message = '未知错误', this.code = 0}); + + factory BaseModel.fromMap(Map map) { + if (map == null) return null; + return BaseModel( + code: map['code'] ?? 0, + message: map['message'] ?? '', + data: map['data'], + ); + } + + factory BaseModel.fromJson(String source) => + BaseModel.fromMap(json.decode(source)); +}