diff --git a/epmet-auth/src/main/java/com/epmet/dto/dingres/DingUserDetailDTO.java b/epmet-auth/src/main/java/com/epmet/dto/dingres/DingUserDetailDTO.java new file mode 100644 index 0000000000..4a46922590 --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/dto/dingres/DingUserDetailDTO.java @@ -0,0 +1,73 @@ +package com.epmet.dto.dingres; + +import lombok.Data; + +/** + * @Description + * @Author yzm + * @Date 2022/9/22 14:16 + */ +@Data +public class DingUserDetailDTO { + // 接口文档:https://open.dingtalk.com/document/isvapp-server/query-user-details + /** + * 员工的userId。 + */ + private String userid; + + /** + * 员工在当前开发者企业账号范围内的唯一标识。 + */ + private String unionid; + + /** + * 员工姓名。 + */ + private String name; + /** + * 头像。 + *

+ * 说明 员工使用默认头像,不返回该字段,手动设置头像会返回 + */ + private String avatar; + /** + * 国际电话区号。 + *

+ * 说明 第三方企业应用不返回该字段;如需获取state_code,可以使用钉钉统一授权套件方式获取。 + */ + private String state_code; + /** + * 手机号码。 + *

+ * 说明 + * 企业内部应用,只有应用开通通讯录邮箱等个人信息权限,才会返回该字段。 + * 第三方企业应用不返回该字段,如需获取mobile,可以使用钉钉统一授权套件方式获取。 + */ + private String mobile; + /** + * 是否号码隐藏: + *

+ * true:隐藏 + *

+ * false:不隐藏 + *

+ * 说明 隐藏手机号后,手机号在个人资料页隐藏,但仍可对其发DING、发起钉钉免费商务电话。 + */ + private String hide_mobile; + /** + * 分机号。 + *

+ * 说明 第三方企业应用不返回该参数。 + */ + private String telephone; + + /** + * 员工的企业邮箱。 + * + * 如果员工的企业邮箱没有开通,返回信息中不包含该数据。 + * + * 说明 第三方企业应用不返回该参数。 + */ + private String org_email; +} + diff --git a/epmet-auth/src/main/java/com/epmet/dto/dingres/V2UserGetuserinfoResDTO.java b/epmet-auth/src/main/java/com/epmet/dto/dingres/V2UserGetuserinfoResDTO.java new file mode 100644 index 0000000000..ea9eb3b46b --- /dev/null +++ b/epmet-auth/src/main/java/com/epmet/dto/dingres/V2UserGetuserinfoResDTO.java @@ -0,0 +1,54 @@ +package com.epmet.dto.dingres; + +import lombok.Data; + +/** + * @Description + * @Author yzm + * @Date 2022/9/22 14:08 + */ +@Data +public class V2UserGetuserinfoResDTO { + // 接口返参:https://open.dingtalk.com/document/orgapp-server/obtain-the-userid-of-a-user-by-using-the-log-free + /** + * 用户的userid。 + */ + private String userid; + /** + * 设备ID。 + */ + private String device_id; + /** + * 是否是管理员。 + *

+ * true:是 + *

+ * false:不是 + */ + private Boolean sys; + /** + * 级别。 + *

+ * 1:主管理员 + *

+ * 2:子管理员 + *

+ * 100:老板 + *

+ * 0:其他(如普通员工) + */ + private Number sys_level; + /** + * 用户关联的unionId。 + */ + private String associated_unionid; + /** + * 用户unionId。 + */ + private String unionid; + /** + * 用户名字。 + */ + private String name; +} + 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 236a46e2b9..10c3f8a2e8 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 @@ -11,6 +11,7 @@ import com.epmet.common.token.constant.LoginConstant; import com.epmet.commons.rocketmq.messages.LoginMQMsg; import com.epmet.commons.tools.constant.AppClientConstant; import com.epmet.commons.tools.constant.ServiceConstant; +import com.epmet.commons.tools.constant.StrConstant; import com.epmet.commons.tools.enums.EnvEnum; import com.epmet.commons.tools.exception.EpmetErrorCode; import com.epmet.commons.tools.exception.EpmetException; @@ -28,6 +29,8 @@ import com.epmet.commons.tools.validator.PhoneValidatorUtils; import com.epmet.constant.AuthHttpUrlConstant; import com.epmet.constant.SmsTemplateConstant; import com.epmet.dto.*; +import com.epmet.dto.dingres.DingUserDetailDTO; +import com.epmet.dto.dingres.V2UserGetuserinfoResDTO; import com.epmet.dto.form.*; import com.epmet.dto.result.*; import com.epmet.feign.EpmetMessageOpenFeignClient; @@ -846,17 +849,49 @@ public class ThirdLoginServiceImpl implements ThirdLoginService, ResultDataResol private DingLoginResiFormDTO getDingLoginResiFormDTOMd(String miniAppId, String authCode) { DingMiniInfoCache dingMiniInfo = CustomerDingDingRedis.getDingMiniInfo(miniAppId); + // 1、服务商获取第三方应用授权企业的access_token,文档地址:https://open.dingtalk.com/document/orgapp-server/obtains-the-enterprise-authorized-credential // 烟台的CorpId: dingaae55cbc47a96845f5bf40eda33b7ba0 - DingTalkResult res = dingTalkClientToken.getThirdAuthCorpAccessToken(dingMiniInfo.getSuiteKey(), dingMiniInfo.getSuiteSecret(), "abc", "dingaae55cbc47a96845f5bf40eda33b7ba0"); + String yantaiCorpId = "dingaae55cbc47a96845f5bf40eda33b7ba0"; + DingTalkResult res = dingTalkClientToken.getThirdAuthCorpAccessToken(dingMiniInfo.getSuiteKey(), dingMiniInfo.getSuiteSecret(), "abc", yantaiCorpId); if (!res.success() || StringUtils.isBlank(res.getData())) { + log.error(String.format("企业内部应用免登服务商获取第三方应用授权企业的access_token失败,customKey:%s,customSecret:%s,corpId:%s", dingMiniInfo.getSuiteSecret(), dingMiniInfo.getSuiteSecret(), yantaiCorpId)); throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "服务商获取第三方应用授权企业的access_token", "服务商获取第三方应用授权企业的access_token"); } + String accessToken = res.getData(); + log.info(String.format("1、服务商获取第三方应用授权企业的access_token返参:%s", accessToken)); + // 2、通过免登码获取用户信息,文档地址:https://open.dingtalk.com/document/orgapp-server/obtain-the-userid-of-a-user-by-using-the-log-free - // dingTalkClientToken.getTodo(); + DingTalkResult v2UserGetuserinfoRes = dingTalkClientToken.getUserInfo(accessToken, authCode); + if (!v2UserGetuserinfoRes.success() || StringUtils.isBlank(v2UserGetuserinfoRes.getData())) { + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "通过免登码获取用户信息异常", "通过免登码获取用户信息异常"); + } + log.info(String.format("2、通过免登码获取用户信息返参:%s", v2UserGetuserinfoRes.getData())); + V2UserGetuserinfoResDTO v2UserGetuserinfoResDTO = JSON.parseObject(v2UserGetuserinfoRes.getData(), V2UserGetuserinfoResDTO.class); + if (null == v2UserGetuserinfoResDTO || StringUtils.isBlank(v2UserGetuserinfoResDTO.getUserid())) { + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "获取钉钉用户userid为空", "获取钉钉用户userid为空"); + } + + // 3、查询用户详情,文档地址:https://open.dingtalk.com/document/isvapp-server/query-user-details - // dingTalkClientToken.getTodo(); + DingTalkResult v2UserGetRes = dingTalkClientToken.getUserDetail(v2UserGetuserinfoResDTO.getUserid(), accessToken); + if (!v2UserGetRes.success() || StringUtils.isBlank(v2UserGetRes.getData())) { + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "根据userId查询用户详情失败", "根据userId查询用户详情失败"); + } + log.info(String.format("3、查询用户详情:%s", v2UserGetRes.getData())); + DingUserDetailDTO dingUserDetailDTO = JSON.parseObject(v2UserGetRes.getData(), DingUserDetailDTO.class); + if (null == dingUserDetailDTO || StringUtils.isBlank(dingUserDetailDTO.getMobile())) { + throw new EpmetException(EpmetErrorCode.EPMET_COMMON_OPERATION_FAIL.getCode(), "获取手机号为空", "获取手机号为空"); + } + DingLoginResiFormDTO dingLoginResiFormDTO = new DingLoginResiFormDTO(); + dingLoginResiFormDTO.setAvatarUrl(dingUserDetailDTO.getAvatar()); + dingLoginResiFormDTO.setEmail(dingUserDetailDTO.getOrg_email()); + dingLoginResiFormDTO.setMobile(dingUserDetailDTO.getMobile()); + dingLoginResiFormDTO.setNick(dingUserDetailDTO.getName()); + dingLoginResiFormDTO.setOpenId(StrConstant.EPMETY_STR); + dingLoginResiFormDTO.setStateCode(dingUserDetailDTO.getState_code()); + dingLoginResiFormDTO.setUnionId(dingUserDetailDTO.getUnionid()); return dingLoginResiFormDTO; }