optimization install

This commit is contained in:
Tokumeikoi 2020-02-08 16:16:12 +08:00
parent 3f4e3d46fb
commit ec73224671

View File

@ -41,66 +41,70 @@ class V2boardInstall extends Command
*/ */
public function handle() 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 { try {
DB::connection()->getPdo(); $this->info("
} catch (\Exception $e) { __ ______ ____ _
$this->error('数据库连接失败'); \ \ / /___ \| __ ) ___ __ _ _ __ __| |
} \ \ / / __) | _ \ / _ \ / _` | '__/ _` |
$file = \File::get(base_path() . '/database/install.sql'); \ V / / __/| |_) | (_) | (_| | | | (_| |
if (!$file) { \_/ |_____|____/ \___/ \__,_|_| \__,_|
$this->error('数据库文件不存在'); ");
} if (\File::exists(base_path() . '/.lock')) {
$sql = str_replace("\n", "", $file); abort(500, 'V2board 已安装,如需重新安装请删除目录下.lock文件');
$sql = preg_split("/;/", $sql); }
if (!is_array($sql)) { if (!\File::exists(base_path() . '/.env')) {
$this->error('数据库文件格式有误'); if (!copy(base_path() . '/.env.example', base_path() . '/.env')) {
} abort(500, '复制环境文件失败,请检查目录权限');
$this->info('正在导入数据库请稍等...'); }
foreach ($sql as $item) { }
try { $this->saveToEnv([
DB::select(DB::raw($item)); 'APP_KEY' => 'base64:' . base64_encode(Encrypter::generateKey('AES-256-CBC')),
} catch (\Exception $e) { '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('一切就绪'); $this->info('一切就绪');
\File::put(base_path() . '/.lock', time()); \File::put(base_path() . '/.lock', time());
} catch (\Exception $e) {
$this->error($e->getMessage());
}
} }
private function registerAdmin($email, $password) private function registerAdmin($email, $password)
@ -108,7 +112,7 @@ class V2boardInstall extends Command
$user = new User(); $user = new User();
$user->email = $email; $user->email = $email;
if (strlen($password) < 8) { if (strlen($password) < 8) {
$this->error('管理员密码长度最小为8位字符'); abort(500, '管理员密码长度最小为8位字符');
} }
$user->password = password_hash($password, PASSWORD_DEFAULT); $user->password = password_hash($password, PASSWORD_DEFAULT);
$user->v2ray_uuid = Helper::guid(true); $user->v2ray_uuid = Helper::guid(true);