Zhiqim Console(管理控制台)是知启蒙框架中最简洁的管理控制台组件,没有数据库,只保留一个账号,非常适用于后端程序嵌入WEB控制台模式,包括首页、登录、主界面、左侧菜单和欢迎页功能,依赖该组件实现基本的账号验证,通过覆盖原则增加自有功能。

森中灵 最后提交于1月前 替换lib
ZmrSessionUser.java6KB
/*
 * 版权所有 (C) 2015 知启蒙(ZHIQIM) 保留所有权利。[遇见知启蒙,邂逅框架梦,本文采用木兰宽松许可证第2版]
 * 
 * https://zhiqim.org/project/zhiqim_components/zhiqim_console.htm
 *
 * Zhiqim Console is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
package org.zhiqim.manager;

import org.zhiqim.httpd.HttpSessionUser;
import org.zhiqim.httpd.context.ZmlContextConstants;
import org.zhiqim.httpd.context.annotation.AnLogNot;
import org.zhiqim.kernel.annotation.AnAlias;
import org.zhiqim.kernel.util.Strings;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.manager.dbo.ZmrOperator;
import org.zhiqim.manager.rule.ZmrMenuRule;

/**
 * 操作员会话用户信息
 *
 * @version v1.6.0 @author zouzhigang 2021-3-21 新建与整理
 */
@AnAlias("ZmrSessionUser")
@AnLogNot
public class ZmrSessionUser extends HttpSessionUser implements ZmlContextConstants, ZmrConstants
{
    private ZmrOperator operator;
    private String welcomeUrl;
    
    //两个iFrame内属性,最后访问页和是否边展开导航
    private String includeUrl;
    private boolean sidebar = true;
    
    @Override
    public String getSessionName()
    {
        return "超级管理员";
    }
    
    /**********************************************************************************************/
    //初始创建,用于登录绑定前操作,使用init开头的方法,无需刷新到可能的共享会话
    /**********************************************************************************************/
    
    public ZmrSessionUser initOperator(ZmrOperator operator)
    {
        this.operator = operator;
        return this;
    }
    
    public ZmrSessionUser initWelcomeUrl(String welcomeUrl)
    {
        this.welcomeUrl = welcomeUrl;
        return this;
    }

    public ZmrSessionUser initIncludeUrl(String includeUrl)
    {
        this.includeUrl = includeUrl;
        return this;
    }
    
    /**********************************************************************************************/
    //中途设置,setOperator/setIncludeUrl/setSidebar,需要flush
    /**********************************************************************************************/
    
    public void setOperator(ZmrOperator operator)
    {
        initOperator(operator).flush();
    }
    
    /** 由页面AJAX设置 */
    public void setIncludeUrl(String includeUrl)
    {
        initIncludeUrl(includeUrl).flush();
    }
    
    public void setSidebar(boolean sidebar)
    {
        this.sidebar = sidebar;
        this.flush();
    }
    
    /**************************************************************************************/
    //获取属性
    /**************************************************************************************/
    
    public ZmrOperator getOperator()
    {
        return operator;
    }
    
    public String getOperatorCode()
    {
        return operator.getOperatorCode();
    }
    
    public String getRootPath(String path)
    {
        return getContext().getRootPath(path);
    }
    
    public String getPathInContext(String path)
    {
        int ind = path.indexOf("?");
        if (ind != -1)
            path = path.substring(0, ind);
        
        String contextPath = getContext().getContextPath();
        if (!"/".equals(contextPath) && path.startsWith(contextPath))
            path = Strings.trimLeft(path, contextPath);
        
        return path;
    }
    
    /**************************************************************************************/
    //iFrame中的主页和边导航相关信息,不flush到可能的远程会话管理中
    /**************************************************************************************/
    
    public boolean hasSidebar()
    {
        return sidebar;
    }

    public String getSidebarClass()
    {
        return sidebar?"":"z-hide";
    }
    
    public boolean isWelcomeUrl()
    {
        return Validates.isEqual(welcomeUrl, includeUrl);
    }
    
    public String getIncludeUrl()
    {
        return includeUrl;
    }
    
    public String showSidebarMenu(String name, String ico, String url)
    {
        if (Validates.isEmptyBlank(name) || Validates.isEmptyBlank(ico) || Validates.isEmptyBlank(url))
            return _EMPTY_;
        
        if (!url.startsWith("/"))
            url = "/"+getContext().getAttribute(ZMR_PATH)+"/"+url;
        url = getRootPath(url);
        StringBuilder strb = new StringBuilder();
        strb.append("<li onclick=\"Zin.doClickChildMenu(this, '").append(url).append("');\"");
        
        if (Validates.isNotEmpty(includeUrl))
        {
            String destUrl = getPathInContext(url);
            String menuUrl = getPathInContext(includeUrl);
            if (!destUrl.equals(menuUrl))
            {//不等到菜单规则中再检查一次
                menuUrl = ZmrMenuRule.getMenuUrl(menuUrl);
            }
            
            if (destUrl.equals(menuUrl))
            {//相等表示选中
                strb.append(" class=\"active onloadmenu\"");
            }
        }

        strb.append(" data-text=\"").append(name).append("\"");
        strb.append(">");
        strb.append("<i class=\"z-font z-mg-r10 ").append(ico).append("\"></i>").append(name).append("</li>");

        return strb.toString();
    }
    
    /**************************************************************************************/
    //头像相关信息
    /**************************************************************************************/
    
    public String getAvatar50()
    {
        return operator.getOperatorAvatar()==0?getRootPath(_PATH_SERVICE_RES_AVATAR_):getAvatarUrl(getOperatorAvatar(), 50);
    }

    public String getAvatar100()
    {
        return operator.getOperatorAvatar()==0?getRootPath(_PATH_SERVICE_RES_AVATAR_):getAvatarUrl(getOperatorAvatar(), 100);
    }
    
    public String getAvatar150()
    {
        return operator.getOperatorAvatar()==0?getRootPath(_PATH_SERVICE_RES_AVATAR_):getAvatarUrl(getOperatorAvatar(), 150);
    }
    
    public long getOperatorAvatar()
    {
        return operator.getOperatorAvatar();
    }
    
    public String getAvatarUrl(long avatarId, int avatarSize)
    {
        return getRootPath(new StringBuffer("/").append(getContext().getAttribute(ZMR_PATH)).append("/avatar/").append(avatarId).append("/").append(avatarSize).append(".png").toString());
    }
}