weixin-java-tools是轻量性的,扩张性比较强的微信应用第三方插件,今天我给大家讲一下如何在spring boot应用中集成微信小程序相关API。
1.pom文件配置
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-miniapp</artifactId>
<version>${weixin-java-miniapp.version}</version>
</dependency>
2.创建contoller供外部调用
package com.cloud.controller;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.alibaba.fastjson.JSONObject;
import com.yycunluo.cloud.service.WeixinServiceFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;
/**
* 微信小程序用户接口
*
*
*/
@RestController
@RequestMapping("/maUser")
public class WxMaUserController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@Autowired
private Environment environment;
/**
* 登陆接口
*/
@RequestMapping(value = "/getUserByCode",method = RequestMethod.POST)
public JSONObject login(@RequestBody JSONObject inData) {
String code = inData.getString("code");
String appId = inData.getString("appId");
JSONObject json = new JSONObject();
try {
final WxMaService wxMaService = WeixinServiceFactory.getWxMaService(appId, environment);
WxMaJscode2SessionResult session = wxMaService.getUserService().getSessionInfo(code);
json.put("code", 0);
json.put("message", "success");
json.put("data", session);
} catch (Exception e) {
logger.error("调用微信服务器出现异常", e);
json.put("code", 1812041738);
json.put("message", "获取微信用户数据失败");
}
return json;
}
/**
*
* 获取用户信息接口
*
*/
@GetMapping("/getUserInfo")
public JSONObject getUserInfo(@RequestBody JSONObject inData){
JSONObject json = new JSONObject();
try {
String appId = inData.getString("appId");
String sessionKey = inData.getString("sessionKey");
String signature = inData.getString("signature");
String rawData = inData.getString("rawData");
String encryptedData = inData.getString("encryptedData");
String iv = inData.getString("iv");
final WxMaService wxMaService = WeixinServiceFactory.getWxMaService(appId, environment);
// 用户信息校验
if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
json.put("code",1812041739);
json.put("message", "获取用户信息接口出错");
return json;
}
// 解密用户信息
WxMaUserInfo userInfo = wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
json.put("code", 0);
json.put("message", "success");
json.put("data", userInfo);
}catch (Exception e) {
logger.error("调用微信服务器出现异常", e);
json.put("code", 1812041738);
json.put("message", "获取微信用户数据失败");
}
return json;
}
/**
*
* 获取用户绑定手机号信息
*
*/
@GetMapping("/getUserPhone")
public JSONObject getUserPhone(@RequestBody JSONObject inData) {
JSONObject json = new JSONObject();
try {
String appId = inData.getString("appId");
String sessionKey = inData.getString("sessionKey");
String signature = inData.getString("signature");
String rawData = inData.getString("rawData");
String encryptedData = inData.getString("encryptedData");
String iv = inData.getString("iv");
final WxMaService wxMaService = WeixinServiceFactory.getWxMaService(appId, environment);
// 用户信息校验
if (!wxMaService.getUserService().checkUserInfo(sessionKey, rawData, signature)) {
json.put("code",1812050942);
json.put("message", "获取用户信息接口出错");
return json;
}
// 解密用户信息
WxMaPhoneNumberInfo wxMaPhoneNumberInfo = wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
json.put("code", 0);
json.put("message", "success");
json.put("data", wxMaPhoneNumberInfo);
}catch (Exception e) {
logger.error("调用微信服务器出现异常", e);
json.put("code", 1812050942);
json.put("message", "获取微信用户数据失败");
}
return json;
}
}
3.通过接口映射的接口可以进行访问。