package com.huimv.env.device.config; import cn.binarywang.wx.miniapp.api.WxMaService; import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage; import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.logging.Logger; @Component public class WeChatMessageHelperTest { private static WxMaService wxMaService = new WxMaServiceImpl(); /** * 发送小程序订阅消息 * @param appletsSubType 小程序类型(developer为开发版、trial为体验版、formal为正式版) * @param appSubTempId 订阅消息模板id * @param appSubPagePath 跳转小程序地址 * @param appletsAppid 小程序appId * @param appletsSecret 小程序secret * @param appletsSubToken 小程序token * @param appletsSubAesKey 小程序EncodingAESKey * @param appletsSubDataFormat 数据格式 * @param wxMaSubMsgList 消息列表 * @param userIds 接收用户id * @return void * @version V1.0 * modify history */ public static void sendWechatAppSubMessage(String appletsSubType, String appSubTempId, String appSubPagePath, String appletsAppid, String appletsSecret, String appletsSubToken, String appletsSubAesKey, String appletsSubDataFormat, ArrayList wxMaSubMsgList, String... userIds) throws Exception { /* // wxMaSubMsgList组装示例,在业务里组装好,再调用系统消息发送工具,系统消息判断是否需要发送小程序订阅消息进行分发,此处不做展示 ArrayList wxMaSubscribeData = new ArrayList<>(); WxMaSubscribeMessage.MsgData characterString1 = new WxMaSubscribeMessage.MsgData(); characterString1.setName("character_string1"); characterString1.setValue("DR15325419846198615651"); wxMaSubscribeData.add(characterString1); WxMaSubscribeMessage.MsgData thing3 = new WxMaSubscribeMessage.MsgData(); thing3.setName("thing3"); thing3.setValue("请XXXXXXXX"); wxMaSubscribeData.add(thing3); WxMaSubscribeMessage.MsgData time5 = new WxMaSubscribeMessage.MsgData(); time5.setName("time5"); time5.setValue(DateUtil.format(new Date(),DateUtil.DATE_TIME_FORMAT)); wxMaSubscribeData.add(time5); */ WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); // 小程序appId config.setAppid(appletsAppid); // 小程序secret config.setSecret(appletsSecret); // 小程序token config.setToken(appletsSubToken); // 小程序EncodingAESKey config.setAesKey(appletsSubAesKey); // 数据格式 config.setMsgDataFormat(appletsSubDataFormat); wxMaService.setWxMaConfig(config); for (String userId : userIds) { // TODO:根据用户id获取OpenId,测试时写死 WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage(); // 要推送的用户openid subscribeMessage.setToUser("openId"); // 小程序类型(developer为开发版、trial为体验版、formal为正式版),默认为formal(正式版) subscribeMessage.setMiniprogramState(appletsSubType); // 订阅消息模板id subscribeMessage.setTemplateId(appSubTempId); // 跳转小程序地址 subscribeMessage.setPage(appSubPagePath); // 消息列表(组装示例请看方法开始注释部分) subscribeMessage.setData(wxMaSubMsgList); try { // 发送订阅消息 wxMaService.getMsgService().sendSubscribeMsg(subscribeMessage); } catch (Exception e) { System.out.println("微信小程序订阅消息推送失败,接收userId: " + userId+" "+ e); } } } }