package com.smartor.service.impl; import com.smartor.domain.WeChatSendVo; import com.smartor.service.WeChatService; 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.template.WxMpTemplateData; import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; import org.springframework.stereotype.Service; import java.util.List; @Service public class WeChatServiceImpl implements WeChatService { @Value("${appid}") private String appid; @Value("${appSecret}") private String appSecret; @Override public Boolean sendMessageToFollowers(WeChatSendVo weChatSendVo) { WxMpService wxMpService; wxMpService = new WxMpServiceImpl(); wxMpService.setWxMpConfigStorage(new WxMpInMemoryConfigStorage()); WxMpInMemoryConfigStorage wxMpConfigStorage = (WxMpInMemoryConfigStorage) wxMpService.getWxMpConfigStorage(); wxMpConfigStorage.setAppId(appid); wxMpConfigStorage.setSecret(appSecret); try { List openIdList = wxMpService.getUserService().userList(null).getOpenids(); // for (String openId : openIdList) { WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser(weChatSendVo.getOpenid()).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; } }