upgrade.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\command;
  4. use app\model\SettingModel;
  5. use Axios;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. use think\console\Command;
  8. use think\console\Input;
  9. use think\console\Output;
  10. class upgrade extends Command
  11. {
  12. private ?string $authCode = null;
  13. protected function configure(): void
  14. {
  15. // 指令配置
  16. $this->setName('upgrade')->setDescription('程序升级');
  17. }
  18. private function initAuth()
  19. {
  20. $authCode = SettingModel::Config('authCode', '', true);
  21. if (strlen($authCode) == 0) {
  22. $authCode = env('authCode', '');
  23. }
  24. $this->authCode = $authCode;
  25. }
  26. protected function execute(Input $input, Output $output): void
  27. {
  28. echo "您确实要更新程序吗?请输入[Y/N]: ";
  29. $response = trim(fgets(STDIN));
  30. if (strtoupper($response) === 'Y') {
  31. $this->upgrade();
  32. } elseif (strtoupper($response) === 'N') {
  33. echo "取消更新程序。\n";
  34. } else {
  35. echo "无效的输入,请输入 Y 或 N。\n";
  36. }
  37. }
  38. function upgrade()
  39. {
  40. $this->initAuth();
  41. $version = app_version;
  42. print_r("即将开始程序升级任务\n当前版本号:v{$version}\n");
  43. $result = Axios::http()->post('https://auth.mtab.cc/getUpGrade', [
  44. 'timeout' => 30,
  45. 'form_params' => [
  46. 'authorization_code' => $this->authCode,
  47. 'version_code' => app_version_code,
  48. ]
  49. ]);
  50. if ($result->getStatusCode() == 200) {
  51. $json = json_decode($result->getBody()->getContents(), true);
  52. if ($json['code'] === 1) {
  53. print_r("检测到新版本\n");
  54. $upGrade = new \Upgrade2();
  55. if (!empty($json['info']['update_zip'])) {
  56. $upGrade->update_download_url = $json['info']['update_zip'];
  57. }
  58. if (!empty($json['info']['update_sql'])) {
  59. $upGrade->update_sql_url = $json['info']['update_sql'];
  60. }
  61. try {
  62. $upGrade->run(true); //启动任务
  63. print_r("更新完毕\n");
  64. } catch (\Exception $e) {
  65. print_r($e->getMessage() . "\n");
  66. }
  67. } else {
  68. print_r($json['msg'] . "\n");
  69. }
  70. } else {
  71. print_r("远程授权服务器连接失败\n");
  72. }
  73. }
  74. }