package vip.xiaonuo.coldchain.modular.push.utils;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSONObject;
import vip.xiaonuo.coldchain.modular.push.config.PushConfigure;

import java.util.Map;



/**
 * 微信登录工具类
 *
 */
public class WeChatUtil {

    private final static String ERROR_CODE = "errcode";
    private final static String accessTokenUrl = PushConfigure.getAccessTokenUrl();
    private final static String appId = PushConfigure.getAppId();
    private final static String secret = PushConfigure.getSecret();
    private final static String userInfoUrl = PushConfigure.getUserInfoUrl();

    /**
     * 获取微信授权code
     *
     * @param state state信息
     * @return 返回微信授权code的URL
     */
//    public static String getUrl() {
//
//        try {
//            StringBuffer url = new StringBuffer();
//            url.append(GET_CODE_URL)
//                    .append("?appid=")
//                    .append(APPID)
//                    .append("&redirect_uri=")
//                    .append(URLEncoder.encode(REDIRECT_URL, "UTF-8"))
//                    .append("&response_type=code&scope=snsapi_userinfo&state=STATE")
//                    .append("#wechat_redirect");
//            return url.toString();
//        } catch (UnsupportedEncodingException e) {
//            throw new RuntimeException("URL格式化异常");
//        }
//
//    }

    /**
     * 获取微信AccessToken
     *
     * @param code 用户code
     * @return 返回包含微信AccessToken的Map
     */
    public static Map<?, ?> getAccessToken(String code) {
        StringBuffer url = new StringBuffer();
        url.append(accessTokenUrl)
                .append("?appid=").append(appId)
                .append("&secret=").append(secret)
                .append("&code=").append(code)
                .append("&grant_type=authorization_code");
        String rs = HttpUtil.get(url.toString());
        Map<?, ?> map = JSONObject.parseObject(rs, Map.class);
        if (null == map.get(ERROR_CODE)) {
            return map;
        } else {
            throw new RuntimeException("获取access_token出错");
        }
    }

    /**
     * 刷新AccessToken
     *
     * @param refreshToken 用户刷新token
     * @return 返回包含刷新后的微信AccessToken的Map
     */
//    public static Map<?, ?> refreshToken(String refreshToken) {
//        StringBuffer url = new StringBuffer();
//        url.append(REFRESH_TOKEN_URL)
//                .append("?appid=").append(APPID)
//                .append("&grant_type=refresh_token&refresh_token=").append(refreshToken);
//        String rs = HttpUtil.get(url.toString());
//        Map<?, ?> map = JSONObject.parseObject(rs, Map.class);
//        if (null == map.get(ERROR_CODE)) {
//            return map;
//        } else {
//            throw new RuntimeException("刷新access_token出错");
//        }
//    }

    /**
     * 获取用户信息
     *
     * @param accessToken 微信AccessToken
     * @param openid 用户的openid
     * @return 返回包含用户信息的JSON字符串
     */
    public static String getUserInfo(String accessToken, String openid) {
        StringBuffer url = new StringBuffer();
        url.append(userInfoUrl)
                .append("?access_token=").append(accessToken)
                .append("&openid=").append(openid)
                .append("&lang=zh_CN");
        return HttpUtil.get(url.toString());
    }
}
