From 00e784ba980e2ee868d9e0162aed16941640e2fa Mon Sep 17 00:00:00 2001 From: Wisp X <1591788658@qq.com> Date: Tue, 20 Nov 2018 18:14:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9B=E5=AE=89=E8=A3=85=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 2 +- application/index/controller/Base.php | 5 +- application/index/controller/Install.php | 70 ++++++++++++++++-------- 3 files changed, 52 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 65a9a468..ed5afd7b 100644 --- a/README.md +++ b/README.md @@ -63,7 +63,7 @@ 升级过程并不复杂,总共分为四个步骤。 1. [下载](https://github.com/wisp-x/lsky-pro/releases)最新版程序并解压。 2. 备份原程序 ```config/db.php``` 文件,若怕出问题备份所有文件。 -3. 将db.php文件复制到新版程序的 ```config``` 文件夹,然后在新版程序根目录新建空文件: ```install.lock```。 +3. 将db.php文件复制到新版程序的 ```config``` 文件夹。 4. 覆盖最新版程序到根目录即可。 如何修改网站运行目录? diff --git a/application/index/controller/Base.php b/application/index/controller/Base.php index 89bacc00..70c9e393 100644 --- a/application/index/controller/Base.php +++ b/application/index/controller/Base.php @@ -14,6 +14,7 @@ use think\Controller; use think\Exception; use think\facade\Config; use think\facade\Session; +use think\facade\Env; class Base extends Controller { @@ -37,7 +38,7 @@ class Base extends Controller parent::initialize(); // 检测程序是否已安装 - if (!file_exists(\think\facade\Env::get('root_path') . 'install.lock')) { + if (!file_exists(Env::get('config_path') . 'db.php')) { return $this->redirect(url('/install')); } @@ -126,4 +127,4 @@ class Base extends Controller return true; } -} \ No newline at end of file +} diff --git a/application/index/controller/Install.php b/application/index/controller/Install.php index 8074e1bf..ec757f73 100644 --- a/application/index/controller/Install.php +++ b/application/index/controller/Install.php @@ -10,6 +10,7 @@ namespace app\index\controller; use app\common\model\Users; use think\Controller; +use think\Db; use think\Exception; use think\facade\Cookie; use think\facade\Env; @@ -19,12 +20,11 @@ class Install extends Controller { public function index($step = 1) { - $rootPath = Env::get('root_path'); $configPath = Env::get('config_path'); // 检测是否已安装 - if (file_exists($rootPath . 'install.lock') && !Session::has('install_success')) { + if (file_exists($configPath . 'db.php') && !Session::has('install_success')) { exit('你已安装成功,请勿重复安装!'); } @@ -67,6 +67,39 @@ class Install extends Controller if (!$mysqli->multi_query($sqlFile)) { throw new Exception('数据写入失败'); } + Session::set('db', [ + 'hostname' => $hostname, + 'database' => $database, + 'username' => $username, + 'password' => $password, + 'hostport' => $hostport, + ]); + } catch (Exception $e) { + return $this->error($e->getMessage()); + } + return $this->success('数据写入成功'); + } + break; + case 3: + // 设置管理员账号密码 + if ($this->request->isPost()) { + try { + $data = $this->request->post(); + $data['is_admin'] = 1; + $data['quota'] = 1073741824; + $data['update_time'] = time(); + $data['create_time'] = time(); + if ($data['password'] != $data['password_confirm']) { + throw new Exception('两次输入的密码不一致!'); + } + $data['password'] = md5($data['password']); + $data['reg_ip'] = request()->ip(); + $dbConfig = Session::get('db'); + $hostname = $dbConfig['hostname']; + $database = $dbConfig['database']; + $username = $dbConfig['username']; + $password = $dbConfig['password']; + $hostport = $dbConfig['hostport']; $dbPath = $configPath . 'db.php'; $str = <<error($e->getMessage()); - } - return $this->success('数据写入成功'); - } - break; - case 3: - // 设置管理员账号密码 - if ($this->request->isPost()) { - try { - $data = $this->request->post(); - $validate = $this->validate($data, 'Users.Install'); - if (true !== $validate) { - throw new Exception($validate); - } - $data['is_admin'] = 1; - $data['quota'] = 1073741824; - Users::create($data); - fopen($rootPath . 'install.lock', 'w'); + $db = Db::connect(array_merge($dbConfig, [ + // 数据库类型 + 'type' => 'mysql', + // 数据库连接参数 + 'params' => [], + // 数据库编码默认采用utf8 + 'charset' => 'utf8mb4', + // 数据库表前缀 + 'prefix' => 'lsky_', + ])); + unset($data['password_confirm']); + $db->name('users')->insert($data); } catch (Exception $e) { + @unlink($configPath . 'db.php'); return $this->error($e->getMessage()); } Session::flash('install_success', true);