Files
swiftadmin/public/static/js/center.js

484 lines
16 KiB
JavaScript
Raw Normal View History

/*!
* CommonJS For Home - v1.0.0 - 2022-10-10
* https://github.com/meystack/swiftadmin
* Copyright (c) meystack
* Licensed Apache2.0
2022-08-19 19:48:37 +08:00
*/
layui.use(['jquery', 'form', 'upload', 'table', 'dropdown'], function () {
2022-08-19 19:48:37 +08:00
let $ = layui.$;
let form = layui.form;
let layer = layui.layer;
2022-11-28 19:11:12 +08:00
let table = layui.table;
2022-08-19 19:48:37 +08:00
let upload = layui.upload;
2022-11-28 19:11:12 +08:00
let dropdown = layui.dropdown;
/**
* 短消息下拉框
* @param count
*/
window.bellMessage = function (count) {
dropdown.render({
2022-11-28 19:11:12 +08:00
elem: '#notice'
, trigger: 'hover'
, align: 'center'
, data: [{
title: !count ? '暂无消息' : '您有<b class="msg">' + count + '</b>条未读消息'
}], ready: function (elemPanel, elem) {
}
, click: function (data, index) {
console.log(data, index);
2022-11-28 19:11:12 +08:00
let elem = $('.layui-nav-tree li [lay-href="/user/message"]');
$(elem).parents('.layui-nav-item').addClass('layui-nav-itemed');
$(elem).trigger('click');
}
});
}
2022-08-19 19:48:37 +08:00
/**
* 前端全局对象
* @access object
* screen: 屏幕宽度
* event: 事件对象
* Cookie: Cookie操作对象
* */
2022-08-19 19:48:37 +08:00
window.Home = {
screen: function () {
let width = $(window).width()
if (width > 1200) {
2022-08-19 19:48:37 +08:00
return 3; //大屏幕
} else if (width > 992) {
2022-08-19 19:48:37 +08:00
return 2; //中屏幕
} else if (width > 768) {
2022-08-19 19:48:37 +08:00
return 1; //小屏幕
} else {
return 0; //超小屏幕
}
},
event: {
closeDialog: function (that) {
2022-08-19 19:48:37 +08:00
that = that || this;
let _type = $(that).parents(".layui-layer").attr("type");
if (typeof _type === "undefined") {
parent.layer.close(parent.layer.getFrameIndex(window.name));
} else {
2022-08-19 19:48:37 +08:00
let layerId = $(that).parents(".layui-layer").attr("id").substring(11);
layer.close(layerId);
}
}
},
// cookie
Cookie: { // 获取cookies
2022-08-19 19:48:37 +08:00
'Set': function (name, value, days) {
let exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
2022-08-19 19:48:37 +08:00
document.cookie = name + "=" + escape(value) + ";path=/;expires=" + exp.toUTCString();
},
'Get': function (name) {
let arr = document.cookie.match(new RegExp("(^| )" + name + "=([^;]*)(;|$)"));
if (arr != null) {
return unescape(arr[2]);
}
return null;
2022-08-19 19:48:37 +08:00
},
'Del': function (name) {
let exp = new Date();
exp.setTime(exp.getTime() - 1);
let cval = this.Get(name);
if (cval != null) {
document.cookie = name + "=" + escape(cval) + ";path=/;expires=" + exp.toUTCString();
}
}
}
}
/**
* 监听全局form表单提交
* @param data
* @param callback
* @type button lay-filter="submitIframe"
*/
form.on('submit(submitIframe)', function (data) {
2022-08-19 19:48:37 +08:00
let that = $(this), _form = that.parents('form'),
_url = _form.attr("action") || false,
_close = that.data("close") || undefined,
_reload = that.data('reload');
// 开启节流
that.attr("disabled", true);
$.post(_url,
data.field, function (res) {
if (res.code === 200) {
2022-08-19 19:48:37 +08:00
top.layer.msg(res.msg);
switch (_reload) {
case 'top':
top.location.reload();
break;
case 'parent':
parent.location.reload();
break;
case 'self':
location.reload();
break;
default:
}
if (typeof res.url !== 'undefined' && res.url) {
location.href = res.url;
}
// 默认关闭
if (_close === undefined) {
Home.event.closeDialog(that);
}
} else {
top.layui.layer.msg(res.msg, 'error');
2022-08-19 19:48:37 +08:00
}
try {
/**
* token重载下
* 框架也需要更新
*/
if (typeof res.data.__token__ !== 'undefined') {
$('input#__token__').val(res.data.__token__);
}
} catch (e) {
}
that.attr("disabled", false);
2022-08-19 19:48:37 +08:00
}, 'json');
return false;
})
/**
* 监听form表单搜索
* 默认表格ID: lay-tableList
2022-08-19 19:48:37 +08:00
*/
form.on('submit(formSearch)', function (data) {
let field = data.field;
2022-08-19 19:48:37 +08:00
for (const key in field) {
if (!field[key]) {
delete field[key];
}
}
layui.table.reload('lay-tableList', {
page: {curr: 1},
where: field
});
})
// 监听全局事件
$(document).on("click", "*[sa-event]", function () {
let name = $(this).attr("sa-event");
let obj = Home.event[name];
obj && obj.call(this, $(this));
});
let uploadURL = '/index/user/upload';
2022-08-19 19:48:37 +08:00
layui.each($('*[lay-upload]'), function (index, elem) {
let that = $(this),
2022-08-19 19:48:37 +08:00
name = $(elem).attr('lay-upload') || undefined,
type = $(elem).data('type') || 'normal',
size = $(elem).data('size') || 51200, // 限制最大5M
accept = $(elem).data('accept') || 'file';
2022-08-19 19:48:37 +08:00
// 文件上传回调
let uploadFiles = {
2022-08-19 19:48:37 +08:00
normal: function (res, name) {
$('input.' + name).prop('value', res.url);
$('img.' + name).prop('src', res.url);
},
images: function (res, name) {
let o = $('img.' + name);
2022-08-19 19:48:37 +08:00
o.prop('src', res.url);
o.parent('div').removeClass('layui-hide');
$('input.' + name).val(res.url);
$(elem).find('p,i,hr').addClass('layui-hide');
},
multiple: function (res, name) {
let index = $('.layui-imagesbox .layui-input-inline');
2022-08-19 19:48:37 +08:00
index = index.length ? index.length - 1 : 0;
let html = '<div class="layui-input-inline">';
html += '<img src="' + res.url + '" alt="alt" >';
2022-08-19 19:48:37 +08:00
html += '<input type="text" name="' + name + '[' + index + '][src]" class="layui-hide" value="' + res.url + '">';
html += '<input type="text" name="' + name + '[' + index + '][title]" class="layui-input" placeholder="图片简介">';
html += '<span class="layui-badge layui-badge-red" onclick="layui.$(this).parent().remove();">删除</span></div>';
$(elem).parent().before(html);
}
}
// 执行上传操作
upload.render({
elem: elem
, url: uploadURL
, method: 'post'
, size: size
, accept: accept
2022-08-19 19:48:37 +08:00
, before: function (res) {
// 关闭按钮点击
that.prop("disabled", true);
}, done: function (res, index, file) {
if (res.code === 200 && res.url !== '') {
if (typeof res.chunkId != 'undefined') {
2022-08-19 19:48:37 +08:00
layer.close(window[res.chunkId]);
}
layer.msg(res.msg);
uploadFiles[type](res, name);
} else {
// 错误消息
layer.error(res.msg);
}
that.prop("disabled", false);
2022-08-19 19:48:37 +08:00
}
})
})
// 全局监听打开窗口
$(document).on('click', "*[lay-open]", function () {
let clickThis = $(this),
2022-08-19 19:48:37 +08:00
config = {
url: clickThis.data('url') || undefined,
object: clickThis.data('object') || 'self',
type: clickThis.data('type') || 2,
area: clickThis.data('area') || "auto",
offset: clickThis.data('offset') || "25%",
title: clickThis.data('title') || false,
maxmin: clickThis.data('maxmin') || false,
auto: clickThis.data('auto') || "undefined",
2022-08-19 19:48:37 +08:00
iframeAuto: false
}
let firstURL = config.url.substring(0, 1);
if (firstURL && firstURL === '#') {
config.type = 1;
config.url = $(config.url).html();
}
if (config.area !== "auto") {
config.area = config.area.split(',');
if (config.area.length === 2 && config.area[1] === '100%') {
config.offset = 0;
if (typeof config.url == 'object') {
config.url = config.url[0];
}
} else if (config.area.length === 1) {
config.iframeAuto = true;
}
}
/**
* 获取窗口索引
* @type {Window | (WorkerGlobalScope & Window)}
*/
let hierarchy = self;
2022-08-19 19:48:37 +08:00
if (config.object === 'top') {
hierarchy = top;
2022-08-19 19:48:37 +08:00
} else if (config.object === 'parent') {
hierarchy = parent;
2022-08-19 19:48:37 +08:00
}
hierarchy.layer.open({
2022-08-19 19:48:37 +08:00
type: config.type,
area: config.area,
title: config.title,
offset: config.offset,
maxmin: config.maxmin,
shadeClose: true,
scrollbar: true,
content: config.url,
success: function (layero, index) {
2022-08-19 19:48:37 +08:00
config.iframeAuto && layer.iframeAuto(index);
2022-08-19 19:48:37 +08:00
// 页面层才渲染
if (config.type === 1) {
layui.form.render();
layui.form.on("submit(submitPage)", function (post) {
let that = $(this),
url = that.parents('form').attr('action');
2022-08-19 19:48:37 +08:00
// 开始POST提交数据
that.attr('disabled', true);
$.post(url, post.field, function (res) {
if (res.code === 200) {
Home.event.closeDialog(that);
if ($(that).data('reload')) {
location.reload();
2022-08-19 19:48:37 +08:00
}
layer.msg(res.msg);
} else {
layer.msg(res.msg, 'error');
}
that.attr('disabled', false);
}, 'json');
2022-08-19 19:48:37 +08:00
return false;
})
}
}
})
})
/**
* 表格批量操作
* @param obj
*/
2022-11-28 19:11:12 +08:00
$(document).on("click", "*[lay-batch]", function (obj) {
let that = $(this)
, tableId = that.data("table") || null
, fields = that.data("field") || undefined
2022-11-28 19:11:12 +08:00
, list = table.checkStatus(tableId);
let field = ['id'];
2022-11-28 19:11:12 +08:00
if (typeof fields !== 'undefined') {
field = field.concat(fields.split(','));
}
if (list.data.length === 0) {
layer.msg('请勾选数据', 'error');
return false;
}
let data = {};
for (let n in field) {
let e = field[n];
2022-11-28 19:11:12 +08:00
field[e] = [];
for (let i in list.data) {
2022-11-28 19:11:12 +08:00
field[e].push(list.data[i][e]);
}
data[e] = field[e];
}
layer.confirm('确定执行批量操作', function (index) {
$.ajax({
url: that.data("url"),
2022-11-28 19:11:12 +08:00
type: 'post',
data: data,
dataType: 'json',
success: function (res) {
if (res.code === 200) {
layer.msg(res.msg);
table.reload(tableId);
} else {
layer.msg(res.msg, 'error');
}
}
})
layer.close(index);
})
})
/**
* 监听ajax操作
* @param obj
*/
$(document).on("click", "*[lay-ajax]", function (obj) {
let clickThis = $(this), config = {
url: clickThis.attr('data-url') || "undefined",
type: clickThis.data('type') || 'post',
dataType: clickThis.data('dataType') || 'json',
timeout: clickThis.data('timeout') || '6000',
tableId: clickThis.data('table') || clickThis.data('batch'),
reload: clickThis.data('reload'),
jump: clickThis.data('jump') || false,
confirm: clickThis.data('confirm'),
2022-08-19 19:48:37 +08:00
}, defer = $.Deferred();
// 定义初始化对象
let data = {}
// 获取拼接参数
, packet = clickThis.attr("data-data") || null
, object = clickThis.attr("data-object") || undefined;
2022-08-19 19:48:37 +08:00
if (config.confirm !== undefined) {
config.confirm = config.confirm || '确定执行此操作吗?';
layer.confirm(config.confirm, function (index) {
runningAjax(config);
2022-08-19 19:48:37 +08:00
layer.close(index);
}, function (index) {
2022-08-19 19:48:37 +08:00
layer.close(index);
return false;
})
}
// 传递类数据
if (typeof object !== "undefined") {
object = object.split(',');
for (let i = 0; i < object.length; i++) {
let ele = object[i].split(":");
data[ele[0]] = $('.' + ele[1]).val();
2022-08-19 19:48:37 +08:00
}
}
// 传递对象数据
if (packet !== 'null') {
packet = new Function("return " + packet)();
data = $.extend({}, data, packet);
2022-08-19 19:48:37 +08:00
}
// 传递input表单数据
let input = clickThis.data('input') || undefined;
2022-08-19 19:48:37 +08:00
if (typeof input !== undefined) {
let attribute = layui.$('.' + input).val();
2022-08-19 19:48:37 +08:00
}
// 回调函数
let runningAjax = function (config) {
2022-08-19 19:48:37 +08:00
// 执行AJAX操作
$.ajax({
url: config.url,
type: config.type,
dataType: config.dataType,
timeout: config.timeout,
data: data,
xhrFields: {
withCredentials: true
},
crossDomain: true,
success: function (res) {
2022-08-19 19:48:37 +08:00
if (res.code === 200) {
layer.msg(res.msg);
if (typeof res.data.text !== 'undefined') {
$(clickThis).text(res.data.text);
2022-08-19 19:48:37 +08:00
}
switch (config.reload) {
case 'top':
top.location.reload();
break;
case 'parent':
parent.location.reload();
break;
case 'self':
location.reload();
break;
default:
}
if (typeof (config.tableId) !== "undefined") {
layui.table.reload(config.tableId);
}
} else {
layer.msg(res.msg, 'error');
2022-08-19 19:48:37 +08:00
}
},
error: function (res) {
layer.msg('Access methods failure', 'error');
2022-08-19 19:48:37 +08:00
}
})
}
if (!config.confirm) {
runningAjax(config);
2022-08-19 19:48:37 +08:00
}
})
})