Files
swiftadmin/public/static/system/module/plugin.js

364 lines
13 KiB
JavaScript
Raw Normal View History

2022-08-19 19:48:37 +08:00
/* 插件管理模块 */
2023-08-04 11:07:55 +08:00
layui.define(['admin','show'], function (exports) {
let $ = layui.jquery;
let table = layui.table;
let form = layui.form;
let admin = layui.admin;
let show = layui.show;
let plugin = {
apiUrl: app_Config.api,
baseUrl: app_Config.app,
data:{}, // 插件数据
2022-08-19 19:48:37 +08:00
request: function (name, version, url) {
let index = layer.load();
2023-08-04 11:07:55 +08:00
let token = admin.getStorage('api_cross_token') || null;
2022-08-19 19:48:37 +08:00
if (token == null || token === 'undefined') {
layer.close(index);
plugin.login();
return false;
}
$.post(plugin.baseUrl + url, {
name: name,
version: version,
token: token,
}, function (res) {
layer.closeAll();
if (res.code === 200) {
2023-08-04 11:07:55 +08:00
show.msg(res.msg);
window.plugins[name] = res.data;
table.reloadData('lay-tableList');
2022-08-19 19:48:37 +08:00
top.layui.admin.reloadLayout();
} else {
2023-08-04 11:07:55 +08:00
show.error(res.msg);
2022-08-19 19:48:37 +08:00
// 登录超时
if (res.code === -101) {
plugin.login();
return false;
}
// 付费插件
if (res.code === -102) {
plugin.pay(res.data);
return false;
}
}
}, 'json');
}
2023-08-04 11:07:55 +08:00
, upgrade: function (data, token, url) {
2022-08-19 19:48:37 +08:00
let html = '<blockquote class="layui-elem-quote layui-elem-upgrade">温馨提示<br/>';
2023-08-04 11:07:55 +08:00
html += '<span class="upgrade-title">确认升级 《' + data.pluginName + '》 ' + data.version + ' 版本吗?</span><br/>';
2022-08-19 19:48:37 +08:00
html += '1、请务必做好服务器代码和数据库备份<br/>';
html += '2、升级后如出现冗余数据请根据需要移除即可<br/>';
html += '3、请勿跨版本升级如必要请参考插件使用文档<br/>';
html += '4、已部署完成的插件请确保服务器Web权限可读写<br/>';
html += '5、生产环境下更新维护插件请勿在流量高峰期操作<br/>';
html += '</blockquote>';
2023-08-04 11:07:55 +08:00
let confirm = layer.confirm(html, {
title: '更新提示',
2022-08-19 19:48:37 +08:00
}, function () {
layer.close(confirm);
2023-08-04 11:07:55 +08:00
plugin.request(data.name, data.version, plugin.getUrl('Plugin', 'upgrade'));
}, function () {
});
2022-08-19 19:48:37 +08:00
}
, login: function () {
layer.open({
type: 1,
title: '登录',
shadeClose: true,
area: ['500px', '350px'],
content: plugin.getHtml(),
success: function (index, layero) {
form.on('submit(login)', function (data) {
$.post(plugin.apiUrl + '/user/login',
data.field, function (res) {
if (res.code === 200) {
2023-08-04 11:07:55 +08:00
admin.setStorage('api_cross_token', res.data.token);
2022-08-19 19:48:37 +08:00
layer.closeAll();
plugin.againClick();
} else {
2023-08-04 11:07:55 +08:00
show.error(res.msg);
2022-08-19 19:48:37 +08:00
}
}, 'json')
return false;
})
}
})
}
, clearLogin: function () {
layer.msg('清除登录信息成功');
2023-08-04 11:07:55 +08:00
admin.setStorage('api_cross_token', null);
2022-08-19 19:48:37 +08:00
}
, pay: function (data) {
layer.open({
type: 2,
2023-08-04 11:07:55 +08:00
title: '立即支付',
area: ['500px', '550px'],
2022-08-19 19:48:37 +08:00
offset: "30px",
resize: false,
shade: 0.8,
shadeClose: true,
2022-11-28 19:11:12 +08:00
content: data.pay_url,
2022-08-19 19:48:37 +08:00
success: function (index, layero) {
window.onmessage = function (res) {
let data = res.data;
if (res.data !== null && data.code === 200) {
layer.close(layero);
plugin.againClick();
}
}
}
});
}
, againClick: function () {
2023-08-04 11:07:55 +08:00
if (plugin.data == null || plugin.data === 'undefined') {
return false;
2022-08-19 19:48:37 +08:00
}
2023-08-04 11:07:55 +08:00
plugin.request(plugin.data.name, plugin.data.version, plugin.getUrl('Plugin', 'install'));
}
, install(name, version) {
plugin.data = {name: name, version: version};
plugin.request(name, version, plugin.getUrl('Plugin', 'install'));
2022-08-19 19:48:37 +08:00
}
, uninstall: function (name, tables) {
let appURL = plugin.baseUrl;
$.post(appURL + plugin.getUrl('Plugin', 'uninstall'), {
name: name,
tables: tables,
}, function (res) {
if (res.code === 200) {
2023-08-04 11:07:55 +08:00
show.msg(res.msg);
2022-08-19 19:48:37 +08:00
delete window.plugins[name];
2023-08-04 11:07:55 +08:00
table.reloadData('lay-tableList');
2022-08-19 19:48:37 +08:00
} else {
2023-08-04 11:07:55 +08:00
show.error(res.msg);
2022-08-19 19:48:37 +08:00
}
layer.close(window.unIndex);
}, 'json');
}
, getUrl(type, action) {
return '/system/' + type + '/' + action;
}
, getTableData: function (that) {
if (that == null || that === 'undefined') {
return false;
}
2023-08-04 11:07:55 +08:00
let index = $(that).parents('tr').attr('data-index');
2022-08-19 19:48:37 +08:00
index = table.cache['lay-tableList'][index];
return index;
}
, getHtml: function () {
2023-08-04 11:07:55 +08:00
let html = '<form class="layui-form layui-form-fixed" style="padding-right:15px;" >';
2022-08-19 19:48:37 +08:00
html += '<blockquote class="layui-elem-quote layui-elem-plugin">';
html += '</blockquote><div style="height:20px;"></div>';
html += '<div class="layui-form-item">';
html += '<label class="layui-form-label">用户邮箱</label>';
html += '<div class="layui-input-block">';
2023-06-19 14:32:30 +08:00
html += '<input type="text" name="nickname" style="width:330px;" lay-verify="required" placeholder="请输入邮箱或用户名" autocomplete="off" class="layui-input" >';
2022-08-19 19:48:37 +08:00
html += '</div></div>';
html += '<div class="layui-form-item"><label class="layui-form-label">密码</label>';
html += '<div class="layui-input-block">';
html += '<input type="password" name="pwd" style="width:330px;" lay-verify="required" placeholder="请输入密码" class="layui-input">';
html += '</div></div>';
html += '<div class="layui-form-item" style="margin-top: 22px;text-align: center;">';
html += '<a class="layui-btn layui-btn-primary" href="http://www.swiftadmin.net/user/register" target="_blank">注册</a>';
html += '<button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="login">登录</button>';
html += '</div></form> ';
return html;
},
};
/**
* 查询插件信息
*/
$('.layui-plugin-select').click(function () {
2023-08-04 11:07:55 +08:00
let that = $(this);
2022-08-19 19:48:37 +08:00
if (that.hasClass('active') && !that.hasClass('first')) {
that.removeClass('active');
that.siblings('span.first').addClass('active');
} else {
that.siblings('.active').removeClass('active');
that.addClass('active');
}
2023-08-04 11:07:55 +08:00
let data = {}, elem = $('.active');
2022-08-19 19:48:37 +08:00
elem.each(function (e, n) {
2023-08-04 11:07:55 +08:00
let type = $(n).parent().attr('name');
data[type] = $(n).attr('data-value') || '';
2022-08-19 19:48:37 +08:00
})
2023-08-04 11:07:55 +08:00
let b = ['type', 'pay', 'label'];
2022-08-19 19:48:37 +08:00
for (let i in b) {
if (!data[b[i]]) {
data[b[i]] = '';
}
}
table.reload('lay-tableList', {
where: data,
url: plugin.apiUrl + "plugin/index",
});
})
/**
* 安装插件
* @param that
* @returns {boolean}
*/
$(document).on("click", ".install", function () {
let name = plugin.getTableData(this)['name'];
2023-08-04 11:07:55 +08:00
let version = plugin.getTableData(this)['version'];
plugin.install(name, version);
2022-08-19 19:48:37 +08:00
})
/**
* 更改插件状态
* @param that
* @returns {boolean}
*/
form.on('switch(pluginStatus)', function (obj) {
let objs = {
id: $(this).attr('value'),
status: obj.elem.checked ? 1 : 0
};
let that = $(this), options = {
error: function (res) {
$(obj.elem).prop('checked', !obj.elem.checked);
form.render('checkbox');
2023-08-04 11:07:55 +08:00
show.msg(res.msg);
2022-08-19 19:48:37 +08:00
},
success: function (res) {
layer.msg(res.msg);
if (typeof window.plugins[objs.id] !== 'undefined') {
window.plugins[objs.id].status = objs.status;
}
top.layui.admin.reloadLayout();
}
};
let bubble = $('.bubble');
if (bubble.length) {
$(bubble).removeClass('bubble');
return false;
}
2023-08-04 11:07:55 +08:00
admin.event.request(that, objs, options);
2022-08-19 19:48:37 +08:00
});
/**
* 卸载插件
* @param that
* @returns {boolean}
*/
$(document).on("click", ".uninstall", function (obj) {
2023-08-04 11:07:55 +08:00
let name = plugin.getTableData(this)['name'];
let html = '<form class="layui-form layui-form-fixed" style="padding-right:15px;background: #f2f2f2;" >';
2022-08-19 19:48:37 +08:00
html += '<blockquote class="layui-elem-quote layui-elem-uninstall">温馨提示<br/>';
html += '确认卸载 《 ' + name + ' 》 吗?<br/>';
html += '1、卸载前请自行备份插件数据库 <br/>';
html += '2、插件文件及数据库表删除后无法找回 <br/>';
html += '3、插件如已被二次开发请自行清除冗余文件<br/>';
html += '</blockquote>';
html += '<div class="layui-form-item">';
html += '<div class="layui-input-inline" style="padding-left: 5px;">';
html += '<div class="layui-plugin-tables layui-badge-red"></div></div></div>';
html += '<div class="layui-footer" style="margin-top: 22px;text-align: center;">';
html += '<button type="submit" class="layui-btn layui-btn-normal" lay-submit lay-filter="start">确定</button>';
html += '<button type="button" class="layui-btn layui-btn-primary" sa-event="closeDialog" >关闭</button>';
html += '</div></form> ';
2023-08-04 11:07:55 +08:00
2022-08-19 19:48:37 +08:00
layer.open({
type: 1,
2023-08-04 11:07:55 +08:00
title: '卸载插件',
2022-08-19 19:48:37 +08:00
shadeClose: true,
area: ['380px', '300px'],
content: html,
success: function (index, layero) {
/**
* 请求服务器
* @param 卸载数据表
*/
form.on('submit(start)', function (data) {
let tables = [];
let lists = $('.layui-plugin-tables').children('span');
lists.each(function (e, n) {
if (e >= 1) {
tables.push($(n).html());
}
})
window.unIndex = layer.load();
layer.close(layero);
plugin.uninstall(name, tables);
return false;
})
}
})
})
/**
* 重载本地插件
* @param that
*/
$('#pluginInstall').click(function (res) {
2023-08-04 11:07:55 +08:00
table.reloadData('lay-tableList', {
2022-08-19 19:48:37 +08:00
url: plugin.baseUrl + '/system/Plugin/index',
});
})
/**
* 更新插件缓存
* @param that
*/
$('#pluginCache').click(function (res) {
2023-08-04 11:07:55 +08:00
let confirm = layer.confirm('确定要更新缓存吗?', {
2022-08-19 19:48:37 +08:00
title: '更新提示'
}, function () {
$.get(plugin.baseUrl + plugin.getUrl('Admin', 'clear?type=plugin'), {}, function (res) {
if (res.code === 200) {
layer.msg(res.msg);
layer.close(confirm);
} else {
2023-08-04 11:07:55 +08:00
show.error(res.msg);
2022-08-19 19:48:37 +08:00
}
})
});
})
/**
* 插件幻灯片
*/
$('body').on('click', '.layui-icon-picture', function (res) {
let index = plugin.getTableData(this), data = [];
if (typeof index.album !== 'undefined' && index.album.length) {
for (let i in index.album) {
data.push({
src: index.album[i].src,
thumb: index.album[i].src,
})
}
}
layer.photos({
photos: {data: data},
shadeClose: true,
closeBtn: 2,
anim: 10
})
})
exports('plugin', plugin);
});