guyue
2025-08-18 b2be889ee9931b8ca97d77258466d75043b22786
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
90
91
92
package com.stylefeng.guns.modular.system.util;
 
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Component;
 
/**
 * @BelongsProject: xxx
 * @BelongsPackage: com.xxx.commons.utils
 * @ClassName WxMaSubscribeMessageUtil
 * @Author: handsome boy__LiuWenCheng
 * @CreateTime: 2024-09-05  15:58
 */
 
@Component
public class WxMaSubscribeMessageUtil {
    public static String appId = "wx4d36488fa2cd2718";
    public static String appSecret = "f3874e9c813f31a2e90c37346eb74d82";
 
 
    /**
     * 活动即将开始通知
     * @param openId
     */
    public static void push(String openId,String name ,Double money) {
        String accessToken = getAccessToken();
        String url = "https://api.weixin.qq.com/cgi-bin/message/subscribe/send?access_token=" + accessToken;
        JSONObject data = new JSONObject();
        data.put("touser", openId);
        data.put("template_id", "loQAaLfhJvkkvZL0ILKbwKJiXiHq5db6SLqeLX_V54c");
        data.put("page", "/pages/index/index/index" );
 
        // 设置消息内容
        JSONObject messageData = new JSONObject();
 
 
        JSONObject thing1 = new JSONObject();
        thing1.put("value", name);
        messageData.put("thing1", thing1);
 
 
 
        JSONObject time2 = new JSONObject();
        time2.put("value", "您的专属优惠券已到账,快点打车用掉吧~");
        messageData.put("thing2", time2);
 
        // 转化成年月日时分的时间格式
        JSONObject thing3 = new JSONObject();
        thing3.put("value", money);
        messageData.put("amount3", thing3);
 
 
        // 打印 messageData 的内容
        System.out.println("messageData: " + messageData.toJSONString());
 
        data.put("data", messageData);
        // 打印 data 的内容
        System.out.println("data: " + data.toJSONString());
 
        // 发送消息
        String result = HttpUtil.post(url, data.toString());
        System.out.println("订阅消息结果:" + result);
    }
 
    public static void main(String[] args) {
        push("oTrbv6yS0oceps4FVbW6UcaIWscI","张三", Double.valueOf(10.0d));
    }
 
 
 
    public static String getAccessToken() {
 
 
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        url = url.replace("APPID", appId).replace("APPSECRET", appSecret);
        try {
 
            String result = HttpUtil.get(url);
            JSONObject jsonObject = JSON.parseObject(result);
            return jsonObject.getString("access_token");
        } catch (Exception e) {
            System.err.println("请求失败:" + e.getMessage());
            e.printStackTrace();
            return null;
        }
 
    }
 
 
}