From ec73224671a00fae2a9f5a2aff2b9c6711c5eb21 Mon Sep 17 00:00:00 2001 From: Tokumeikoi Date: Sat, 8 Feb 2020 16:16:12 +0800 Subject: [PATCH] optimization install --- app/Console/Commands/V2boardInstall.php | 120 ++++++++++++------------ 1 file changed, 62 insertions(+), 58 deletions(-) diff --git a/app/Console/Commands/V2boardInstall.php b/app/Console/Commands/V2boardInstall.php index 296b66ee..ba9943e8 100644 --- a/app/Console/Commands/V2boardInstall.php +++ b/app/Console/Commands/V2boardInstall.php @@ -41,66 +41,70 @@ class V2boardInstall extends Command */ public function handle() { - $this->info(" - __ ______ ____ _ - \ \ / /___ \| __ ) ___ __ _ _ __ __| | - \ \ / / __) | _ \ / _ \ / _` | '__/ _` | - \ V / / __/| |_) | (_) | (_| | | | (_| | - \_/ |_____|____/ \___/ \__,_|_| \__,_| - "); - if (\File::exists(base_path() . '/.lock')) { - $this->error('V2board 已安装,如需重新安装请删除目录下.lock文件'); - } - if (!\File::exists(base_path() . '/.env')) { - if (!copy(base_path() . '/.env.example', base_path() . '/.env')) { - $this->error('复制环境文件失败,请检查目录权限'); - } - } - $this->saveToEnv([ - 'APP_KEY' => 'base64:' . base64_encode(Encrypter::generateKey('AES-256-CBC')), - 'DB_HOST' => $this->ask('请输入数据库地址(默认:localhost)', 'localhost'), - 'DB_DATABASE' => $this->ask('请输入数据库名'), - 'DB_USERNAME' => $this->ask('请输入数据库用户名'), - 'DB_PASSWORD' => $this->ask('请输入数据库密码') - ]); - \Artisan::call('config:clear'); - \Artisan::call('config:cache'); try { - DB::connection()->getPdo(); - } catch (\Exception $e) { - $this->error('数据库连接失败'); - } - $file = \File::get(base_path() . '/database/install.sql'); - if (!$file) { - $this->error('数据库文件不存在'); - } - $sql = str_replace("\n", "", $file); - $sql = preg_split("/;/", $sql); - if (!is_array($sql)) { - $this->error('数据库文件格式有误'); - } - $this->info('正在导入数据库请稍等...'); - foreach ($sql as $item) { - try { - DB::select(DB::raw($item)); - } catch (\Exception $e) { + $this->info(" + __ ______ ____ _ + \ \ / /___ \| __ ) ___ __ _ _ __ __| | + \ \ / / __) | _ \ / _ \ / _` | '__/ _` | + \ V / / __/| |_) | (_) | (_| | | | (_| | + \_/ |_____|____/ \___/ \__,_|_| \__,_| + "); + if (\File::exists(base_path() . '/.lock')) { + abort(500, 'V2board 已安装,如需重新安装请删除目录下.lock文件'); + } + if (!\File::exists(base_path() . '/.env')) { + if (!copy(base_path() . '/.env.example', base_path() . '/.env')) { + abort(500, '复制环境文件失败,请检查目录权限'); + } + } + $this->saveToEnv([ + 'APP_KEY' => 'base64:' . base64_encode(Encrypter::generateKey('AES-256-CBC')), + 'DB_HOST' => $this->ask('请输入数据库地址(默认:localhost)', 'localhost'), + 'DB_DATABASE' => $this->ask('请输入数据库名'), + 'DB_USERNAME' => $this->ask('请输入数据库用户名'), + 'DB_PASSWORD' => $this->ask('请输入数据库密码') + ]); + \Artisan::call('config:clear'); + \Artisan::call('config:cache'); + try { + DB::connection()->getPdo(); + } catch (\Exception $e) { + abort(500, '数据库连接失败'); + } + $file = \File::get(base_path() . '/database/install.sql'); + if (!$file) { + abort(500, '数据库文件不存在'); + } + $sql = str_replace("\n", "", $file); + $sql = preg_split("/;/", $sql); + if (!is_array($sql)) { + abort(500, '数据库文件格式有误'); + } + $this->info('正在导入数据库请稍等...'); + foreach ($sql as $item) { + try { + DB::select(DB::raw($item)); + } catch (\Exception $e) { + } + } + $this->info('数据库导入完成'); + $email = ''; + while (!$email) { + $email = $this->ask('请输入管理员邮箱?'); + } + $password = ''; + while (!$password) { + $password = $this->ask('请输入管理员密码?'); + } + if (!$this->registerAdmin($email, $password)) { + abort(500, '管理员账号注册失败,请重试'); } - } - $this->info('数据库导入完成'); - $email = ''; - while (!$email) { - $email = $this->ask('请输入管理员邮箱?'); - } - $password = ''; - while (!$password) { - $password = $this->ask('请输入管理员密码?'); - } - if (!$this->registerAdmin($email, $password)) { - $this->error('管理员账号注册失败,请重试'); - } - $this->info('一切就绪'); - \File::put(base_path() . '/.lock', time()); + $this->info('一切就绪'); + \File::put(base_path() . '/.lock', time()); + } catch (\Exception $e) { + $this->error($e->getMessage()); + } } private function registerAdmin($email, $password) @@ -108,7 +112,7 @@ class V2boardInstall extends Command $user = new User(); $user->email = $email; if (strlen($password) < 8) { - $this->error('管理员密码长度最小为8位字符'); + abort(500, '管理员密码长度最小为8位字符'); } $user->password = password_hash($password, PASSWORD_DEFAULT); $user->v2ray_uuid = Helper::guid(true);