package com.platform.yijia.utils.weixinapp;


import net.sf.json.JSONException;
import net.sf.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

/**
 * <Title>工具类: WeiXinUtil</Title>
 * <description> 获取微信用户信息和凭证 </description>
 * @author JK
 * @date 2019年11月10日
 */
public class WeiXinUserUtil {

    //微信小程序获取用户信息 https://api.weixin.qq.com/sns/userinfo
    //public final static String weiXinUserInfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID&lang=zh_CN";
    //凭证获取路径
    public final static String token_url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
    //微信小程序路径获取openID "https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=APPSECRET&js_code=CODE&grant_type=authorization_code";
    public final static String openID_url  ="https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=APPSECRET&js_code=CODE&grant_type=authorization_code";
    //根据 appID secret code
    //public final static String request_Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code";

    //日志
    private static final Logger logger = LoggerFactory.getLogger(WeiXinUserUtil.class);

    /*
     * 获取用户的openid和session_key 格式例子:{"session_key":"rlxKT/xhq2bxAuvrMeEq8Q==","openid":"o9RM50ppeCgx065T2PvJjtzB_Gmo"}
     * @param appId     //凭证
     * @param appSecret     //密钥
     * @param code
     * @return
     */
    public static Map<String, Object> getUserSessionKeyAndOpenID(String appId, String appSecret, String code){
        String requestUrl = openID_url.replace("APPID", appId).replace("APPSECRET", appSecret).replace("CODE", code);
        Map<String, Object> resultMap = new HashMap<>();
        //发起GET请求
        JSONObject jsonObject = HttpRequestUtil.getHttpsRequestData(requestUrl, "GET", null);
        //System.out.println("用户openid和session_key:  "+jsonObject);
        if(jsonObject != null && jsonObject.containsKey("openid")){
            resultMap.put("openid", jsonObject.getString("openid"));
            resultMap.put("session_key", jsonObject.getString("session_key"));
        }
        return resultMap;
    }


    /*
     * 获取接口访问凭证
     * @param appId         //凭证
     * @param appSecret     //密钥
     * @return
     */
    public static Token getToken(String appId, String appSecret){
        Token token = null;
        String requestUrl = token_url.replace("APPID", appId).replace("APPSECRET", appSecret);
        //发起GET请求获取凭证
        JSONObject jsonObject = HttpRequestUtil.getHttpsRequestData(requestUrl, "GET", null);
        System.out.println("getToken: "+jsonObject);
        if(jsonObject != null){
            try {
                token = new Token();
                token.setAccessToken(jsonObject.getString("access_token"));
                token.setExpiresIn(jsonObject.getInt("expires_in"));
            }catch (JSONException e){
                e.printStackTrace();
            }
        }
        return token;
    }
}
