1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import 'package:clx_flutter_message/util/string_util.dart';
import 'package:clx_flutter_message/util/toast_util.dart';
import 'package:flutter/material.dart';
import 'clx_flutter_message_platform_interface.dart';
import 'common/constant.dart';
import 'core/api/message_net.dart';
import 'core/model/message_config.dart';
import 'core/model/message_data.dart';
import 'core/notice/notice_dialog_widget.dart';
import 'core/notice/notice_manager.dart';
import 'core/notification/notification_layout/notification_layout_widget.dart';
import 'core/notification/notification_manager.dart';
import 'core/socket/socket_io.dart';
export 'core/model/message_config.dart';
export 'core/model/message_data.dart';
export 'core/notice/notice_dialog_widget.dart';
export 'core/notification/notification_layout/notification_layout_widget.dart';
class ClxFlutterMessage {
Future<String?> getPlatformVersion() {
return ClxFlutterMessagePlatform.instance.getPlatformVersion();
}
}
MessageConfig messageConfig = MessageConfig();
abstract class BaseMessageConfig
with NotificationManager, NoticeManager, MessageNet {
//处理消息页面跳转
Future? onJumpToMessagePage(String page, dynamic arguments);
//连接websocket 获取及时消息
Future<void> connectWebSocket(BuildContext context) async {
String userNo = messageConfig.userKey;
String url = messageConfig.webSocketUrl;
String connectId = '${DateTime.now().microsecondsSinceEpoch}-$userNo';
var params = {
'connectId': connectId,
'productCode': messageConfig.productCode,
'functionKey': messageConfig.inAppAccessKey,
'userKey': userNo,
'companyKey': messageConfig.companyNo
};
var headers = {
'token': messageConfig.accessToken,
'product-code': messageConfig.productCode,
};
Socket.getInstance().onReceivedMessage = (message) {
// 处理消息
_handleMessage(message, context);
};
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();
}
// 处理消息跳转对应页面
Future? gotoDealMessage(MessageData? message) {
if (message?.canHand != true) {
return null;
}
if (message == null) {
ToastUtils.showCenter("消息为空");
return null;
}
if (message.companyNo != messageConfig.companyNo) {
ToastUtils.showCenter("当前公司和消息不匹配");
return null;
}
if (messageConfig.inAppAccessKey != message.accessKey) {
ToastUtils.showCenter("当前角色和消息不匹配");
return null;
}
var data = message.textVo?.data;
if (data == null) {
ToastUtils.showCenter("消息数据为空");
return null;
}
var page = data['jumpPageAppUrl'];
if (page == null) {
ToastUtils.showCenter("消息跳转地址为空");
return null;
}
var arguments = data["jumpPageAppParam"];
return onJumpToMessagePage(page, arguments);
}
// 构造方法
BaseMessageConfig() {
notificationLayoutController = NotificationLayoutController();
noticeDialogWidgetController = NoticeDialogWidgetController();
}
//刷新消息、获取未处理消息,重新连接websocket
Future<void> refreshMessage(BuildContext context) async {
// 校验消息相关配置字段
if (StringUtil.isEmpty(messageConfig.userKey)) {
ToastUtils.showCenter("userNo不能为空");
return;
}
if (StringUtil.isEmpty(messageConfig.companyNo)) {
ToastUtils.showCenter("companyNo不能为空");
return;
}
if (StringUtil.isEmpty(messageConfig.accessToken)) {
ToastUtils.showCenter("登录token不能为空");
return;
}
if (StringUtil.isEmpty(messageConfig.inAppAccessKey)) {
ToastUtils.showCenter("inAppAccessKey不能为空");
return;
}
if (StringUtil.isEmpty(messageConfig.productCode)) {
ToastUtils.showCenter("productCode不能为空");
return;
}
if (StringUtil.isEmpty(messageConfig.webSocketUrl)) {
ToastUtils.showCenter("webSocketUrl不能为空");
return;
}
connectWebSocket(context);
List<MessageData> nList = await getUnReadMessage();
List<MessageData> aList = await getUnReadNotice();
// 处理消息
setNotification(nList);
// 处理公告
setNotice(aList);
}
}