From 0f420a07157bf6e9c643ba6ff86d8b9e046c21b3 Mon Sep 17 00:00:00 2001 From: zhaoxiang <756958008@qq.com> Date: Wed, 15 May 2019 01:19:57 +0800 Subject: [PATCH] =?UTF-8?q?modified=20=E5=AE=8C=E6=88=90=E5=AE=89=E8=A3=85?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/command/Install.php | 86 ++++++++++++++++++++++++++------ application/install/apiadmin.tpl | 2 +- 2 files changed, 71 insertions(+), 17 deletions(-) diff --git a/application/command/Install.php b/application/command/Install.php index b83f1a0..f28213c 100644 --- a/application/command/Install.php +++ b/application/command/Install.php @@ -3,13 +3,12 @@ namespace app\command; use app\util\Strs; -use app\util\Tools; use think\console\Command; use think\console\Input; use think\console\input\Option; use think\console\Output; -use think\Db; -use think\exception\PDOException; +use think\db\Connection; +use think\facade\Env; class Install extends Command { @@ -23,32 +22,54 @@ class Install extends Command { } /** + * php think apiadmin:install --db mysql://root:123456@127.0.0.1:3306/apiadmin2#utf8 * @param Input $input * @param Output $output * @return int|void|null * @throws \think\Exception - * php think apiadmin:install --db mysql://root:123456@127.0.0.1:3306/apiadmin2#utf8 * @author zhaoxiang */ protected function execute(Input $input, Output $output) { + $tplPath = Env::get('app_path') . 'install' . DIRECTORY_SEPARATOR; + $lockFile = $tplPath . 'lock.php'; + + if (file_exists($lockFile)) { + $output->highlight("您已经安装过了,请勿重新安装!"); + $output->highlight("如有需要请删除application/install/lock.php文件再次尝试"); + exit; + } + if ($input->hasOption('db')) { - $now = time(); - $conn = Db::connect($input->getOption('db'))->table('admin_user'); $user = $input->getOption('username'); $pass = $input->getOption('password'); - $auth_key = Strs::uuid(); try { - $conn = Db::connect($input->getOption('db'))->table('admin_user'); -// $root_id = $conn->insertGetId([ -// 'username' => $user, -// 'nickname' => $user, -// 'create_time' => $now, -// 'update_time' => $now, -// 'password' => Tools::userMd5($pass, $auth_key), -// 'create_ip' => ip2long('127.0.0.1') -// ]); + $options = $options = $this->parseDsnConfig($input->getOption('db')); + Connection::instance($options)->getTables($options['database']); + $confPath = Env::get('config_path'); + //处理数据库配置文件 + $dbConf = str_replace([ + '{$DB_TYPE}', '{$DB_HOST}', '{$DB_NAME}', + '{$DB_USER}', '{$DB_PASSWORD}', '{$DB_PORT}', + '{$DB_CHAR}' + ], [ + $options['type'], $options['hostname'], $options['database'], + $options['username'], $options['password'], $options['hostport'], + $options['charset'] + ], file_get_contents($tplPath . 'db.tpl')); + file_put_contents($confPath . 'database.php', $dbConf); + $output->info('数据库配置更新成功'); + + //处理ApiAdmin自定义配置 + $authKey = substr(Strs::uuid(), 1, -1); + $apiConf = str_replace('{$AUTH_KEY}', $authKey, file_get_contents($tplPath . 'apiadmin.tpl')); + file_put_contents($confPath . 'apiadmin.php', $apiConf); + $output->info('ApiAdmin配置更新成功'); + + //生成lock文件,并且写入用户名密码 + file_put_contents($lockFile, " '{$user}', 'password' => '{$pass}'];"); + $output->info('lock文件初始化成功'); } catch (\PDOException $e) { $output->highlight($e->getMessage()); } @@ -56,4 +77,37 @@ class Install extends Command { $output->highlight("请输入数据库配置"); } } + + /** + * DSN解析 + * 格式: mysql://username:passwd@localhost:3306/DbName?param1=val1¶m2=val2#utf8 + * @access private + * @param string $dsnStr + * @return array + */ + private function parseDsnConfig($dsnStr) { + $info = parse_url($dsnStr); + + if (!$info) { + return []; + } + + $dsn = [ + 'type' => $info['scheme'], + 'username' => isset($info['user']) ? $info['user'] : '', + 'password' => isset($info['pass']) ? $info['pass'] : '', + 'hostname' => isset($info['host']) ? $info['host'] : '', + 'hostport' => isset($info['port']) ? $info['port'] : '', + 'database' => !empty($info['path']) ? ltrim($info['path'], '/') : '', + 'charset' => isset($info['fragment']) ? $info['fragment'] : 'utf8', + ]; + + if (isset($info['query'])) { + parse_str($info['query'], $dsn['params']); + } else { + $dsn['params'] = []; + } + + return $dsn; + } } diff --git a/application/install/apiadmin.tpl b/application/install/apiadmin.tpl index 8f92c75..5c36bf9 100644 --- a/application/install/apiadmin.tpl +++ b/application/install/apiadmin.tpl @@ -14,7 +14,7 @@ return [ 'APP_NAME' => 'ApiAdmin', //鉴权相关 - 'USER_ADMINISTRATOR' => [{$ROOT_ID}], + 'USER_ADMINISTRATOR' => ['{$ROOT_ID}'], //安全秘钥 'AUTH_KEY' => '{$AUTH_KEY}',