mirror of
https://gitee.com/apiadmin/ApiAdmin.git
synced 2025-04-06 03:58:00 +08:00
modified 完成安装脚本
This commit is contained in:
parent
aa9ce0f89a
commit
0f420a0715
@ -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 <zhaoxiang051405@gmail.com>
|
||||
*/
|
||||
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, "<?php return ['username' => '{$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;
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ return [
|
||||
'APP_NAME' => 'ApiAdmin',
|
||||
|
||||
//鉴权相关
|
||||
'USER_ADMINISTRATOR' => [{$ROOT_ID}],
|
||||
'USER_ADMINISTRATOR' => ['{$ROOT_ID}'],
|
||||
|
||||
//安全秘钥
|
||||
'AUTH_KEY' => '{$AUTH_KEY}',
|
||||
|
Loading…
x
Reference in New Issue
Block a user