zjh
2025-03-05 6b73ef6f82fa10747c29bdec04cb43d0f7e91598
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package com.ltkj.web.wxUtils;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
 
public class WxUtil {
 
    @Value("${wx.miniapp.appid}")
    private static String APPID;
 
    @Value("${wx.miniapp.secret}")
    private static String APPSECRET;
 
    public static String getAccessToken(){
        String APPID="wx40a545f1a8eb1d9d";
        String APPSECRET="c86c1ff7f91c16380111878e9a259237";
        String accessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+APPID+"&secret="+APPSECRET;
        System.out.println("accessTokenUrl = " + accessTokenUrl);
        HttpClientUtils hru = new HttpClientUtils();
        String access_token = hru.sendHttpGet(accessTokenUrl);
        JSONObject jsonObject= JSON.parseObject(access_token);
        String token = jsonObject.get("access_token").toString();//获取access_token
        System.out.println("access_token = " + token);
        return token;
    }
 
 
    /**
     * 调用微信开放接口subscribeMessage.send发送订阅消息    通用类
     * POST https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=ACCESS_TOKEN
     */
    public static void sendCommonSubscribeMessage(Map<String,Object> params,JSONObject data){
        HttpURLConnection httpConn = null;
        InputStream is = null;
        BufferedReader rd = null;
        String accessToken = null;
        String str = null;
        try
        {
            //获取token  小程序全局唯一后台接口调用凭据
            accessToken = getAccessToken();
 
            JSONObject xmlData = new JSONObject();
            xmlData.put("touser", params.get("touser"));//接收者(用户)的 openid
            xmlData.put("template_id", params.get("template_id"));//所需下发的订阅模板id
            xmlData.put("page", params.get("page"));//点击模板卡片后的跳转页面,仅限本小程序内的页面
            xmlData.put("miniprogram_state", params.get("miniprogram_state"));//跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版
            xmlData.put("lang", "zh_CN");//进入小程序查看”的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN返回值
            xmlData.put("data", data);//小程序模板数据
 
 
            System.out.println("发送模板消息xmlData:" + xmlData);
            URL url = new URL(
                    "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token="
                            + accessToken);
            httpConn = (HttpURLConnection)url.openConnection();
            httpConn.setRequestProperty("Host", "https://api.weixin.qq.com");
            // httpConn.setRequestProperty("Content-Length", String.valueOf(xmlData.));
            httpConn.setRequestProperty("Content-Type", "text/xml; charset=\"UTF-8\"");
            httpConn.setRequestMethod("POST");
            httpConn.setDoInput(true);
            httpConn.setDoOutput(true);
            OutputStream out = httpConn.getOutputStream();
            OutputStreamWriter osw = new OutputStreamWriter(out, "UTF-8");
            osw.write(xmlData.toString());
            osw.flush();
            osw.close();
            out.close();
            is = httpConn.getInputStream();
            rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
 
            while ((str = rd.readLine()) != null)
            {
                System.out.println("返回数据:" + str);
            }
        }
        catch (Exception e)
        {
            System.out.println("发送模板消息失败.." + e.getMessage());
        }
    }
 
}