1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package com.hz.employmentsite.services.impl;
- import com.hz.employmentsite.AppConfig;
- import com.hz.employmentsite.services.service.WechatService;
- import lombok.extern.slf4j.Slf4j;
- 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.Autowired;
- import org.springframework.stereotype.Service;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- import java.util.Map;
- @Service("WechatService")
- @Slf4j
- public class WechatServiceImpl implements WechatService {
- @Autowired
- AppConfig appConfig;
- @Override
- public Integer sentMsg(String toUserId, Map<String,String> sentData){
- String openId = "";
- WxMpInMemoryConfigStorage wxStorage = new WxMpInMemoryConfigStorage();
- wxStorage.setAppId(appConfig.wxAppId); //appID
- wxStorage.setSecret(appConfig.wxAppSecret);//app密钥
- WxMpService wxMpService = new WxMpServiceImpl();
- wxMpService.setWxMpConfigStorage(wxStorage);
- //数据
- List<WxMpTemplateData> data=new ArrayList<>();
- sentData.forEach((key,value)->{
- data.add(new WxMpTemplateData(key,value));
- });
- //2,推送消息
- WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
- .toUser(openId)//要推送的用户openid
- .data(data) //数据
- .templateId(appConfig.wxMessageTemplateId)//模版id
- .url("http://www.baidu.com") // 点击详情跳转地址
- .build();
- //发起推送
- try {
- String msg = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
- } catch (Exception e) {
- log.info("sentMsg:微信消息推送失败"+ e.getMessage());
- e.printStackTrace();
- }
- return 1;
- }
- }
|