package com.template.common.utils;

import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


import java.io.IOException;
import java.util.HashMap;

/**
 * 公众号，推送信息，推给商家信息
 */
public class Message2 {

//小程序
//    static String appId="wx2fc3f45732fae5d3";
//    static String secret="7eee4a49a4470a77f9222995e8511547";

//公众号
    static String appId="wxa46ef222053a1047";
    static String secret="16f74a1265c314fd79fdf90670173467";

    /**
     * 公众号推送消息
     * @param openid 公众号openId
     * @param type  事件
     * @param location  地点
     * @param dateTime  时间
     * @return
     * @throws Exception
     */
    public static String send(String openid,String type,String location,String dateTime)
            throws Exception {

        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret;

        String s = HttpsClient.httpsRequestReturnString(url, "GET", "");
        JSONObject jsonObject = JSONObject.parseObject(s);
        String access_token = jsonObject.getString("access_token");


        JSONObject message = new JSONObject();
        message.put("touser", openid);
        message.put("template_id", "vPuSS7nivkQHxcGzpZB-NedVKzqJ4ifk0FTiBI7Aa20");

        JSONObject small = new JSONObject();
        small.put("appid", "wx2fc3f45732fae5d3");
        small.put("pagepath", "pages/msgWarn/msgWarn");
        message.put("miniprogram", small);
        // 报警类型
        JSONObject const2 = new JSONObject();
        const2.put("value", type);
        const2.put("color", "#173177");

        // 报警位置
        JSONObject thing3 = new JSONObject();
        thing3.put("value", location);
        thing3.put("color", "#173177");

        // 报警时间
        JSONObject time4 = new JSONObject();
        time4.put("value", dateTime);
        time4.put("color", "#173177");

        // 封装data
        JSONObject data = new JSONObject();
        data.put("const2",const2);
        data.put("thing3",thing3);
        data.put("time4",time4);

        message.put("data",data);

        String s1 = HttpsClient.sendJson("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, message);
        System.out.println("s1 = " + s1);
        return s1;
    }


    public static String getToken() throws Exception {
        String url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+appId+"&secret="+secret;

        String s = HttpsClient.httpsRequestReturnString(url, "GET", "");
        JSONObject jsonObject = JSONObject.parseObject(s);
        System.out.println("jsonObject = " + jsonObject);
        String access_token = jsonObject.getString("access_token");
//        HttpsClient.httpsRequestReturnString("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token="+access_token, "POST", message.toJSONString());
        return access_token;
    }




    public static void main(String[] args) throws Exception {
        String o9MsY67wqvQ__o_pOzF7oeN4MQPM = send("o9MsY67wqvQ__o_pOzF7oeN4MQPM","进入区域","IPCamera 01","2024-01-29 16:16:14");
//        String token = getToken();
//        System.out.println("token = " + token);
    }
}
