From b50af788474380c675b9dcd47f83e6de38205387 Mon Sep 17 00:00:00 2001 From: sunyuchao Date: Mon, 31 Aug 2020 17:09:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=AC=E4=B8=89=E6=96=B9=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E5=BE=AE=E4=BF=A1=E7=94=A8=E6=88=B7=E6=89=8B=E6=9C=BA=E5=8F=B7?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ThirdLoginController.java | 21 ++++++++-- .../epmet/dto/form/GetResiWxPhoneFormDTO.java | 39 +++++++++++++++++++ .../com/epmet/service/ThirdLoginService.java | 9 +++++ .../service/impl/ThirdLoginServiceImpl.java | 38 ++++++++++++++++++ 4 files changed, 103 insertions(+), 4 deletions(-) create mode 100644 epmet-auth/src/main/java/com/epmet/dto/form/GetResiWxPhoneFormDTO.java diff --git a/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java b/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java index 924c1a4070..00770d82bb 100644 --- a/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java +++ b/epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java @@ -2,13 +2,11 @@ package com.epmet.controller; import com.epmet.commons.tools.utils.Result; import com.epmet.commons.tools.validator.ValidatorUtils; -import com.epmet.dto.form.LoginFormDTO; -import com.epmet.dto.form.StaffOrgsFormDTO; -import com.epmet.dto.form.ThirdStaffOrgsFormDTO; -import com.epmet.dto.form.ThirdWxmpEnteOrgFormDTO; +import com.epmet.dto.form.*; import com.epmet.dto.result.StaffOrgsResultDTO; import com.epmet.dto.result.UserTokenResultDTO; import com.epmet.service.ThirdLoginService; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -91,4 +89,19 @@ public class ThirdLoginController { return new Result>().ok(staffOrgs); } + /** + * @param formDTO + * @return + * @author sun + * @description 单客户-获取微信用户手机号 + **/ + @PostMapping("getresiwxphone") + public Result getResiWxPhone(@RequestBody GetResiWxPhoneFormDTO formDTO) { + String phone = thirdLoginService.getResiWxPhone(formDTO); + if (StringUtils.isNotBlank(phone) && !"null".equals(phone)) { + return new Result().ok(phone); + } + return new Result().ok(""); + } + } diff --git a/epmet-auth/src/main/java/com/epmet/dto/form/GetResiWxPhoneFormDTO.java b/epmet-auth/src/main/java/com/epmet/dto/form/GetResiWxPhoneFormDTO.java new file mode 100644 index 0000000000..30e7d947d6 --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/dto/form/GetResiWxPhoneFormDTO.java @@ -0,0 +1,39 @@ +package com.epmet.dto.form; + +import lombok.Data; + +import javax.validation.constraints.NotBlank; +import java.io.Serializable; + +/** + * 单客户-获取微信用户手机号 + * + * @author sun + */ +@Data +public class GetResiWxPhoneFormDTO implements Serializable { + private static final long serialVersionUID = 4381236451736209332L; + /** + * 小程序appId + */ + @NotBlank(message = "appId不能为空",groups = {AddUserInternalGroup.class}) + private String appId; + /** + * 微信code + */ + @NotBlank(message = "wxCode不能为空",groups = {AddUserInternalGroup.class}) + private String wxCode; + /** + * 用户信息 + */ + @NotBlank(message = "encryptedData不能为空",groups = {AddUserInternalGroup.class}) + private String encryptedData; + /** + * 加密算法的初始向量 + */ + @NotBlank(message = "iv不能为空",groups = {AddUserInternalGroup.class}) + private String iv; + + public interface AddUserInternalGroup {} + +} diff --git a/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java b/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java index b343bf5a22..3d9c60cc64 100644 --- a/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java +++ b/epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java @@ -1,5 +1,6 @@ package com.epmet.service; +import com.epmet.dto.form.GetResiWxPhoneFormDTO; import com.epmet.dto.form.LoginFormDTO; import com.epmet.dto.form.ThirdStaffOrgsFormDTO; import com.epmet.dto.form.ThirdWxmpEnteOrgFormDTO; @@ -53,4 +54,12 @@ public interface ThirdLoginService { * @description 单客户-手机号密码获取组织 **/ List getMyOrgByPassword(ThirdStaffOrgsFormDTO formDTO); + + /** + * @param formDTO + * @return + * @author sun + * @description 单客户-获取微信用户手机号 + **/ + String getResiWxPhone(GetResiWxPhoneFormDTO formDTO); } diff --git a/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java b/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java index a7980eebe1..9e50e6b796 100644 --- a/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java +++ b/epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java @@ -1,6 +1,8 @@ package com.epmet.service.impl; import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult; +import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; +import cn.binarywang.wx.miniapp.util.crypt.WxMaCryptUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.epmet.common.token.constant.LoginConstant; @@ -549,4 +551,40 @@ public class ThirdLoginServiceImpl implements ThirdLoginService { return userWechatDTO; } + /** + * @param formDTO + * @return + * @author sun + * @description 单客户-获取微信用户手机号 + * 【此接口只适配第三方客户,对于党群e事通客户还是走原接口】 + **/ + @Override + public String getResiWxPhone(GetResiWxPhoneFormDTO formDTO) { + String phone = ""; + try { + //1.根据wxcode获取sessionKey + WxLoginFormDTO resiLoginFormDTO = new WxLoginFormDTO(); + resiLoginFormDTO.setAppId(formDTO.getAppId()); + resiLoginFormDTO.setWxCode(formDTO.getWxCode()); + //此方法会校验appId是否授权,然后在判断客户是否存在,之后才是获取sessionKey,如果只想获取sessionKey需要写新接口 + UserWechatDTO userWechatDTO = this.getUserWeChat(resiLoginFormDTO); + if (null == userWechatDTO || null == userWechatDTO.getSessionKey()) { + logger.error(String.format("调用生成third服务wxcode获取sessionKey失败,对应appId->" + formDTO.getAppId())); + throw new RenException("获取本机号码失败"); + } + + //2.使用sessionKey解密获取手机号 + WxMaPhoneNumberInfo phoneNoInfo = WxMaPhoneNumberInfo.fromJson(WxMaCryptUtils.decrypt(userWechatDTO.getSessionKey(), + formDTO.getEncryptedData(), + formDTO.getIv())); + if (null != phoneNoInfo) { + phone = phoneNoInfo.getPurePhoneNumber(); + } + } catch (Exception e) { + e.printStackTrace(); + log.error(String.format("获取用户微信绑定的手机号接口异常%s", e.getMessage())); + } + return phone; + } + }