From 3f042fec781ca6fd0a55a6675ad8aba8516a6add Mon Sep 17 00:00:00 2001 From: qmqz Date: Fri, 21 Feb 2025 19:24:59 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=20token=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/base/database/DataBase.dart | 3 +++ lib/base/database/impl/MongoDb.dart | 5 +++++ lib/base/discovery/impl/NacosDiscovery.dart | 6 ++++++ lib/base/http/TraceDio.dart | 18 +++++++++++++++--- lib/base/websocket/WebSocket.dart | 9 ++++++++- 5 files changed, 37 insertions(+), 4 deletions(-) diff --git a/lib/base/database/DataBase.dart b/lib/base/database/DataBase.dart index 2fc11c8..2850d23 100644 --- a/lib/base/database/DataBase.dart +++ b/lib/base/database/DataBase.dart @@ -22,6 +22,9 @@ abstract class DataBase { // 执行删除操作 Future delete(String table, dynamic condition); + + //查询数量 + Future count(String tbale, {dynamic condition}); } class DataBaseConfig { diff --git a/lib/base/database/impl/MongoDb.dart b/lib/base/database/impl/MongoDb.dart index be28bf2..01cc0c1 100644 --- a/lib/base/database/impl/MongoDb.dart +++ b/lib/base/database/impl/MongoDb.dart @@ -50,4 +50,9 @@ class MongoDb implements DataBase { await getCollection(table) .update(condition, data, multiUpdate: multiUpdate); } + + @override + Future count(String tbale, {dynamic condition}) async { + return await getCollection(tbale).count(condition); + } } diff --git a/lib/base/discovery/impl/NacosDiscovery.dart b/lib/base/discovery/impl/NacosDiscovery.dart index 2c3d0a4..958e3f5 100644 --- a/lib/base/discovery/impl/NacosDiscovery.dart +++ b/lib/base/discovery/impl/NacosDiscovery.dart @@ -60,6 +60,12 @@ class NacosDiscovery implements Discovery { 'namespaceId': config.namespaceId, }); print(rr); + //判断心跳是否发送成功 + if(rr.data["code"]==20404){ + //实例未注册 重新注册实例 + healthCheck = false; + registerInstance(serviceName, ip, port,groupName: groupName); + } } catch (e) { print(e); } diff --git a/lib/base/http/TraceDio.dart b/lib/base/http/TraceDio.dart index c2dd6f9..db24773 100644 --- a/lib/base/http/TraceDio.dart +++ b/lib/base/http/TraceDio.dart @@ -9,6 +9,7 @@ class TraceDio { final Uuid uuid = Uuid(); static late TraceDio _traceDio; + String? token; TraceDio(Logger logger) : _dio = Dio(), @@ -48,6 +49,9 @@ class TraceDio { tag: "DIO", traceId: traceId, spanId: spanId); + if (response.headers["token"] != null) { + token = response.headers["token"]?[0]; + } return handler.next(response); // 继续处理响应 }, onError: (DioException e, handler) { @@ -71,21 +75,29 @@ class TraceDio { Map? getHeader( {Map? headers, sf.Request? request}) { + headers ??= {}; if (request != null) { return { + ...headers, "X-Trace-ID": request.context['request_trace_id'] as String, "X-Span-ID": request.context['request_span_id'] as String }; } - return null; + if (token != null) { + headers["token"] = token; + } + return headers; } // 发起 GET 请求 Future get(String url, - {Map? queryParameters, sf.Request? request}) async { + {Map? queryParameters, + sf.Request? request, + ResponseType? responseType}) async { return await _dio.get(url, queryParameters: queryParameters, - options: Options(headers: getHeader(request: request))); + options: Options( + headers: getHeader(request: request), responseType: responseType)); } // 发起 POST 请求 diff --git a/lib/base/websocket/WebSocket.dart b/lib/base/websocket/WebSocket.dart index 432263d..77e3259 100644 --- a/lib/base/websocket/WebSocket.dart +++ b/lib/base/websocket/WebSocket.dart @@ -33,6 +33,11 @@ class WebSocket { final channel = WebSocketChannel.connect(wsUrl); try { await channel.ready; + Future.delayed(Duration(seconds: 2), () { + if (_config.onOpen != null) { + _config.onOpen!(); + } + }); } catch (e) { print(e); //连接失败 @@ -73,6 +78,8 @@ class WebSocketConfig { String host; bool reConnect; void Function(String) messgae; + void Function()? onOpen; - WebSocketConfig(this.host, this.messgae, {this.reConnect = true}); + WebSocketConfig(this.host, this.messgae, + {this.reConnect = true, this.onOpen}); }