ソースを参照

优化jQuery兼容

优化插件管理功能
Karson 2 年 前
コミット
3d4a02160c
2 ファイル変更101 行追加76 行削除
  1. 91 74
      public/assets/js/backend/addon.js
  2. 10 2
      public/assets/js/fast.js

+ 91 - 74
public/assets/js/backend/addon.js

@@ -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 = {
         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 userinfo = Controller.api.userinfo.get();
                 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({
-                        url: Config.api_url + '/user/index',
+                        url: Config.api_url + '/user/logintpl',
+                        type: 'post',
                         data: {
-                            uid: userinfo.id,
-                            token: userinfo.token,
-                            version: Config.faversion,
+                            version: Config.faversion
                         }
-                    }, function (data) {
+                    }, function (tpldata, ret) {
                         Layer.open({
-                            content: Template("userinfotpl", userinfo),
+                            content: Template.render(tpldata, {}),
+                            zIndex: 99,
                             area: area,
-                            title: __('Userinfo'),
+                            title: __('Login FastAdmin'),
                             resize: false,
-                            btn: [__('Logout'), __('Close')],
-                            yes: function () {
+                            btn: [__('Login')],
+                            yes: function (index, layero) {
+                                var data = $("form", layero).serializeArray();
+                                data.push({name: "faversion", value: Config.faversion});
                                 Fast.api.ajax({
-                                    url: Config.api_url + '/user/logout',
-                                    data: {uid: userinfo.id, token: userinfo.token, version: Config.faversion}
+                                    url: Config.api_url + '/user/login',
+                                    type: 'post',
+                                    data: data
                                 }, function (data, ret) {
-                                    Controller.api.userinfo.set(null);
+                                    Controller.api.userinfo.set(data);
                                     Layer.closeAll();
-                                    Layer.alert(ret.msg, {title: __('Warning'), icon: 0});
+                                    Layer.alert(ret.msg, {title: __('Warning'), icon: 1});
+                                    return false;
                                 }, 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;
-                    }, function (data) {
-                        Controller.api.userinfo.set(null);
-                        $(that).trigger('click');
+                    });
+                } else {
+                    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;
                     });
-
                 }
             });
 
@@ -616,7 +627,7 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
                     return false;
                 }
                 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"));
                 });
             });
@@ -738,26 +749,32 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function
             },
             userinfo: {
                 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;
                 },
                 set: function (data) {
-                    if (data) {
-                        localStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
+                    if (typeof $.cookie !== 'undefined') {
+                        if (data) {
+                            $.cookie("fastadmin_userinfo", JSON.stringify(data));
+                        } else {
+                            $.removeCookie("fastadmin_userinfo");
+                        }
                     } else {
-                        localStorage.removeItem("fastadmin_userinfo");
+                        if (data) {
+                            sessionStorage.setItem("fastadmin_userinfo", JSON.stringify(data));
+                        } else {
+                            sessionStorage.removeItem("fastadmin_userinfo");
+                        }
                     }
                 }
             },
             refresh: function (table, name) {
                 //刷新左侧边栏
                 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) {

+ 10 - 2
public/assets/js/fast.js

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