修复在更新目录结构后插件无法使用的问题
This commit is contained in:
@@ -602,58 +602,6 @@ powerdata = [
|
||||
create_time=now_time,
|
||||
enable=1,
|
||||
|
||||
), Power(
|
||||
id=60,
|
||||
name='拓展插件',
|
||||
type='0',
|
||||
code='',
|
||||
url='',
|
||||
open_type='',
|
||||
parent_id='0',
|
||||
icon='layui-icon layui-icon-senior',
|
||||
sort=2,
|
||||
create_time=now_time,
|
||||
enable=1,
|
||||
|
||||
), Power(
|
||||
id=61,
|
||||
name='插件管理',
|
||||
type='1',
|
||||
code='admin:plugin:main',
|
||||
url='/plugin',
|
||||
open_type='_iframe',
|
||||
parent_id='60',
|
||||
icon='layui-icon layui-icon',
|
||||
sort=2,
|
||||
create_time=now_time,
|
||||
enable=1,
|
||||
|
||||
), Power(
|
||||
id=62,
|
||||
name='启禁插件',
|
||||
type='2',
|
||||
code='admin:plugin:enable',
|
||||
url='',
|
||||
open_type='',
|
||||
parent_id='61',
|
||||
icon='layui-icon layui-icon',
|
||||
sort=1,
|
||||
create_time=now_time,
|
||||
enable=1,
|
||||
|
||||
), Power(
|
||||
id=63,
|
||||
name='删除插件',
|
||||
type='2',
|
||||
code='admin:plugin:remove',
|
||||
url='',
|
||||
open_type='',
|
||||
parent_id='61',
|
||||
icon='layui-icon layui-icon',
|
||||
sort=2,
|
||||
create_time=now_time,
|
||||
enable=1,
|
||||
|
||||
)
|
||||
|
||||
]
|
||||
|
||||
@@ -46,8 +46,8 @@ class BaseConfig:
|
||||
REDIS_PORT = 6379
|
||||
|
||||
# mysql 配置
|
||||
MYSQL_USERNAME = "root"
|
||||
MYSQL_PASSWORD = "123456"
|
||||
MYSQL_USERNAME = "PearAdminFlask"
|
||||
MYSQL_PASSWORD = "hcCNtYR5GFtRwMab"
|
||||
MYSQL_HOST = "127.0.0.1"
|
||||
MYSQL_PORT = 3306
|
||||
MYSQL_DATABASE = "PearAdminFlask"
|
||||
|
||||
@@ -11,4 +11,4 @@ def init_view(app):
|
||||
register_rights_view(app)
|
||||
register_passport_views(app)
|
||||
register_dept_views(app)
|
||||
# register_plugin_views(app)
|
||||
register_plugin_views(app)
|
||||
|
||||
@@ -18,7 +18,7 @@ def register_plugin_views(app: Flask):
|
||||
app.register_blueprint(plugin_bp)
|
||||
# 载入插件过程
|
||||
# plugin_folder 配置的是插件的文件夹名
|
||||
PLUGIN_ENABLE_FOLDERS = json.loads(app.config['PLUGIN_ENABLE_FOLDERS'])
|
||||
PLUGIN_ENABLE_FOLDERS = app.config['PLUGIN_ENABLE_FOLDERS']
|
||||
for plugin_folder in PLUGIN_ENABLE_FOLDERS:
|
||||
plugin_info = {}
|
||||
try:
|
||||
@@ -39,124 +39,3 @@ def register_plugin_views(app: Flask):
|
||||
info += 'repr(e):\t' + repr(e) + "\n"
|
||||
info += 'traceback.format_exc():\n%s' + traceback.format_exc()
|
||||
print(info)
|
||||
|
||||
|
||||
@plugin_bp.get('/')
|
||||
@authorize("admin:plugin:main", log=True)
|
||||
def main():
|
||||
"""此处渲染管理模板"""
|
||||
return render_template('admin/plugin/main.html')
|
||||
|
||||
|
||||
@plugin_bp.get('/data')
|
||||
@authorize("admin:plugin:main", log=True)
|
||||
def data():
|
||||
"""请求插件数据"""
|
||||
plugin_name = escape(request.args.get("plugin_name"))
|
||||
all_plugins = []
|
||||
count = 0
|
||||
for filename in os.listdir("plugins"):
|
||||
try:
|
||||
with open("plugins/" + filename + "/__init__.json", "r", encoding='utf-8') as f:
|
||||
info = json.loads(f.read())
|
||||
|
||||
if plugin_name is None:
|
||||
if info['plugin_name'].find(plugin_name) == -1:
|
||||
continue
|
||||
|
||||
all_plugins.append(
|
||||
{
|
||||
"plugin_name": info["plugin_name"],
|
||||
"plugin_version": info["plugin_version"],
|
||||
"plugin_description": info["plugin_description"],
|
||||
"plugin_folder_name": filename,
|
||||
"enable": "1" if filename in PLUGIN_ENABLE_FOLDERS else "0"
|
||||
}
|
||||
)
|
||||
count += 1
|
||||
except BaseException as error:
|
||||
print(filename, error)
|
||||
continue
|
||||
return table_api(data=all_plugins, count=count)
|
||||
|
||||
|
||||
@plugin_bp.put('/enable')
|
||||
@authorize("admin:plugin:enable", log=True)
|
||||
def enable():
|
||||
"""启用插件"""
|
||||
plugin_folder_name = request.get_json(force=True).get('plugin_folder_name')
|
||||
if plugin_folder_name:
|
||||
try:
|
||||
if plugin_folder_name not in PLUGIN_ENABLE_FOLDERS:
|
||||
PLUGIN_ENABLE_FOLDERS.append(plugin_folder_name)
|
||||
with open(".flaskenv", "r", encoding='utf-8') as f:
|
||||
flaskenv = f.read() # type: str
|
||||
pos1 = flaskenv.find("PLUGIN_ENABLE_FOLDERS")
|
||||
pos2 = flaskenv.find("\n", pos1)
|
||||
with open(".flaskenv", "w", encoding='utf-8') as f:
|
||||
if pos2 == -1:
|
||||
f.write(flaskenv[:pos1] + "PLUGIN_ENABLE_FOLDERS = " + json.dumps(PLUGIN_ENABLE_FOLDERS))
|
||||
else:
|
||||
f.write(
|
||||
flaskenv[:pos1] + "PLUGIN_ENABLE_FOLDERS = " + json.dumps(PLUGIN_ENABLE_FOLDERS) + flaskenv[
|
||||
pos2:])
|
||||
# 启用插件事件
|
||||
try:
|
||||
getattr(importlib.import_module('plugins.' + plugin_folder_name), "event_enable")()
|
||||
except AttributeError: # 没有插件启用事件就不调用
|
||||
pass
|
||||
except BaseException as error:
|
||||
return fail_api(msg="Crash a error! Info: " + str(error))
|
||||
|
||||
except BaseException as error:
|
||||
return fail_api(msg="Crash a error! Info: " + str(error))
|
||||
return success_api(msg="启用成功,要使修改生效需要重启程序。")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
@plugin_bp.put('/disable')
|
||||
@authorize("admin:plugin:enable", log=True)
|
||||
def disable():
|
||||
"""禁用插件"""
|
||||
plugin_folder_name = request.get_json(force=True).get('plugin_folder_name')
|
||||
if plugin_folder_name:
|
||||
try:
|
||||
if plugin_folder_name in PLUGIN_ENABLE_FOLDERS:
|
||||
PLUGIN_ENABLE_FOLDERS.remove(plugin_folder_name)
|
||||
with open(".flaskenv", "r", encoding='utf-8') as f:
|
||||
flaskenv = f.read() # type: str
|
||||
pos1 = flaskenv.find("PLUGIN_ENABLE_FOLDERS")
|
||||
pos2 = flaskenv.find("\n", pos1)
|
||||
with open(".flaskenv", "w", encoding='utf-8') as f:
|
||||
if pos2 == -1:
|
||||
f.write(flaskenv[:pos1] + "PLUGIN_ENABLE_FOLDERS = " + json.dumps(PLUGIN_ENABLE_FOLDERS))
|
||||
else:
|
||||
f.write(
|
||||
flaskenv[:pos1] + "PLUGIN_ENABLE_FOLDERS = " + json.dumps(PLUGIN_ENABLE_FOLDERS) + flaskenv[
|
||||
pos2:])
|
||||
|
||||
# 禁用插件事件
|
||||
try:
|
||||
getattr(importlib.import_module('plugins.' + plugin_folder_name), "event_disable")()
|
||||
except AttributeError: # 没有插件禁用事件就不调用
|
||||
pass
|
||||
except BaseException as error:
|
||||
return fail_api(msg="Crash a error! Info: " + str(error))
|
||||
|
||||
except BaseException as error:
|
||||
return fail_api(msg="Crash a error! Info: " + str(error))
|
||||
return success_api(msg="禁用成功,要使修改生效需要重启程序。")
|
||||
return fail_api(msg="数据错误")
|
||||
|
||||
|
||||
# 删除
|
||||
@plugin_bp.delete('/remove/<string:plugin_folder_name>')
|
||||
@authorize("admin:mail:remove", log=True)
|
||||
def delete(plugin_folder_name):
|
||||
if plugin_folder_name in PLUGIN_ENABLE_FOLDERS:
|
||||
return fail_api(msg="您必须先禁用插件!")
|
||||
try:
|
||||
shutil.rmtree(os.path.abspath("plugins/" + plugin_folder_name))
|
||||
return success_api(msg="删除成功")
|
||||
except BaseException as error:
|
||||
return fail_api(msg="删除失败!原因:" + str(error))
|
||||
|
||||
@@ -1,288 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>插件管理</title>
|
||||
{% include 'admin/common/header.html' %}
|
||||
<link rel="stylesheet" href="{{ url_for('static', filename='/admin/admin/css/other/user.css') }}"/>
|
||||
</head>
|
||||
<body class="pear-container">
|
||||
{# 查询表单 #}
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<form class="layui-form" action="" lay-filter="plugin-query-form">
|
||||
<div class="layui-form-item">
|
||||
<label class="layui-form-label">插件名称</label>
|
||||
<div class="layui-input-inline">
|
||||
<input type="text" name="plugin_name" placeholder="" class="layui-input">
|
||||
</div>
|
||||
<button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="plugin-query">
|
||||
<i class="layui-icon layui-icon-search"></i>
|
||||
查询
|
||||
</button>
|
||||
<button type="reset" class="pear-btn pear-btn-md">
|
||||
<i class="layui-icon layui-icon-refresh"></i>
|
||||
重置
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
{# 用户表格 #}
|
||||
<div class="user-plugin user-collasped">
|
||||
<div class="layui-card">
|
||||
<div class="layui-card-body">
|
||||
<table id="plugin-table" lay-filter="plugin-table"></table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
{# 表格操作 #}
|
||||
<script type="text/html" id="plugin-toolbar">
|
||||
{% if authorize("admin:plugin:enable") %}
|
||||
{# <button class="pear-btn pear-btn-primary pear-btn-md" lay-event="add">#}
|
||||
{# <i class="pear-icon pear-icon-add"></i>#}
|
||||
{# 新增#}
|
||||
{# </button>#}
|
||||
{% endif %}
|
||||
{% if authorize("admin:plugin:remove") %}
|
||||
{# <button class="pear-btn pear-btn-md" lay-event="batchRemove">#}
|
||||
{# <i class="pear-icon pear-icon-ashbin"></i>#}
|
||||
{# 删除#}
|
||||
{# </button>#}
|
||||
{% endif %}
|
||||
</script>
|
||||
|
||||
{# 用户修改操作 #}
|
||||
<script type="text/html" id="plugin-bar">
|
||||
{% if authorize("admin:plugin:remove") %}
|
||||
<button class="pear-btn pear-btn-danger pear-btn-sm" lay-event="remove"><i
|
||||
class="pear-icon pear-icon-ashbin"></i>
|
||||
</button>
|
||||
{% endif %}
|
||||
</script>
|
||||
|
||||
{# 启动与禁用 #}
|
||||
<script type="text/html" id="plugin-enable">
|
||||
<input type="checkbox" name="enable" value="{{ "{{ d.plugin_folder_name }}" }}" lay-skin="switch"
|
||||
lay-text="启用|禁用"
|
||||
lay-filter="plugin-enable"
|
||||
{{ "{{# if(d.enable==1){ }} checked {{# } }}" }} />
|
||||
</script>
|
||||
|
||||
{# 用户注册时间 #}
|
||||
<script type="text/html" id="plugin-createTime">
|
||||
{{ ' {{layui.util.toDateString(d.create_at, "yyyy-MM-dd HH:mm:ss")}' |safe }}}
|
||||
</script>
|
||||
|
||||
{% include 'admin/common/footer.html' %}
|
||||
|
||||
<script>
|
||||
// 重启程序
|
||||
function restart() {
|
||||
layui.use(['layer'], function () {
|
||||
var $ = layui.jquery;
|
||||
let popup = layui.popup;
|
||||
layer.confirm('需要重启程序吗?这样可以使插件更改生效。(如果没有守护进程需要手动启动。)', {
|
||||
icon: 3,
|
||||
title: '提示'
|
||||
}, function (index) {
|
||||
layer.close(index)
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
url: '/admin/monitor/kill',
|
||||
dataType: 'json',
|
||||
type: 'get',
|
||||
error: function (result) {
|
||||
setInterval(function () {
|
||||
tip = false
|
||||
$.ajax({
|
||||
url: '/admin/monitor',
|
||||
type: 'get',
|
||||
success: function (result) {
|
||||
layer.close(loading)
|
||||
if (tip == false) {
|
||||
popup.success("重启程序成功!", function () {
|
||||
window.location.reload()
|
||||
})
|
||||
}
|
||||
}
|
||||
})
|
||||
}, 1000);
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
layui.use(['table', 'form', 'jquery', 'popup', 'common'], function () {
|
||||
let table = layui.table
|
||||
let form = layui.form
|
||||
let $ = layui.jquery
|
||||
let dtree = layui.dtree
|
||||
let popup = layui.popup
|
||||
let common = layui.common
|
||||
let MODULE_PATH = '/plugin/'
|
||||
|
||||
// 表格数据
|
||||
let cols = [
|
||||
[
|
||||
{% if authorize("admin:plugin:remove") %}
|
||||
{type: 'checkbox'},
|
||||
{% endif %}
|
||||
{field: 'plugin_name', minWidth: 100, title: '插件名称'},
|
||||
{field: 'plugin_version', title: '插件版本'},
|
||||
{field: 'plugin_description', title: '插件描述'},
|
||||
{field: 'plugin_folder_name', hide: true},
|
||||
{field: 'enable', title: '状态', templet: '#plugin-enable', width: 100, align: 'center'},
|
||||
{title: '操作', templet: '#plugin-bar', width: 120, align: 'center'}
|
||||
]
|
||||
]
|
||||
|
||||
// 渲染表格数据
|
||||
table.render({
|
||||
elem: '#plugin-table',
|
||||
url: MODULE_PATH + 'data',
|
||||
page: true,
|
||||
cols: cols,
|
||||
skin: 'line',
|
||||
height: 'full-148',
|
||||
toolbar: '#plugin-toolbar', /*工具栏*/
|
||||
text: {none: '暂无人员信息'},
|
||||
defaultToolbar: [{layEvent: 'refresh', icon: 'layui-icon-refresh'}, 'filter', 'print', 'exports'] /*默认工具栏*/
|
||||
})
|
||||
|
||||
|
||||
// 禁用启用
|
||||
form.on('switch(plugin-enable)', function (obj) {
|
||||
let operate
|
||||
if (obj.elem.checked) {
|
||||
operate = 'enable'
|
||||
} else {
|
||||
operate = 'disable'
|
||||
}
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
url: MODULE_PATH + operate,
|
||||
data: JSON.stringify({plugin_folder_name: this.value}),
|
||||
dataType: 'json',
|
||||
contentType: 'application/json',
|
||||
type: 'put',
|
||||
success: function (result) {
|
||||
layer.close(loading)
|
||||
if (result.success) {
|
||||
popup.success(result.msg)
|
||||
restart()
|
||||
} else {
|
||||
popup.failure(result.msg)
|
||||
}
|
||||
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
table.on('tool(plugin-table)', function (obj) {
|
||||
if (obj.event === 'remove') {
|
||||
window.remove(obj)
|
||||
}
|
||||
})
|
||||
|
||||
table.on('toolbar(plugin-table)', function (obj) {
|
||||
if (obj.event === 'add') {
|
||||
window.add()
|
||||
} else if (obj.event === 'refresh') {
|
||||
window.refresh()
|
||||
} else if (obj.event === 'batchRemove') {
|
||||
window.batchRemove(obj)
|
||||
}
|
||||
})
|
||||
|
||||
form.on('submit(plugin-query)', function (data) {
|
||||
window.refresh(data.field)
|
||||
return false
|
||||
})
|
||||
|
||||
window.add = function () {
|
||||
layer.open({
|
||||
type: 2,
|
||||
title: '新增',
|
||||
shade: 0.1,
|
||||
area: ['550px', '550px'],
|
||||
content: MODULE_PATH + 'add'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
window.remove = function (obj) {
|
||||
layer.confirm('确定要删除', {icon: 3, title: '提示'}, function (index) {
|
||||
layer.close(index)
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
url: MODULE_PATH + 'remove/' + obj.data['plugin_folder_name'],
|
||||
dataType: 'json',
|
||||
type: 'delete',
|
||||
success: function (result) {
|
||||
layer.close(loading)
|
||||
if (result.success) {
|
||||
popup.success(result.msg, function () {
|
||||
obj.del()
|
||||
})
|
||||
} else {
|
||||
popup.failure(result.msg)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
window.batchRemove = function (obj) {
|
||||
let data = table.checkStatus(obj.config.id).data
|
||||
if (data.length === 0) {
|
||||
layer.msg('未选中数据', {
|
||||
icon: 3,
|
||||
time: 1000
|
||||
})
|
||||
return false
|
||||
}
|
||||
var ids = []
|
||||
var hasCheck = table.checkStatus('plugin-table')
|
||||
var hasCheckData = hasCheck.data
|
||||
if (hasCheckData.length > 0) {
|
||||
$.each(hasCheckData, function (index, element) {
|
||||
ids.push(element.id)
|
||||
})
|
||||
}
|
||||
{#console.log(ids);#}
|
||||
layer.confirm('确定要删除选中数据', {
|
||||
icon: 3,
|
||||
title: '提示'
|
||||
}, function (index) {
|
||||
layer.close(index)
|
||||
let loading = layer.load()
|
||||
$.ajax({
|
||||
|
||||
url: MODULE_PATH + 'batchRemove',
|
||||
data: {ids: ids},
|
||||
dataType: 'json',
|
||||
type: 'delete',
|
||||
success: function (result) {
|
||||
layer.close(loading)
|
||||
if (result.success) {
|
||||
popup.success(result.msg, function () {
|
||||
table.reload('plugin-table')
|
||||
})
|
||||
} else {
|
||||
popup.failure(result.msg)
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
window.refresh = function (param) {
|
||||
table.reload('plugin-table', {where: param})
|
||||
}
|
||||
})
|
||||
</script>
|
||||
</html>
|
||||
Reference in New Issue
Block a user