提交 74cec6fc authored 作者: 张国庆's avatar 张国庆

增加网络请求能力,消息接口请求抽取到插件

上级 1b3f1fc2
...@@ -3,6 +3,7 @@ import 'package:clx_flutter_message/util/toast_util.dart'; ...@@ -3,6 +3,7 @@ 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 'common/constant.dart';
import 'core/api/message_net.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_dialog_widget.dart'; import 'core/notice/notice_dialog_widget.dart';
...@@ -23,28 +24,11 @@ class ClxFlutterMessage { ...@@ -23,28 +24,11 @@ class ClxFlutterMessage {
MessageConfig messageConfig = MessageConfig(); MessageConfig messageConfig = MessageConfig();
abstract class BaseMessageConfig with NotificationManager, NoticeManager { abstract class BaseMessageConfig with NotificationManager, NoticeManager,MessageNet{
//处理消息页面跳转 //处理消息页面跳转
void onJumpToMessagePage(String page, dynamic arguments); 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);
//标记消息已经展示
void setMessageShowed(MessageData? message);
//连接websocket 获取及时消息 //连接websocket 获取及时消息
Future<void> connectWebSocket(BuildContext context) async { Future<void> connectWebSocket(BuildContext context) async {
String userNo = messageConfig.userKey; String userNo = messageConfig.userKey;
......
// Dio 网络请求工具类
import 'dart:convert';
import 'package:clx_flutter_message/clx_flutter_message.dart';
// ignore: depend_on_referenced_packages
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
class Api {
static final dio = messageConfig.dio;
// 构建网络请求同步请求
Future<dynamic> requestSync({
required String requestUrl,
required Method method,
Map<String, dynamic>? queryParameters,
Object? data,
}) async {
try {
var response = await dio?.request(
requestUrl,
queryParameters: queryParameters,
data: data,
options: _checkOptions(method.value),
);
try {
var data = response?.data;
if (data is Map) {
return data;
} else if (data is String) {
return _jsonDecode(data);
}
return data;
} catch (e) {
debugPrint("数据解析错误:${e.toString()}");
return {"code": 400, "message": "数据解析错误"};
}
} on DioException catch (e) {
if (e.response != null) {
debugPrint(
"请求错误:${e.response!.statusCode} ${e.response!.requestOptions.path}");
} else {
// Something happened in setting up or sending the request that triggered an Error
debugPrint("请求错误:${e.error}");
}
return {"code": 500, "message": "服务器请求错误"};
}
}
// Options
Options _checkOptions(String method) {
Options options = Options();
options.method = method;
return options;
}
// jsonDecode
Map<String, dynamic> _jsonDecode(String json) {
return jsonDecode(json);
}
}
enum Method {
get,
post,
put,
patch,
delete,
}
extension MethodExtension on Method {
String get value => ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'][index];
}
import 'package:clx_flutter_message/core/api/api.dart';
import '../../clx_flutter_message.dart';
mixin MessageNet {
final api = Api();
//获取未处理消息
Future<List<MessageData>> getUnReadMessage() async {
var result = await _fetchMessage(2);
if (result['code'] == 0) {
var data = result['data']?['records'];
if (data != null && data is List) {
return Future(() => data.map((e) => MessageData.fromJson(e)).toList());
}
}
return Future(() => List.empty());
}
//获取未处理的公告
Future<List<MessageData>> getUnReadNotice() async {
var result = await _fetchMessage(1);
if (result['code'] == 0) {
var data = result['data']?['records'];
if (data != null && data is List) {
return Future(() => data.map((e) => MessageData.fromJson(e)).toList());
}
}
return Future(() => List.empty());
}
// 标记消息为已读
Future<bool> markReadAction(MessageData? message) async {
if (message?.status == 10) return Future.value(true);
var result = await api.requestSync(
method: Method.put,
requestUrl: '/message-in-app/message/record/updateRecord',
queryParameters: {
'id': message?.id,
'status': 10,
});
return result['code'] == 0;
}
// 删除消息
Future<bool> removeMessageAction(MessageData? message) async {
var result = await api.requestSync(
method: Method.put,
requestUrl: '/message-in-app/message/record/deleteMessage',
queryParameters: {
'id': message?.id,
});
return result['code'] == 0;
}
//忽略消息
Future<bool> ignoreAction(MessageData? message) {
return markReadAction(message);
}
//标记消息已经展示
void setMessageShowed(MessageData? message) {
if (message == null) {
return;
}
api.requestSync(
method: Method.put,
requestUrl: '/message-in-app/message/record/updateRecordShow',
queryParameters: {'id': message.id, 'showStatus': 1});
}
Future<dynamic> _fetchMessage(showTypes) async {
var result = await api.requestSync(
requestUrl: '/message-in-app/message/record/pageShowMessage',
method: Method.post,
data: {
'pageNum': 1,
'pageSize': 100,
'accessKey': messageConfig.inAppAccessKey,
'showTypes': [showTypes]
},
);
return result;
}
}
import 'package:clx_flutter_message/clx_flutter_message.dart'; import 'package:clx_flutter_message/clx_flutter_message.dart';
// ignore: depend_on_referenced_packages
import 'package:dio/dio.dart';
class MessageConfig { class MessageConfig {
// websocket 连接地址 // websocket 连接地址
...@@ -15,4 +17,6 @@ class MessageConfig { ...@@ -15,4 +17,6 @@ class MessageConfig {
String accessToken = ""; String accessToken = "";
BaseMessageConfig? messageManagement; BaseMessageConfig? messageManagement;
Dio? dio;
} }
...@@ -18,11 +18,11 @@ dependencies: ...@@ -18,11 +18,11 @@ dependencies:
cached_network_image: ^3.3.0 cached_network_image: ^3.3.0
fluttertoast: 8.2.4 fluttertoast: 8.2.4
get: ^4.6.6 get: ^4.6.6
dev_dependencies: dev_dependencies:
flutter_test: flutter_test:
sdk: flutter sdk: flutter
flutter_lints: ^2.0.0 flutter_lints: ^2.0.0
dio: ^5.7.0
# For information on the generic Dart part of this file, see the # For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec # following page: https://dart.dev/tools/pub/pubspec
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论