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.

115 lines
4.1 KiB

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