您的位置 首页 java

如何在Java服务器端应用里进行微信小程序后端开发

WxJava是轻量级的、扩展性比较强的微信Java应用开发工具包,今天我给大家讲一下如何在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供外部调用:

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2 session Result;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.alibaba. fastjson . json Object;
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 = "/getUserBy code ",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. 通过接口映射的接口可以进行访问。

来源网络,侵权联系删除

文章来源:智云一二三科技

文章标题:如何在Java服务器端应用里进行微信小程序后端开发

文章地址:https://www.zhihuclub.com/180772.shtml

关于作者: 智云科技

热门文章

网站地图