utils.js 3.96 KB

layui.define(["jquery", 'I18nPage'], function (exports) {
    var jQuery = layui.jquery,
        $ = layui.jquery,
        I18nPage = layui.I18nPage;

    //获取url的参数值
    $.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    //把name/value的数组转为obj对象
    $.arrayToObj = function (array) {
        var result = {};
        for (var i = 0; i < array.length; i++) {
            var field = array[i];
            if (field.name in result) {
                result[field.name] += ',' + field.value;
            } else {
                result[field.name] = field.value;
            }
        }
        return result;
    }

    //加载菜单
    $.fn.extend({
        loadMenus: function (modulecode, AreaMenus, CallBack) {
            var _storageKey = "Menus_" + modulecode + "_" + AreaMenus;
            var _storage = getSessionStorage(_storageKey);
            var dom = $(this);

            var _menus = "";
            if (_storage != null) {
                _menus = getMenus(_storage);
            }
            else {
                $.ajax("/base/SysModule/LoadAuthorizedMenus",
                    {
                        async: false
                        , type: "post"
                        , data: { modulecode: modulecode, AreaMenus: AreaMenus }
                        , success: function (result) {
                            if (result !== "") {
                                _menus = getMenus(result);
                                setSessionStorage(_storageKey, result);
                            }
                        }
                    });
            }

            dom.html(dom.html() + _menus);

            if (typeof CallBack !== 'undefined' && typeof CallBack === 'function') {
                CallBack.call(null);
            }
        }
    });

    function getMenus(result) {
        var Lang = I18nPage.GetLang();

        var sb = "";
        var obj = JSON.parse(result);
        $.each(obj,
            function () {
                var element = this;
                sb += ("<a herf='javascript:;' " + " data-type='" + element.DomId +
                    "' " + " class='layui-btn layui-btn-sm " + element.Class +
                    "' ");

                if (Lang == "cn") {
                    if (element.Remark !== null && element.Remark !== "") {
                        sb += "title='" + element.Remark + "' ";
                    }
                }
                else {
                    if (element.RemarkUs !== null && element.RemarkUs !== "") {
                        sb += "title='" + element.RemarkUs + "' ";
                    }
                }

                sb += ">";

                if (element.Icon != null && element.Icon != '') {
                    sb += ("<i class='layui-icon'>" + element.Icon + "</i>");
                }
                if (Lang == "cn") {
                    sb += (element.Name + "</a>");
                }
                else {
                    sb += (element.NameUs + "</a>");
                }
            });

        return sb;
    }

    function setSessionStorage(key, value) {
        let currentTime = new Date().getTime()
        // 设置过期时间 (1小时)
        let pastTime = 1 * 60 * 60 * 1000;
        window.sessionStorage.setItem(key, JSON.stringify({ data: value, time: currentTime + pastTime }))
    }

    function getSessionStorage(key) {
        var value = window.sessionStorage.getItem(key);
        if (value == null) {
            return null;
        }
        let dataObj = JSON.parse(value)
        if (new Date().getTime() > dataObj.time) {
            removeSessionStorage(key);
            return null;
        } else {
            return dataObj.data
        }
    }

    function removeSessionStorage(key) {
        window.sessionStorage.removeItem(key)
    }

    exports('utils');
});