package hls.plugin.weixin.qiye;

import hls.plugin.weixin.common.HttpUtil;
import java.io.PrintStream;
import java.util.Map;
import net.sf.json.JSONException;
import net.sf.json.JSONObject;

public class WXupAndDown
{
  public static String getJSSDKAccessToken(String acceToken_urlx)
  {
    String returnString = "";
    String requestUrl = acceToken_urlx;
    JSONObject jsonObject = JSONObject.fromObject(HttpUtil.httpRequestUrl(
      requestUrl, "GET"));

    if (jsonObject != null) {
      try {
        returnString = jsonObject.getString("access_token");
      } catch (JSONException e) {
        returnString = null;
      }
    }
    return returnString;
  }

  public static String getJSSDKTicket(String access_token, String ticket_urlx)
  {
    String returnString = "";
    String requestUrl = ticket_urlx.replace("ACCESS_TOKEN", access_token);

    JSONObject jsonObject = JSONObject.fromObject(HttpUtil.httpRequestUrl(
      requestUrl, "GET"));

    if (jsonObject != null) {
      try {
        returnString = jsonObject.getString("ticket");
      } catch (JSONException e) {
        returnString = null;
      }
    }
    return returnString;
  }

  public static Map<String, String> getSignture(String id, String secrect, String url)
  {
    String acceToken_urlx = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=" + id + "&corpsecret=" + secrect;
    String ticket_urlx = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKEN";
    String js_accessToken = getJSSDKAccessToken(acceToken_urlx);
    String jsapi_ticket = getJSSDKTicket(js_accessToken, ticket_urlx);

    Map map = WxJSsign.sign(jsapi_ticket, url);
    return map;
  }

  public static void main(String[] args) {
    String id = "wx4257809fde0f5986";
    String secrect = "iTgSpAaEYejcyPBLlyikzx6kvSVuiMBxotfAkAdWAKo6nF8cefdJSTgLXqivXpZ7";
    String url = "http://cmsplus.com.cn";

    Map map = getSignture(id, secrect, url);
    System.out.println("1" + (String)map.get("nonceStr"));
    System.out.println("2" + (String)map.get("timestamp"));
    System.out.println("3" + (String)map.get("signature"));
  }
}