9 changed files with 161 additions and 0 deletions
			
			
		| @ -0,0 +1,28 @@ | |||||
|  | package com.epmet.dto.form; | ||||
|  | 
 | ||||
|  | import lombok.Data; | ||||
|  | 
 | ||||
|  | import javax.validation.constraints.NotBlank; | ||||
|  | import java.io.Serializable; | ||||
|  | 
 | ||||
|  | /** | ||||
|  |  * @Author zxc | ||||
|  |  * @DateTime 2022/9/14 14:52 | ||||
|  |  * @DESC | ||||
|  |  */ | ||||
|  | @Data | ||||
|  | public class ExemptLoginUserDetailFormDTO implements Serializable { | ||||
|  | 
 | ||||
|  |     private static final long serialVersionUID = -4333806195203619201L; | ||||
|  | 
 | ||||
|  |     public interface ExemptLoginUserDetailForm{} | ||||
|  | 
 | ||||
|  |     @NotBlank(message = "code不能为空",groups = ExemptLoginUserDetailForm.class) | ||||
|  |     private String code; | ||||
|  | 
 | ||||
|  |     @NotBlank(message = "corpId不能为空",groups = ExemptLoginUserDetailForm.class) | ||||
|  |     private String corpId; | ||||
|  | 
 | ||||
|  |     @NotBlank(message = "miniAppId不能为空",groups = ExemptLoginUserDetailForm.class) | ||||
|  |     private String miniAppId; | ||||
|  | } | ||||
| @ -0,0 +1,14 @@ | |||||
|  | package com.epmet.service; | ||||
|  | 
 | ||||
|  | import com.epmet.dto.form.ExemptLoginUserDetailFormDTO; | ||||
|  | 
 | ||||
|  | /** | ||||
|  |  * @Author zxc | ||||
|  |  * @DateTime 2022/9/14 14:56 | ||||
|  |  * @DESC | ||||
|  |  */ | ||||
|  | public interface DingTalkService { | ||||
|  | 
 | ||||
|  |     Object getExemptLoginUserDetail(ExemptLoginUserDetailFormDTO formDTO); | ||||
|  | 
 | ||||
|  | } | ||||
| @ -0,0 +1,78 @@ | |||||
|  | package com.epmet.service.impl; | ||||
|  | 
 | ||||
|  | import com.alibaba.fastjson.JSON; | ||||
|  | import com.aliyun.dingtalk.module.Result; | ||||
|  | import com.dingtalk.api.DefaultDingTalkClient; | ||||
|  | import com.dingtalk.api.DingTalkClient; | ||||
|  | import com.dingtalk.api.request.OapiV2UserGetRequest; | ||||
|  | import com.dingtalk.api.request.OapiV2UserGetuserinfoRequest; | ||||
|  | import com.dingtalk.api.response.OapiV2UserGetResponse; | ||||
|  | import com.dingtalk.api.response.OapiV2UserGetuserinfoResponse; | ||||
|  | import com.epmet.commons.tools.exception.EpmetException; | ||||
|  | import com.epmet.dao.OpenSyncBizDataDao; | ||||
|  | import com.epmet.dto.DingMiniInfoDTO; | ||||
|  | import com.epmet.dto.form.ExemptLoginUserDetailFormDTO; | ||||
|  | import com.epmet.redis.DingDingCallbackRedis; | ||||
|  | import com.epmet.service.DingTalkService; | ||||
|  | import com.taobao.api.ApiException; | ||||
|  | import com.taobao.dingtalk.client.DingTalkClientToken; | ||||
|  | import com.taobao.dingtalk.vo.result.AccessTokenResult; | ||||
|  | import lombok.extern.slf4j.Slf4j; | ||||
|  | import org.springframework.beans.factory.annotation.Autowired; | ||||
|  | import org.springframework.stereotype.Service; | ||||
|  | 
 | ||||
|  | import java.util.Map; | ||||
|  | 
 | ||||
|  | /** | ||||
|  |  * @Author zxc | ||||
|  |  * @DateTime 2022/9/14 14:57 | ||||
|  |  * @DESC | ||||
|  |  */ | ||||
|  | @Service | ||||
|  | @Slf4j | ||||
|  | public class DingTalkServiceImpl implements DingTalkService { | ||||
|  | 
 | ||||
|  |     @Autowired | ||||
|  |     private DingTalkClientToken dingTalkClientToken; | ||||
|  |     @Autowired | ||||
|  |     private OpenSyncBizDataDao openSyncBizDataDao; | ||||
|  |     @Autowired | ||||
|  |     private DingDingCallbackRedis dingCallbackRedis; | ||||
|  | 
 | ||||
|  |     @Override | ||||
|  |     public Object getExemptLoginUserDetail(ExemptLoginUserDetailFormDTO formDTO) { | ||||
|  |         DingMiniInfoDTO dingMiniInfo = openSyncBizDataDao.getDingMiniInfoByAppId(formDTO.getMiniAppId()); | ||||
|  | 
 | ||||
|  |         Result<AccessTokenResult> isvAccessTokenToken = dingTalkClientToken.getIsvAccessTokenToken(formDTO.getCorpId(), dingMiniInfo.getSuiteKey(), dingMiniInfo.getSuiteSecret(), dingCallbackRedis.get(dingMiniInfo.getSuiteKey())); | ||||
|  |         if (!isvAccessTokenToken.success()){ | ||||
|  |             throw new EpmetException("获取accessToken失败..."); | ||||
|  |         } | ||||
|  |         String accessToken = isvAccessTokenToken.getData().getAccessToken(); | ||||
|  |         DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getuserinfo"); | ||||
|  |         OapiV2UserGetuserinfoRequest req = new OapiV2UserGetuserinfoRequest(); | ||||
|  |         req.setCode(formDTO.getCode()); | ||||
|  |         Object o = new Object(); | ||||
|  |         try { | ||||
|  |             OapiV2UserGetuserinfoResponse rsp = client.execute(req, accessToken); | ||||
|  |             Map<String,Object> map = JSON.parseObject(rsp.getBody(), Map.class); | ||||
|  |             if (!map.containsValue("ok")){ | ||||
|  |                 throw new EpmetException("通过免登码获取用户信息失败..."); | ||||
|  |             } | ||||
|  |             Map<String,Object> userInfo = JSON.parseObject(map.get("result").toString(), Map.class); | ||||
|  |             DingTalkClient userDetailClient = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/get"); | ||||
|  |             OapiV2UserGetRequest userDetailRequest = new OapiV2UserGetRequest(); | ||||
|  |             userDetailRequest.setUserid(userInfo.get("userid").toString()); | ||||
|  |             userDetailRequest.setLanguage("zh_CN"); | ||||
|  |             OapiV2UserGetResponse execute = userDetailClient.execute(userDetailRequest, accessToken); | ||||
|  |             Map<String,Object> userDetailResult = JSON.parseObject(execute.getBody(), Map.class); | ||||
|  |             if (!userDetailResult.containsValue("ok")){ | ||||
|  |                 throw new EpmetException("查询用户详情失败..."); | ||||
|  |             } | ||||
|  |             o = userDetailResult.get("result"); | ||||
|  |         } catch (ApiException e) { | ||||
|  |             log.error(e.getMessage()); | ||||
|  |             e.printStackTrace(); | ||||
|  |         } | ||||
|  |         return o; | ||||
|  |     } | ||||
|  | } | ||||
					Loading…
					
					
				
		Reference in new issue