<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.integration.entrance.bean.OutterLoginBean" %>
<%@ page import="weaver.integration.entrance.service.EntranceService" %>
<%@ page import="weaver.integration.entrance.service.IOutterSysServer" %>
<%@ page import="weaver.integration.entrance.service.OutterSysServer" %>
<%@ page import="weaver.integration.entrance.service.permission.IPermissionService" %>
<%@ page import="weaver.integration.entrance.service.permission.PermissionService" %>
<%@ page import="weaver.integration.entrance.utils.StringUtils" %>
<%@ page import="weaver.integration.entrance.exceptions.OtherParamsEncryptException" %>
<%@ page import="weaver.integration.entrance.exceptions.AccountOrPasswordEncryptException" %>
<%@ page import="weaver.integration.logging.LoggerFactory" %>
<%@ page import="weaver.integration.logging.Logger" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>

<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="weaver.systeminfo.SystemEnv" %>
<%@ page import="java.util.Objects" %>
<%@ page import="cn.hutool.core.map.MapUtil" %>
<%@ page import="weaver.general.BaseBean" %>

<%--<script type="text/javascript" src="/js/jquery/jquery_wev8.js"></script>--%>
<%--<script src="http://libs.baidu.com/jquery/2.0.0/jquery.js"></script>--%>
<%@ include file="/systeminfo/init_wev8.jsp" %>


<script language=javascript src="<%=weaver.general.GCONST.getContextPath()%>/wui/theme/ecology8/jquery/js/zDialog_wev8.js"></script>

<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="RecordSet" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="RecordSet1" class="weaver.conn.RecordSet" scope="page"/>

<%

    Logger integration_logger = LoggerFactory.getLogger();
	BaseBean bb = new BaseBean();

    User this_user = HrmUserVarify.getUser(request,response);

    //企业微信集成时，构造的用户session会缺少loginid，导致在企业微信中引用集成登录时，因获取不到用户名导致无法单点登录。
    if(Util.null2String(this_user.getLoginid()).equals("")){
        //查询loginid
        RecordSet.executeSql("select loginid from HrmResource where id="+this_user.getUID());
        if(RecordSet.next()){
            this_user.setLoginid(Util.null2String(RecordSet.getString("loginid")));
        }
        //重新设置用户session
        request.getSession(true).setAttribute("weaver_user@bean", this_user);
    }

    IPermissionService permissionService=new PermissionService();
    String operationType = permissionService.getOperationType(request);

    String sysid = Util.null2String(request.getParameter("id"));// 系统标识
    String coremailPath = Util.null2String(request.getParameter("coremailPath"));

    integration_logger.info("sysid=========================" + sysid);
    integration_logger.info("operationType=========================" + operationType);
    integration_logger.info("coremailPath=========================" + coremailPath);

    if ("".equals(operationType)) {
        // 权限判断
        // 得到有权限查看的集成登录
        String access = permissionService.isAccess(sysid, request, response);
		integration_logger.info("access=========================" + access);
		baseBean.writeLog("access状态码->"+access);
        if (!StringUtils.isBlank(access)){
            if(access.startsWith("/")&&!access.startsWith(weaver.general.GCONST.getContextPath())){
                access = weaver.general.GCONST.getContextPath()+access;
            }
            response.sendRedirect(access);
            return;
        }
    }

    EntranceService entranceService = new EntranceService();
    IOutterSysServer   outterSysServer = new OutterSysServer();
    OutterLoginBean loginBean = null;
    Map<String, Object> map = new HashMap<String, Object>();
    try {
        loginBean = entranceService.getLoginBean(outterSysServer, sysid, operationType, request, response);
        map = entranceService.getUrl(outterSysServer, loginBean, operationType, request, response);
        //coremail 特殊处理
        if (!StringUtils.isEmpty(coremailPath)) {
            integration_logger.info("处理coremailPath" + coremailPath);
            String redirectUrl = MapUtil.getStr(map, "redirectUrl");
            String isRedirect = MapUtil.getStr(map, "isRedirect");
            integration_logger.info("处理coremail redirectUrl" + redirectUrl);
            integration_logger.info("处理coremail isRedirect" + isRedirect);
            if (!StringUtils.isEmpty(redirectUrl) && "true".equalsIgnoreCase(isRedirect)) {
                String replaceUrl = redirectUrl.replace("#mail.list|{\"fid\":1}", coremailPath);
                integration_logger.info("处理coremail replaceUrl" + replaceUrl);
                map.put("redirectUrl", replaceUrl);
            }
        }
        integration_logger.info("map=========================" + JSONObject.toJSONString(map));
    } catch (ClassNotFoundException e) {
%>
<script language=javascript>
    top.Dialog.alert("<%= SystemEnv.getHtmlLabelNames("508601",this_user.getLanguage()) %>", function () {
        window.close();
    }, 420, 130);
</script>
<%
}catch (NoSuchMethodException e){
    e.printStackTrace();
%>
<script language=javascript>
    top.Dialog.alert("<%= SystemEnv.getHtmlLabelNames("508602",this_user.getLanguage()) %>", function () {
        window.close();
    }, 420, 130);
</script>
<%
}catch (IllegalAccessException e){
%>
<script language=javascript>
    top.Dialog.alert("<%=SystemEnv.getHtmlLabelNames("508603",this_user.getLanguage()) %>", function () {
        window.close();
    }, 420, 130);
</script>
<%
}catch (InstantiationException e){
%>
<script language=javascript>
    top.Dialog.alert("<%= SystemEnv.getHtmlLabelNames("508604",this_user.getLanguage())%>", function () {
        window.close();
    }, 420, 130);
</script>
<%
}catch (NoClassDefFoundError e){
%>
<script language=javascript>
    top.Dialog.alert("<%= SystemEnv.getHtmlLabelNames("508605",this_user.getLanguage())%>", function () {
        window.close();
    }, 420, 130);
</script>
<%
}catch (OtherParamsEncryptException e) {
%>
<script language=javascript>
    top.Dialog.alert("<%= SystemEnv.getHtmlLabelNames("508614",this_user.getLanguage())%>", function () {
        window.close();
    }, 420, 130);
</script>
<%

} catch (AccountOrPasswordEncryptException e) {
%>
<script language=javascript>
    top.Dialog.alert("<%= SystemEnv.getHtmlLabelNames("508615",this_user.getLanguage())%>", function () {
        window.close();
    }, 420, 130);
</script>
<%
}catch (Exception e){
%>
<script language=javascript>
    top.Dialog.alert("系统异常，请联系管理员", function () {
        window.close();
    }, 420, 130);
</script>
<%
    }

    if (Objects.nonNull(map)){
        if (map.get("isError")!= null&&"true".equals(map.get("isError"))){
%>
<script language=javascript>
    top.Dialog.alert("<%= map.get("errorMsg") %>", function () {
        window.close();
    }, 420, 130);
</script>
<%
            return;
        }
		baseBean.writeLog("进入前");
        if (map.get("isRedirect")!=null && "true".equals(map.get("isRedirect"))){
			baseBean.writeLog("redirectUrl111111=========================");
			integration_logger.info("redirectUrl111111=========================");
            response.sendRedirect(map.get("redirectUrl").toString());
            return;
        }
        if (map.get("isRedirect")!=null && "false".equals(map.get("isRedirect"))){
            out.println(map.get("redirectUrl").toString());
            return;
        }
    }else {
        integration_logger.error("map is null ====================================================");
    }

%>





