WxMaConfiguration.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package vip.xiaonuo.weixin.miniapp.config;
  2. import cn.binarywang.wx.miniapp.api.WxMaService;
  3. import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
  4. import cn.binarywang.wx.miniapp.bean.WxMaKefuMessage;
  5. import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
  6. import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
  7. import cn.binarywang.wx.miniapp.message.WxMaMessageHandler;
  8. import cn.binarywang.wx.miniapp.message.WxMaMessageRouter;
  9. import com.google.common.collect.Lists;
  10. import lombok.extern.slf4j.Slf4j;
  11. import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
  12. import me.chanjar.weixin.common.error.WxErrorException;
  13. import me.chanjar.weixin.common.error.WxRuntimeException;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  16. import org.springframework.context.annotation.Bean;
  17. import org.springframework.context.annotation.Configuration;
  18. import java.io.File;
  19. import java.util.List;
  20. import java.util.stream.Collectors;
  21. /**
  22. * @author <a href="https://github.com/binarywang">Binary Wang</a>
  23. */
  24. @Slf4j
  25. @Configuration
  26. @EnableConfigurationProperties(WxMaProperties.class)
  27. public class WxMaConfiguration {
  28. private final WxMaProperties properties;
  29. @Autowired
  30. public WxMaConfiguration(WxMaProperties properties) {
  31. this.properties = properties;
  32. }
  33. @Bean
  34. public WxMaService wxMaService() {
  35. List<WxMaProperties.Config> configs = this.properties.getConfigs();
  36. if (configs == null) {
  37. throw new WxRuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
  38. }
  39. WxMaService maService = new WxMaServiceImpl();
  40. maService.setMultiConfigs(
  41. configs.stream()
  42. .map(a -> {
  43. WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
  44. // WxMaDefaultConfigImpl config = new WxMaRedisConfigImpl(new JedisPool());
  45. // 使用上面的配置时,需要同时引入jedis-lock的依赖,否则会报类无法找到的异常
  46. config.setAppid(a.getAppid());
  47. config.setSecret(a.getSecret());
  48. config.setToken(a.getToken());
  49. config.setAesKey(a.getAesKey());
  50. config.setMsgDataFormat(a.getMsgDataFormat());
  51. return config;
  52. }).collect(Collectors.toMap(WxMaDefaultConfigImpl::getAppid, a -> a, (o, n) -> o)));
  53. return maService;
  54. }
  55. @Bean
  56. public WxMaMessageRouter wxMaMessageRouter(WxMaService wxMaService) {
  57. final WxMaMessageRouter router = new WxMaMessageRouter(wxMaService);
  58. router
  59. .rule().handler(logHandler).next()
  60. .rule().async(false).content("订阅消息").handler(subscribeMsgHandler).end()
  61. .rule().async(false).content("文本").handler(textHandler).end()
  62. .rule().async(false).content("图片").handler(picHandler).end()
  63. .rule().async(false).content("二维码").handler(qrcodeHandler).end();
  64. return router;
  65. }
  66. private final WxMaMessageHandler subscribeMsgHandler = (wxMessage, context, service, sessionManager) -> {
  67. service.getMsgService().sendSubscribeMsg(WxMaSubscribeMessage.builder()
  68. .templateId("此处更换为自己的模板id")
  69. .data(Lists.newArrayList(
  70. new WxMaSubscribeMessage.MsgData("keyword1", "339208499")))
  71. .toUser(wxMessage.getFromUser())
  72. .build());
  73. return null;
  74. };
  75. private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> {
  76. log.info("收到消息:" + wxMessage.toString());
  77. service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson())
  78. .toUser(wxMessage.getFromUser()).build());
  79. return null;
  80. };
  81. private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> {
  82. service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息")
  83. .toUser(wxMessage.getFromUser()).build());
  84. return null;
  85. };
  86. private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> {
  87. try {
  88. WxMediaUploadResult uploadResult = service.getMediaService()
  89. .uploadMedia("image", "png",
  90. ClassLoader.getSystemResourceAsStream("tmp.png"));
  91. service.getMsgService().sendKefuMsg(
  92. WxMaKefuMessage
  93. .newImageBuilder()
  94. .mediaId(uploadResult.getMediaId())
  95. .toUser(wxMessage.getFromUser())
  96. .build());
  97. } catch (WxErrorException e) {
  98. e.printStackTrace();
  99. }
  100. return null;
  101. };
  102. private final WxMaMessageHandler qrcodeHandler = (wxMessage, context, service, sessionManager) -> {
  103. try {
  104. final File file = service.getQrcodeService().createQrcode("123", 430);
  105. WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia("image", file);
  106. service.getMsgService().sendKefuMsg(
  107. WxMaKefuMessage
  108. .newImageBuilder()
  109. .mediaId(uploadResult.getMediaId())
  110. .toUser(wxMessage.getFromUser())
  111. .build());
  112. } catch (WxErrorException e) {
  113. e.printStackTrace();
  114. }
  115. return null;
  116. };
  117. }