package com.tiesheng.platform.config.wxmp.bean;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * @author hao
 */
public class WxOAuth2AccessToken {

    private String accessToken;
    private String openid;

    public static WxOAuth2AccessToken create(String appId, String secret, String code) {
        String response = HttpUtil.get("https://api.weixin.qq.com/sns/oauth2/access_token" +
                "?appid=" + appId + "&secret=" + secret + "&code=" + code + "&grant_type=authorization_code");
        JSONObject respJson = JSONUtil.parseObj(response);

        WxOAuth2AccessToken oAuth2AccessToken = new WxOAuth2AccessToken();
        oAuth2AccessToken.setOpenid(respJson.getStr("openid"));
        oAuth2AccessToken.setAccessToken(respJson.getStr("access_token"));
        return oAuth2AccessToken;
    }


    ///////////////////////////////////////////////////////////////////////////
    // setter\getter
    ///////////////////////////////////////////////////////////////////////////

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getOpenid() {
        return openid;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }
}
