11package com .phantoms .phantomsbackend .controller ;
22
3+ import com .phantoms .phantomsbackend .common .utils .RisingStonesLoginTool ;
34import com .zaxxer .hikari .HikariDataSource ;
45import com .zaxxer .hikari .HikariPoolMXBean ;
56import io .swagger .v3 .oas .annotations .Operation ;
1516import org .springframework .http .ResponseEntity ;
1617import org .springframework .jdbc .core .JdbcTemplate ;
1718import org .springframework .web .bind .annotation .GetMapping ;
19+ import org .springframework .web .bind .annotation .PostMapping ;
1820import org .springframework .web .bind .annotation .RestController ;
1921
2022import javax .sql .DataSource ;
@@ -53,6 +55,12 @@ public class PingController {
5355 @ Autowired
5456 private RedisConnectionFactory redisConnectionFactory ;
5557
58+ @ Autowired
59+ private RisingStonesLoginTool risingStonesLoginTool ;
60+
61+ @ Autowired
62+ private com .phantoms .phantomsbackend .common .scheduler .DaoYuKeyMonitorScheduler daoYuKeyMonitorScheduler ;
63+
5664 @ Value ("${app.version:unknown}" )
5765 private String appVersion ;
5866
@@ -218,4 +226,170 @@ private Map<String, String> getSystemDetails() {
218226 details .put ("user.country" , System .getProperty ("user.country" ));
219227 return details ;
220228 }
229+
230+ @ PostMapping ("/check/daoyu-key" )
231+ @ Operation (summary = "手动检查DaoYu Key有效性" ,
232+ description = "手动触发DaoYu Key有效性检查,并返回检查结果" ,
233+ responses = {
234+ @ ApiResponse (responseCode = "200" , description = "检查完成" ,
235+ content = @ Content (schema = @ Schema (implementation = Map .class )))
236+ })
237+ public ResponseEntity <Map <String , Object >> checkDaoYuKey () {
238+ Map <String , Object > response = new HashMap <>();
239+ response .put ("timestamp" , LocalDateTime .now ());
240+
241+ try {
242+ // 获取当前监控状态
243+ Map <String , Object > monitorStatus = daoYuKeyMonitorScheduler .getMonitorStatus ();
244+ response .put ("monitorStatus" , monitorStatus );
245+
246+ // 执行手动检查
247+ daoYuKeyMonitorScheduler .manualCheck ();
248+
249+ // 获取检查后的状态
250+ Map <String , Object > updatedStatus = daoYuKeyMonitorScheduler .getMonitorStatus ();
251+ response .put ("updatedStatus" , updatedStatus );
252+
253+ // 尝试获取登录结果
254+ String [] loginResult = risingStonesLoginTool .getDaoYuTokenAndCookie ();
255+ if (loginResult [0 ] != null && loginResult [1 ] != null ) {
256+ response .put ("status" , "SUCCESS" );
257+ response .put ("message" , "DaoYu Key检查通过,系统正常运行" );
258+ response .put ("tokenAvailable" , true );
259+ response .put ("tokenPrefix" , loginResult [0 ].substring (0 , Math .min (20 , loginResult [0 ].length ())) + "..." );
260+ response .put ("cookieAvailable" , true );
261+ } else {
262+ response .put ("status" , "FAILED" );
263+ response .put ("message" , "DaoYu Key检查失败,返回的token或cookie为空" );
264+ response .put ("tokenAvailable" , false );
265+ response .put ("cookieAvailable" , false );
266+ }
267+
268+ } catch (Exception e ) {
269+ response .put ("status" , "ERROR" );
270+ response .put ("message" , "DaoYu Key检查过程中发生异常: " + e .getMessage ());
271+ response .put ("error" , e .getClass ().getSimpleName ());
272+ response .put ("tokenAvailable" , false );
273+ response .put ("cookieAvailable" , false );
274+ }
275+
276+ return ResponseEntity .ok (response );
277+ }
278+
279+ @ GetMapping ("/status/daoyu-key" )
280+ @ Operation (summary = "获取DaoYu Key监控状态" ,
281+ description = "获取当前DaoYu Key监控的状态信息,不触发实际检查" ,
282+ responses = {
283+ @ ApiResponse (responseCode = "200" , description = "状态信息获取成功" ,
284+ content = @ Content (schema = @ Schema (implementation = Map .class )))
285+ })
286+ public ResponseEntity <Map <String , Object >> getDaoYuKeyStatus () {
287+ Map <String , Object > response = new HashMap <>();
288+ response .put ("timestamp" , LocalDateTime .now ());
289+
290+ try {
291+ // 获取监控状态
292+ Map <String , Object > monitorStatus = daoYuKeyMonitorScheduler .getMonitorStatus ();
293+ response .putAll (monitorStatus );
294+
295+ // 尝试快速检查当前状态(不发送通知)
296+ String [] loginResult = risingStonesLoginTool .getDaoYuTokenAndCookie ();
297+ boolean isKeyValid = loginResult [0 ] != null && loginResult [1 ] != null ;
298+
299+ response .put ("currentKeyValid" , isKeyValid );
300+ response .put ("lastCheckTime" , LocalDateTime .now ().format (java .time .format .DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" )));
301+
302+ if (isKeyValid ) {
303+ response .put ("status" , "HEALTHY" );
304+ response .put ("message" , "DaoYu Key当前有效" );
305+ response .put ("tokenPrefix" , loginResult [0 ].substring (0 , Math .min (20 , loginResult [0 ].length ())) + "..." );
306+ } else {
307+ response .put ("status" , "UNHEALTHY" );
308+ response .put ("message" , "DaoYu Key当前无效" );
309+ }
310+
311+ } catch (Exception e ) {
312+ response .put ("status" , "UNKNOWN" );
313+ response .put ("message" , "无法确定DaoYu Key状态: " + e .getMessage ());
314+ response .put ("error" , e .getClass ().getSimpleName ());
315+ response .put ("currentKeyValid" , false );
316+ }
317+
318+ return ResponseEntity .ok (response );
319+ }
320+
321+ @ PostMapping ("/refresh/daoyu-key" )
322+ @ Operation (summary = "手动刷新DaoYu Key缓存" ,
323+ description = "手动刷新DaoYu Key缓存并检查有效性" ,
324+ responses = {
325+ @ ApiResponse (responseCode = "200" , description = "刷新完成" ,
326+ content = @ Content (schema = @ Schema (implementation = Map .class )))
327+ })
328+ public ResponseEntity <Map <String , Object >> refreshDaoYuKey () {
329+ Map <String , Object > response = new HashMap <>();
330+ response .put ("timestamp" , LocalDateTime .now ());
331+
332+ try {
333+ // 获取刷新前的状态
334+ Map <String , Object > beforeStatus = daoYuKeyMonitorScheduler .getMonitorStatus ();
335+ response .put ("beforeRefresh" , beforeStatus );
336+
337+ // 执行手动刷新
338+ risingStonesLoginTool .refreshDaoYuKeyCache ();
339+
340+ // 等待缓存刷新完成
341+ Thread .sleep (3000 );
342+
343+ // 执行检查
344+ daoYuKeyMonitorScheduler .manualCheck ();
345+
346+ // 获取刷新后的状态
347+ Map <String , Object > afterStatus = daoYuKeyMonitorScheduler .getMonitorStatus ();
348+ response .put ("afterRefresh" , afterStatus );
349+
350+ response .put ("status" , "SUCCESS" );
351+ response .put ("message" , "DaoYu Key缓存刷新完成" );
352+
353+ } catch (Exception e ) {
354+ response .put ("status" , "ERROR" );
355+ response .put ("message" , "DaoYu Key缓存刷新失败: " + e .getMessage ());
356+ response .put ("error" , e .getClass ().getSimpleName ());
357+ }
358+
359+ return ResponseEntity .ok (response );
360+ }
361+
362+ @ PostMapping ("/config/daoyu-notification" )
363+ @ Operation (summary = "配置DaoYu Key通知设置" ,
364+ description = "修改DaoYu Key监控的通知配置" ,
365+ responses = {
366+ @ ApiResponse (responseCode = "200" , description = "配置更新成功" ,
367+ content = @ Content (schema = @ Schema (implementation = Map .class )))
368+ })
369+ public ResponseEntity <Map <String , Object >> configureDaoYuNotification () {
370+ Map <String , Object > response = new HashMap <>();
371+ response .put ("timestamp" , LocalDateTime .now ());
372+
373+ try {
374+ // 获取当前配置
375+ Map <String , Object > currentStatus = daoYuKeyMonitorScheduler .getMonitorStatus ();
376+ boolean currentNotifyOnSuccess = (boolean ) currentStatus .get ("notifyOnSuccess" );
377+
378+ // 切换配置(这里可以根据需求改为接收参数)
379+ boolean newNotifyOnSuccess = !currentNotifyOnSuccess ;
380+ daoYuKeyMonitorScheduler .setNotifyOnSuccess (newNotifyOnSuccess );
381+
382+ response .put ("status" , "SUCCESS" );
383+ response .put ("message" , "通知配置已更新" );
384+ response .put ("notifyOnSuccess" , newNotifyOnSuccess );
385+ response .put ("previousNotifyOnSuccess" , currentNotifyOnSuccess );
386+
387+ } catch (Exception e ) {
388+ response .put ("status" , "ERROR" );
389+ response .put ("message" , "配置更新失败: " + e .getMessage ());
390+ response .put ("error" , e .getClass ().getSimpleName ());
391+ }
392+
393+ return ResponseEntity .ok (response );
394+ }
221395}
0 commit comments