优化jQuery兼容

优化插件管理功能
pull/446/head
Karson 2023-06-15 15:02:09 +08:00
parent 1004bdedad
commit 3d4a02160c
2 changed files with 101 additions and 76 deletions

View File

@ -1,4 +1,4 @@
define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) { define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template', 'cookie'], function ($, undefined, Backend, Table, Form, Template, undefined) {
var Controller = { var Controller = {
index: function () { index: function () {
// 初始化表格参数配置 // 初始化表格参数配置
@ -291,84 +291,95 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
var area = [$(window).width() > 800 ? '500px' : '95%', $(window).height() > 600 ? '400px' : '95%']; var area = [$(window).width() > 800 ? '500px' : '95%', $(window).height() > 600 ? '400px' : '95%'];
var userinfo = Controller.api.userinfo.get(); var userinfo = Controller.api.userinfo.get();
if (!userinfo) { if (!userinfo) {
Layer.open({
content: Template("logintpl", {}),
zIndex: 99,
area: area,
title: __('Login FastAdmin'),
resize: false,
btn: [__('Login'), __('Register')],
yes: function (index, layero) {
Fast.api.ajax({
url: Config.api_url + '/user/login',
type: 'post',
data: {
account: $("#inputAccount", layero).val(),
password: $("#inputPassword", layero).val(),
version: Config.faversion,
}
}, function (data, ret) {
Controller.api.userinfo.set(data);
Layer.closeAll();
Layer.alert(ret.msg, {title: __('Warning'), icon: 1});
return false;
}, function (data, ret) {
});
},
btn2: function () {
return false;
},
success: function (layero, index) {
this.checkEnterKey = function (event) {
if (event.keyCode === 13) {
$(".layui-layer-btn0").trigger("click");
return false;
}
};
$(document).on('keydown', this.checkEnterKey);
$(".layui-layer-btn1", layero).prop("href", "https://www.fastadmin.net/user/register.html").prop("target", "_blank");
},
end: function () {
$(document).off('keydown', this.checkEnterKey);
}
});
} else {
Fast.api.ajax({ Fast.api.ajax({
url: Config.api_url + '/user/index', url: Config.api_url + '/user/logintpl',
type: 'post',
data: { data: {
uid: userinfo.id, version: Config.faversion
token: userinfo.token,
version: Config.faversion,
} }
}, function (data) { }, function (tpldata, ret) {
Layer.open({ Layer.open({
content: Template("userinfotpl", userinfo), content: Template.render(tpldata, {}),
zIndex: 99,
area: area, area: area,
title: __('Userinfo'), title: __('Login FastAdmin'),
resize: false, resize: false,
btn: [__('Logout'), __('Close')], btn: [__('Login')],
yes: function () { yes: function (index, layero) {
var data = $("form", layero).serializeArray();
data.push({name: "faversion", value: Config.faversion});
Fast.api.ajax({ Fast.api.ajax({
url: Config.api_url + '/user/logout', url: Config.api_url + '/user/login',
data: {uid: userinfo.id, token: userinfo.token, version: Config.faversion} type: 'post',
data: data
}, function (data, ret) { }, function (data, ret) {
Controller.api.userinfo.set(null); Controller.api.userinfo.set(data);
Layer.closeAll(); Layer.closeAll();
Layer.alert(ret.msg, {title: __('Warning'), icon: 0}); Layer.alert(ret.msg, {title: __('Warning'), icon: 1});
return false;
}, function (data, ret) { }, function (data, ret) {
Controller.api.userinfo.set(null);
Layer.closeAll();
Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
}); });
},
success: function (layero, index) {
this.checkEnterKey = function (event) {
if (event.keyCode === 13) {
$(".layui-layer-btn0").trigger("click");
return false;
}
};
$(document).on('keydown', this.checkEnterKey);
},
end: function () {
$(document).off('keydown', this.checkEnterKey);
} }
}); });
return false; return false;
}, function (data) { });
Controller.api.userinfo.set(null); } else {
$(that).trigger('click'); Fast.api.ajax({
url: Config.api_url + '/user/userinfotpl',
type: 'post',
data: {
version: Config.faversion
}
}, function (tpldata, ret) {
Fast.api.ajax({
url: Config.api_url + '/user/index',
data: {
uid: userinfo.id,
token: userinfo.token,
version: Config.faversion,
}
}, function (data) {
Layer.open({
content: Template.render(tpldata, userinfo),
area: area,
title: __('Userinfo'),
resize: false,
btn: [__('Logout'), __('Close')],
yes: function () {
Fast.api.ajax({
url: Config.api_url + '/user/logout',
data: {uid: userinfo.id, token: userinfo.token, version: Config.faversion}
}, function (data, ret) {
Controller.api.userinfo.set(null);
Layer.closeAll();
Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
}, function (data, ret) {
Controller.api.userinfo.set(null);
Layer.closeAll();
Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
});
}
});
return false;
}, function (data) {
Controller.api.userinfo.set(null);
$(that).trigger('click');
return false;
});
return false; return false;
}); });
} }
}); });
@ -616,7 +627,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
return false; return false;
} }
Template.helper("__", __); Template.helper("__", __);
Layer.confirm(Template("uninstalltpl", {addon: Config['addons'][name]}), {focusBtn: false}, function (index, layero) { Layer.confirm(Template("uninstalltpl", {addon: Config['addons'][name]}), {focusBtn: false, title: __("Warning")}, function (index, layero) {
uninstall(name, false, $("input[name='droptables']", layero).prop("checked")); uninstall(name, false, $("input[name='droptables']", layero).prop("checked"));
}); });
}); });
@ -738,26 +749,32 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
}, },
userinfo: { userinfo: {
get: function () { get: function () {
var userinfo = localStorage.getItem("fastadmin_userinfo"); if (typeof $.cookie !== 'undefined') {
var userinfo = $.cookie('fastadmin_userinfo');
} else {
var userinfo = sessionStorage.getItem("fastadmin_userinfo");
}
return userinfo ? JSON.parse(userinfo) : null; return userinfo ? JSON.parse(userinfo) : null;
}, },
set: function (data) { set: function (data) {
if (data) { if (typeof $.cookie !== 'undefined') {
localStorage.setItem("fastadmin_userinfo", JSON.stringify(data)); if (data) {
$.cookie("fastadmin_userinfo", JSON.stringify(data));
} else {
$.removeCookie("fastadmin_userinfo");
}
} else { } else {
localStorage.removeItem("fastadmin_userinfo"); if (data) {
sessionStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
} else {
sessionStorage.removeItem("fastadmin_userinfo");
}
} }
} }
}, },
refresh: function (table, name) { refresh: function (table, name) {
//刷新左侧边栏 //刷新左侧边栏
Fast.api.refreshmenu(); Fast.api.refreshmenu();
//刷新插件JS缓存
Fast.api.ajax({url: require.toUrl('addons.js'), loading: false}, function () {
return false;
}, function () {
return false;
});
//刷新行数据 //刷新行数据
if ($(".operate[data-name='" + name + "']").length > 0) { if ($(".operate[data-name='" + name + "']").length > 0) {

View File

@ -103,8 +103,10 @@ define(['jquery', 'bootstrap', 'toastr', 'layer', 'lang'], function ($, undefine
//获取修复后可访问的cdn链接 //获取修复后可访问的cdn链接
cdnurl: function (url, domain) { cdnurl: function (url, domain) {
var rule = new RegExp("^((?:[a-z]+:)?\\/\\/|data:image\\/)", "i"); var rule = new RegExp("^((?:[a-z]+:)?\\/\\/|data:image\\/)", "i");
var cdnurl = Config.upload.cdnurl; if(typeof domain === 'undefined'){
url = rule.test(url) || (cdnurl && url.indexOf(cdnurl) === 0) ? url : cdnurl + url; var cdnurl = Config.upload.cdnurl;
url = rule.test(url) || (cdnurl && url.indexOf(cdnurl) === 0) ? url : cdnurl + url;
}
if (domain && !rule.test(url)) { if (domain && !rule.test(url)) {
domain = typeof domain === 'string' ? domain : location.origin; domain = typeof domain === 'string' ? domain : location.origin;
url = domain + url; url = domain + url;
@ -318,6 +320,12 @@ define(['jquery', 'bootstrap', 'toastr', 'layer', 'lang'], function ($, undefine
}); });
}, },
init: function () { init: function () {
// jQuery兼容处理
$.fn.extend({
size: function () {
return $(this).length;
}
});
// 对相对地址进行处理 // 对相对地址进行处理
$.ajaxSetup({ $.ajaxSetup({
beforeSend: function (xhr, setting) { beforeSend: function (xhr, setting) {