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);
 }

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2026 CODEPRJ.COM