<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="org.apache.http.client.methods.CloseableHttpResponse" %>
<%@ page import="org.apache.http.impl.client.CloseableHttpClient" %>
<%@ page import="org.apache.http.impl.client.HttpClients" %>
<%@ page import="org.apache.http.client.methods.HttpPost" %>
<%@ page import="org.apache.http.entity.StringEntity" %>
<%@ page import="org.apache.http.HttpEntity" %>
<%@ page import="org.apache.http.util.EntityUtils" %>
<%@ page import="java.io.IOException" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="java.util.Calendar" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.interfaces.mfkj.util.HttpUtils" %>
<jsp:useBean id="rst" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="rs1" class="weaver.conn.RecordSet" scope="page" />
<jsp:useBean id="bb" class="weaver.general.BaseBean" />
<%

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

    int userid = user.getUID();
    String email = "";
    if(userid == 1){
        email = "lyla.zhang@matfron.com";
    }else{
        email = user.getEmail();
    }

    //企业编码
    String corpCode = bb.getPropValue("syn_othersys_mfkj","corpCode");
    //用户名称
    String userName = bb.getPropValue("syn_othersys_mfkj","userName");
    //加密密码
    String password = bb.getPropValue("syn_othersys_mfkj","password");
    //获取 token  url
    String token_url = bb.getPropValue("syn_othersys_mfkj","token_url");
    // 面试官工作台单点登录
    String interview_login_url = bb.getPropValue("syn_othersys_mfkj","interview_login_url");
    //uf_dy_token   modeuuid
    String token_uuid = bb.getPropValue("syn_othersys_mfkj","token_uuid");

    JSONObject jsonObject = new JSONObject();
    jsonObject.put("corpCode",corpCode);
    jsonObject.put("userName",userName);
    jsonObject.put("password",password);

    Map<String,Object> tokenMap = new HashMap<>();
    Map<String,String> headers = new HashMap<>();
    String encode = "utf-8";
    String token = "";
    String expire = "";

    Calendar cal1 = Calendar.getInstance();
    long now_time = cal1.getTimeInMillis();

    rst.executeQuery("select * from uf_dy_token where modeuuid='"+ token_uuid +"'");
    rst.next();
    expire = Util.null2o(rst.getString("expire"));
    token = Util.null2o(rst.getString("token"));

    long timestamp = Long.parseLong(expire)*1000;
    //token已过期，重新调用获取新的token
    if(now_time > timestamp){
        tokenMap = HttpUtils.getTokenInfo(token_url,headers,jsonObject.toString(),encode);
        if(tokenMap.size() > 0){
            token = (String) tokenMap.get("token");
            expire = (String) tokenMap.get("expire");
            //更新token和有效时间戳
            rs1.executeUpdate("update uf_dy_token set token='"+ token +"',expire='"+ expire +"' where modeuuid='"+ token_uuid +"'");
        }
    }

    headers.put("Authorization", "Bearer "+token);

    JSONObject json1 = new JSONObject();
    json1.put("email",email);
    String urll = getLoginUrl(interview_login_url,headers,json1.toString(),"UTF-8");
%>

<%!
    /**
     * 单点的 url
     * @param url
     * @param headers
     * @param stringJson
     * @param encode
     * @return
     */
    public static String getLoginUrl(String url, Map<String,String> headers, String stringJson, String encode){
        String result = "";
        if(encode == null){
            encode = "utf-8";
        }
        String content = null;
        CloseableHttpResponse  httpResponse = null;
        CloseableHttpClient closeableHttpClient = null;
        try {
            closeableHttpClient = HttpClients.createDefault();
            HttpPost httpost = new HttpPost(url);
            //设置header
            httpost.setHeader("Content-type", "application/json");
            if (headers != null && headers.size() > 0) {
                for (Map.Entry<String, String> entry : headers.entrySet()) {
                    httpost.setHeader(entry.getKey(),entry.getValue());
                }
            }
            //组织请求参数
            StringEntity stringEntity = new StringEntity(stringJson, encode);
            httpost.setEntity(stringEntity);
            //响应信息
            httpResponse = closeableHttpClient.execute(httpost);
            HttpEntity entity = httpResponse.getEntity();
            content = EntityUtils.toString(entity, encode);
            if(content != null && !"".equals(content)){
                JSONObject json = JSONObject.parseObject(content);
                String code = json.getString("state");
                if("0".equals(code)){
                    String data = json.getString("data");
                    JSONObject json_data = JSONObject.parseObject(data);
                    result = json_data.getString("url");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            try {
                httpResponse.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {  //关闭连接、释放资源
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
%>


<script type="text/javascript">
    var redirecturl = "<%=urll%>";
    window.location.href = redirecturl;
</script>