fix: change install
This commit is contained in:
@@ -3,6 +3,7 @@ declare (strict_types=1);
|
||||
|
||||
namespace app\install\controller;
|
||||
|
||||
use app\common\library\DataBase;
|
||||
use think\facade\Cache;
|
||||
use app\BaseController;
|
||||
|
||||
@@ -53,7 +54,7 @@ class Index extends BaseController
|
||||
}
|
||||
|
||||
return view('/index/step1', [
|
||||
'checkEnv' => $this->checkEnv(),
|
||||
'checkEnv' => $this->checkEnv(),
|
||||
'checkDirFile' => $this->checkDirFile(),
|
||||
]);
|
||||
}
|
||||
@@ -187,91 +188,36 @@ class Index extends BaseController
|
||||
}
|
||||
|
||||
// 读取SQL文件加载进缓存
|
||||
$mysqlPath = root_path('app/install'). 'install.sql';
|
||||
$mysqlPath = root_path('app/install') . 'install.sql';
|
||||
$sqlRecords = file_get_contents($mysqlPath);
|
||||
$sqlRecords = str_replace("\r", "\n", $sqlRecords);
|
||||
$sqlRecords = str_ireplace("\r", "\n", $sqlRecords);
|
||||
|
||||
// 替换数据库表前缀
|
||||
$sqlRecords = explode(";\n", $sqlRecords);
|
||||
$sqlRecords = str_replace(" `__PREFIX__", " `{$mysqlInfo['prefix']}", $sqlRecords);
|
||||
|
||||
// 缓存任务总数
|
||||
$recordCount = count($sqlRecords);
|
||||
if ($recordCount >= 1 && is_numeric($recordCount)) {
|
||||
Cache::set('sqlRecords', $sqlRecords);
|
||||
Cache::set('recordCount', $recordCount);
|
||||
} else {
|
||||
return $this->error('读取install.sql出错');
|
||||
}
|
||||
|
||||
return $this->success('success');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取安装进度
|
||||
*
|
||||
* @return \support\Response|void
|
||||
* @throws \Psr\SimpleCache\InvalidArgumentException
|
||||
*/
|
||||
public function progress()
|
||||
{
|
||||
if (request()->isAjax()) {
|
||||
|
||||
$mysqlInfo = Cache::get('mysqlInfo');
|
||||
$sqlRecords = Cache::get('sqlRecords');
|
||||
$recordCount = Cache::get('recordCount');
|
||||
|
||||
// 链接数据库
|
||||
$sqlConnect = @mysqli_connect($mysqlInfo['hostname'] . ':' . $mysqlInfo['hostport'], $mysqlInfo['username'], $mysqlInfo['password']);
|
||||
mysqli_select_db($sqlConnect, $mysqlInfo['database']);
|
||||
mysqli_query($sqlConnect, "set names utf8mb4");
|
||||
|
||||
$key = input('key') ?? 1;
|
||||
if (isset($sqlRecords[$key - 1])) {
|
||||
|
||||
$sqlLine = trim($sqlRecords[$key - 1]);
|
||||
foreach ($sqlRecords as $index => $sqlLine) {
|
||||
$sqlLine = trim($sqlLine);
|
||||
if (!empty($sqlLine)) {
|
||||
try {
|
||||
// 创建表数据
|
||||
if (substr($sqlLine, 0, 12) == 'CREATE TABLE') {
|
||||
$name = preg_replace("/^CREATE TABLE `(\w+)` .*/s", "\\1", $sqlLine);
|
||||
$msg = "创建数据表 {$name}...";
|
||||
|
||||
if (mysqli_query($sqlConnect, $sqlLine) !== false) {
|
||||
$msg .= '成功!';
|
||||
} else {
|
||||
$msg .= '失败!';
|
||||
}
|
||||
} else {
|
||||
if (mysqli_query($sqlConnect, $sqlLine) === false) {
|
||||
throw new \Exception(mysqli_error($sqlConnect));
|
||||
}
|
||||
if (mysqli_query($sqlConnect, $sqlLine) === false) {
|
||||
throw new \Exception(mysqli_error($sqlConnect));
|
||||
}
|
||||
} catch (\Throwable $th) {
|
||||
return $this->error($th->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
// 修改初始化密码
|
||||
if ($key == ($recordCount - 1)) {
|
||||
$pwd = encryptPwd($mysqlInfo['pwd']);
|
||||
mysqli_query($sqlConnect, "UPDATE {$mysqlInfo['prefix']}admin SET pwd='{$pwd}' where id = 1");
|
||||
}
|
||||
|
||||
// 更新进度
|
||||
$progress = round(($key / $recordCount) * 100) . '%';
|
||||
$result = [
|
||||
'code' => 200,
|
||||
'total' => $recordCount,
|
||||
'key' => $key,
|
||||
'msg' => $msg ?? '',
|
||||
'progress' => $progress,
|
||||
];
|
||||
|
||||
Cache::set('progress', $progress);
|
||||
return json($result);
|
||||
}
|
||||
|
||||
$pwd = encryptPwd($mysqlInfo['pwd']);
|
||||
mysqli_query($sqlConnect, "UPDATE {$mysqlInfo['prefix']}admin SET pwd='{$pwd}' where id = 1");
|
||||
|
||||
return $this->success('success');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -302,7 +248,7 @@ class Index extends BaseController
|
||||
|
||||
// 清理安装包
|
||||
Cache::clear();
|
||||
recursive_delete(root_path('app/install'));
|
||||
recursive_delete(root_path('app' . DIRECTORY_SEPARATOR . 'install'));
|
||||
system_reload();
|
||||
} catch (\Throwable $th) {
|
||||
return $this->error($th->getMessage());
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
<div class="layui-header">
|
||||
<div class="layui-container">
|
||||
<h1 class="logo"><a href="https://www.swiftadmin.net"><img class="pic" src="__ADMINIMAGES__/logo.png" alt=""><span>SwiftAdmin</span></a></h1>
|
||||
<h1 class="logo"><a href="https://www.swiftadmin.net"><img class="pic" src="__ADMINIMAGES__logo.png" alt=""><span>SwiftAdmin</span></a></h1>
|
||||
<div class="title">安装向导</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
<div class="layui-header">
|
||||
<div class="layui-container">
|
||||
<h1 class="logo"><a href="https://www.SwiftAdmin.net"><img class="pic" src="__ADMINIMAGES__/logo.png" alt=""><span>SwiftAdmin</span></a></h1>
|
||||
<h1 class="logo"><a href="https://www.SwiftAdmin.net"><img class="pic" src="__ADMINIMAGES__logo.png" alt=""><span>SwiftAdmin</span></a></h1>
|
||||
<div class="title">安装向导</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
|
||||
<div class="layui-header">
|
||||
<div class="layui-container">
|
||||
<h1 class="logo"><a href="https://www.SwiftAdmin.net"><img class="pic" src="__ADMINIMAGES__/logo.png" alt=""><span>SwiftAdmin</span></a></h1>
|
||||
<h1 class="logo"><a href="https://www.SwiftAdmin.net"><img class="pic" src="__ADMINIMAGES__logo.png" alt=""><span>SwiftAdmin</span></a></h1>
|
||||
<div class="title">安装向导</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -16,12 +16,11 @@
|
||||
|
||||
<div class="layui-header">
|
||||
<div class="layui-container">
|
||||
<h1 class="logo"><a href="https://www.SwiftAdmin.net"><img class="pic" src="__ADMINIMAGES__/logo.png" alt=""><span>SwiftAdmin</span></a></h1>
|
||||
<h1 class="logo"><a href="https://www.SwiftAdmin.net"><img class="pic" src="__ADMINIMAGES__logo.png" alt=""><span>SwiftAdmin</span></a></h1>
|
||||
<div class="title">安装向导</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="layui-content" style="padding-top: 0px;">
|
||||
<div class="layui-container">
|
||||
<div class="layui-row">
|
||||
@@ -51,14 +50,8 @@
|
||||
</div>
|
||||
<div class="layui-card-body">
|
||||
<div class="layui-install">
|
||||
<div class="tips">正在安装,请稍后...</div>
|
||||
<div class="layui-progress layui-progress-big" lay-showPercent="true">
|
||||
<div class="layui-progress-bar layui-bg-blue" lay-percent=""></div>
|
||||
</div>
|
||||
|
||||
<div class="layui-install-logs"><li>创建数据表...</li></div>
|
||||
<div class="tips">正在安装,请勿刷新...</div>
|
||||
</div>
|
||||
|
||||
<div class="layui-complete" style="display: none">
|
||||
<div class="layui-success">SWIFTADMIN安装成功</div>
|
||||
<blockquote class="layui-elem-quote layui-text" style="border-left: 0px;text-align: left;margin-bottom: 30px;">
|
||||
@@ -83,12 +76,10 @@
|
||||
<div class="layui-footer">copyright © 2020 SwiftAdmin all rights reserved.</div>
|
||||
</body>
|
||||
|
||||
|
||||
<script type="text/javascript">
|
||||
layui.use(['jquery','layer','element'],function() {
|
||||
var layer = layui.layer;
|
||||
var jquery = layui.jquery;
|
||||
var element = layui.element;
|
||||
|
||||
// 随机字符串
|
||||
function randomString(len) {
|
||||
@@ -104,68 +95,18 @@
|
||||
|
||||
// 加载数据信息
|
||||
jquery.get('/install/index/install',{},function(res){
|
||||
if (res.code == 200) {
|
||||
queryProgress();
|
||||
if (res.code === 200) {
|
||||
jquery('.layui-install').remove();
|
||||
var admin = jquery('.layui-admin');
|
||||
admin.attr('href','/manage');
|
||||
admin.html('后台地址:http://'+ location.host +'/manage');
|
||||
jquery('.layui-btn-admin').attr('href','/manage');
|
||||
jquery('.layui-complete').show();
|
||||
jquery.get('/install/index/clear',{},function(res){});
|
||||
} else {
|
||||
layer.msg(res.msg,'error');
|
||||
}
|
||||
});
|
||||
|
||||
var progress = 1;
|
||||
var queryProgress = function() {
|
||||
|
||||
jquery.ajax({
|
||||
url: '/install/index/progress',
|
||||
data: {
|
||||
key: progress
|
||||
},
|
||||
type: 'get',
|
||||
async: false,
|
||||
dataType: 'json',
|
||||
success:function(res) {
|
||||
|
||||
// 设置延时
|
||||
if (!res || typeof res.msg === undefined) {
|
||||
setTimeout(queryProgress, 100);
|
||||
return false;
|
||||
}
|
||||
|
||||
// 查找元素
|
||||
var html = '';
|
||||
var logsElem = jquery('.layui-install-logs');
|
||||
if (res.code === 200) {
|
||||
if (res.msg) {
|
||||
html += '<li class="'+ res.key +'">'+res.msg+'</li>';
|
||||
}
|
||||
if (progress < res.total) {
|
||||
progress += 1;
|
||||
}
|
||||
} else if (res.code === 101) { // 错误信息
|
||||
html = '<li><font color="red">'+res.msg+'</font></li>';
|
||||
}
|
||||
|
||||
if (parseInt(res.progress) >= 100) {
|
||||
jquery('.layui-install').remove();
|
||||
var admin = jquery('.layui-admin');
|
||||
admin.attr('href','/manage');
|
||||
admin.html('后台地址:http://'+ location.host +'/manage');
|
||||
jquery('.layui-btn-admin').attr('href','/manage');
|
||||
jquery('.layui-complete').show();
|
||||
jquery.get('/install/index/clear',{},function(res){});
|
||||
}
|
||||
else {
|
||||
// 移动焦点
|
||||
logsElem.append(html);
|
||||
if (logsElem[0].scrollHeight >= logsElem[0].clientHeight) {
|
||||
var scrollTop = logsElem[0].scrollHeight-logsElem[0].clientHeight;
|
||||
logsElem.scrollTop(scrollTop);
|
||||
}
|
||||
setTimeout(queryProgress, 10);
|
||||
}
|
||||
jquery('.layui-progress-bar').attr('lay-percent',res.progress) && element.render();
|
||||
}
|
||||
});
|
||||
}
|
||||
})
|
||||
</script>
|
||||
</html>
|
||||
Reference in New Issue
Block a user