Java实现微信小程序发送服务通知
Java代码实现发送微信小程序服务通知
JAR包引入
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-miniapp</artifactId> <version>4.0.0</version> </dependency>
准备工作当然不止后端的JAR包引入,我们还需要选择一个将要发送消息通知的模板,此步如果是全栈大怨种就看该官方文档,很详细,如果是后端好哥们,就让前端自己研究,宗旨是需要拿到模板ID很重要!!见下图,为我圈中的那个ID,需要被后端拿到。
后端工作:
我们先看我们所需要的所有配置,笔者均写到了yml中,可能不是太好看,见谅~
一:编写配置文件
该配置文件笔者这里为两个,大家也可以只写一个,主要目的是为了注入引入的JAR包中的WxMaService,只有用这个,我们才能进行调用API
配置一 WxConfig
这里需要注意的是笔者的WxProperties是写的另一个配置文件,如果大家觉得写配置文件很麻烦,也可以写一个,采用手动注入值的放肆,这个WxProperties见下一个配置文件:
/** * @describe: * @author: jiazl / * @version: v1.0 */ @Slf4j @Configuration @EnableConfigurationProperties(WxProperties.class) public class WxConfig { private final WxProperties properties; @Autowired public WxConfig(WxProperties properties) { this.properties = properties; } @Bean public WxMaService getService() { if (properties == null || properties.getAppid() == null || properties.getSecret() == null) { throw new WxRuntimeException("required wechat param not found"); } WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); config.setAppid(properties.getAppid()); config.setSecret(properties.getSecret()); config.setToken(properties.getToken()); config.setAesKey(properties.getAesKey()); config.setMsgDataFormat(properties.getMsgDataFormat()); WxMaService service = new WxMaServiceImpl(); service.setWxMaConfig(config); return service; }
配置二:WxProperties
采用配置类注入的方式,如果大家不想写配置,可以采用手动设置值,目的是为了让上面的配置中的service注入到容器里面。
三 代码编写
笔者这里的Controller很乱,因为里面不仅有发送消息的代码,也有RabbitMq的代码,当然这都是和具体业务相关,我们的重点是sendSubscribeMsg 到这一步,消息就发送成功了,我们可以看到实例:
到这里,我们需要有几个地方注意: 1 模板中当前状态,温馨提示,工单创建时间均为参数对应值,代码是为发送前的Map里面 2 Controller最上面有一行:
private static final String PAGES_ZP = "pages/draft-review/list/list";
敬礼! salute!!
上一篇:
uniapp开发微信小程序-2.页面制作
下一篇:
微信小程序开发—基础入门(一)