记得上下班打卡 | git大法好,push需谨慎

Commit 617ab3aa authored by 张国柄's avatar 张国柄

~DEL.API:原H5根据wechat.code换取openid;

parent f46be968
package com.liquidnet.service.adam.controller; package com.liquidnet.service.adam.controller;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import com.aliyuncs.DefaultAcsClient; import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.dypnsapi.model.v20170525.GetMobileRequest; import com.aliyuncs.dypnsapi.model.v20170525.GetMobileRequest;
import com.aliyuncs.dypnsapi.model.v20170525.GetMobileResponse; import com.aliyuncs.dypnsapi.model.v20170525.GetMobileResponse;
import com.aliyuncs.exceptions.ClientException; import com.aliyuncs.exceptions.ClientException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport; import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport; import com.github.xiaoymin.knife4j.annotations.ApiSupport;
...@@ -16,12 +13,10 @@ import com.liquidnet.common.sms.processor.SmsProcessor; ...@@ -16,12 +13,10 @@ import com.liquidnet.common.sms.processor.SmsProcessor;
import com.liquidnet.commons.lang.constant.LnsEnum; import com.liquidnet.commons.lang.constant.LnsEnum;
import com.liquidnet.commons.lang.core.JwtValidator; import com.liquidnet.commons.lang.core.JwtValidator;
import com.liquidnet.commons.lang.util.*; import com.liquidnet.commons.lang.util.*;
import com.liquidnet.service.adam.constant.AdamWechatConst;
import com.liquidnet.service.adam.dto.AdamThirdPartParam; import com.liquidnet.service.adam.dto.AdamThirdPartParam;
import com.liquidnet.service.adam.dto.vo.AdamLoginInfoVo; import com.liquidnet.service.adam.dto.vo.AdamLoginInfoVo;
import com.liquidnet.service.adam.dto.vo.AdamUserInfoVo; import com.liquidnet.service.adam.dto.vo.AdamUserInfoVo;
import com.liquidnet.service.adam.service.AdamRdmService; import com.liquidnet.service.adam.service.AdamRdmService;
import com.liquidnet.service.adam.service.AdamWechatService;
import com.liquidnet.service.adam.service.IAdamUserService; import com.liquidnet.service.adam.service.IAdamUserService;
import com.liquidnet.service.base.ErrorMapping; import com.liquidnet.service.base.ErrorMapping;
import com.liquidnet.service.base.ResponseDto; import com.liquidnet.service.base.ResponseDto;
...@@ -31,7 +26,6 @@ import io.swagger.annotations.ApiImplicitParam; ...@@ -31,7 +26,6 @@ import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -75,8 +69,8 @@ public class AdamLoginController { ...@@ -75,8 +69,8 @@ public class AdamLoginController {
IAdamUserService adamUserService; IAdamUserService adamUserService;
@Autowired @Autowired
SmsProcessor smsProcessor; SmsProcessor smsProcessor;
@Autowired // @Autowired
AdamWechatService adamWechatService; // AdamWechatService adamWechatService;
@Value("${liquidnet.reviewer.app-login.mobile}") @Value("${liquidnet.reviewer.app-login.mobile}")
private String reviewMobile; private String reviewMobile;
...@@ -398,51 +392,51 @@ public class AdamLoginController { ...@@ -398,51 +392,51 @@ public class AdamLoginController {
return ResponseDto.success(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli()); return ResponseDto.success(LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli());
} }
@ApiOperationSupport(order = 10) // @ApiOperationSupport(order = 10)
@ApiOperation(value = "微信小程序登录凭证校验", notes = "这里仅用于获取OPENID使用。登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。更多使用方法详见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html") // @ApiOperation(value = "微信小程序登录凭证校验", notes = "这里仅用于获取OPENID使用。登录凭证校验。通过 wx.login 接口获得临时登录凭证 code 后传到开发者服务器调用此接口完成登录流程。更多使用方法详见 https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/login/auth.code2Session.html")
@GetMapping(value = {"wxa/code2session"}) // @GetMapping(value = {"wxa/code2session"})
public ResponseDto<String> wxaCode2Session(@RequestParam String jsCode) { // public ResponseDto<String> wxaCode2Session(@RequestParam String jsCode) {
String openId = null, respJStr = null; // String openId = null, respJStr = null;
try { // try {
String url = AdamWechatConst.API_URL_JS_CODE2SESSION.replace("APPID", AdamWechatConst.zhengzaiAppletAppid) // String url = AdamWechatConst.API_URL_JS_CODE2SESSION.replace("APPID", AdamWechatConst.zhengzaiAppletAppid)
.replace("SECRET", AdamWechatConst.zhengzaiAppletSecret).replace("JSCODE", jsCode); // .replace("SECRET", AdamWechatConst.zhengzaiAppletSecret).replace("JSCODE", jsCode);
log.debug("jsCode={},url={}", jsCode, url); // log.debug("jsCode={},url={}", jsCode, url);
respJStr = HttpUtil.get(url, null); // respJStr = HttpUtil.get(url, null);
JsonNode respJNode = JsonUtils.fromJson(respJStr, JsonNode.class), respErrcode; // JsonNode respJNode = JsonUtils.fromJson(respJStr, JsonNode.class), respErrcode;
if (null == respJNode || (((respErrcode = respJNode.get("errcode")) != null) && !"0".equalsIgnoreCase(respErrcode.asText()))) { // if (null == respJNode || (((respErrcode = respJNode.get("errcode")) != null) && !"0".equalsIgnoreCase(respErrcode.asText()))) {
log.warn("WX.API调用失败[{}]", respJStr); // log.warn("WX.API调用失败[{}]", respJStr);
return ResponseDto.success(null); // return ResponseDto.success(null);
} // }
openId = respJNode.get("openid").asText(); // openId = respJNode.get("openid").asText();
} catch (Exception e) { // } catch (Exception e) {
log.error("WX.API调用异常[jsCode:{},respJStr={}]", jsCode, respJStr, e); // log.error("WX.API调用异常[jsCode:{},respJStr={}]", jsCode, respJStr, e);
} // }
log.debug("jsCode={},respJStr={}", jsCode, respJStr); // log.debug("jsCode={},respJStr={}", jsCode, respJStr);
return ResponseDto.success(openId); // return ResponseDto.success(openId);
} // }
//
@ApiOperationSupport(order = 11) // @ApiOperationSupport(order = 11)
@ApiOperation(value = "微信网站应用登录", notes = "这里仅用于获取OPENID使用。方法详见 https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html") // @ApiOperation(value = "微信网站应用登录", notes = "这里仅用于获取OPENID使用。方法详见 https://developers.weixin.qq.com/doc/oplatform/Website_App/WeChat_Login/Wechat_Login.html")
@GetMapping(value = {"wx/oauth2/access_token"}) // @GetMapping(value = {"wx/oauth2/access_token"})
public ResponseDto<String> wxOauth2AccessToken(@RequestParam String code) { // public ResponseDto<String> wxOauth2AccessToken(@RequestParam String code) {
String openId = null, respJStr = null; // String openId = null, respJStr = null;
try { // try {
String url = AdamWechatConst.API_URL_OAUTH2_ACCESS_TOKEN.replace("APPID", AdamWechatConst.zhengzaiServiceAppid) // String url = AdamWechatConst.API_URL_OAUTH2_ACCESS_TOKEN.replace("APPID", AdamWechatConst.zhengzaiServiceAppid)
.replace("SECRET", AdamWechatConst.zhengzaiServiceSecret).replace("CODE", code); // .replace("SECRET", AdamWechatConst.zhengzaiServiceSecret).replace("CODE", code);
log.debug("code={},url={}", code, url); // log.debug("code={},url={}", code, url);
respJStr = HttpUtil.get(url, null); // respJStr = HttpUtil.get(url, null);
JsonNode respJNode = JsonUtils.fromJson(respJStr, JsonNode.class), respErrcode; // JsonNode respJNode = JsonUtils.fromJson(respJStr, JsonNode.class), respErrcode;
if (null == respJNode || (((respErrcode = respJNode.get("errcode")) != null) && !"0".equalsIgnoreCase(respErrcode.asText()))) { // if (null == respJNode || (((respErrcode = respJNode.get("errcode")) != null) && !"0".equalsIgnoreCase(respErrcode.asText()))) {
log.warn("WX.API调用失败[{}]", respJStr); // log.warn("WX.API调用失败[{}]", respJStr);
return ResponseDto.success(null); // return ResponseDto.success(null);
} // }
openId = respJNode.get("openid").asText(); // openId = respJNode.get("openid").asText();
} catch (Exception e) { // } catch (Exception e) {
log.error("WX.API调用异常[jsCode:{},respJStr={}]", code, respJStr, e); // log.error("WX.API调用异常[jsCode:{},respJStr={}]", code, respJStr, e);
} // }
log.debug("code={},respJStr={}", code, respJStr); // log.debug("code={},respJStr={}", code, respJStr);
return ResponseDto.success(openId); // return ResponseDto.success(openId);
} // }
/* ---------------------------- Internal Method ---------------------------- */ /* ---------------------------- Internal Method ---------------------------- */
/* ---------------------------- Internal Method ---------------------------- */ /* ---------------------------- Internal Method ---------------------------- */
......
package com.liquidnet.service.adam.service; //package com.liquidnet.service.adam.service;
//
import cn.binarywang.wx.miniapp.api.WxMaService; //import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; //import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; //import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; //import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import com.liquidnet.service.adam.config.WechatMaConfigure; //import com.liquidnet.service.adam.config.WechatMaConfigure;
import me.chanjar.weixin.common.error.WxErrorException; //import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; //import org.springframework.stereotype.Service;
//
@Service //@Service
public class AdamWechatService { //public class AdamWechatService {
@Autowired // @Autowired
WechatMaConfigure wechatMaConfigure; // WechatMaConfigure wechatMaConfigure;
//
public WxMaJscode2SessionResult sessionInfo(String code, Integer anum) throws WxErrorException { // public WxMaJscode2SessionResult sessionInfo(String code, Integer anum) throws WxErrorException {
WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum); // WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum);
return wxMaService.getUserService().getSessionInfo(code); // return wxMaService.getUserService().getSessionInfo(code);
} // }
//
public WxMaUserInfo userInfo(String sessionKey, String encryptedData, String iv, Integer anum) { // public WxMaUserInfo userInfo(String sessionKey, String encryptedData, String iv, Integer anum) {
WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum); // WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum);
return wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv); // return wxMaService.getUserService().getUserInfo(sessionKey, encryptedData, iv);
} // }
//
public WxMaPhoneNumberInfo phoneNumberInfo(String sessionKey, String encryptedData, String iv, Integer anum) { // public WxMaPhoneNumberInfo phoneNumberInfo(String sessionKey, String encryptedData, String iv, Integer anum) {
WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum); // WxMaService wxMaService = wechatMaConfigure.getWxMaService(anum);
return wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv); // return wxMaService.getUserService().getPhoneNoInfo(sessionKey, encryptedData, iv);
} // }
} //}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment