V2boardInstall.php 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. \Artisan::call('config:cache');
  42. DB::connection()->getPdo();
  43. $file = \File::get(base_path() . '/install.sql');
  44. if (!$file) {
  45. abort(500, '数据库文件不存在');
  46. }
  47. $sql = str_replace("\n", "", $file);
  48. $sql = preg_split("/;/", $sql);
  49. if (!is_array($sql)) {
  50. abort(500, '数据库文件格式有误');
  51. }
  52. $this->info('正在导入数据库请稍等...');
  53. foreach($sql as $item) {
  54. try {
  55. DB::select(DB::raw($item));
  56. } catch (\Exception $e) {}
  57. }
  58. $email = '';
  59. while (!$email) {
  60. $email = $this->ask('请输入管理员邮箱?');
  61. }
  62. $password = '';
  63. while (!$password) {
  64. $password = $this->ask('请输入管理员密码?');
  65. }
  66. if (!$this->registerAdmin($email, $password)) {
  67. abort(500, '管理员账号注册失败,请重试');
  68. }
  69. $this->info('一切就绪');
  70. \File::put(base_path() . '/.lock', time());
  71. }
  72. private function registerAdmin ($email, $password) {
  73. $user = new User();
  74. $user->email = $email;
  75. $user->password = password_hash($password, PASSWORD_DEFAULT);
  76. $user->v2ray_uuid = Helper::guid(true);
  77. $user->token = Helper::guid();
  78. $user->is_admin = 1;
  79. return $user->save();
  80. }
  81. }