优化代码

This commit is contained in:
coolxitech 2024-10-29 14:08:28 +08:00
parent 82246fd12b
commit 9c4d201aeb

View File

@ -1,11 +1,12 @@
<?php
namespace app\controller;
use PDO;
use Exception;
use app\BaseController;
use think\facade\View;
use PDO;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
class Install extends BaseController
{
@ -15,23 +16,24 @@ class Install extends BaseController
return '当前已经安装成功,如果需要重新安装,请手动删除根目录.env文件';
}
if(request()->isPost()){
$mysql_host = input('post.mysql_host', null, 'trim');
$mysql_port = intval(input('post.mysql_port', '3306'));
$mysql_user = input('post.mysql_user', null, 'trim');
$mysql_pwd = input('post.mysql_pwd', null, 'trim');
$mysql_name = input('post.mysql_name', null, 'trim');
$mysql_prefix = input('post.mysql_prefix', 'cloud_', 'trim');
$admin_username = input('post.admin_username', null, 'trim');
$admin_password = input('post.admin_password', null, 'trim');
$mysql_host = Request::post('mysql_host');
$mysql_port = intval(Request::post('mysql_port', '3306'));
$mysql_user = Request::post('mysql_user', null, 'trim');
$mysql_pwd = Request::post('mysql_pwd', null, 'trim');
$mysql_name = Request::post('mysql_name', null, 'trim');
$mysql_prefix = Request::post('mysql_prefix', 'cloud_', 'trim');
$admin_username = Request::post('admin_username', null, 'trim');
$admin_password = Request::post('admin_password', null, 'trim');
if(!$mysql_host || !$mysql_user || !$mysql_pwd || !$mysql_name || !$admin_username || !$admin_password){
return json(['code'=>0, 'msg'=>'必填项不能为空']);
}
$configdata = file_get_contents(app()->getRootPath().'.example.env');
$configdata = str_replace(['{dbhost}','{dbname}','{dbuser}','{dbpwd}','{dbport}','{dbprefix}'], [$mysql_host, $mysql_name, $mysql_user, $mysql_pwd, $mysql_port, $mysql_prefix], $configdata);
$configData = file_get_contents(app()->getRootPath().'.example.env');
$configData = str_replace(['{dbhost}','{dbname}','{dbuser}','{dbpwd}','{dbport}','{dbprefix}'], [$mysql_host, $mysql_name, $mysql_user, $mysql_pwd, $mysql_port, $mysql_prefix], $configData);
try{
$DB = Db::connect();
$DB=new PDO("mysql:host=".$mysql_host.";dbname=".$mysql_name.";port=".$mysql_port,$mysql_user,$mysql_pwd);
}catch(Exception $e){
if($e->getCode() == 2002){
@ -56,7 +58,9 @@ class Install extends BaseController
$sqls[]="REPLACE INTO `".$mysql_prefix."config` VALUES ('sys_key', '".random(16)."')";
$sqls[]="INSERT INTO `".$mysql_prefix."user` (`username`,`password`,`level`,`regtime`,`lasttime`,`status`) VALUES ('".addslashes($admin_username)."', '$password', 2, NOW(), NOW(), 1)";
$success=0;$error=0;$errorMsg=null;
$success = 0;
$error = 0;
$errorMsg = null;
foreach ($sqls as $value) {
$value=trim($value);
if(empty($value))continue;
@ -70,7 +74,7 @@ class Install extends BaseController
}
}
if(empty($errorMsg)){
if(!file_put_contents(app()->getRootPath().'.env', $configdata)){
if(!file_put_contents(app()->getRootPath().'.env', $configData)){
return json(['code'=>0, 'msg'=>'保存失败,请确保网站根目录有写入权限']);
}
Cache::clear();