提交 946b4ca0 authored 作者: 张国庆's avatar 张国庆

增加接入方法

上级 8cf0f6fd
import 'package:clx_flutter_message/util/string_util.dart'; import 'package:clx_flutter_message/util/string_util.dart';
import 'package:clx_flutter_message/util/toast_util.dart'; import 'package:clx_flutter_message/util/toast_util.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'clx_flutter_message_platform_interface.dart'; import 'clx_flutter_message_platform_interface.dart';
import 'common/constant.dart';
import 'core/model/message_config.dart'; import 'core/model/message_config.dart';
import 'core/model/message_data.dart'; import 'core/model/message_data.dart';
import 'core/notice/notice_manager.dart';
import 'core/notification/notification_manager.dart'; import 'core/notification/notification_manager.dart';
import 'core/socket/socket_io.dart'; import 'core/socket/socket_io.dart';
...@@ -16,7 +17,7 @@ class ClxFlutterMessage { ...@@ -16,7 +17,7 @@ class ClxFlutterMessage {
MessageConfig messageConfig = MessageConfig(); MessageConfig messageConfig = MessageConfig();
abstract class BaseMessageConfig with NotificationManager { abstract class BaseMessageConfig with NotificationManager, NoticeManager {
//处理消息页面跳转 //处理消息页面跳转
void onJumpToMessagePage(String page, dynamic arguments); void onJumpToMessagePage(String page, dynamic arguments);
...@@ -39,7 +40,7 @@ abstract class BaseMessageConfig with NotificationManager { ...@@ -39,7 +40,7 @@ abstract class BaseMessageConfig with NotificationManager {
Future<bool> setMessageShowed(MessageData? message); Future<bool> setMessageShowed(MessageData? message);
//连接websocket 获取及时消息 //连接websocket 获取及时消息
Future<void> connectWebSocket() async { Future<void> connectWebSocket(BuildContext context) async {
String userNo = messageConfig.userKey; String userNo = messageConfig.userKey;
String url = messageConfig.webSocketUrl; String url = messageConfig.webSocketUrl;
String connectId = '${DateTime.now().microsecondsSinceEpoch}-$userNo'; String connectId = '${DateTime.now().microsecondsSinceEpoch}-$userNo';
...@@ -59,11 +60,37 @@ abstract class BaseMessageConfig with NotificationManager { ...@@ -59,11 +60,37 @@ abstract class BaseMessageConfig with NotificationManager {
Socket.getInstance().onReceivedMessage = (message) { Socket.getInstance().onReceivedMessage = (message) {
// 处理消息 // 处理消息
_handleMessage(message, context);
}; };
await Socket.getInstance().connect(url, params, headers); await Socket.getInstance().connect(url, params, headers);
} }
// 处理socket消息
void _handleMessage(dynamic data, context) {
var mBizType = data?['bizType'];
if (mBizType == bizType) {
var body = MessageData.fromJson(data?['body']);
if (body.showType == '1') {
/// 首页公告
insertNotice(body);
} else if (body.showType == '2') {
/// 全局通知
insertNotification(body);
showNotification(context);
}
} else {
debugPrint('未知消息类型');
debugPrint(data.toString());
}
}
close() async {
clear();
clearNoticeDialog();
return await Socket.getInstance().close();
}
// 处理消息跳转对应页面 // 处理消息跳转对应页面
void gotoDealMessage(MessageData? message) { void gotoDealMessage(MessageData? message) {
if (message?.canHand != true) { if (message?.canHand != true) {
...@@ -97,7 +124,7 @@ abstract class BaseMessageConfig with NotificationManager { ...@@ -97,7 +124,7 @@ abstract class BaseMessageConfig with NotificationManager {
} }
//刷新消息、获取未处理消息,重新连接websocket //刷新消息、获取未处理消息,重新连接websocket
void refreshMessage(BuildContext context) { Future<void> refreshMessage(BuildContext context) async {
// 校验消息相关配置字段 // 校验消息相关配置字段
if (StringUtil.isEmpty(messageConfig.userKey)) { if (StringUtil.isEmpty(messageConfig.userKey)) {
ToastUtils.showCenter("userNo不能为空"); ToastUtils.showCenter("userNo不能为空");
...@@ -124,9 +151,15 @@ abstract class BaseMessageConfig with NotificationManager { ...@@ -124,9 +151,15 @@ abstract class BaseMessageConfig with NotificationManager {
ToastUtils.showCenter("webSocketUrl不能为空"); ToastUtils.showCenter("webSocketUrl不能为空");
return; return;
} }
connectWebSocket(context);
List<MessageData> nList = await getUnReadMessage();
List<MessageData> aList = await getUnReadNotice();
// 处理消息
setNotification(nList);
// 处理公告
setNotice(aList);
getUnReadMessage();
getUnReadNotice();
connectWebSocket();
} }
} }
...@@ -4,8 +4,6 @@ const btnBgColor = Color(0xFF73BC8B); ...@@ -4,8 +4,6 @@ const btnBgColor = Color(0xFF73BC8B);
const Color overlayColor = Color(0x33FFFFFF); const Color overlayColor = Color(0x33FFFFFF);
const Color appMain = Color(0xFF73BC8B); const Color appMain = Color(0xFF73BC8B);
const String companyNo = "待定";
const String inAppAccessKey = "待定"; const String bizType = "in_app";
const String socketAddress = "待定";
/// 消息组件配置
abstract class MessageConfig {
/// 消息组件配置
const MessageConfig();
/// 消息跳转页面配置方法
void onJumpToMessagePage(String page, dynamic arguments);
}
\ No newline at end of file
...@@ -202,7 +202,7 @@ class NoticeDialogWidgetController extends GetxController { ...@@ -202,7 +202,7 @@ class NoticeDialogWidgetController extends GetxController {
/// 插入公告 /// 插入公告
void insertNotice(MessageData data) { void insertNotice(MessageData data) {
_insertNode(data); _insertNode(data);
update(); _initData();
} }
/// 处理公告 /// 处理公告
...@@ -224,13 +224,13 @@ class NoticeDialogWidgetController extends GetxController { ...@@ -224,13 +224,13 @@ class NoticeDialogWidgetController extends GetxController {
/// 隐藏当前的 /// 隐藏当前的
showDialog = false; showDialog = false;
update(); _initData();
/// 显示下一个 /// 显示下一个
if (headNode != null) { if (headNode != null) {
Future.delayed(const Duration(milliseconds: 300)).then((value) { Future.delayed(const Duration(milliseconds: 300)).then((value) {
showDialog = true; showDialog = true;
update(); _initData();
}); });
} }
} }
...@@ -240,13 +240,13 @@ class NoticeDialogWidgetController extends GetxController { ...@@ -240,13 +240,13 @@ class NoticeDialogWidgetController extends GetxController {
_insertNode(MessageData.fromJson(element)); _insertNode(MessageData.fromJson(element));
} }
update(); _initData();
} }
void clear() { void clear() {
headNode = null; headNode = null;
showDialog = false; showDialog = false;
update(); _initData();
} }
void ignore() { void ignore() {
...@@ -261,19 +261,9 @@ class NoticeDialogWidgetController extends GetxController { ...@@ -261,19 +261,9 @@ class NoticeDialogWidgetController extends GetxController {
next(); next();
} }
// @override
// void onInit() {
// super.onInit();
// }
@override @override
void onReady() { void onReady() {
super.onReady(); super.onReady();
_initData(); _initData();
} }
// @override
// void onClose() {
// super.onClose();
// }
} }
import 'package:clx_flutter_message/core/notice/notice_dialog_widget.dart';
import 'package:clx_flutter_message/util/extension.dart';
import '../model/message_data.dart';
mixin NoticeManager {
final NoticeDialogWidgetController? _controller =
findOtherLogic<NoticeDialogWidgetController>();
void insertNotice(MessageData data) {
_controller?.insertNotice(data);
}
void clearNoticeDialog() {
_controller?.clear();
}
void setNotice(List<dynamic> data) {
_controller?.setNotice(data);
}
}
import 'package:get/get.dart';
class NoteController extends GetxController {
NoteController();
_initData() {
update(["note"]);
}
void onTap() {}
// @override
// void onInit() {
// super.onInit();
// }
@override
void onReady() {
super.onReady();
_initData();
}
// @override
// void onClose() {
// super.onClose();
// }
}
library note;
export './controller.dart';
export './view.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'index.dart';
class NotePage extends GetView<NoteController> {
const NotePage({super.key});
// 主视图
Widget _buildView() {
return const Center(
child: Text("NotePage"),
);
}
@override
Widget build(BuildContext context) {
return GetBuilder<NoteController>(
init: NoteController(),
id: "note",
builder: (_) {
return Scaffold(
appBar: AppBar(title: const Text("note")),
body: SafeArea(
child: _buildView(),
),
);
},
);
}
}
...@@ -174,10 +174,6 @@ class NotificationLayoutController extends GetxController { ...@@ -174,10 +174,6 @@ class NotificationLayoutController extends GetxController {
update(); update();
} }
// @override
// void onInit() {
// super.onInit();
// }
@override @override
void onReady() { void onReady() {
...@@ -185,8 +181,4 @@ class NotificationLayoutController extends GetxController { ...@@ -185,8 +181,4 @@ class NotificationLayoutController extends GetxController {
_initData(); _initData();
} }
// @override
// void onClose() {
// super.onClose();
// }
} }
import 'package:get/get.dart';
S? findOtherLogic<S>({String? tag}) {
if (Get.isRegistered<S>(tag: tag)) {
return Get.find<S>(tag: tag);
}
return null;
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论