clx_flutter_message.dart 5.1 KB
Newer Older
张国庆's avatar
张国庆 committed
1 2 3
import 'package:clx_flutter_message/util/string_util.dart';
import 'package:clx_flutter_message/util/toast_util.dart';
import 'package:flutter/material.dart';
4
import 'package:get/get.dart';
张国庆's avatar
张国庆 committed
5
import 'clx_flutter_message_platform_interface.dart';
张国庆's avatar
张国庆 committed
6
import 'common/constant.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 14 15 16 17 18 19 20 21 22
import 'core/socket/socket_io.dart';

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

MessageConfig messageConfig = MessageConfig();

张国庆's avatar
张国庆 committed
23
abstract class BaseMessageConfig with NotificationManager, NoticeManager {
张国庆's avatar
张国庆 committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
  //处理消息页面跳转
  void onJumpToMessagePage(String page, dynamic arguments);

  //获取未处理消息
  Future<List<MessageData>> getUnReadMessage();

  //获取未处理的公告
  Future<List<MessageData>> getUnReadNotice();

  // 标记消息为已读
  Future<bool> markReadAction(MessageData? message);

  // 删除消息
  Future<bool> removeMessageAction(MessageData? message);

  //忽略消息
  Future<bool> ignoreAction(MessageData? message);

  //标记消息已经展示
43
  void setMessageShowed(MessageData? message);
张国庆's avatar
张国庆 committed
44 45

  //连接websocket 获取及时消息
张国庆's avatar
张国庆 committed
46
  Future<void> connectWebSocket(BuildContext context) async {
张国庆's avatar
张国庆 committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
    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
66
      _handleMessage(message, context);
张国庆's avatar
张国庆 committed
67 68 69 70 71
    };

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

张国庆's avatar
张国庆 committed
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
  // 处理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
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
  // 处理消息跳转对应页面
  void gotoDealMessage(MessageData? message) {
    if (message?.canHand != true) {
      return;
    }
    if (message == null) {
      ToastUtils.showCenter("消息为空");
      return;
    }
    if (message.companyNo != messageConfig.companyNo) {
      ToastUtils.showCenter("当前公司和消息不匹配");
      return;
    }
    if (messageConfig.inAppAccessKey != message.accessKey) {
      ToastUtils.showCenter("当前角色和消息不匹配");
      return;
    }
    var data = message.textVo?.data;
    if (data == null) {
      ToastUtils.showCenter("消息数据为空");
      return;
    }
    var page = data['jumpPageAppUrl'];
    if (page == null) {
      ToastUtils.showCenter("消息跳转地址为空");
      return;
    }
    var arguments = data["jumpPageAppParam"];

    onJumpToMessagePage(page, arguments);
  }

129 130 131
  // 构造方法
  BaseMessageConfig() {
    //通过getx 注入  NotificationLayoutController
张国庆's avatar
张国庆 committed
132
    notificationLayoutController = NotificationLayoutController();
133
    //通过getx 注入  NoticeDialogWidgetController
张国庆's avatar
张国庆 committed
134
    noticeDialogWidgetController = NoticeDialogWidgetController();
135 136
  }

张国庆's avatar
张国庆 committed
137
  //刷新消息、获取未处理消息,重新连接websocket
张国庆's avatar
张国庆 committed
138
  Future<void> refreshMessage(BuildContext context) async {
张国庆's avatar
张国庆 committed
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    // 校验消息相关配置字段
    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
165 166 167 168 169 170 171 172 173
    connectWebSocket(context);

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

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