新增sql执行控制台
parent
463640b12f
commit
0040ad10f0
|
|
@ -0,0 +1,74 @@
|
|||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ShopXO 国内领先企业级B2C免费开源电商系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: Devil
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\admin\controller;
|
||||
|
||||
use app\service\SqlconsoleService;
|
||||
|
||||
/**
|
||||
* sql控制台
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 0.0.1
|
||||
* @datetime 2016-12-01T21:51:08+0800
|
||||
*/
|
||||
class Sqlconsole extends Common
|
||||
{
|
||||
/**
|
||||
* 构造方法
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 0.0.1
|
||||
* @datetime 2016-12-03T12:39:08+0800
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// 调用父类前置方法
|
||||
parent::__construct();
|
||||
|
||||
// 登录校验
|
||||
$this->IsLogin();
|
||||
|
||||
// 权限校验
|
||||
$this->IsPower();
|
||||
}
|
||||
|
||||
/**
|
||||
* [Index 首页]
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 0.0.1
|
||||
* @datetime 2017-02-26T19:13:29+0800
|
||||
*/
|
||||
public function Index()
|
||||
{
|
||||
return $this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* sql执行
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 0.0.1
|
||||
* @datetime 2016-12-15T11:03:30+0800
|
||||
*/
|
||||
public function Implement()
|
||||
{
|
||||
// 是否ajax请求
|
||||
if(!IS_AJAX)
|
||||
{
|
||||
return $this->error('非法访问');
|
||||
}
|
||||
|
||||
// 开始处理
|
||||
return SqlconsoleService::Implement(input());
|
||||
}
|
||||
}
|
||||
?>
|
||||
|
|
@ -0,0 +1,61 @@
|
|||
{{include file="public/header" /}}
|
||||
|
||||
<!-- right content start -->
|
||||
<div class="content-right">
|
||||
<div class="content">
|
||||
<div class="am-alert am-alert-warning am-radius" data-am-alert>
|
||||
<button type="button" class="am-close">×</button>
|
||||
<p class="fs-12">PS:非开发人员请不要随意执行任何SQL语句,操作可能导致将整个系统数据库被删除。</p>
|
||||
</div>
|
||||
|
||||
<!-- form start -->
|
||||
<form class="am-form form-validation view-save" action="{{:MyUrl('admin/sqlconsole/implement')}}" method="POST" request-type="ajax-fun" request-value="ViewImplementBack">
|
||||
<div class="am-form-group">
|
||||
<textarea rows="6" name="sql" class="am-radius" placeholder="SQL语句" data-validation-message="请填写需要执行的SQL语句" required>{{if !empty($data)}}{{$data.sql}}{{/if}}</textarea>
|
||||
</div>
|
||||
<div class="am-form-group">
|
||||
<button type="submit" class="am-btn am-btn-primary am-radius btn-loading-example am-btn-sm w100" data-am-loading="{loadingText:'处理中...'}">执行</button>
|
||||
</div>
|
||||
</form>
|
||||
<!-- form end -->
|
||||
|
||||
<!-- tips -->
|
||||
<div class="sql-tips">
|
||||
<div class="am-alert am-alert-success am-radius" data-am-alert style="display: none;"></div>
|
||||
<div class="am-alert am-alert-danger am-radius" data-am-alert style="display: none;"></div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<!-- right content end -->
|
||||
|
||||
<!-- footer start -->
|
||||
{{include file="public/footer" /}}
|
||||
<!-- footer end -->
|
||||
|
||||
<script type="text/javascript">
|
||||
// 返回处理
|
||||
function ViewImplementBack(e)
|
||||
{
|
||||
$('form.form-validation').find('button[type="submit"]').button('reset');
|
||||
$.AMUI.progress.done();
|
||||
if(e.code == 0)
|
||||
{
|
||||
$.AMUI.progress.done();
|
||||
Prompt(e.msg, 'success');
|
||||
if((e.data || null) != null)
|
||||
{
|
||||
$('.sql-tips .am-alert-success').show();
|
||||
$('.sql-tips .am-alert-danger').hide();
|
||||
$('.sql-tips .am-alert-success').html(e.data);
|
||||
} else {
|
||||
$('.sql-tips .am-alert-success').hide();
|
||||
$('.sql-tips .am-alert-danger').hide();
|
||||
}
|
||||
} else {
|
||||
Prompt(e.msg);
|
||||
$('.sql-tips .am-alert-success').hide();
|
||||
$('.sql-tips .am-alert-danger').show();
|
||||
$('.sql-tips .am-alert-danger').html(e.msg);
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
|
@ -258,7 +258,10 @@ return [
|
|||
// 连接dsn
|
||||
'dsn' => '',
|
||||
// 数据库连接参数
|
||||
'params' => [],
|
||||
'params' => [
|
||||
\PDO::ATTR_CASE => \PDO::CASE_LOWER,
|
||||
\PDO::ATTR_EMULATE_PREPARES => true,
|
||||
],
|
||||
// 数据库编码默认采用utf8mb4
|
||||
'charset' => '{$params['DB_CHARSET']}',
|
||||
// 数据库表前缀
|
||||
|
|
|
|||
|
|
@ -0,0 +1,75 @@
|
|||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | ShopXO 国内领先企业级B2C免费开源电商系统
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2011~2019 http://shopxo.net All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: Devil
|
||||
// +----------------------------------------------------------------------
|
||||
namespace app\service;
|
||||
|
||||
use think\Db;
|
||||
|
||||
/**
|
||||
* SQL控制台服务层
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 0.0.1
|
||||
* @datetime 2016-12-01T21:51:08+0800
|
||||
*/
|
||||
class SqlconsoleService
|
||||
{
|
||||
/**
|
||||
* SQL执行
|
||||
* @author Devil
|
||||
* @blog http://gong.gg/
|
||||
* @version 1.0.0
|
||||
* @date 2018-12-18
|
||||
* @desc description
|
||||
* @param [array] $params [输入参数]
|
||||
*/
|
||||
public static function Implement($params = [])
|
||||
{
|
||||
// 请求参数
|
||||
$p = [
|
||||
[
|
||||
'checked_type' => 'empty',
|
||||
'key_name' => 'sql',
|
||||
'error_msg' => '执行SQL不能为空',
|
||||
]
|
||||
];
|
||||
$ret = ParamsChecked($params, $p);
|
||||
if($ret !== true)
|
||||
{
|
||||
return DataReturn($ret, -1);
|
||||
}
|
||||
|
||||
// 转为数组
|
||||
$sql_all = preg_split("/;[\r\n]+/", $params['sql']);
|
||||
|
||||
$success = 0;
|
||||
$failure = 0;
|
||||
foreach($sql_all as $v)
|
||||
{
|
||||
if (!empty($v))
|
||||
{
|
||||
if(Db::execute($v) !== false)
|
||||
{
|
||||
$success++;
|
||||
} else {
|
||||
$failure++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if($failure > 0)
|
||||
{
|
||||
return DataReturn('sql运行失败['.$failure.']条', -1);
|
||||
}
|
||||
|
||||
return DataReturn('sql运行成功', 0, 'sql运行成功[success: '.$success.', failure: '.$failure.']');
|
||||
}
|
||||
}
|
||||
?>
|
||||
File diff suppressed because one or more lines are too long
Loading…
Reference in New Issue