SpringBoot中,使用WxJava SDK 实现微信小程序
2020-12-19 本文已影响0人
singleZhang2010
概述
WxJava SDK是一个比较实用的第三方微信开发 Java SDK
github地址:https://github.com/Wechat-Group/WxJava
SpringBoot项目中使用WxJava SDK中的weixin-java-miniapp
- pom文件中加入依赖
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
</dependency>
- 配置文件yml中加入配置信息
demo:
# 开发者应该设置成自己的wx相关信息
wx:
app-id: wx60fac1f18be01481
app-secret: 318ceca0f27ffeae6e6baafd3a5730bd
mch-id: 123123
mch-key: xxxxxx
notify-url: http://www.example.com/wx/order/pay-notify
# 商户证书文件路径
# 请参考“商户证书”一节 https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=4_3
key-path: xxxxx
- 创建WxProperties.java
@Configuration
@ConfigurationProperties(prefix = "demo.wx")
public class WxProperties {
private String appId;
private String appSecret;
private String mchId;
private String mchKey;
private String notifyUrl;
private String keyPath;
public String getNotifyUrl() {
return notifyUrl;
}
public void setNotifyUrl(String notifyUrl) {
this.notifyUrl = notifyUrl;
}
public String getMchKey() {
return mchKey;
}
public void setMchKey(String mchKey) {
this.mchKey = mchKey;
}
public String getAppId() {
return this.appId;
}
public void setAppId(String appId) {
this.appId = appId;
}
public String getAppSecret() {
return appSecret;
}
public void setAppSecret(String appSecret) {
this.appSecret = appSecret;
}
public String getMchId() {
return mchId;
}
public void setMchId(String mchId) {
this.mchId = mchId;
}
public String getKeyPath() {
return keyPath;
}
public void setKeyPath(String keyPath) {
this.keyPath = keyPath;
}
}
- 创建WxConfig配置类
@Configuration
public class WxConfig {
@Autowired
private WxProperties properties;
@Bean
public WxMaConfig wxMaConfig() {
WxMaInMemoryConfig config = new WxMaInMemoryConfig();
config.setAppid(properties.getAppId());
config.setSecret(properties.getAppSecret());
return config;
}
@Bean
public WxMaService wxMaService(WxMaConfig maConfig) {
WxMaService service = new WxMaServiceImpl();
service.setWxMaConfig(maConfig);
return service;
}
@Bean
public WxPayConfig wxPayConfig() {
WxPayConfig payConfig = new WxPayConfig();
payConfig.setAppId(properties.getAppId());
payConfig.setMchId(properties.getMchId());
payConfig.setMchKey(properties.getMchKey());
payConfig.setNotifyUrl(properties.getNotifyUrl());
payConfig.setKeyPath(properties.getKeyPath());
payConfig.setTradeType("JSAPI");
payConfig.setSignType("MD5");
return payConfig;
}
@Bean
public WxPayService wxPayService(WxPayConfig payConfig) {
WxPayService wxPayService = new WxPayServiceImpl();
wxPayService.setConfig(payConfig);
return wxPayService;
}
}
5.做完上述准备后,在接口层调试一下,创建WxAuthController.java
/**
* 鉴权服务
*/
@RestController
@RequestMapping("/wx/auth")
@Validated
public class WxAuthController {
@Autowired
private WxMaService wxService;
@PostMapping("login_by_weixin")
public Object loginByWeixin(@RequestBody WxLoginInfo wxLoginInfo, HttpServletRequest request) {
String code = wxLoginInfo.getCode();
UserInfo userInfo = wxLoginInfo.getUserInfo();
if (code == null || userInfo == null) {
return ResponseUtil.badArgument();
}
String sessionKey = null;
String openId = null;
try {
WxMaJscode2SessionResult result = this.wxService.getUserService().getSessionInfo(code);
sessionKey = result.getSessionKey();
openId = result.getOpenid();
} catch (Exception e) {
e.printStackTrace();
}
if (sessionKey == null || openId == null) {
return ResponseUtil.fail();
}
// TODO openId 获取后的业务逻辑实现,如获取用户信息或者未注册用户创建新账号等等
Map<Object, Object> result = new HashMap<Object, Object>();
// TODO返回数据填充
return ResponseUtil.ok(result);
}
}
总结
OK,以上就是SpringBoot中,使用WxJava SDK 实现微信小程序登录的方法,学会了么?在微信小程序开发中用得比较多的。