新增 token自动提交

This commit is contained in:
2025-02-21 19:24:59 +08:00
parent 7fdaf42695
commit 3f042fec78
5 changed files with 37 additions and 4 deletions

View File

@@ -22,6 +22,9 @@ abstract class DataBase {
// 执行删除操作 // 执行删除操作
Future<void> delete(String table, dynamic condition); Future<void> delete(String table, dynamic condition);
//查询数量
Future<int> count(String tbale, {dynamic condition});
} }
class DataBaseConfig { class DataBaseConfig {

View File

@@ -50,4 +50,9 @@ class MongoDb implements DataBase {
await getCollection(table) await getCollection(table)
.update(condition, data, multiUpdate: multiUpdate); .update(condition, data, multiUpdate: multiUpdate);
} }
@override
Future<int> count(String tbale, {dynamic condition}) async {
return await getCollection(tbale).count(condition);
}
} }

View File

@@ -60,6 +60,12 @@ class NacosDiscovery implements Discovery {
'namespaceId': config.namespaceId, 'namespaceId': config.namespaceId,
}); });
print(rr); print(rr);
//判断心跳是否发送成功
if(rr.data["code"]==20404){
//实例未注册 重新注册实例
healthCheck = false;
registerInstance(serviceName, ip, port,groupName: groupName);
}
} catch (e) { } catch (e) {
print(e); print(e);
} }

View File

@@ -9,6 +9,7 @@ class TraceDio {
final Uuid uuid = Uuid(); final Uuid uuid = Uuid();
static late TraceDio _traceDio; static late TraceDio _traceDio;
String? token;
TraceDio(Logger logger) TraceDio(Logger logger)
: _dio = Dio(), : _dio = Dio(),
@@ -48,6 +49,9 @@ class TraceDio {
tag: "DIO", tag: "DIO",
traceId: traceId, traceId: traceId,
spanId: spanId); spanId: spanId);
if (response.headers["token"] != null) {
token = response.headers["token"]?[0];
}
return handler.next(response); // 继续处理响应 return handler.next(response); // 继续处理响应
}, },
onError: (DioException e, handler) { onError: (DioException e, handler) {
@@ -71,21 +75,29 @@ class TraceDio {
Map<String, dynamic>? getHeader( Map<String, dynamic>? getHeader(
{Map<String, dynamic>? headers, sf.Request? request}) { {Map<String, dynamic>? headers, sf.Request? request}) {
headers ??= {};
if (request != null) { if (request != null) {
return { return {
...headers,
"X-Trace-ID": request.context['request_trace_id'] as String, "X-Trace-ID": request.context['request_trace_id'] as String,
"X-Span-ID": request.context['request_span_id'] as String "X-Span-ID": request.context['request_span_id'] as String
}; };
} }
return null; if (token != null) {
headers["token"] = token;
}
return headers;
} }
// 发起 GET 请求 // 发起 GET 请求
Future<Response> get(String url, Future<Response> get(String url,
{Map<String, dynamic>? queryParameters, sf.Request? request}) async { {Map<String, dynamic>? queryParameters,
sf.Request? request,
ResponseType? responseType}) async {
return await _dio.get(url, return await _dio.get(url,
queryParameters: queryParameters, queryParameters: queryParameters,
options: Options(headers: getHeader(request: request))); options: Options(
headers: getHeader(request: request), responseType: responseType));
} }
// 发起 POST 请求 // 发起 POST 请求

View File

@@ -33,6 +33,11 @@ class WebSocket {
final channel = WebSocketChannel.connect(wsUrl); final channel = WebSocketChannel.connect(wsUrl);
try { try {
await channel.ready; await channel.ready;
Future.delayed(Duration(seconds: 2), () {
if (_config.onOpen != null) {
_config.onOpen!();
}
});
} catch (e) { } catch (e) {
print(e); print(e);
//连接失败 //连接失败
@@ -73,6 +78,8 @@ class WebSocketConfig {
String host; String host;
bool reConnect; bool reConnect;
void Function(String) messgae; 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});
} }