liusheng
2025-05-09 3cc02d185227daee7cd738ca9f713aab5025ec9b
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
package com.ruoyi.web.test;
 
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
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();
        }
    }
 
}