V2boardInstall.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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() . '/database/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. }
  60. $email = '';
  61. while (!$email) {
  62. $email = $this->ask('请输入管理员邮箱?');
  63. }
  64. $password = '';
  65. while (!$password) {
  66. $password = $this->ask('请输入管理员密码?');
  67. }
  68. if (!$this->registerAdmin($email, $password)) {
  69. abort(500, '管理员账号注册失败,请重试');
  70. }
  71. $this->info('一切就绪');
  72. \File::put(base_path() . '/.lock', time());
  73. }
  74. private function registerAdmin($email, $password)
  75. {
  76. $user = new User();
  77. $user->email = $email;
  78. if (strlen($password) < 8) {
  79. abort(500, '管理员密码长度最小为8位字符');
  80. }
  81. $user->password = password_hash($password, PASSWORD_DEFAULT);
  82. $user->v2ray_uuid = Helper::guid(true);
  83. $user->token = Helper::guid();
  84. $user->is_admin = 1;
  85. return $user->save();
  86. }
  87. }