Common.js 2.56 KB
function loadMenus(modulecode, AreaMenus) {
    var _storageKey = "Menus_" + modulecode + "_" + AreaMenus;
    var _storage = getSessionStorage(_storageKey);

    var _menus = "";
    if (_storage != null) {
        _menus = getMenus(_storage);
    }
    else {
        layui.jquery.ajax("/base/SysModule/LoadAuthorizedMenus?modulecode=" + modulecode + "&AreaMenus=" + AreaMenus,
            {
                async: false,
                success: function (result) {
                    if (result !== "") {
                        _menus = getMenus(result);
                        setSessionStorage(_storageKey, result);
                    }
                }
            });
    }

    return _menus;
}

function getMenus(result) {
    var Lang = "cn";
    if (window.localStorage.getItem("Lang") !== null) {
        Lang = window.localStorage.getItem("Lang");
    }

    var sb = "";
    var obj = JSON.parse(result);
    layui.jquery.each(obj,
        function () {
            var element = this;

            sb += ("<a herf='javascript:;' " + " lay-event='" + 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)
}