diff --git a/lib/const/api.dart b/lib/const/api.dart new file mode 100644 index 0000000..5bf79f0 --- /dev/null +++ b/lib/const/api.dart @@ -0,0 +1,20 @@ +class API { + ///HOST + static const String host = 'http://test.akuhotel.com:8804'; + + ///接口基础地址 + static const String baseURL = '$host/IntelligentCommunity/manage'; + + ///静态资源路径 + static String get resource => '$host/static'; + + static String image(String path) => '$resource$path'; + + static const int networkTimeOut = 10000; + static _Login login = _Login(); +} + +class _Login { + ///登录/退出:发送短信验证码 + String get sendSMS => '/login/sendMMSLogin'; +} diff --git a/lib/main.dart b/lib/main.dart index ce93ee6..785b9e5 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -5,6 +5,7 @@ import 'package:aku_community_manager/provider/manage_provider.dart'; import 'package:aku_community_manager/provider/outdoor_provider.dart'; import 'package:aku_community_manager/provider/user_provider.dart'; import 'package:aku_community_manager/ui/home/home_page.dart'; +import 'package:aku_community_manager/ui/splash/splash_page.dart'; import 'package:amap_map_fluttify/amap_map_fluttify.dart'; import 'package:bot_toast/bot_toast.dart'; import 'package:flutter/cupertino.dart'; @@ -38,7 +39,7 @@ class MyApp extends StatelessWidget { ], child: GetMaterialApp( title: '小蜜蜂管家', - home: HomePage(), + home: SplashPage(), builder: BotToastInit(), navigatorObservers: [ BotToastNavigatorObserver(), diff --git a/lib/ui/splash/splash_page.dart b/lib/ui/splash/splash_page.dart new file mode 100644 index 0000000..fc38cd4 --- /dev/null +++ b/lib/ui/splash/splash_page.dart @@ -0,0 +1,30 @@ +import 'package:aku_community_manager/ui/home/home_page.dart'; +import 'package:aku_community_manager/ui/widgets/common/aku_scaffold.dart'; +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; +import 'package:power_logger/power_logger.dart'; + +class SplashPage extends StatefulWidget { + SplashPage({Key key}) : super(key: key); + + @override + _SplashPageState createState() => _SplashPageState(); +} + +class _SplashPageState extends State { + @override + void initState() { + super.initState(); + Future.delayed(Duration(milliseconds: 300), () { + if (mounted) PowerLogger.init(context); + }); + Future.delayed(Duration(milliseconds: 3000), () { + Get.to(HomePage()); + }); + } + + @override + Widget build(BuildContext context) { + return Scaffold(); + } +} diff --git a/lib/utils/network/base_file_model.dart b/lib/utils/network/base_file_model.dart new file mode 100644 index 0000000..a42550b --- /dev/null +++ b/lib/utils/network/base_file_model.dart @@ -0,0 +1,19 @@ +class BaseFileModel { + String message; + String url; + bool status; + BaseFileModel({ + this.message, + this.url, + this.status, + }); + + BaseFileModel.err( + {this.message = '未知错误', this.url = '', this.status = false}); + + BaseFileModel.fromJson(Map json) { + message = json['message'] ?? ''; + url = json['url'] ?? null; + status = json['status'] ?? false; + } +} diff --git a/lib/utils/network/base_list_model.dart b/lib/utils/network/base_list_model.dart new file mode 100644 index 0000000..e576232 --- /dev/null +++ b/lib/utils/network/base_list_model.dart @@ -0,0 +1,27 @@ +class BaseListModel { + int pageCount; + int rowCount; + List tableList; + BaseListModel({ + this.pageCount, + this.rowCount, + this.tableList, + }); + BaseListModel.zero({ + this.pageCount = 0, + this.rowCount = 0, + this.tableList = const [], + }); + + BaseListModel.err({ + this.pageCount = 0, + this.rowCount = 0, + this.tableList = const [], + }); + + BaseListModel.fromJson(Map json) { + pageCount = json['pageCount'] ?? 0; + rowCount = json['rowCount'] ?? 0; + tableList = json['tableList'] ?? []; + } +} diff --git a/lib/utils/network/base_model.dart b/lib/utils/network/base_model.dart new file mode 100644 index 0000000..c5f0d95 --- /dev/null +++ b/lib/utils/network/base_model.dart @@ -0,0 +1,20 @@ +class BaseModel { + int code; + String message; + bool status; + dynamic data; + BaseModel({ + this.code, + this.message, + this.data, + this.status, + }); + + BaseModel.err({this.message = '未知错误', this.status = false}); + + BaseModel.fromJson(Map json) { + message = json['message'] ?? ''; + data = json['data'] ?? null; + status = json['status'] ?? false; + } +} diff --git a/lib/utils/network/login_model.dart b/lib/utils/network/login_model.dart new file mode 100644 index 0000000..d0d7fc9 --- /dev/null +++ b/lib/utils/network/login_model.dart @@ -0,0 +1,21 @@ +class LoginModel { + String message; + String token; + bool status; + + LoginModel({this.message, this.token, this.status}); + + LoginModel.fromJson(Map json) { + message = json['message']; + token = json['token']; + status = json['status']; + } + + Map toJson() { + final Map data = new Map(); + data['message'] = this.message; + data['token'] = this.token; + data['status'] = this.status; + return data; + } +} diff --git a/lib/utils/network/net_util.dart b/lib/utils/network/net_util.dart new file mode 100644 index 0000000..503479a --- /dev/null +++ b/lib/utils/network/net_util.dart @@ -0,0 +1,168 @@ +// Dart imports: +import 'dart:io'; + +// Package imports: +import 'package:aku_community_manager/const/api.dart'; +import 'package:aku_community_manager/utils/network/base_file_model.dart'; +import 'package:aku_community_manager/utils/network/base_list_model.dart'; +import 'package:aku_community_manager/utils/network/base_model.dart'; +import 'package:bot_toast/bot_toast.dart'; +import 'package:dio/dio.dart'; +import 'package:get/get.dart' hide Response, FormData, MultipartFile; +import 'package:power_logger/power_logger.dart'; + + + +class NetUtil { + Dio _dio; + static final NetUtil _netUtil = NetUtil._internal(); + + factory NetUtil() => _netUtil; + + Dio get dio => _dio; + + NetUtil._internal() { + BaseOptions options = BaseOptions( + baseUrl: API.baseURL, + connectTimeout: API.networkTimeOut, + receiveTimeout: API.networkTimeOut, + sendTimeout: API.networkTimeOut, + headers: {}, + ); + if (_dio == null) _dio = Dio(options); + dio.interceptors.add(InterceptorsWrapper( + onRequest: (RequestOptions options) async => options, + onResponse: (Response response) async { + LoggerData.addData(response); + return response; + }, + onError: (DioError error) async { + _parseErr(error); + return error; + }, + )); + } + + ///call auth after login + auth(String token) { + _dio.options.headers.putIfAbsent('App-Admin-Token', () => token); + } + + /// ## alias of Dio().get + /// + /// GET method + Future get( + String path, { + Map params, + bool showMessage = false, + }) async { + try { + Response res = await _dio.get(path, queryParameters: params); + BaseModel baseModel = BaseModel.fromJson(res.data); + _parseRequestError(baseModel, showMessage: showMessage); + return baseModel; + } on DioError catch (e) { + _parseErr(e); + } + return BaseModel.err(); + } + + /// ## alias of Dio().post + /// + /// POST method + /// + /// only work with JSON. + Future post( + String path, { + Map params, + bool showMessage = false, + }) async { + try { + Response res = await _dio.post(path, data: params); + + BaseModel baseModel = BaseModel.fromJson(res.data); + _parseRequestError(baseModel, showMessage: showMessage); + + return baseModel; + } on DioError catch (e) { + _parseErr(e); + } + return BaseModel.err(); + } + + Future getList( + String path, { + Map params, + }) async { + try { + Response res = await _dio.get(path, queryParameters: params); + BaseListModel baseListModel = BaseListModel.fromJson(res.data); + return baseListModel; + } on DioError catch (e) { + _parseErr(e); + } + return BaseListModel.err(); + } + + Future upload(String path, File file) async { + try { + Response res = await _dio.post(path, + data: FormData.fromMap({ + 'file': await MultipartFile.fromFile(file.path), + })); + BaseFileModel baseListModel = BaseFileModel.fromJson(res.data); + return baseListModel; + } on DioError catch (e) { + print(e); + } + return BaseFileModel.err(); + } + + Future> uploadFiles(List files, String api) async { + List urls = []; + if (files.isEmpty) { + return []; + } else { + for (var item in files) { + BaseFileModel model = await NetUtil().upload(api, item); + urls.add(model.url); + } + } + + return urls; + } + + _parseErr(DioError err) { + LoggerData.addData(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; + } + } + + _parseRequestError(BaseModel model, {bool showMessage = false}) { + // final userProvider = Provider.of(Get.context, listen: false); + // if (!model.status && model.message == '登录失效,请登录' && userProvider.isLogin) { + // userProvider.logout(); + // Get.offAll(SignInPage()); + // } + if (!model.status || showMessage) { + BotToast.showText(text: model.message); + } + } +} diff --git a/pubspec.lock b/pubspec.lock index 8d2f70a..e6c0a0c 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -136,6 +136,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "0.3.5" + dio: + dependency: "direct main" + description: + name: dio + url: "https://pub.flutter-io.cn" + source: hosted + version: "3.0.10" expandable: dependency: "direct main" description: @@ -176,6 +183,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "2.1.6" + flutter_highlight: + dependency: transitive + description: + name: flutter_highlight + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.6.0" flutter_localizations: dependency: "direct main" description: flutter @@ -226,6 +240,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "3.15.0" + highlight: + dependency: transitive + description: + name: highlight + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.6.0" http: dependency: transitive description: @@ -261,6 +282,13 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "0.16.1" + logger: + dependency: "direct main" + description: + name: logger + url: "https://pub.flutter-io.cn" + source: hosted + version: "0.9.4" matcher: dependency: transitive description: @@ -324,6 +352,22 @@ packages: url: "https://pub.flutter-io.cn" source: hosted version: "1.0.3" + power_logger: + dependency: "direct main" + description: + path: "." + ref: HEAD + resolved-ref: "1469b68b32bb5a7d02e6c89c5a9933ef77f738ad" + url: "http://192.168.2.201:8099/aku_fe/power_logger.git" + source: git + version: "0.0.1" + pretty_json: + dependency: transitive + description: + name: pretty_json + url: "https://pub.flutter-io.cn" + source: hosted + version: "1.1.0" provider: dependency: "direct main" description: diff --git a/pubspec.yaml b/pubspec.yaml index 818744e..3302ca3 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -47,6 +47,12 @@ dependencies: permission_handler: ^5.0.1+1 + dio: + + power_logger: + git: + url: http://192.168.2.201:8099/aku_fe/power_logger.git + logger: ^0.9.4 aku_ui: git: url: http://test.akuhotel.com:8099/aku_fe/aku_ui.git