package com.tohi.common.pwdutil;

import cn.hutool.http.HttpUtil;
import com.tohi.common.dto.Singleton;
import org.apache.commons.lang.StringUtils;
import net.sf.json.JSONObject;

import java.util.*;

public class WxUtils {

    public static String appId = "wwe4f3e6def9ee9538";

    public static String appSecret = "Yzu6F95cbbxfDN4kg6pubY_BuCIEJL4BFFvJIg-Ye2U";
    //获取access_token的url
    public final static String js_api_ticket_url = "https://qyapi.weixin.qq.com/cgi-bin/get_jsapi_ticket?access_token=ACCESS_TOKEN";

    /**https://qyapi.weixin.qq.com/cgi-bin/ticket/get
     * 获取access_token
     *
     * @return
     */
    private static String getAccessToken() {
        String rel = "";
        Singleton singleton = Singleton.getInstance();
        Map<String, String> map = singleton.getMap();
        String time = map.get("access_token_time");
        String accessToken = map.get("access_token");
        Long nowDate = new Date().getTime();
        //这里设置过期时间 3000*1000就好了
        if (accessToken != null && time != null && nowDate - Long.parseLong(time) < 7200 * 1000) {
            rel = accessToken;
        } else {
            String url = String.format("https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=%s&corpsecret=%s", appId, appSecret);
            String result = HttpUtil.get(url);
            if (StringUtils.isBlank(result)) {
                return null;
            }
            JSONObject responseJsonObject = JSONObject.fromObject(result);
            map.put("access_token_time", nowDate + "");
            map.put("access_token", responseJsonObject.getString("access_token"));
            rel = responseJsonObject.getString("access_token");
            System.out.println("access_token====="+rel);
        }
        return rel;
    }

    private static String getJsapiTicket(String accessToken) {
        String rel = "";
        Singleton singleton = Singleton.getInstance();
        Map<String, String> map = singleton.getMap();
        String js_api_ticketn_time = map.get("js_api_ticketn_time");
        String ticket = map.get("ticket");
        Long nowDate = new Date().getTime();
        if (ticket != null && js_api_ticketn_time != null && nowDate - Long.parseLong(js_api_ticketn_time) < 7200 * 1000) {
            rel = ticket;
        } else {
            String url = js_api_ticket_url.replace("ACCESS_TOKEN", accessToken);
            System.out.println(url);
            String result = HttpUtil.get(url);
            if (StringUtils.isBlank(result)) {
                return null;
            }
            JSONObject responseJsonObject = JSONObject.fromObject(result);
            map.put("js_api_ticketn_time", nowDate + "");
            map.put("ticket", responseJsonObject.getString("ticket"));
            rel = responseJsonObject.getString("ticket");
            System.out.println("ticket==="+rel);
        }
        return rel;
    }

    public static Map getConfig(String url) {
        String accessToken = getAccessToken();
        String ticket = getJsapiTicket(accessToken);
        String nonceStr = create_nonce_str();
        String timestamp = create_timestamp();
        String string1 = "jsapi_ticket=" + ticket +
                "&noncestr=" + nonceStr +
                "&timestamp=" + timestamp +
                "&url=" + url;
        Map<String, Object> map = new HashMap();
        map.put("appId", appId);
        map.put("timestamp", timestamp);
        map.put("nonceStr", nonceStr);
        map.put("signature", SHA1.encode(string1));
        return map;
    }

    private static String create_nonce_str() {
        return UUID.randomUUID().toString();
    }

    private static String create_timestamp() {
        return Long.toString(System.currentTimeMillis() / 1000);
    }



}
