clx_flutter_message.dart 4.8 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 76 77 78 79 80 81 82 83
  // 处理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();
  }

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

113
    return onJumpToMessagePage(page, arguments);
张国庆's avatar
张国庆 committed
114 115
  }

116 117
  // 构造方法
  BaseMessageConfig() {
张国庆's avatar
张国庆 committed
118 119
    notificationLayoutController = NotificationLayoutController();
    noticeDialogWidgetController = NoticeDialogWidgetController();
120 121
  }

张国庆's avatar
张国庆 committed
122
  //刷新消息、获取未处理消息,重新连接websocket
张国庆's avatar
张国庆 committed
123
  Future<void> refreshMessage(BuildContext context) async {
张国庆's avatar
张国庆 committed
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
    // 校验消息相关配置字段
    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;
    }
张国庆's avatar
张国庆 committed
150 151 152 153 154 155 156 157 158
    connectWebSocket(context);

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

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