modified 完成安装脚本

This commit is contained in:
zhaoxiang 2019-05-15 01:19:57 +08:00
parent aa9ce0f89a
commit 0f420a0715
2 changed files with 71 additions and 17 deletions

View File

@ -3,13 +3,12 @@
namespace app\command; namespace app\command;
use app\util\Strs; use app\util\Strs;
use app\util\Tools;
use think\console\Command; use think\console\Command;
use think\console\Input; use think\console\Input;
use think\console\input\Option; use think\console\input\Option;
use think\console\Output; use think\console\Output;
use think\Db; use think\db\Connection;
use think\exception\PDOException; use think\facade\Env;
class Install extends Command { 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 Input $input
* @param Output $output * @param Output $output
* @return int|void|null * @return int|void|null
* @throws \think\Exception * @throws \think\Exception
* php think apiadmin:install --db mysql://root:123456@127.0.0.1:3306/apiadmin2#utf8
* @author zhaoxiang <zhaoxiang051405@gmail.com> * @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
protected function execute(Input $input, Output $output) { 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')) { if ($input->hasOption('db')) {
$now = time();
$conn = Db::connect($input->getOption('db'))->table('admin_user');
$user = $input->getOption('username'); $user = $input->getOption('username');
$pass = $input->getOption('password'); $pass = $input->getOption('password');
$auth_key = Strs::uuid();
try { try {
$conn = Db::connect($input->getOption('db'))->table('admin_user'); $options = $options = $this->parseDsnConfig($input->getOption('db'));
// $root_id = $conn->insertGetId([ Connection::instance($options)->getTables($options['database']);
// 'username' => $user, $confPath = Env::get('config_path');
// 'nickname' => $user,
// 'create_time' => $now,
// 'update_time' => $now,
// 'password' => Tools::userMd5($pass, $auth_key),
// 'create_ip' => ip2long('127.0.0.1')
// ]);
//处理数据库配置文件
$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, "<?php return ['username' => '{$user}', 'password' => '{$pass}'];");
$output->info('lock文件初始化成功');
} catch (\PDOException $e) { } catch (\PDOException $e) {
$output->highlight($e->getMessage()); $output->highlight($e->getMessage());
} }
@ -56,4 +77,37 @@ class Install extends Command {
$output->highlight("请输入数据库配置"); $output->highlight("请输入数据库配置");
} }
} }
/**
* DSN解析
* 格式: mysql://username:passwd@localhost:3306/DbName?param1=val1&param2=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;
}
} }

View File

@ -14,7 +14,7 @@ return [
'APP_NAME' => 'ApiAdmin', 'APP_NAME' => 'ApiAdmin',
//鉴权相关 //鉴权相关
'USER_ADMINISTRATOR' => [{$ROOT_ID}], 'USER_ADMINISTRATOR' => ['{$ROOT_ID}'],
//安全秘钥 //安全秘钥
'AUTH_KEY' => '{$AUTH_KEY}', 'AUTH_KEY' => '{$AUTH_KEY}',