V2boardInstall.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\User;
  5. use App\Utils\Helper;
  6. use Illuminate\Support\Facades\DB;
  7. class V2boardInstall extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'v2board:install';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'v2board 安装';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return mixed
  34. */
  35. public function handle()
  36. {
  37. if (\File::exists(base_path() . '/.lock')) {
  38. abort(500, 'V2board 已安装,如需重新安装请删除目录下.lock文件');
  39. }
  40. \Artisan::call('key:generate');
  41. sleep(2);
  42. \Artisan::call('config:cache');
  43. DB::connection()->getPdo();
  44. $file = \File::get(base_path() . '/install.sql');
  45. if (!$file) {
  46. abort(500, '数据库文件不存在');
  47. }
  48. $sql = str_replace("\n", "", $file);
  49. $sql = preg_split("/;/", $sql);
  50. if (!is_array($sql)) {
  51. abort(500, '数据库文件格式有误');
  52. }
  53. $this->info('正在导入数据库请稍等...');
  54. foreach($sql as $item) {
  55. try {
  56. DB::select(DB::raw($item));
  57. } catch (\Exception $e) {}
  58. }
  59. $email = '';
  60. while (!$email) {
  61. $email = $this->ask('请输入管理员邮箱?');
  62. }
  63. $password = '';
  64. while (!$password) {
  65. $password = $this->ask('请输入管理员密码?');
  66. }
  67. if (!$this->registerAdmin($email, $password)) {
  68. abort(500, '管理员账号注册失败,请重试');
  69. }
  70. $this->info('一切就绪');
  71. \File::put(base_path() . '/.lock', time());
  72. }
  73. private function registerAdmin ($email, $password) {
  74. $user = new User();
  75. $user->email = $email;
  76. $user->password = password_hash($password, PASSWORD_DEFAULT);
  77. $user->v2ray_uuid = Helper::guid(true);
  78. $user->token = Helper::guid();
  79. $user->is_admin = 1;
  80. return $user->save();
  81. }
  82. }