mitao
2024-09-21 f44e4d609e7efaed9eac545137970b1e334f8106
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.ruoyi.goods.utli;
 
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateData;
import cn.binarywang.wx.miniapp.bean.WxMaTemplateMessage;
import  cn.binarywang.wx.miniapp.config.WxMaInMemoryConfig;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
 
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class wxPush {
 
 
    //微信
    private static final String ACCESS_TOKEN_HOST = "https://api.weixin.qq.com/cgi-bin/token";
 
    private static final String WX_APPID = "wx69e3ac6e13a889b7";
 
    private static final String WX_SECRET = "1b8bcfcb681524ac553e72054e5271ef";
 
    public String push(String openid, String formid, String skuName, BigDecimal seckillPrice, LocalDateTime startTime) {
 
        //1,配置小程序信息
 
        WxMaInMemoryConfig wxConfig = new WxMaInMemoryConfig();
        wxConfig.setAppid("wx69e3ac6e13a889b7");//小程序appid
 
        wxConfig.setSecret("1b8bcfcb681524ac553e72054e5271ef");//小程序AppSecret
 
 
 
        WxMaService wxMaService = new WxMaServiceImpl();
 
        wxMaService.setWxMaConfig(wxConfig);
        String responseAccessToken=null;
        try {
            responseAccessToken = getAccessTokenByWX();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
 
 
        //2,设置模版信息(keyword1:类型,keyword2:内容)
 
 
        List<WxMaTemplateData> templateDataList = new ArrayList<>(4);
 
        WxMaTemplateData data1 = new WxMaTemplateData("thing1", skuName);
 
        WxMaTemplateData data2 = new WxMaTemplateData("amount3", seckillPrice.toString());
 
        WxMaTemplateData data3 = new WxMaTemplateData("thing4", seckillPrice.toString());
 
        WxMaTemplateData data4 = new WxMaTemplateData("thing5","秒杀活动开始啦,快来抢购");
 
        templateDataList.add(data1);
 
        templateDataList.add(data2);
 
        templateDataList.add(data3);
 
        templateDataList.add(data4);
 
 
 
        //3,设置推送消息
 
        WxMaTemplateMessage templateMessage = WxMaTemplateMessage.builder()
 
                .toUser(openid)//要推送的用户openid
 
                .formId(formid)//收集到的formid
 
                .templateId("NHmHEbK8SjzafWgnErAxNR0b2XJzSehv2kPqbQefolU")//推送的模版id(在小程序后台设置)
 
                .data(templateDataList)//模版信息
 
                .page("pages/index/index")//要跳转到小程序那个页面
 
                .build();
 
        //4,发起推送
 
        try {
            wxMaService.getMsgService().sendTemplateMsg(templateMessage);
 
        } catch (WxErrorException e) {
 
            System.out.println("推送失败:" + e.getMessage());
 
            return e.getMessage();
 
        }
 
        return "推送成功";
 
    }
 
    public static String getAccessTokenByWX() throws Exception {
        String host = ACCESS_TOKEN_HOST + "?appid=" + WX_APPID + "&secret=" + WX_SECRET + "&grant_type=client_credential";
        Map<String, String> headers = new HashMap<>(8);
        HttpResponse response = HttpUtils.doGet(host, "", "GET", headers, null);
        return EntityUtils.toString(response.getEntity());
    }
}