package easydo.technology.core.wx;

import com.alibaba.fastjson.JSONObject;

import zmo.technology.lite.core.ZeroException;
import zmo.technology.lite.httpcli.ZeroHttpClient;

public class EasydoWx {

    public static final String OPEN_ID = "openid";
    public static final String SESSION_KEY = "session_key";

    public static final String WIFI_MINI_APPID = "wxc23a2c2dceda414d";
    public static final String WIFI_MINI_APP_SECRET = "deb739a34e69763e845e41685a46af72";
    public static final String WIFI_MINI_URL = "https://api.weixin.qq.com/sns/jscode2session?appid=<APPID>&secret=<SECRET>&js_code=<JSCODE>&grant_type=authorization_code";

    public static JSONObject exchangeOpenID(String jscode) throws Exception {
        JSONObject jsonObject = ZeroHttpClient.request(WIFI_MINI_URL
                .replace("<JSCODE>", jscode)
                .replace("<APPID>", WIFI_MINI_APPID)
                .replace("<SECRET>", WIFI_MINI_APP_SECRET))
                .get().json();
        if (jsonObject.containsKey("errcode"))
            throw new ZeroException(jsonObject.getString("errmsg"));
        return jsonObject;
    }
}
