如何在Java服務器端應用里進行微信小程序后端開發(fā)(微信小程序java后端搭建)
私信我或關注微信號:猿來如此呀,回復:學習,獲取免費學習資源包。
WxJava是輕量級的、擴展性比較強的微信Java應用開發(fā)工具包,今天我給大家講一下如何在Spring Boot應用中集成微信小程序相關API。
1.pom文件配置:
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-miniapp</artifactId> <version>${weixin-java-miniapp.version}</version></dependency>
2.創(chuàng)建contoller供外部調(diào)用:
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(“調(diào)用微信服務器出現(xiàn)異常”, e); json.put(“code”, 1812041738); json.put(“message”, “獲取微信用戶數(shù)據(jù)失敗”); } 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(“調(diào)用微信服務器出現(xiàn)異常”, e); json.put(“code”, 1812041738); json.put(“message”, “獲取微信用戶數(shù)據(jù)失敗”); } 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(“調(diào)用微信服務器出現(xiàn)異常”, e); json.put(“code”, 1812050942); json.put(“message”, “獲取微信用戶數(shù)據(jù)失敗”); } return json; }}
3. 通過接口映射的接口可以進行訪問。
來源網(wǎng)絡,侵權聯(lián)系刪除