diff --git a/lib/main.dart b/lib/main.dart index c86c007..9f21035 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,9 @@ // Flutter imports: + import 'package:aku_community_manager/provider/message_provider.dart'; import 'package:aku_community_manager/tools/user_tool.dart'; +import 'package:aku_community_manager/utils/websocket/fier_dialog.dart'; +import 'package:aku_community_manager/utils/websocket/web_socket_util.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -26,6 +29,13 @@ void main() async { JPush jpush = new JPush(); const isProduct = const bool.fromEnvironment('ISPRODUCT'); DevUtil.setDev(!isProduct); + WebSocketUtil().initWebSocket( + // heartDuration: Duration(seconds: 5), + onError: (e) { + LoggerData.addData(e); + }, onReceiveMes: (message) async { + await FireDialog.fireAlert(message); + }); jpush.addEventHandler( // 接收通知回调方法。 onReceiveNotification: (Map? message) async { diff --git a/lib/provider/user_provider.dart b/lib/provider/user_provider.dart index bd93220..7bd66a3 100644 --- a/lib/provider/user_provider.dart +++ b/lib/provider/user_provider.dart @@ -4,6 +4,7 @@ import 'dart:io'; // Flutter imports: import 'package:aku_community_manager/models/user/user_info_model.dart'; import 'package:aku_community_manager/provider/message_provider.dart'; +import 'package:aku_community_manager/utils/websocket/web_socket_util.dart'; import 'package:flutter/material.dart'; // Package imports: @@ -20,7 +21,6 @@ import 'package:aku_community_manager/utils/network/base_file_model.dart'; import 'package:aku_community_manager/utils/network/base_model.dart'; import 'package:aku_community_manager/utils/network/net_util.dart'; - //登录状态管理 class UserProvider extends ChangeNotifier { bool _isLogin = false; @@ -35,6 +35,8 @@ class UserProvider extends ChangeNotifier { await HiveStore.appBox!.put('login', true); _profileModel = await updateProfile(); _infoModel = await updateUserInfo(); + WebSocketUtil().setUser(infoModel!.id.toString()); + WebSocketUtil().startWebSocket(); // await setCurrentHouse((_userDetailModel?.estateNames?.isEmpty ?? true) // ? '' // : _userDetailModel?.estateNames?.first); @@ -48,7 +50,8 @@ class UserProvider extends ChangeNotifier { ///更新用户profile Future updateProfile() async { - final messageProvider = Provider.of(Get.context!,listen: false); + final messageProvider = + Provider.of(Get.context!, listen: false); messageProvider.updateMessage(); BaseModel? model = await NetUtil().get(API.user.profile); if (model.data == null) @@ -72,6 +75,7 @@ class UserProvider extends ChangeNotifier { ///注销登录 logout() async { await NetUtil().get(API.auth.logout, showMessage: true); + WebSocketUtil().closeWebSocket(); NetUtil().logout(); _isLogin = false; await HiveStore.appBox!.delete('token'); @@ -90,10 +94,6 @@ class UserProvider extends ChangeNotifier { notifyListeners(); } - - - - ///修改昵称 setNickName(String name) { _infoModel!.nickName = name; diff --git a/lib/ui/login/login_sms_page.dart b/lib/ui/login/login_sms_page.dart index 3824515..fecb84b 100644 --- a/lib/ui/login/login_sms_page.dart +++ b/lib/ui/login/login_sms_page.dart @@ -10,6 +10,7 @@ import 'package:bot_toast/bot_toast.dart'; import 'package:dio/dio.dart'; import 'package:get/get.dart' hide Response; import 'package:pin_input_text_field/pin_input_text_field.dart'; +import 'package:power_logger/power_logger.dart'; import 'package:provider/provider.dart'; // Project imports: @@ -120,19 +121,24 @@ class _LoginSMSPageState extends State { onChanged: (text) async { if (text.length == 6) { Function cancel = BotToast.showLoading(); - Response response = await NetUtil().dio!.post( - API.auth.login, - data: {'tel': widget.phone, 'code': text}, - ); - if (response.data['status'] == true) { - await userProvider.setLogin(response.data['token']); + try { + Response response = await NetUtil().dio!.post( + API.auth.login, + data: {'tel': widget.phone, 'code': text}, + ); + if (response.data['status'] == true) { + await userProvider.setLogin(response.data['token']); + cancel(); + Get.offAll(HomePage()); + } else { + _textEditingController.clear(); + cancel(); + BotToast.showText(text: '登陆失败'); + Get.off(LoginPage()); + } + } catch (e) { + LoggerData.addData(e); cancel(); - Get.offAll(HomePage()); - } else { - _textEditingController.clear(); - cancel(); - BotToast.showText(text: '登陆失败'); - Get.off(LoginPage()); } } diff --git a/lib/ui/splash/splash_page.dart b/lib/ui/splash/splash_page.dart index e2165ba..7f7f980 100644 --- a/lib/ui/splash/splash_page.dart +++ b/lib/ui/splash/splash_page.dart @@ -64,7 +64,7 @@ class _SplashPageState extends State { Future.delayed(Duration(milliseconds: 2000), () async { await _initOp(); - Get.off(HomePage()); + Get.off(()=>HomePage()); }); } diff --git a/lib/utils/jpush_message_parse.dart b/lib/utils/jpush_message_parse.dart index 0f4301c..fe4df87 100644 --- a/lib/utils/jpush_message_parse.dart +++ b/lib/utils/jpush_message_parse.dart @@ -1,14 +1,11 @@ // Dart imports: import 'dart:convert'; +import 'package:aku_community_manager/utils/websocket/fier_dialog.dart'; + // Flutter imports: -import 'package:flutter/cupertino.dart'; -import 'package:flutter/material.dart'; // Package imports: -import 'package:flutter_screenutil/flutter_screenutil.dart'; -import 'package:get/get.dart'; -import 'package:velocity_x/velocity_x.dart'; class JpushMessageParse { final Map message; @@ -28,36 +25,12 @@ class JpushMessageParse { type = _innerMap['type'] ?? '0'; switch (type) { case '1': - await fireAlert(subTitle!); + await FireDialog.fireAlert(subTitle!); break; default: } } } - Future fireAlert(String subTitle) async { - await Get.dialog( - CupertinoAlertDialog( - title: Text('发现火灾!请立刻组织疏散人群!'), - content: Column( - children: [ - Text(subTitle), - 10.w.heightBox, - Icon( - CupertinoIcons.bell_fill, - color: Colors.red, - size: 48.w, - ), - ], - ), - actions: [ - CupertinoDialogAction( - child: Text('确认'), - onPressed: () => Get.back(), - ), - ], - ), - barrierDismissible: false, - ); - } + } diff --git a/lib/utils/websocket/fier_dialog.dart b/lib/utils/websocket/fier_dialog.dart new file mode 100644 index 0000000..95dd0fd --- /dev/null +++ b/lib/utils/websocket/fier_dialog.dart @@ -0,0 +1,34 @@ + +import 'package:flutter/cupertino.dart'; +import 'package:flutter/material.dart'; +import 'package:get/get.dart'; +import 'package:flutter_screenutil/flutter_screenutil.dart'; +import 'package:velocity_x/velocity_x.dart'; + +class FireDialog { + static Future fireAlert(String subTitle) async { + await Get.dialog( + CupertinoAlertDialog( + title: Text('发现火灾!请立刻组织疏散人群!'), + content: Column( + children: [ + Text(subTitle), + 10.w.heightBox, + Icon( + CupertinoIcons.bell_fill, + color: Colors.red, + size: 48.w, + ), + ], + ), + actions: [ + CupertinoDialogAction( + child: Text('确认'), + onPressed: () => Get.back(), + ), + ], + ), + barrierDismissible: false, + ); + } +} \ No newline at end of file diff --git a/pubspec.lock b/pubspec.lock index 6372c83..61f08dc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1044,7 +1044,7 @@ packages: source: hosted version: "3.0.1" web_socket_channel: - dependency: transitive + dependency: "direct main" description: name: web_socket_channel url: "https://pub.flutter-io.cn" diff --git a/pubspec.yaml b/pubspec.yaml index a290b5d..cdeffbb 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -28,6 +28,8 @@ dependencies: qr_code_scanner: ^0.4.0-nullsafety.0 #toast bot_toast: ^4.0.1 + #websocket + web_socket_channel: ^2.1.0 extended_text: ^5.0.4