新增插件卸载时删除相关数据表功能

pull/192/MERGE
Karson 2020-08-22 20:55:13 +08:00
parent 3d9cde96a1
commit 260198e1cb
4 changed files with 162 additions and 98 deletions

View File

@ -8,6 +8,7 @@ use think\addons\AddonException;
use think\addons\Service; use think\addons\Service;
use think\Cache; use think\Cache;
use think\Config; use think\Config;
use think\Db;
use think\Exception; use think\Exception;
/** /**
@ -19,6 +20,7 @@ use think\Exception;
class Addon extends Backend class Addon extends Backend
{ {
protected $model = null; protected $model = null;
protected $noNeedRight = ['get_table_list'];
public function _initialize() public function _initialize()
{ {
@ -144,14 +146,26 @@ class Addon extends Backend
{ {
$name = $this->request->post("name"); $name = $this->request->post("name");
$force = (int)$this->request->post("force"); $force = (int)$this->request->post("force");
$droptables = (int)$this->request->post("droptables");
if (!$name) { if (!$name) {
$this->error(__('Parameter %s can not be empty', 'name')); $this->error(__('Parameter %s can not be empty', 'name'));
} }
if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) { if (!preg_match("/^[a-zA-Z0-9]+$/", $name)) {
$this->error(__('Addon name incorrect')); $this->error(__('Addon name incorrect'));
} }
//只有开启调试且为超级管理员才允许删除相关数据库
$tables = [];
if ($droptables && Config::get("app_debug") && $this->auth->isSuperAdmin()) {
$tables = get_addon_tables($name);
}
try { try {
Service::uninstall($name, $force); Service::uninstall($name, $force);
if ($tables) {
//删除插件关联表
foreach ($tables as $index => $table) {
Db::execute("DROP TABLE IF EXISTS `{$table}`");
}
}
$this->success(__('Uninstall successful')); $this->success(__('Uninstall successful'));
} catch (AddonException $e) { } catch (AddonException $e) {
$this->result($e->getData(), $e->getCode(), __($e->getMessage())); $this->result($e->getData(), $e->getCode(), __($e->getMessage()));
@ -367,4 +381,14 @@ class Addon extends Backend
$callback = $this->request->get('callback') ? "jsonp" : "json"; $callback = $this->request->get('callback') ? "jsonp" : "json";
return $callback($result); return $callback($result);
} }
/**
* 获取插件相关表
*/
public function get_table_list()
{
$name = $this->request->post("name");
$tables = get_addon_tables($name);
$this->success('', null, ['tables' => $tables]);
}
} }

View File

@ -90,4 +90,10 @@ return [
'Addon already exists' => '上传的插件已经存在', 'Addon already exists' => '上传的插件已经存在',
'Unable to open the zip file' => '无法打开ZIP文件', 'Unable to open the zip file' => '无法打开ZIP文件',
'Unable to extract the file' => '无法解压ZIP文件', 'Unable to extract the file' => '无法解压ZIP文件',
'Are you sure you want to unstall %s?' => '确认卸载插件《%s》?',
'Delete all the addon file and cannot be recovered!' => '卸载将会删除所有插件文件且不可找回!!!',
'Delete all the addon database and cannot be recovered!' => '删除所有插件相关数据表且不可找回!!!',
'Please backup important data manually before uninstall!' => '如有重要数据请备份后再操作!!!',
'The following data tables will be deleted' => '以下插件数据表将会被删除',
'The Addon did not create a data table' => '插件未创建任何数据表',
]; ];

View File

@ -53,9 +53,11 @@
position: absolute; position: absolute;
box-shadow: 0px 0px 2px #f11414; box-shadow: 0px 0px 2px #f11414;
} }
.form-userinfo .breadcrumb { .form-userinfo .breadcrumb {
margin-bottom: 10px; margin-bottom: 10px;
} }
.btn-toggle { .btn-toggle {
padding: 0; padding: 0;
} }
@ -205,6 +207,17 @@
<%}%> <%}%>
</div> </div>
</script> </script>
<script id="uninstalltpl" type="text/html">
<div class="">
<div class=""><%=__("Are you sure you want to unstall %s?", addon['title'])%>
<p class="text-danger">{:__('Delete all the addon file and cannot be recovered!')} </p>
{if config('app_debug')}
<p class="text-danger"><input type="checkbox" name="droptables" id="droptables" data-name="<%=addon['name']%>"/> {:__('Delete all the addon database and cannot be recovered!')} </p>
{/if}
<p class="text-danger">{:__('Please backup important data manually before uninstall!')}</p>
</div>
</div>
</script>
<script id="conflicttpl" type="text/html"> <script id="conflicttpl" type="text/html">
<div class="alert alert-dismissable alert-danger"> <div class="alert alert-dismissable alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button> <button type="button" class="close" data-dismiss="alert">×</button>

View File

@ -196,6 +196,26 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1}); table.bootstrapTable('refresh', {url: $(this).data("url"), pageNumber: 1});
return false; return false;
}); });
var tables = [];
$(document).on("click", "#droptables", function () {
if ($(this).prop("checked")) {
Fast.api.ajax({
url: "addon/get_table_list",
async: false,
data: {name: $(this).data("name")}
}, function (data) {
tables = data.tables;
return false;
});
var html;
html = tables.length > 0 ? '<div class="alert alert-warning-light droptablestips" style="max-width:480px;max-height:300px;overflow-y: auto;">' + __('The following data tables will be deleted') + '<br>' + tables.join("<br>") + '</div>'
: '<div class="alert alert-warning-light droptablestips">' + __('The Addon did not create a data table') + '</div>';
$(html).insertAfter($(this).closest("p"));
} else {
$(".droptablestips").remove();
}
$(window).resize();
});
// 会员信息 // 会员信息
$(document).on("click", ".btn-userinfo", function () { $(document).on("click", ".btn-userinfo", function () {
@ -350,10 +370,10 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
}); });
}; };
var uninstall = function (name, force) { var uninstall = function (name, force, droptables) {
Fast.api.ajax({ Fast.api.ajax({
url: 'addon/uninstall', url: 'addon/uninstall',
data: {name: name, force: force ? 1 : 0} data: {name: name, force: force ? 1 : 0, droptables: droptables ? 1 : 0}
}, function (data, ret) { }, function (data, ret) {
delete Config['addons'][name]; delete Config['addons'][name];
Layer.closeAll(); Layer.closeAll();
@ -372,7 +392,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
}, },
yes: function () { yes: function () {
uninstall(name, true); uninstall(name, true, droptables);
} }
}); });
@ -466,8 +486,9 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
Layer.alert(__('Please disable addon first'), {icon: 7}); Layer.alert(__('Please disable addon first'), {icon: 7});
return false; return false;
} }
Layer.confirm(__('Uninstall tips', Config['addons'][name].title), function () { Template.helper("__", __);
uninstall(name, false); Layer.confirm(Template("uninstalltpl", {addon: Config['addons'][name]}), function (index, layero) {
uninstall(name, false, $("input[name='droptables']", layero).prop("checked"));
}); });
}); });