clx_flutter_message.dart 5.2 KB
Newer Older
张国庆's avatar
张国庆 committed
1 2 3 4
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';
张国庆's avatar
张国庆 committed
5
import 'common/constant.dart';
6
import 'core/api/message_net.dart';
张国庆's avatar
张国庆 committed
7 8
import 'core/model/message_config.dart';
import 'core/model/message_data.dart';
9
import 'core/notice/notice_dialog_widget.dart';
张国庆's avatar
张国庆 committed
10
import 'core/notice/notice_manager.dart';
11
import 'core/notification/notification_layout/notification_layout_widget.dart';
张国庆's avatar
张国庆 committed
12
import 'core/notification/notification_manager.dart';
张国庆's avatar
张国庆 committed
13
import 'core/socket/socket_io.dart';
张国庆's avatar
张国庆 committed
14 15 16 17
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';
张国庆's avatar
张国庆 committed
18 19 20 21 22 23 24 25 26

class ClxFlutterMessage {
  Future<String?> getPlatformVersion() {
    return ClxFlutterMessagePlatform.instance.getPlatformVersion();
  }
}

MessageConfig messageConfig = MessageConfig();

27 28
abstract class BaseMessageConfig
    with NotificationManager, NoticeManager, MessageNet {
张国庆's avatar
张国庆 committed
29
  //处理消息页面跳转
30
  Future? onJumpToMessagePage(String page, dynamic arguments);
张国庆's avatar
张国庆 committed
31 32

  //连接websocket 获取及时消息
张国庆's avatar
张国庆 committed
33
  Future<void> connectWebSocket(BuildContext context) async {
张国庆's avatar
张国庆 committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    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) {
      // 处理消息
张国庆's avatar
张国庆 committed
53
      _handleMessage(message, context);
张国庆's avatar
张国庆 committed
54 55 56 57 58
    };

    await Socket.getInstance().connect(url, params, headers);
  }

张国庆's avatar
张国庆 committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  // 处理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());
    }
张国庆's avatar
张国庆 committed
76 77

    handleMessage(data);
张国庆's avatar
张国庆 committed
78 79
  }

张国庆's avatar
张国庆 committed
80 81 82
  // 处理长连接消息
  void handleMessage(dynamic data);

张国庆's avatar
张国庆 committed
83 84 85 86 87 88
  close() async {
    clear();
    clearNoticeDialog();
    return await Socket.getInstance().close();
  }

张国庆's avatar
张国庆 committed
89
  // 处理消息跳转对应页面
90
  Future? gotoDealMessage(MessageData? message) {
张国庆's avatar
张国庆 committed
91
    if (message?.canHand != true) {
92
      return null;
张国庆's avatar
张国庆 committed
93 94 95
    }
    if (message == null) {
      ToastUtils.showCenter("消息为空");
96
      return null;
张国庆's avatar
张国庆 committed
97 98 99
    }
    if (message.companyNo != messageConfig.companyNo) {
      ToastUtils.showCenter("当前公司和消息不匹配");
100
      return null;
张国庆's avatar
张国庆 committed
101 102 103
    }
    if (messageConfig.inAppAccessKey != message.accessKey) {
      ToastUtils.showCenter("当前角色和消息不匹配");
104
      return null;
张国庆's avatar
张国庆 committed
105 106 107 108
    }
    var data = message.textVo?.data;
    if (data == null) {
      ToastUtils.showCenter("消息数据为空");
109
      return null;
张国庆's avatar
张国庆 committed
110 111 112 113
    }
    var page = data['jumpPageAppUrl'];
    if (page == null) {
      ToastUtils.showCenter("消息跳转地址为空");
114
      return null;
张国庆's avatar
张国庆 committed
115 116 117
    }
    var arguments = data["jumpPageAppParam"];

118
    return onJumpToMessagePage(page, arguments);
张国庆's avatar
张国庆 committed
119 120
  }

121 122
  // 构造方法
  BaseMessageConfig() {
张国庆's avatar
张国庆 committed
123 124
    notificationLayoutController = NotificationLayoutController();
    noticeDialogWidgetController = NoticeDialogWidgetController();
125 126
  }

张国庆's avatar
张国庆 committed
127
  // 刷新消息、获取未处理消息,重新连接websocket
张国庆's avatar
张国庆 committed
128
  Future<void> refreshMessage(BuildContext context) async {
张国庆's avatar
张国庆 committed
129
    // 校验消息相关配置字段
张国庆's avatar
张国庆 committed
130 131 132
    final validationResult = validateMessageConfig();
    if (validationResult != null) {
      ToastUtils.showCenter(validationResult);
张国庆's avatar
张国庆 committed
133 134 135
      return;
    }

张国庆's avatar
张国庆 committed
136 137 138 139 140 141 142 143 144
    connectWebSocket(context);

    List<MessageData> nList = await getUnReadMessage();
    List<MessageData> aList = await getUnReadNotice();

    // 处理消息
    setNotification(nList);
    // 处理公告
    setNotice(aList);
张国庆's avatar
张国庆 committed
145
  }
张国庆's avatar
张国庆 committed
146

147 148 149 150 151 152 153 154 155 156 157 158
  // 标记全部消息已读
  Future<bool> markAllRead() async {
    var res = await confirmCleanMessage();
    return res['code'] == 0;
  }

  // 获取未读消息数量

  Future<dynamic> getUnReadCount() {
    return getUnReadMessageCount();
  }

张国庆's avatar
张国庆 committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
  // 校验消息配置字段
  String? validateMessageConfig() {
    if (StringUtil.isEmpty(messageConfig.userKey)) {
      return "userNo不能为空";
    }
    if (StringUtil.isEmpty(messageConfig.companyNo)) {
      return "companyNo不能为空";
    }
    if (StringUtil.isEmpty(messageConfig.accessToken)) {
      return "登录token不能为空";
    }
    if (StringUtil.isEmpty(messageConfig.inAppAccessKey)) {
      return "inAppAccessKey不能为空";
    }
    if (StringUtil.isEmpty(messageConfig.productCode)) {
      return "productCode不能为空";
    }
    if (StringUtil.isEmpty(messageConfig.webSocketUrl)) {
      return "webSocketUrl不能为空";
    }
    return null; // 所有校验通过
  }
张国庆's avatar
张国庆 committed
181
}