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}); }