package com.bohuikeji.frame.module.admin.utils;


import com.alibaba.fastjson.JSONObject;
import com.bohuikeji.frame.module.admin.entity.JSAPI.WxPayBo;
import jodd.http.HttpRequest;
import lombok.extern.slf4j.Slf4j;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;


/**
 * 移动端微信登陆
 * 1、客户App端微信登陆
 * 2、骑手App端微信登陆
 */
@Slf4j
public class WxAppLoginUtils {
    public static final String CUSTAPPID = "wxcf990538adccb931";//客户微信APPID
    public static final String CUStAPPSECRET = "3095dc1829a47afff5c3598681f8bb7b";//客户微信APPSECRET
    public static final String COUAPPID = "wx1305f153eaf8bf23";//骑手微信APPID
    public static final String COUAPPSECRET = "870a6e442b0d8bc0076ecaebf61bb51b";//骑手微信APPSECRET

    /**
     * 客户端App登陆绑定请求
     * @throws
     * @title weChatLogin
     * @description 微信授权登录
     * @author Kuangzc
     * @updateTime 2019-9-12 16:00:51
     */
    public static String getWxChatLogin(String code) {
            //通过第一步获得的code获取微信授权信息
            String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WxAppLoginUtils.CUSTAPPID + "&secret="
                    + WxAppLoginUtils.CUStAPPSECRET + "&code=" + code + "&grant_type=authorization_code";
            JSONObject jsonObject = WxAppLoginUtils.doGetJson(url);
            String openid = jsonObject.getString("openid");
            String token = jsonObject.getString("access_token");
            String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token + "&openid=" + openid + "&lang=zh_CN";
            JSONObject userInfo = WxAppLoginUtils.doGetJson(infoUrl);
            return userInfo.getString("openId");
    }

    /**
     * 客户端小程序绑定微信
     * @throws
     * @title weChatLogin
     * @description 微信授权登录
     * @author Kuangzc
     * @updateTime 2019-9-12 16:00:51
     */
    public static JSONObject getWxMpLogin(String code) {
        WxPayBo wxPayBo = new WxPayBo();
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid=" + wxPayBo.getAppid() + "&secret="
                + "bb03f68922307171da460f6320f8f9f4" + "&js_code=" + code + "&grant_type=authorization_code";
        JSONObject jsonObject = WxAppLoginUtils.doGetJson(url);
        return jsonObject;
    }

    /**
     * 骑手端App登陆绑定请求
     * @throws
     * @title weChatLogin
     * @description 微信授权登录
     * @author Kuangzc
     * @updateTime 2019-9-12 16:00:51
     */
    public static JSONObject custhatLogin(String code) {
        //通过第一步获得的code获取微信授权信息
        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + WxAppLoginUtils.CUSTAPPID + "&secret="
                + WxAppLoginUtils.CUStAPPSECRET + "&code=" + code + "&grant_type=authorization_code";
        JSONObject jsonObject = WxAppLoginUtils.doGetJson(url);
        String openid = jsonObject.getString("openid");
        String token = jsonObject.getString("access_token");
        String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + token + "&openid=" + openid + "&lang=zh_CN";
        JSONObject userInfo = WxAppLoginUtils.doGetJson(infoUrl);
        return userInfo;
    }


    /**
     * 微信授权登录调用Util
     * @param URL
     * @return
     * @throws IOException
     */
        public static JSONObject doGetJson(String URL) {
            JSONObject jsonObject = null;
            HttpURLConnection conn = null;
            InputStream is = null;
            BufferedReader br = null;
            StringBuilder result = new StringBuilder();
            try {
                //创建远程url连接对象
                URL url = new URL(URL);
                //通过远程url连接对象打开一个连接，强转成HTTPURLConnection类
                conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                //设置连接超时时间和读取超时时间
                conn.setConnectTimeout(15000);
                conn.setReadTimeout(60000);
                conn.setRequestProperty("Accept", "application/json");
                //发送请求
                conn.connect();
                //通过conn取得输入流，并使用Reader读取
                if (200 == conn.getResponseCode()) {
                    is = conn.getInputStream();
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String line;
                    while ((line = br.readLine()) != null) {
                        result.append(line);
                        System.out.println(line);
                    }
                } else {
                    System.out.println("ResponseCode is an error code:" + conn.getResponseCode());
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (br != null) {
                        br.close();
                    }
                    if (is != null) {
                        is.close();
                    }
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
                conn.disconnect();
            }
            jsonObject = JSONObject.parseObject(result.toString());
            return jsonObject;
        }


}
