package com.ssj.weixin.utils;


import com.alibaba.fastjson.JSONObject;
import com.ssj.framework.core.util.SystemResourceLocator;
import com.ssj.framework.weixin.util.WeixinOauth2Token;
import com.ssj.framework.weixin.util.WeixinUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 公众平台通用接口工具类 
 */  
public class LoginWxUtil {

	private  static String appId = (String) SystemResourceLocator.getValue("weixin_pc_appId");

	private  static String appSecret = (String) SystemResourceLocator.getValue("weixin_pc_appSecret");


	
	private static Logger log = LoggerFactory.getLogger(LoginWxUtil.class);

	// OAuth2.0通过code换取网页授权access_token
	protected final static String OAUTH2_ACCESSTOKEN_URL="https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";



	/**
	 * 1.通过code换取网页授权access_token
	 * @param
	 * @param
	 * @param code 填写第一步获取的code参数
	 * @return WeixinOauth2Token
	 */
	public static WeixinOauth2Token getOauth2AccessToken(String code) {
		WeixinOauth2Token wat=null;
		String requestUrl=OAUTH2_ACCESSTOKEN_URL.replace("APPID", appId).replace("SECRET", appSecret).replace("CODE", code);
		log.info("requestUrl-->"+requestUrl);
		//获取网页授权凭证
		JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);
		if (null != jsonObject) {
			log.info("获取用户信息jsonObject---" +jsonObject.toJSONString());  
			try {
				wat=new WeixinOauth2Token();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getIntValue("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));
				if(jsonObject.containsKey("unionid")){
					wat.setUnionId(jsonObject.getString("unionid"));
				}
			} catch (Exception e) {
				int errorCode=jsonObject.getIntValue("errcode");
				String errorMsg=jsonObject.getString("errmsg");
				log.error("获取网页授权凭证失败errcode:{"+errorCode+"} errmsg:{"+errorMsg+"}");
				return null;
			}
		}
		return wat;
	}
	public static WeixinOauth2Token getOauth2AccessToken(String code,String appId,String appSecret ) {
		WeixinOauth2Token wat=null;
		String requestUrl=OAUTH2_ACCESSTOKEN_URL.replace("APPID", appId).replace("SECRET", appSecret).replace("CODE", code);
		log.info("requestUrl-->"+requestUrl);
		//获取网页授权凭证
		JSONObject jsonObject = WeixinUtil.httpRequest(requestUrl, "GET", null);
		if (null != jsonObject) {
			log.info("获取用户信息jsonObject---" +jsonObject.toJSONString());
			try {
				wat=new WeixinOauth2Token();
				wat.setAccessToken(jsonObject.getString("access_token"));
				wat.setExpiresIn(jsonObject.getIntValue("expires_in"));
				wat.setRefreshToken(jsonObject.getString("refresh_token"));
				wat.setOpenId(jsonObject.getString("openid"));
				wat.setScope(jsonObject.getString("scope"));
				if(jsonObject.containsKey("unionid")){
					wat.setUnionId(jsonObject.getString("unionid"));
				}
			} catch (Exception e) {
				int errorCode=jsonObject.getIntValue("errcode");
				String errorMsg=jsonObject.getString("errmsg");
				log.error("获取网页授权凭证失败errcode:{"+errorCode+"} errmsg:{"+errorMsg+"}");
				return null;
			}
		}
		return wat;
	}

}  
