package hls.plugin.weixin.fuwu;

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

public class GetOpenid
{
  public static String getAccessTokenURL(String code, String appid, String secret)
  {
    String mutiurl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appid + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code";
    return mutiurl;
  }

  public String getPenid(String code) {
    String openid = "";
    GetPropertiesValue gp = new GetPropertiesValue();
    String requestUrl = getAccessTokenURL(code, (String)gp.GetValue().get("appid"), (String)gp.GetValue().get("secret"));
    JSONObject jsonObject = JSONObject.fromObject(HttpUtil.httpRequestUrl(
      requestUrl, "GET"));

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

    return openid;
  }
}