赞
踩
<!-- wxjava-小程序 -->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>4.6.0</version>
</dependency>
wx:
miniapp:
configs:
- appid: #必填
secret: #必填
token: #微信小程序消息服务器配置的token
aesKey: #微信小程序消息服务器配置的EncodingAESKey
msgDataFormat: JSON
@Data @ConfigurationProperties(prefix = "wx.miniapp") public class WxMaProperties { private List<Config> configs; @Data public static class Config { /** * 设置微信小程序的appid */ private String appid; /** * 设置微信小程序的Secret */ private String secret; /** * 设置微信小程序消息服务器配置的token */ private String token; /** * 设置微信小程序消息服务器配置的EncodingAESKey */ private String aesKey; /** * 消息格式,XML或者JSON */ private String msgDataFormat; } }
@Slf4j @Configuration @EnableConfigurationProperties(WxMaProperties.class) public class WxMaConfiguration { private final WxMaProperties properties; @Autowired public WxMaConfiguration(WxMaProperties properties) { this.properties = properties; } @Bean public WxMaService wxMaService() { List<WxMaProperties.Config> configs = this.properties.getConfigs(); if (configs == null) { throw new WxRuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!"); } WxMaService maService = new WxMaServiceImpl(); maService.setMultiConfigs( configs.stream() .map(a -> { WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); // WxMaDefaultConfigImpl config = new WxMaRedisConfigImpl(new JedisPool()); // 使用上面的配置时,需要同时引入jedis-lock的依赖,否则会报类无法找到的异常 config.setAppid(a.getAppid()); config.setSecret(a.getSecret()); config.setToken(a.getToken()); config.setAesKey(a.getAesKey()); config.setMsgDataFormat(a.getMsgDataFormat()); return config; }).collect(Collectors.toMap(WxMaDefaultConfigImpl::getAppid, a -> a, (o, n) -> o))); return maService; } @Bean public WxMaMessageRouter wxMaMessageRouter(WxMaService wxMaService) { final WxMaMessageRouter router = new WxMaMessageRouter(wxMaService); router .rule().handler(logHandler).next() .rule().async(false).content("订阅消息").handler(subscribeMsgHandler).end() .rule().async(false).content("文本").handler(textHandler).end() .rule().async(false).content("图片").handler(picHandler).end() .rule().async(false).content("二维码").handler(qrcodeHandler).end(); return router; } private final WxMaMessageHandler subscribeMsgHandler = (wxMessage, context, service, sessionManager) -> { service.getMsgService().sendSubscribeMsg(WxMaSubscribeMessage.builder() .templateId("此处更换为自己的模板id") .data(Lists.newArrayList( new WxMaSubscribeMessage.MsgData("keyword1", "339208499"))) .toUser(wxMessage.getFromUser()) .build()); return null; }; private final WxMaMessageHandler logHandler = (wxMessage, context, service, sessionManager) -> { log.info("收到消息:" + wxMessage.toString()); service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("收到信息为:" + wxMessage.toJson()) .toUser(wxMessage.getFromUser()).build()); return null; }; private final WxMaMessageHandler textHandler = (wxMessage, context, service, sessionManager) -> { service.getMsgService().sendKefuMsg(WxMaKefuMessage.newTextBuilder().content("回复文本消息") .toUser(wxMessage.getFromUser()).build()); return null; }; private final WxMaMessageHandler picHandler = (wxMessage, context, service, sessionManager) -> { try { WxMediaUploadResult uploadResult = service.getMediaService() .uploadMedia("image", "png", ClassLoader.getSystemResourceAsStream("tmp.png")); service.getMsgService().sendKefuMsg( WxMaKefuMessage .newImageBuilder() .mediaId(uploadResult.getMediaId()) .toUser(wxMessage.getFromUser()) .build()); } catch (WxErrorException e) { e.printStackTrace(); } return null; }; private final WxMaMessageHandler qrcodeHandler = (wxMessage, context, service, sessionManager) -> { try { final File file = service.getQrcodeService().createQrcode("123", 430); WxMediaUploadResult uploadResult = service.getMediaService().uploadMedia("image", file); service.getMsgService().sendKefuMsg( WxMaKefuMessage .newImageBuilder() .mediaId(uploadResult.getMediaId()) .toUser(wxMessage.getFromUser()) .build()); } catch (WxErrorException e) { e.printStackTrace(); } return null; }; }
@Slf4j @Component @RequiredArgsConstructor public class WxUtils { private final WxMaService wxMaService; /** * 获取openid * * @param code code * @return {@code String} */ public String getSessionInfo(String code) { String openid = ""; try { WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code); openid = session.getOpenid(); // TODO 可以增加自己的逻辑,关联业务相关数据 // 清理ThreadLocal WxMaConfigHolder.remove(); } catch (WxErrorException e) { } finally { WxMaConfigHolder.remove(); } return openid; } /** * 获取手机号信息 * * @param code 手机号的code * @return {@code String} */ public String getPhoneNoInfo(String code) { String phoneNumber = ""; try { WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(code); phoneNumber = phoneNoInfo.getPhoneNumber(); // TODO 可以增加自己的逻辑,关联业务相关数据 // 清理ThreadLocal WxMaConfigHolder.remove(); } catch (WxErrorException e) { } finally { WxMaConfigHolder.remove(); } return phoneNumber; } /** * 获取小程序码 */ public void createWxaCodeUnlimit() { try { WxMaCodeLineColor lineColor = new WxMaCodeLineColor(); File file = wxMaService.getQrcodeService().createWxaCodeUnlimit("1", "", false, "release", 430, false, lineColor, false); // 获取文件的绝对路径 String filePath = file.getAbsolutePath(); System.out.println("文件路径: " + filePath); // TODO 可以增加自己的逻辑,关联业务相关数据 // 清理ThreadLocal WxMaConfigHolder.remove(); } catch (WxErrorException e) { } finally { WxMaConfigHolder.remove(); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。