1、常量
public interface WechatConsts {
// String WECHAT_APP_ID="wx0afb876c6fc70e1e";
// String WECHAT_APP_SECRET="1e7db4c466a2846006836c0007991d49";
// String TEMPLATE_ID="cLrzp2ThnYNhKUySQADT8Yqizyd9Cdm7qUzKgOFSoBs";
String WECHAT_APP_ID="wx9648a4f1ddb1abfa";
String WECHAT_APP_SECRET="5db2c92690351b04f01a06913abdf186";
String TEMPLATE_ID="cLrzp2ThnYNhKUySQADT8Yqizyd9Cdm7qUzKgOFSoBs";
}
2、controller代码
@RestController
@RequestMapping("/api/WechatTemplateMsg")
public class WechatTemplateMsgController {
@Autowired
WechatTemplateMsgService wechatTemplateMsgService;
@Autowired
private GoodsInfoService goodsInfoService;
@Autowired
private OrderSubFormDao subFormDao;
@Autowired
private WechatUserInfoService userInfoService;
//获取活动提醒
@RequestMapping("getActivity")
public R getactivityMsg(String openid, String activity_name, String activity_address, String activity_time)
{
wechatTemplateMsgService.common(openid,activity_name,activity_address,activity_time,"您报名的活动即将开始");
return R.ok();
}
//获取签到确认通知
@RequestMapping("getCheckInMsg")
public R getTemplateMsg(String orderId)
{
String openid="";
String activity_name="";
String activity_address="";
String activity_time="";
OrderSubFormEntity subFormEntity = subFormDao.selectById(orderId);
if (subFormEntity!=null){
GoodsInfoEntity goodsInfo =goodsInfoService.getById(subFormEntity.getGoodsId());
if (goodsInfo!=null) {
openid=userInfoService.getOpenIdByOrderId(subFormEntity.getOrderId());
activity_name = goodsInfo.getName();
activity_address = goodsInfo.getAddress();
activity_time = DateUtil.formatDateTime(goodsInfo.getStartTime());
}
}
wechatTemplateMsgService.getCheckInMsg(openid,activity_name,activity_address,activity_time,"您已成功签到");
return R.ok();
}
}
3、service代码
@Service
public class WechatTemplateMsgService {
public void common(String openid, String activity_name, String activity_address, String activity_time,String texttitle)
{
String accessToken = TokenUtil.getAccessToken();
WechatTemplateMsg wechatTemplateMsg = new WechatTemplateMsg();
TreeMap<String, TreeMap<String, String>> data = new TreeMap<>();
TreeMap<String, String> title = wechatTemplateMsg.item(texttitle,"#333");
TreeMap<String, String> name = wechatTemplateMsg.item(activity_name,"#333");
TreeMap<String, String> address = wechatTemplateMsg.item(activity_address,"#333");
TreeMap<String, String> time = wechatTemplateMsg.item(activity_time,"#333");
data.put("first",title);
data.put("keyword1",name);
data.put("keyword2",address);
data.put("keyword3",time);
wechatTemplateMsg.setData(data);
wechatTemplateMsg.setAccessToken(accessToken);
wechatTemplateMsg.setOpenId(openid);
wechatTemplateMsg.setTemplateId(TEMPLATE_ID);
sendTemplateMsg(wechatTemplateMsg);
}
public void activityInfo(String openid, String activity_name, String activity_address, String activity_time,String peopleName,String texttitle)
{
String accessToken = TokenUtil.getAccessToken();
WechatTemplateMsg wechatTemplateMsg = new WechatTemplateMsg();
TreeMap<String, TreeMap<String, String>> data = new TreeMap<>();
TreeMap<String, String> title = wechatTemplateMsg.item(texttitle,"#333");
TreeMap<String, String> name = wechatTemplateMsg.item(activity_name,"#333");
TreeMap<String, String> address = wechatTemplateMsg.item(activity_address,"#333");
TreeMap<String, String> time = wechatTemplateMsg.item(activity_time,"#333");
TreeMap<String, String> personame = wechatTemplateMsg.item(peopleName,"#333");
data.put("first",title);
data.put("keyword1",name);
data.put("keyword2",time);
data.put("keyword3",address);
data.put("keyword4",personame);
wechatTemplateMsg.setData(data);
wechatTemplateMsg.setAccessToken(accessToken);
wechatTemplateMsg.setOpenId(openid);
wechatTemplateMsg.setTemplateId("xBZxTeDpFZsMUd0eAr7wYVgBYUqNiyRX6PokzI0FmiQ");
sendTemplateMsg(wechatTemplateMsg);
}
public void getCheckInMsg(String openid, String activity_name, String activity_address, String activity_time,String texttitle)
{
String accessToken = TokenUtil.getAccessToken();
WechatTemplateMsg wechatTemplateMsg = new WechatTemplateMsg();
TreeMap<String, TreeMap<String, String>> data = new TreeMap<>();
TreeMap<String, String> title = wechatTemplateMsg.item(texttitle,"#333");
TreeMap<String, String> name = wechatTemplateMsg.item(activity_name,"#333");
TreeMap<String, String> address = wechatTemplateMsg.item(activity_address,"#333");
TreeMap<String, String> time = wechatTemplateMsg.item(activity_time,"#333");
data.put("first",title);
data.put("keyword1",name);//员工
data.put("keyword2",address);//时间
data.put("keyword3",time);//地点
data.put("keyword4",address);//类型
data.put("keyword5",time);//备注
wechatTemplateMsg.setData(data);
wechatTemplateMsg.setAccessToken(accessToken);
wechatTemplateMsg.setOpenId(openid);
wechatTemplateMsg.setTemplateId("t7RnEVXY9q3Vdu_3RR4Qw4HpieYcKMhjnUIgXUAD_38");
sendTemplateMsg(wechatTemplateMsg);
}
//模板消息推送
public void sendTemplateMsg(WechatTemplateMsg wechatTemplateMsg) {
Map<String, Object> postData = new HashMap<>(4);
postData.put("touser",wechatTemplateMsg.getOpenId());
postData.put("template_id",wechatTemplateMsg.getTemplateId());
/* postData.put("url",wechatTemplateMsg.getUrl());*/
postData.put("data",wechatTemplateMsg.getData());
String postDataStr = JSONUtil.toJsonStr(postData);
String postUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + wechatTemplateMsg.getAccessToken();
String urlResult = HttpUtil.post(postUrl, postDataStr);
JSONObject jsonUrl = JSONUtil.parseObj(urlResult);
}
}