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.

121 lines
4.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years 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. //刷新插件列表
  24. if(!$this->refresh_plugin_list($input, $output)){
  25. return;
  26. }
  27. $count = 0;
  28. $type = intval(config_get('updateall_type'));
  29. $json_arr = Plugins::get_plugin_list();
  30. //循环下载缺少的插件
  31. foreach($json_arr['list'] as $plugin){
  32. if($type == 0 && ($plugin['type']==8 || $plugin['type']==12) || $type == 1 && $plugin['type']==12 || $plugin['type']==10 || $plugin['type']==5) continue;
  33. foreach($plugin['versions'] as $version){
  34. $ver = $version['m_version'].'.'.$version['version'];
  35. if(isset($version['download'])){
  36. if(!file_exists(get_data_dir().'plugins/other/'.$version['download'])){
  37. if(!$this->download_plugin($input, $output, $plugin['name'], $ver)){
  38. sleep(1);
  39. $this->download_plugin($input, $output, $plugin['name'], $ver);
  40. }
  41. sleep(1);
  42. $count++;
  43. }
  44. }else{
  45. if(!file_exists(get_data_dir().'plugins/package/'.$plugin['name'].'-'.$ver.'.zip')){
  46. if(!$this->download_plugin($input, $output, $plugin['name'], $ver)){
  47. sleep(1);
  48. $this->download_plugin($input, $output, $plugin['name'], $ver);
  49. }
  50. sleep(1);
  51. $count++;
  52. }
  53. }
  54. }
  55. }
  56. $imgcount = 0;
  57. //循环下载缺少的插件图片
  58. /*foreach($json_arr['list'] as $plugin){
  59. if(isset($plugin['min_image']) && strpos($plugin['min_image'], 'fname=')){
  60. $fname = substr($plugin['min_image'], strpos($plugin['min_image'], '?fname=')+7);
  61. if(!file_exists(get_data_dir().'plugins/other/'.$fname)){
  62. $this->download_plugin_image($input, $output, $fname);
  63. sleep(1);
  64. $imgcount++;
  65. }
  66. }
  67. }*/
  68. $output->writeln('本次成功下载'.$count.'个插件'.($imgcount>0?','.$imgcount.'个图片':''));
  69. config_set('runtime', date('Y-m-d H:i:s'));
  70. }
  71. private function refresh_plugin_list(Input $input, Output $output){
  72. try{
  73. Plugins::refresh_plugin_list();
  74. Db::name('log')->insert(['uid' => 1, 'action' => '刷新插件列表', 'data' => '刷新插件列表成功', 'addtime' => date("Y-m-d H:i:s")]);
  75. $output->writeln('刷新插件列表成功');
  76. return true;
  77. }catch(\Exception $e){
  78. $output->writeln($e->getMessage());
  79. errorlog($e->getMessage());
  80. return false;
  81. }
  82. }
  83. private function download_plugin(Input $input, Output $output, $plugin_name, $version){
  84. $fullname = $plugin_name.'-'.$version;
  85. try{
  86. Plugins::download_plugin($plugin_name, $version);
  87. Db::name('log')->insert(['uid' => 1, 'action' => '下载插件', 'data' => $fullname, 'addtime' => date("Y-m-d H:i:s")]);
  88. $output->writeln('下载插件: '.$fullname.' 成功');
  89. return true;
  90. }catch(\Exception $e){
  91. $output->writeln($fullname.' '.$e->getMessage());
  92. errorlog($fullname.' '.$e->getMessage());
  93. return false;
  94. }
  95. }
  96. private function download_plugin_image(Input $input, Output $output, $fname){
  97. try{
  98. Plugins::download_plugin_other($fname);
  99. $output->writeln('下载图片: '.$fname.' 成功');
  100. return true;
  101. }catch(\Exception $e){
  102. $output->writeln($fname.' '.$e->getMessage());
  103. errorlog($fname.' '.$e->getMessage());
  104. return false;
  105. }
  106. }
  107. }