优化安装系统的流程,增加创建数据库异常捕获

This commit is contained in:
hdm 2023-02-20 10:57:04 +08:00
parent 05c999a28d
commit 72d294aa11
2 changed files with 62 additions and 66 deletions

View File

@ -78,9 +78,17 @@ class Index
// 验证失败 输出错误信息
return to_assign(1, $e->getError());
}
$dbName = $data['DB_NAME'];
// 连接数据库
$link = @new mysqli("{$data['DB_HOST']}:{$data['DB_PORT']}", $data['DB_USER'], $data['DB_PWD']);
$dbName = $data['DB_NAME'];
//验证表是否存在
try {
// 连接数据库
$link = @new mysqli("{$data['DB_HOST']}:{$data['DB_PORT']}", $data['DB_USER'], $data['DB_PWD']);
} catch (\Exception $e) {
// 这是进行异常捕获,创建数据库
$error = $e->getMessage();
return to_assign(1, '数据库链接失败:' . $error);die;
}
// 获取错误信息
$error = $link->connect_error;
if (!is_null($error)) {
@ -93,12 +101,22 @@ class Index
if ($link->server_info < 5.0) {
return to_assign(1, '请将您的mysql升级到5.0以上');die;
}
// 创建数据库并选中
if (!$link->select_db($dbName)) {
//创建数据库
$sql = "CREATE DATABASE IF NOT EXISTS `{$dbName}` DEFAULT CHARACTER SET utf8mb4";
//验证表是否存在
try {
// 这里是主体代码
$isDB=$link->query('SHOW TABLES LIKE '."'".$dbName."'");
if(!$isDB){
//创建数据库并选中
$sql = "CREATE DATABASE IF NOT EXISTS `{$dbName}` DEFAULT CHARACTER SET utf8mb4";
$link->query($sql);
}
} catch (\Exception $e) {
// 这是进行异常捕获,创建数据库并选中
$error = $e->getMessage();
$sql = "CREATE DATABASE IF NOT EXISTS `{$dbName}` DEFAULT CHARACTER SET utf8mb4";
$link->query($sql);
}
}
$link->select_db($dbName);
// 导入sql数据并创建表
$oa_sql = file_get_contents(CMS_ROOT . '/app/install/data/gouguoa.sql');

View File

@ -18,12 +18,13 @@
background: url("{__IMG__}/bg_pattern.png"), -webkit-linear-gradient(to left, #34a853, #4285f4);
background: url("{__IMG__}/bg_pattern.png"), linear-gradient(to left, #34a853, #4285f4);
}
.layui-form-item{margin-bottom:10px;}
h3{padding-bottom:10px; font-weight:600;}
</style>
</head>
<body>
<div style="width:200px;margin: 20px auto;"><img src="{__IMG__}/login_logo.png" alt="勾股OA安装"
width="200"></div>
<div style="width:200px;margin: 20px auto;"><img src="{__IMG__}/login_logo.png" alt="勾股OA安装" width="200"></div>
<div style="width:888px;margin:0 auto 30px;">
<div class="layui-layout layui-layout-admin">
<div class="layui-header layui-bg-red" style="border-radius:6px 6px 0 0;position:relative;">
@ -32,98 +33,85 @@
<li class="layui-nav-item">v{:CMS_VERSION}</li>
</ul>
</div>
<div style="padding:20px; background-color:#fff;line-height: 27px; border-radius:0 0 6px 6px">
<div style="padding:15px 20px; background-color:#fff;line-height: 27px; border-radius:0 0 6px 6px">
<form class="layui-form" action="" id="form">
<h3>数据库配置</h3><br>
<h3>数据库配置</h3>
<div class="layui-form-item">
<label class="layui-form-label">数据库类型</label>
<div class="layui-input-inline">
<input type="text" name="DB_TYPE" required lay-verify="required" autocomplete="off"
class="layui-input" value="mysql" disabled="disabled">
<input type="text" name="DB_TYPE" required lay-verify="required" autocomplete="off" class="layui-input" value="mysql" disabled="disabled">
</div>
<div class="layui-form-mid layui-word-aux">固定为mysql不可更改</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">数据库地址</label>
<div class="layui-input-inline">
<input type="text" name="DB_HOST" required lay-verify="required" autocomplete="off"
class="layui-input" value="127.0.0.1" lay-reqText="请输入数据库地址">
<input type="text" name="DB_HOST" required lay-verify="required" autocomplete="off" class="layui-input" value="127.0.0.1" lay-reqText="请输入数据库地址">
</div>
<div class="layui-form-mid layui-word-aux">数据库服务器地址一般为127.0.0.1</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">数据库端口</label>
<div class="layui-input-inline">
<input type="text" name="DB_PORT" required lay-verify="required" autocomplete="off"
class="layui-input" value="3306" lay-reqText="请输入数据库端口">
<input type="text" name="DB_PORT" required lay-verify="required" autocomplete="off" class="layui-input" value="3306" lay-reqText="请输入数据库端口">
</div>
<div class="layui-form-mid layui-word-aux">数据库端口一般为3306</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">数据库名</label>
<div class="layui-input-inline">
<input type="text" name="DB_NAME" required lay-verify="required" autocomplete="off"
class="layui-input" value="oa" lay-reqText="请输入数据库名">
<input type="text" name="DB_NAME" required lay-verify="required" autocomplete="off" class="layui-input" value="oa" lay-reqText="请输入数据库名">
</div>
<div class="layui-form-mid layui-word-aux">系统数据库名,必须包含字母,不能有"-"等特殊符号</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">用户名</label>
<div class="layui-input-inline">
<input type="text" name="DB_USER" required lay-verify="required" autocomplete="off"
class="layui-input" value="root" lay-reqText="请输入数据库用户名">
<input type="text" name="DB_USER" required lay-verify="required" autocomplete="off" class="layui-input" value="root" lay-reqText="请输入数据库用户名">
</div>
<div class="layui-form-mid layui-word-aux">连接数据库的用户名</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">密码</label>
<div class="layui-input-inline">
<input type="password" name="DB_PWD" required lay-verify="required" autocomplete="off"
class="layui-input" lay-reqText="请输入数据库连接密码">
<input type="password" name="DB_PWD" required lay-verify="required" autocomplete="off" class="layui-input" lay-reqText="请输入数据库连接密码">
</div>
<div class="layui-form-mid layui-word-aux">连接数据库的密码</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">表前缀</label>
<div class="layui-input-inline">
<input type="text" name="DB_PREFIX" required lay-verify="required" autocomplete="off"
class="layui-input" value="oa_" lay-reqText="请输入数据库表前缀">
<input type="text" name="DB_PREFIX" required lay-verify="required" autocomplete="off" class="layui-input" value="oa_" lay-reqText="请输入数据库表前缀">
</div>
<div class="layui-form-mid layui-word-aux">建议使用默认,同一个数据库安装多个系统时需更改,否则会覆盖</div>
</div>
<hr>
<h3>管理员配置</h3><br>
<h3>管理员配置</h3>
<div class="layui-form-item">
<label class="layui-form-label">管理员账号</label>
<div class="layui-input-block" style="max-width: 500px;">
<input type="text" name="username" lay-verify="required" autocomplete="off"
class="layui-input" lay-reqText="请输入管理员账户">
<div class="layui-input-inline" style="width:150px;">
<input type="text" name="username" lay-verify="required" autocomplete="off" class="layui-input" lay-reqText="请输入管理员账户">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">登录密码</label>
<div class="layui-input-inline">
<input type="password" name="password" lay-verify="required" autocomplete="off"
class="layui-input" lay-reqText="请输入管理员账户密码">
<div class="layui-input-inline" style="width:150px;">
<input type="password" name="password" lay-verify="required" autocomplete="off" class="layui-input" lay-reqText="请输入管理员账户密码">
</div>
<label class="layui-form-label">确认密码</label>
<div class="layui-input-inline">
<input type="password" name="password_confirm" lay-verify="required" autocomplete="off"
class="layui-input" lay-reqText="请重复输入管理员账户密码">
<div class="layui-input-inline" style="width:150px;">
<input type="password" name="password_confirm" lay-verify="required" autocomplete="off" class="layui-input" lay-reqText="请重复输入管理员账户密码">
</div>
</div>
<br>
<div class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="demo"
style="display: none;" id="progress">
<div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
<div style="padding:10px 0">
<div class="layui-progress layui-progress-big" lay-showpercent="true" lay-filter="demo" style="display: none;" id="progress">
<div class="layui-progress-bar layui-bg-blue" lay-percent="0%"></div>
</div>
</div>
<div class="layui-form-item">
<div style="margin:10px auto;width: 190px;">
<div style="margin:10px auto; width: 190px;">
<a href="/index.php?s=install/index/step2" class="layui-btn layui-bg-cyan">上一步</a>
<button class="layui-btn layui-bg-blue" lay-submit="" lay-filter="install">安装系统</button>
<button class="layui-btn layui-bg-blue" lay-submit="" lay-filter="install" id="install">安装系统</button>
</div>
</div>
</form>
@ -160,6 +148,7 @@
//监听提交
form.on('submit(install)', function (data) {
$('#progress').css('display', 'block');
let n=0;
var timer = setInterval(function () {
n = n + Math.random() * 10 | 0;
if (n > 99) {
@ -167,41 +156,32 @@
clearInterval(timer);
}
element.progress('demo', n + '%');
}, 50 + Math.random() * 100);
}, 200 + Math.random() * 100);
$("#install").html(n);
$.ajax({
url: "/index.php?s=install/index/install",
type: "post",
data: data.field,
beforeSend: function () {
// 禁用按钮防止重复提交
$("#install").attr({disabled: "disabled"}).html('创建中...');
$("#install").attr({disabled: "disabled"}).html('安装中...');
},
success: function (res) {
clearInterval(timer);
if (res.code == 1) {
$('#progress').css('display', 'none');
layer.msg(res.msg);
} else {
if (n == 99) {
element.progress('demo', 100 + '%');
$('#form').hide();
$('#complete').show();
return false;
} else if (n < 99) {
var ref = setInterval(function () {
if (n == 99) {
clearInterval(ref);
element.progress('demo', 100 + '%');
$('#form').hide();
$('#complete').show();
}
}, 500)
}
element.progress('demo', 100 + '%');
$('#form').hide();
$('#complete').show();
install_ajax();
return false;
}
},
complete: function () {
$("#install").removeAttr("disabled").html('提交');
clearInterval(timer);
$("#install").removeAttr("disabled").html('安装系统');
}
})
return false;
@ -209,7 +189,5 @@
});
</script>
</body>
</html>