You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

106 lines
4.0 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\command;
  4. use think\console\Command;
  5. use think\console\Input;
  6. use think\console\input\Argument;
  7. use think\console\input\Option;
  8. use think\console\Output;
  9. use think\facade\Db;
  10. use think\facade\Config;
  11. use app\lib\Plugins;
  12. class UpdateAll extends Command
  13. {
  14. protected function configure()
  15. {
  16. $this->setName('updateall')
  17. ->setDescription('the updateall command');
  18. }
  19. protected function execute(Input $input, Output $output)
  20. {
  21. $res = Db::name('config')->cache('configs',0)->column('value','key');
  22. Config::set($res, 'sys');
  23. if(!config_get('bt_type') && config_get('bt_url') || config_get('bt_type')==1 && config_get('bt_surl')){
  24. $this->process_plugins($input, $output, 'Linux');
  25. }
  26. if(!config_get('wbt_type') && config_get('wbt_url') || config_get('wbt_type')==1 && config_get('wbt_surl')){
  27. $this->process_plugins($input, $output, 'Windows');
  28. }
  29. config_set('runtime', date('Y-m-d H:i:s'));
  30. }
  31. private function process_plugins(Input $input, Output $output, $os){
  32. //刷新插件列表
  33. if(!$this->refresh_plugin_list($input, $output, $os)){
  34. return;
  35. }
  36. $count = 0;
  37. $type = intval(config_get($os=='Windows'?'updateall_type_win':'updateall_type'));
  38. $json_arr = Plugins::get_plugin_list($os);
  39. //循环下载缺少的插件
  40. foreach($json_arr['list'] as $plugin){
  41. if($type == 0 && ($plugin['type']==8 || $plugin['type']==12) || $type == 1 && $plugin['type']==12 || $plugin['type']==10 || $plugin['type']==5) continue;
  42. foreach($plugin['versions'] as $version){
  43. $ver = $version['m_version'].'.'.$version['version'];
  44. if(isset($version['download'])){
  45. if(!file_exists(get_data_dir().'plugins/other/'.$version['download'])){
  46. if(!$this->download_plugin($input, $output, $plugin['name'], $ver, $os)){
  47. sleep(1);
  48. $this->download_plugin($input, $output, $plugin['name'], $ver, $os);
  49. }
  50. sleep(1);
  51. $count++;
  52. }
  53. }else{
  54. if(!file_exists(get_data_dir($os).'plugins/package/'.$plugin['name'].'-'.$ver.'.zip')){
  55. if(!$this->download_plugin($input, $output, $plugin['name'], $ver, $os)){
  56. sleep(1);
  57. $this->download_plugin($input, $output, $plugin['name'], $ver, $os);
  58. }
  59. sleep(1);
  60. $count++;
  61. }
  62. }
  63. }
  64. }
  65. $output->writeln($os.'本次成功下载'.$count.'个插件');
  66. }
  67. private function refresh_plugin_list(Input $input, Output $output, $os){
  68. try{
  69. Plugins::refresh_plugin_list($os);
  70. Db::name('log')->insert(['uid' => 1, 'action' => '刷新插件列表', 'data' => '刷新'.$os.'插件列表成功', 'addtime' => date("Y-m-d H:i:s")]);
  71. $output->writeln('刷新'.$os.'插件列表成功');
  72. return true;
  73. }catch(\Exception $e){
  74. $output->writeln($e->getMessage());
  75. errorlog($e->getMessage());
  76. return false;
  77. }
  78. }
  79. private function download_plugin(Input $input, Output $output, $plugin_name, $version, $os){
  80. $fullname = $plugin_name.'-'.$version;
  81. try{
  82. Plugins::download_plugin($plugin_name, $version, $os);
  83. Db::name('log')->insert(['uid' => 1, 'action' => '下载插件', 'data' => $fullname.' os:'.$os, 'addtime' => date("Y-m-d H:i:s")]);
  84. $output->writeln('下载'.$os.'插件: '.$fullname.' 成功');
  85. return true;
  86. }catch(\Exception $e){
  87. $output->writeln($fullname.' '.$e->getMessage());
  88. errorlog($fullname.' '.$e->getMessage());
  89. return false;
  90. }
  91. }
  92. }