1.maven引入包 貌似是4以上的包才可以 记得关注包的版本号
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>3.7.0</version>
</dependency>
2.初始化小程序配置参数
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaMsgServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.chinacreator.c2.config.ConfigManager;
import com.chinacreator.c2.web.exception.InvalidRestParamException;
import com.chinacreator.eamp.logisticsinternet.core.utils.ToolUtil;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*/
@Configuration
public class WxMaWxMaConfiguration {
private static WxMaService wxService = null;
public static WxMaService getMaService() {
if (wxService == null) {
throw new InvalidRestParamException(String.format("未找到微信小程序的配置"));
}
return wxService;
}
@PostConstruct
public void init() throws Exception{
//接入订阅消息推送 微信小程序包从3.4升级到3.7
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
config.setAppid(ConfigManager.getInstance().getConfig("wxmini_appId"));
config.setSecret(ConfigManager.getInstance().getConfig("wxmini_secretKey"));
config.setToken(ConfigManager.getInstance().getConfig(""));
config.setAesKey(ConfigManager.getInstance().getConfig(""));
config.setMsgDataFormat("JSON");
// String port = ConfigManager.getInstance().getConfig("wxmini_http_proxy_port");
// String host = ConfigManager.getInstance().getConfig("wxmini_http_proxy_host");
// //内网请求微信通过这种代理服务
// if(StringUtils.isNotBlank(port) && StringUtils.isNotBlank(port)){
// config.setHttpProxyPort(Integer.parseInt(port));
// config.setHttpProxyHost(host);
// }
wxService = new WxMaServiceImpl();
wxService.setWxMaConfig(config);
}
}
3.小程序端消息订阅 这个可参考前端api
4.后台消息推送方法
//发送订阅消息
public Map sendSubscribeMsg(Map<String,String> content){
Map<String,String> result = new HashMap();
String code = "0";
String errMsg = null;
try {
//组装消息主体
WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage();
subscribeMessage.setTemplateId(订阅消息模板ID);
subscribeMessage.setToUser(小程序用户OpenId);
content = new HashMap<>();
content.put("examineContent","农产品审核");
content.put("examineResult","农产品");
content.put("examineTime","2020-10-16 15:35");
content.put("applyName","张义");
content.put("applyTime","2020-10-10 10:35");
List<WxMaSubscribeMessage.Data> msgContent = new ArrayList<>();
/**
* 审核内容
* {{thing7.DATA}}
*
* 审核结果
* {{thing2.DATA}}
*
* 审核时间
* {{date3.DATA}}
*
* 申请人
* {{name4.DATA}}
*
* 申请时间
* {{time14.DATA}}
*/
String template = "{"thing7":"examineContent","thing2":"examineResult","date3":"examineTime","name4":"applyName","time14":"applyTime"}";
Map<String,String> teamplateMap = ToolUtil.jsonToBean(template,Map.class);
//组装消息内容
for(String key: teamplateMap.keySet()){
String valueKey = teamplateMap.get(key);
WxMaSubscribeMessage.Data msgElement = new WxMaSubscribeMessage.Data();
msgElement.setName(key);
msgElement.setValue(content.get(valueKey));
msgContent.add(msgElement);
}
subscribeMessage.setData(msgContent);
subscribeMessage.setPage("/produce/home/index");
//发送消息
WxMaMsgServiceImpl msg = new WxMaMsgServiceImpl(WxMaWxMaConfiguration.getMaService());
msg.sendSubscribeMsg(subscribeMessage);
code = "1";
} catch (WxErrorException e) {
e.printStackTrace();
code = "-1";
errMsg = e.getMessage();
}
result.put("code",code);
result.put("errMsg",errMsg);
return result;
}