1111import com .xaoxiao .convenientaccess .sync .SyncTask ;
1212import com .xaoxiao .convenientaccess .sync .SyncTaskManager ;
1313import com .xaoxiao .convenientaccess .whitelist .BatchOperation ;
14+ import com .xaoxiao .convenientaccess .utils .UuidUtils ;
1415import com .xaoxiao .convenientaccess .whitelist .WhitelistEntry ;
1516import com .xaoxiao .convenientaccess .whitelist .WhitelistManager ;
1617import com .xaoxiao .convenientaccess .whitelist .WhitelistStats ;
@@ -112,27 +113,44 @@ public void handleAddPlayer(HttpServletRequest request, HttpServletResponse resp
112113 String requestBody = readRequestBody (request );
113114 JsonObject json = JsonParser .parseString (requestBody ).getAsJsonObject ();
114115
115- // 参数验证
116- if (!json .has ("name" ) || !json .has ("uuid" ) ||
117- !json .has ("added_by_name" ) || !json .has ("added_by_uuid" )) {
118- sendJsonResponse (response , 400 , ApiResponse .badRequest ("缺少必需参数" ));
116+ // 参数验证 - 要求name、added_by_name、added_by_uuid、source,uuid变为可选
117+ if (!json .has ("name" ) || !json .has ("added_by_name" ) || !json .has ("added_by_uuid" ) || !json .has ("source" )) {
118+ sendJsonResponse (response , 400 , ApiResponse .badRequest ("缺少必需参数: name, added_by_name, added_by_uuid, source" ));
119119 return ;
120120 }
121121
122122 String name = json .get ("name" ).getAsString ();
123- String uuid = json .get ("uuid" ).getAsString ();
123+ String providedUuid = json .has ( "uuid" ) ? json . get ("uuid" ).getAsString () : null ;
124124 String addedByName = json .get ("added_by_name" ).getAsString ();
125125 String addedByUuid = json .get ("added_by_uuid" ).getAsString ();
126- String sourceStr = json .has ("source" ) ? json .get ("source" ).getAsString () : "ADMIN" ;
126+ String sourceStr = json .get ("source" ).getAsString ();
127+
128+ // 处理时间戳 - 如果前端提供则使用,否则使用当前时间
129+ LocalDateTime addedAt ;
130+ if (json .has ("added_at" )) {
131+ try {
132+ String addedAtStr = json .get ("added_at" ).getAsString ();
133+ addedAt = LocalDateTime .parse (addedAtStr , DateTimeFormatter .ISO_LOCAL_DATE_TIME );
134+ } catch (Exception e ) {
135+ sendJsonResponse (response , 400 , ApiResponse .badRequest ("时间格式无效,请使用ISO格式: yyyy-MM-ddTHH:mm:ss" ));
136+ return ;
137+ }
138+ } else {
139+ addedAt = LocalDateTime .now ();
140+ }
127141
128142 // 验证参数格式
129143 if (!isValidPlayerName (name )) {
130144 sendJsonResponse (response , 400 , ApiResponse .badRequest ("玩家名称格式无效" ));
131145 return ;
132146 }
133147
134- if (!isValidUuid (uuid )) {
135- sendJsonResponse (response , 400 , ApiResponse .badRequest ("UUID格式无效" ));
148+ // 生成或验证UUID
149+ String uuid ;
150+ try {
151+ uuid = UuidUtils .getOrGenerateUuid (name , providedUuid );
152+ } catch (IllegalArgumentException e ) {
153+ sendJsonResponse (response , 400 , ApiResponse .badRequest ("玩家名不能为空" ));
136154 return ;
137155 }
138156
@@ -145,7 +163,7 @@ public void handleAddPlayer(HttpServletRequest request, HttpServletResponse resp
145163 }
146164
147165 // 添加玩家
148- whitelistManager .addPlayer (name , uuid , addedByName , addedByUuid , source )
166+ whitelistManager .addPlayer (name , uuid , addedByName , addedByUuid , source , addedAt )
149167 .thenAccept (success -> {
150168 if (success ) {
151169 // 创建同步任务
@@ -155,6 +173,7 @@ public void handleAddPlayer(HttpServletRequest request, HttpServletResponse resp
155173 result .addProperty ("uuid" , uuid );
156174 result .addProperty ("name" , name );
157175 result .addProperty ("added" , true );
176+ result .addProperty ("uuid_generated" , providedUuid == null || providedUuid .trim ().isEmpty ());
158177
159178 sendJsonResponse (response , 201 , ApiResponse .success (result , "玩家添加成功" ));
160179 } else {
@@ -359,13 +378,37 @@ public void handleBatchOperation(HttpServletRequest request, HttpServletResponse
359378 JsonObject json = JsonParser .parseString (requestBody ).getAsJsonObject ();
360379
361380 // 参数验证
362- if (!json .has ("operation" ) || !json .has ("players" )) {
363- sendJsonResponse (response , 400 , ApiResponse .badRequest ("缺少必需参数: operation, players" ));
381+ if (!json .has ("operation" ) || !json .has ("players" ) || ! json . has ( "source" ) ) {
382+ sendJsonResponse (response , 400 , ApiResponse .badRequest ("缺少必需参数: operation, players, source " ));
364383 return ;
365384 }
366385
367386 String operation = json .get ("operation" ).getAsString ();
368387 JsonArray playersArray = json .getAsJsonArray ("players" );
388+ String sourceStr = json .get ("source" ).getAsString ();
389+
390+ // 验证source参数
391+ WhitelistEntry .Source source ;
392+ try {
393+ source = WhitelistEntry .Source .fromString (sourceStr );
394+ } catch (IllegalArgumentException e ) {
395+ sendJsonResponse (response , 400 , ApiResponse .badRequest ("无效的来源类型: " + sourceStr ));
396+ return ;
397+ }
398+
399+ // 解析时间戳(可选)
400+ LocalDateTime addedAt ;
401+ if (json .has ("added_at" )) {
402+ try {
403+ String addedAtStr = json .get ("added_at" ).getAsString ();
404+ addedAt = LocalDateTime .parse (addedAtStr , DateTimeFormatter .ISO_LOCAL_DATE_TIME );
405+ } catch (Exception e ) {
406+ sendJsonResponse (response , 400 , ApiResponse .badRequest ("无效的时间格式,请使用ISO格式: " + e .getMessage ()));
407+ return ;
408+ }
409+ } else {
410+ addedAt = LocalDateTime .now ();
411+ }
369412
370413 if (playersArray .size () == 0 ) {
371414 sendJsonResponse (response , 400 , ApiResponse .badRequest ("玩家列表不能为空" ));
@@ -380,15 +423,6 @@ public void handleBatchOperation(HttpServletRequest request, HttpServletResponse
380423 // 获取操作者信息
381424 String addedByName = json .has ("added_by_name" ) ? json .get ("added_by_name" ).getAsString () : "API" ;
382425 String addedByUuid = json .has ("added_by_uuid" ) ? json .get ("added_by_uuid" ).getAsString () : "00000000-0000-0000-0000-000000000000" ;
383- String sourceStr = json .has ("source" ) ? json .get ("source" ).getAsString () : "ADMIN" ;
384-
385- WhitelistEntry .Source source ;
386- try {
387- source = WhitelistEntry .Source .fromString (sourceStr );
388- } catch (IllegalArgumentException e ) {
389- sendJsonResponse (response , 400 , ApiResponse .badRequest ("无效的来源类型" ));
390- return ;
391- }
392426
393427 if ("add" .equalsIgnoreCase (operation )) {
394428 // 批量添加
@@ -400,20 +434,29 @@ public void handleBatchOperation(HttpServletRequest request, HttpServletResponse
400434 for (int i = 0 ; i < playersArray .size (); i ++) {
401435 JsonObject playerObj = playersArray .get (i ).getAsJsonObject ();
402436
403- if (!playerObj .has ("name" ) || ! playerObj . has ( "uuid" ) ) {
404- sendJsonResponse (response , 400 , ApiResponse .badRequest ("玩家信息缺少name或uuid字段 " ));
437+ if (!playerObj .has ("name" )) {
438+ sendJsonResponse (response , 400 , ApiResponse .badRequest ("玩家信息缺少name字段 " ));
405439 return ;
406440 }
407441
408442 String name = playerObj .get ("name" ).getAsString ();
409- String uuid = playerObj .get ("uuid" ).getAsString ();
443+ String providedUuid = playerObj .has ("uuid" ) ? playerObj .get ("uuid" ).getAsString () : null ;
444+
445+ if (!isValidPlayerName (name )) {
446+ sendJsonResponse (response , 400 , ApiResponse .badRequest ("无效的玩家名: " + name ));
447+ return ;
448+ }
410449
411- if (!isValidPlayerName (name ) || !isValidUuid (uuid )) {
412- sendJsonResponse (response , 400 , ApiResponse .badRequest ("无效的玩家数据: " + name + " (" + uuid + ")" ));
450+ // 生成或验证UUID
451+ String uuid ;
452+ try {
453+ uuid = UuidUtils .getOrGenerateUuid (name , providedUuid );
454+ } catch (IllegalArgumentException e ) {
455+ sendJsonResponse (response , 400 , ApiResponse .badRequest ("玩家名不能为空: " + name ));
413456 return ;
414457 }
415458
416- batchOperation .addEntry (name , uuid , source );
459+ batchOperation .addEntry (name , uuid , source , addedAt );
417460 }
418461
419462 // 执行批量添加
0 commit comments