liusheng
2025-03-14 d5222c7ae76d34cf8a7e32fee1ed15c988c3b330
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
package com.ruoyi.web.test;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.web.controller.smartor.tools.WeChatController;
import com.smartor.domain.WeChatSendVo;
import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class WeChatService {
    private WxMpService wxMpService;
 
    public WeChatService(String appId, String appSecret) {
        wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(new WxMpInMemoryConfigStorage());
        WxMpInMemoryConfigStorage wxMpConfigStorage = (WxMpInMemoryConfigStorage) wxMpService.getWxMpConfigStorage();
        wxMpConfigStorage.setAppId(appId);
        wxMpConfigStorage.setSecret(appSecret);
    }
 
    public void sendMessageToFollowers(String messageContent) {
        try {
            // 获取所有关注者的OpenID列表
            List<String> openIdList = wxMpService.getUserService().userList(null).getOpenids();
            for (String openId : openIdList) {
                WxMpKefuMessage wxMpKefuMessage = WxMpKefuMessage.TEXT().toUser(openId).content("利湖公众号测试").build();
//                try {
//                    boolean b = wxMpService.getKefuService().sendKefuMessage(wxMpKefuMessage);
//                    if(b==false){
//                        continue;
//                    }
//                }catch (WxErrorException e){
//                    continue;
//                }
 
                WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser(openId).templateId("oG3pJHPVWpE81DmZsua_2tKwgJ97r0qz37z56ns7NB4").url("www.baidu.com").build();
                templateMessage.addData(new WxMpTemplateData("{first", "你好,根据你的记录信息,为你生成近期的康复报告", "#FFB6C1"));
                templateMessage.addData(new WxMpTemplateData("keyword1", "2023-05-15-2023-05-16", "#B95EA6"));
                templateMessage.addData(new WxMpTemplateData("keyword2", "共测量血糖10次,其中正常7次、偏高2次、偏低1次;平均每日饮食摄入热量1578大卡,在每日热量控制范围内;平均每日散步1小时,满足康复需要", "#173177"));
                templateMessage.addData(new WxMpTemplateData("remark", "213456", "#87cefa"));
//                templateMessage.addData(new WxMpTemplateData("low","AA","#FF6347"));
//                templateMessage.addData(new WxMpTemplateData("scq_day","AA","#FF1493"));
//                templateMessage.addData(new WxMpTemplateData("bir_day","AA","#FF00FF" ));
//                templateMessage.addData(new WxMpTemplateData("daily_english_cn","AA","#800080"));
//                templateMessage.addData(new WxMpTemplateData("daily_english_en","AA","#FFA500"));
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
    }
 
    public static void main(String[] args) {
//        String appId = "wx1c5243d2337753f4";
//        String appSecret = "84a2186a0d175e88345267c716516cd3";
//        WeChatService weChatService = new WeChatService(appId, appSecret);
//        weChatService.sendMessageToFollowers("你在说啥呢????");
//        // new com.smartor.controller.BaseSmsaccountController().sendMessageToFollowers("AIJSl-IF6M05NzaXofPYbT5wgSP3bWH7I9OBmvJqsaU","www.baidu.com");
        WeChatController weChatController = new WeChatController();
        JSONArray templateList = weChatController.getTemplateList();
        for (int i = 0; i < templateList.size(); i++) {
            JSONObject template = templateList.getJSONObject(i);
            System.out.println("Template ID: " + template.getString("template_id"));
            System.out.println("Title: " + template.getString("title"));
            System.out.println("Content: " + template.getString("content"));
            System.out.println("===============================");
            WeChatSendVo weChatSendVo = new WeChatSendVo();
            weChatSendVo.setTemplateId(template.getString("template_id"));
            Map<String, Object> map = new HashMap<>();
            map.put("first", "武器的卡");
            map.put("keyword1", "看我付款看我付款未开放看我付款");
            map.put("keyword2", "12657698798797");
            map.put("remark", "啊啊撒啊啊啊");
            weChatSendVo.setContent(map);
//            weChatController.sendMessageToFollowers(weChatSendVo);
        }
 
    }
}