autoTokenController.java 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package com.yyandywt99.pandoraNext.controller;
  2. import com.yyandywt99.pandoraNext.anno.Log;
  3. import com.yyandywt99.pandoraNext.pojo.Result;
  4. import com.yyandywt99.pandoraNext.pojo.token;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.scheduling.annotation.Scheduled;
  7. import org.springframework.web.bind.annotation.PostMapping;
  8. import org.springframework.web.bind.annotation.RequestBody;
  9. import org.springframework.web.bind.annotation.RequestMapping;
  10. import org.springframework.web.bind.annotation.RestController;
  11. /**
  12. * @author Yangyang
  13. * @create 2023-11-11 18:19
  14. */
  15. @RestController
  16. @RequestMapping("/api")
  17. public class autoTokenController {
  18. @Autowired
  19. private com.yyandywt99.pandoraNext.service.apiService apiService;
  20. @Autowired
  21. private apiController apiColltroller;
  22. /**
  23. * 自动更新Token
  24. * 更换tokens.json里存储的Tokens
  25. * 自动重启
  26. * @return "更新成功" or "更新失败"
  27. * @throws Exception
  28. */
  29. @Log
  30. @Scheduled(cron = "0 3 0 */5 * ?")
  31. public Result toUpdateToken(){
  32. try {
  33. String res = apiService.autoUpdateToken("");
  34. if(res.contains("修改Token成功")){
  35. try {
  36. apiColltroller.restartContainer("PandoraNext");
  37. return Result.success(res);
  38. } catch (Exception e) {
  39. throw new RuntimeException(e);
  40. }
  41. }
  42. } catch (Exception e) {
  43. e.printStackTrace();
  44. }
  45. return Result.error("更新失败");
  46. }
  47. /**
  48. * 自动更新指定用户名的Token
  49. * @return "更新成功" or "刷新Token失败,请尝重新刷新!”
  50. * @throws Exception
  51. */
  52. @Log
  53. @PostMapping ("updateToken")
  54. public Result toUpdateToken(@RequestBody token token){
  55. try {
  56. String res = apiService.autoUpdateSimpleToken(token);
  57. if(res != null && res.length() > 300){
  58. return Result.success(res);
  59. }
  60. } catch (Exception e) {
  61. e.printStackTrace();
  62. }
  63. return Result.error("刷新Token失败,请尝重新刷新!");
  64. }
  65. }