package com.cnd3b.utility.wechatdock;

import com.alibaba.fastjson.JSONObject;
import com.cnd3b.common.BaseClass;
import com.cnd3b.utility.WebRequest;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Calendar;

public class WechatDock_xcx extends BaseClass {
    public static String appid = "wx35f0c132ec566bec";
    public static String secret = "66011bc5f6450352b00cfe7e0bd896c4";


    //有效期2小时，需定时刷新，重复获取将导致上次获取的失效
    private static String access_token = "";
    private static long access_token_timelimit = 0L;

    private static String getAccessToken() {
        long nowtime = Calendar.getInstance().getTimeInMillis();
        if (access_token_timelimit < nowtime) {
            String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret;
            String res = new WebRequest().doGet(url);
            JSONObject resobject = JSONObject.parseObject(res);
            if (resobject.containsKey("access_token")) {
                access_token = resobject.getString("access_token");
                access_token_timelimit = nowtime + 1000 * resobject.getIntValue("expires_in");
            }
        }
        return access_token;
    }

    //有效期2小时，需定时刷新，重复获取将导致上次获取的失效
    private static String ticket = "";
    private static long ticket_timelimit = 0L;

    private static String getTicket() {
        long nowtime = Calendar.getInstance().getTimeInMillis();
        if (ticket_timelimit < nowtime) {
            String url = "https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + getAccessToken() + "&type=jsapi";
            String res = new WebRequest().doGet(url);

            JSONObject resobject = JSONObject.parseObject(res);
            if (resobject.containsKey("errcode") && "0".equals(resobject.getString("errcode"))) {
                ticket = resobject.getString("ticket");
                ticket_timelimit = nowtime + 1000 * resobject.getIntValue("expires_in");
            }
        }
        return ticket;
    }

    public static JSONObject wechatDock(String url) {
        String nonceStr = Calendar.getInstance().getTimeInMillis() + "";
        String jsapi_ticket = getTicket();
        String timestamp = Calendar.getInstance().getTimeInMillis() + "";

        String a = "jsapi_ticket=" + jsapi_ticket + "&noncestr=" + nonceStr + "&timestamp=" + timestamp + "&url=" + url;
        String signature = DigestUtils.shaHex(a);

        JSONObject resultobject = new JSONObject();
        resultobject.put("appId", appid);
        resultobject.put("timestamp", timestamp);
        resultobject.put("nonceStr", nonceStr);
        resultobject.put("signature", signature);

        return resultobject;
    }


    public String getOpenId(String wechat_code) {
        WebRequest request = new WebRequest();
        String result = request.doGet("https://api.weixin.qq.com/sns/jscode2session?appid=" + appid + "&secret=" + secret + "&js_code=" + wechat_code + "&grant_type=authorization_code");
        JSONObject openidobject = JSONObject.parseObject(result);
        if (openidobject.containsKey("openid")) {
            return openidobject.getString("openid");
        } else {
            return "";
        }
    }

}
