weixin-java-miniapp 基本用法
1、pom配置
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-miniapp</artifactId> <version>4.1.0</version> </dependency>
2、application.yml配置
wx:
miniapp:
appid: wx**********************
secret: f6******************************
3、小程序配置文件
@Data @Component @ConfigurationProperties(prefix = "wx.miniapp") public class WxMaProperties { /** * 设置微信小程序的appid */ private String appid; /** * 设置微信小程序的Secret */ private String secret; }
4、WxMaConfiguration
@Slf4j @Configuration public class WxMaConfig { @Autowired private WxMaProperties wxMaProperties; /** * 初始化微信服务 会自动维护获取access_token * @return */ @Bean public WxMaService wxMaService() { log.info("============初始化微信小程序服务============"); WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl(); config.setAppid(wxMaProperties.getAppid()); config.setSecret(wxMaProperties.getSecret()); WxMaService service = new WxMaServiceImpl(); service.setWxMaConfig(config); return service; } }
5、使用
@Resource private WxMaService wxMaService; WxMaUserService userService = wxMaService.getUserService(); WxMaJscode2SessionResult sessionInfo = userService.getSessionInfo(code); //获取openid sessionInfo.getOpenid();
sessionInfo.getSessionKey();
public AjaxResult getPhone(String encryptedData,String iv,Long userId){ log.info("入参:encryptedData={}",encryptedData); log.info("入参:iv={}",iv); log.info("入参:userId={}",userId); String sessionKey = redisCache.getCacheObject(AppConstant.ACF_USER_SESSION_ + userId); // 解密 WxMaPhoneNumberInfo phoneNoInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv); //没有区号的手机号 String phone = phoneNoInfo.getPurePhoneNumber(); log.info("解密后的手机号={}",phone); }
版权声明:本文为大山008原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。