liusheng
2024-06-05 2cba0d00dc45998105129a0c26cb546282cd39a8
ruoyi-admin/src/main/java/com/ruoyi/web/controller/smartor/tools/WeChatController.java
@@ -3,7 +3,9 @@
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.smartor.domain.WeChatSendVo;
import com.smartor.service.WeChatService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import me.chanjar.weixin.common.exception.WxErrorException;
@@ -42,6 +44,9 @@
    @Value("${appSecret}")
    private String appSecret;
    @Autowired
    private WeChatService weChatService;
    /**
     * 获取模板信息
@@ -73,26 +78,8 @@
     */
    @ApiOperation("微信公众号信息发送")
    @PostMapping("/sendMessageToFollowers")
    public Boolean sendMessageToFollowers(@RequestBody WeChatSendVo weChatSendVo) {
        WxMpService wxMpService;
        wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(new WxMpInMemoryConfigStorage());
        WxMpInMemoryConfigStorage wxMpConfigStorage = (WxMpInMemoryConfigStorage) wxMpService.getWxMpConfigStorage();
        wxMpConfigStorage.setAppId(appid);
        wxMpConfigStorage.setSecret(appSecret);
        try {
            List<String> openIdList = wxMpService.getUserService().userList(null).getOpenids();
            for (String openId : openIdList) {
                WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser(openId).templateId(weChatSendVo.getTemplateId()).url(weChatSendVo.getUrl()).build();
                for (String key : weChatSendVo.getContent().keySet()) {
                    templateMessage.addData(new WxMpTemplateData(key, weChatSendVo.getContent().get(key).toString()));
                }
                wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
            }
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return true;
    public AjaxResult sendMessageToFollowers(@RequestBody WeChatSendVo weChatSendVo) {
        return toAjax(weChatService.sendMessageToFollowers(weChatSendVo));
    }