WxJava - 微信开发 Java SDK
maven 依赖
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>4.0.0</version>
</dependency>
application.yml配置
wechat:
miniapp:
appId: wx701*********
appSecret: a374ae************************
WxMaConfiguration
/**
* 微信小程序
*/
@Data
@Configuration
@ConditionalOnClass(WxMaService.class)
@ConfigurationProperties(prefix ="wechat.miniapp")
public class WxMaConfiguration {
/**
* 设置微信小程序的appId
*/
private StringappId;
/**
* 设置微信小程序的Secret
*/
private StringappSecret;
@Bean
@ConditionalOnMissingBean(WxMaService.class)
public WxMaService wxMaService() {
WxMaDefaultConfigImpl config =new WxMaDefaultConfigImpl();
config.setAppid(this.getAppId());
config.setSecret(this.getAppSecret());
WxMaService service =new WxMaServiceImpl();
service.setWxMaConfig(config);
return service;
}
}
使用
public String login(String code, String signature, String rawData, String encryptedData, String iv)throws WxErrorException {
WxMaJscode2SessionResult res =wxMaService.getUserService().getSessionInfo(code);
String sessionKey = res.getSessionKey();
// 用户信息校验
if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
return R.error("微信授权失败");
}
// 解密用户信息
WxMaUserInfo userInfo =wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
String openId=userInfo.getOpenId();
return openId;
}
网友评论