package com.qunzhixinxi.hnqz.admin.util;

import com.alibaba.fastjson.JSONObject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.HashMap;
import java.util.Map;

/**
 * 微信小程序获取openid
 * @author Mr.Lin
 */
public class GetOpenIDUtil {
	public static final String appid="wxd03398e1bff2b241";
	public static final String appsecret="f745fa594d51534845e8412433951a20";

	// 网页授权接口
//    public final static String GetPageAccessTokenUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";//
//    public final static String GetPageAccessTokenUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=CODE&grant_type=authorization_code";
	public  static Map<String,Object> oauth2GetOpenid(String code) {
		String requestUrl ="https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+appsecret+"&js_code="+code+"&grant_type=authorization_code";
		HttpClient client = null;
		Map<String,Object> result =new HashMap<String,Object>();
		try {
			client = new DefaultHttpClient();
			HttpGet httpget = new HttpGet(requestUrl);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			String response = client.execute(httpget, responseHandler);
			JSONObject OpenidJSONO=JSONObject.parseObject(response);
			String openid =String.valueOf(OpenidJSONO.get("openid"));
			String session_key=String.valueOf(OpenidJSONO.get("session_key"));
			String unionid=String.valueOf(OpenidJSONO.get("unionid"));
			String errcode=String.valueOf(OpenidJSONO.get("errcode"));
			String errmsg=String.valueOf(OpenidJSONO.get("errmsg"));

			result.put("openid", openid);
			result.put("sessionKey", session_key);
			result.put("unionid", unionid);
			result.put("errcode", errcode);
			result.put("errmsg", errmsg);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			client.getConnectionManager().shutdown();
		}
		return result;
	}
}
