From 76d073ee8427e0285ca98d805a83707996079286 Mon Sep 17 00:00:00 2001 From: WenG <450292408@qq.com> Date: Fri, 9 Oct 2020 09:15:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=92=89=E9=92=89=E5=A4=87=E4=BB=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../properties/DingDingAppProperties.java | 13 ++- .../controller/api/DemoController.java | 36 ++++++ .../DingDingMessageResourceController.java | 64 ++++++++++ .../service/IDingDingAppUserService.java | 7 ++ .../service/IDingDingMessageService.java | 38 ++++++ .../impl/DingDingAppUserServiceImpl.java | 19 +++ .../service/impl/DingDingMessageService.java | 109 ++++++++++++++++++ 7 files changed, 285 insertions(+), 1 deletion(-) create mode 100644 cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/api/DemoController.java create mode 100644 cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/resource/DingDingMessageResourceController.java create mode 100644 cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingMessageService.java create mode 100644 cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingMessageService.java diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/config/properties/DingDingAppProperties.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/config/properties/DingDingAppProperties.java index e91ca57..8197a75 100644 --- a/cloud-common-dingding/src/main/java/com/cm/common/dingding/config/properties/DingDingAppProperties.java +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/config/properties/DingDingAppProperties.java @@ -17,6 +17,7 @@ import org.springframework.context.annotation.Configuration; @ConfigurationProperties(prefix = "open-platform.dingding.app") public class DingDingAppProperties { + private Long agentId; private Boolean active; private String appKey; private String appSecret; @@ -24,6 +25,14 @@ public class DingDingAppProperties { private String useridByUnionidUrl; private String userinfoByIdUrl; + public Long getAgentId() { + return agentId; + } + + public void setAgentId(Long agentId) { + this.agentId = agentId; + } + public Boolean getActive() { return active; } @@ -75,7 +84,9 @@ public class DingDingAppProperties { @Override public String toString() { final StringBuilder sb = new StringBuilder("{"); - sb.append("\"active\":") + sb.append("\"agentId\":") + .append(agentId); + sb.append(",\"active\":") .append(active); sb.append(",\"appKey\":\"") .append(appKey).append('\"'); diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/api/DemoController.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/api/DemoController.java new file mode 100644 index 0000000..7adb48b --- /dev/null +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/api/DemoController.java @@ -0,0 +1,36 @@ +package com.cm.common.dingding.controller.api; + +import com.cm.common.constants.ISystemConstant; +import com.cm.common.dingding.service.IDingDingMessageService; +import com.cm.common.result.SuccessResult; +import io.swagger.annotations.Api; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DemoController + * @Description: + * @Author: WangGeng + * @Date: 2020/9/24 16:38 + * @Version: 1.0 + **/ +@Api(tags = ISystemConstant.API_TAGS_APP_PREFIX + "钉钉消息接口") +@RestController +@RequestMapping(ISystemConstant.API_PREFIX + "/demomessage") +public class DemoController { + + @Autowired + private IDingDingMessageService dingDingMessageService; + + @GetMapping("sendmessage/{phone}") + private SuccessResult sendMessage(@PathVariable("phone") String phone) { + return dingDingMessageService.send(phone, "测试"); + } + +} diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/resource/DingDingMessageResourceController.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/resource/DingDingMessageResourceController.java new file mode 100644 index 0000000..ac744ee --- /dev/null +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/controller/resource/DingDingMessageResourceController.java @@ -0,0 +1,64 @@ +package com.cm.common.dingding.controller.resource; + +import com.cm.common.base.AbstractController; +import com.cm.common.constants.ISystemConstant; +import com.cm.common.dingding.service.IDingDingMessageService; +import com.cm.common.exception.ParamsException; +import com.cm.common.result.ErrorResult; +import com.cm.common.result.SuccessResult; +import com.cm.common.utils.RegexUtil; +import io.swagger.annotations.*; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import java.util.Arrays; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DingDingMessageController + * @Description: 钉钉消息 + * @Author: WangGeng + * @Date: 2020/9/24 13:40 + * @Version: 1.0 + **/ +@Api(tags = ISystemConstant.API_TAGS_RESOURCE_PREFIX + "钉钉消息接口") +@RestController +@RequestMapping(ISystemConstant.RESOURCE_PREFIX + "/dingdingmessage") +public class DingDingMessageResourceController extends AbstractController { + + @Autowired + private IDingDingMessageService dingDingMessageService; + + @ApiOperation(value = "发送消息", notes = "发送消息接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"), + @ApiImplicitParam(name = "phone", value = "phone", paramType = "query"), + @ApiImplicitParam(name = "content", value = "content", paramType = "query"), + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PostMapping("send") + public SuccessResult send(@RequestParam("phone") String phone, @RequestParam("content") String content) { + if (!RegexUtil.isPhone(phone)) { + throw new ParamsException("手机格式错误"); + } + return dingDingMessageService.send(phone, content); + } + + @ApiOperation(value = "通过手机列表发送消息", notes = "通过手机列表发送消息接口") + @ApiImplicitParams({ + @ApiImplicitParam(name = "access_token", value = "access_token", paramType = "query"), + @ApiImplicitParam(name = "phones", value = "phone", paramType = "query"), + @ApiImplicitParam(name = "content", value = "content", paramType = "query"), + }) + @ApiResponses({@ApiResponse(code = 400, message = "请求失败", response = ErrorResult.class)}) + @PostMapping("sendbyphones") + public SuccessResult sendByPhones(@RequestParam("phones") String phones, @RequestParam("content") String content) { + return dingDingMessageService.sendByPhones(Arrays.asList(phones.split("_")), content); + } + +} diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppUserService.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppUserService.java index 4ff45a2..b1e6379 100644 --- a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppUserService.java +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingAppUserService.java @@ -33,5 +33,12 @@ public interface IDingDingAppUserService { */ OapiUserGetResponse getUserByUserId(String userId) throws SearchException; + /** + * 通过手机获取用户ID + * + * @param phone + * @return + */ + String getUserIdByPhone(String phone); } diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingMessageService.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingMessageService.java new file mode 100644 index 0000000..4944a0d --- /dev/null +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/IDingDingMessageService.java @@ -0,0 +1,38 @@ +package com.cm.common.dingding.service; + +import com.cm.common.exception.SearchException; +import com.cm.common.result.SuccessResult; + +import java.util.List; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: IDingDingMessageService + * @Description: 钉钉消息 + * @Author: WangGeng + * @Date: 2020/9/24 13:39 + * @Version: 1.0 + **/ +public interface IDingDingMessageService { + /** + * 发送消息 + * + * @param phone + * @param content + * @return + * @throws SearchException + */ + SuccessResult send(String phone, String content) throws SearchException; + + /** + * 通过手机列表发送消息 + * + * @param phones + * @param content + * @return + * @throws SearchException + */ + SuccessResult sendByPhones(List phones, String content) throws SearchException; +} diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppUserServiceImpl.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppUserServiceImpl.java index 1cc979c..77c8a35 100644 --- a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppUserServiceImpl.java +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingAppUserServiceImpl.java @@ -7,8 +7,10 @@ import com.cm.common.dingding.service.IDingDingAppUserService; import com.cm.common.exception.SearchException; import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DingTalkClient; +import com.dingtalk.api.request.OapiUserGetByMobileRequest; import com.dingtalk.api.request.OapiUserGetRequest; import com.dingtalk.api.request.OapiUserGetUseridByUnionidRequest; +import com.dingtalk.api.response.OapiUserGetByMobileResponse; import com.dingtalk.api.response.OapiUserGetResponse; import com.dingtalk.api.response.OapiUserGetUseridByUnionidResponse; import com.taobao.api.ApiException; @@ -67,4 +69,21 @@ public class DingDingAppUserServiceImpl extends AbstractService implements IDing } } + @Override + public String getUserIdByPhone(String phone) { + try { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/get_by_mobile"); + OapiUserGetByMobileRequest oapiUserGetByMobileRequest = new OapiUserGetByMobileRequest(); + oapiUserGetByMobileRequest.setMobile(phone); + OapiUserGetByMobileResponse oapiUserGetByMobileResponse = client.execute(oapiUserGetByMobileRequest, DingDingAppManager.getInstance().getAccessToken()); + if (oapiUserGetByMobileResponse.getErrcode() != 0) { + throw new SearchException(oapiUserGetByMobileResponse.getErrmsg()); + } + return oapiUserGetByMobileResponse.getUserid(); + } catch (ApiException e) { + LOG.error(e.getMessage(), e); + return null; + } + } + } diff --git a/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingMessageService.java b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingMessageService.java new file mode 100644 index 0000000..dd9299c --- /dev/null +++ b/cloud-common-dingding/src/main/java/com/cm/common/dingding/service/impl/DingDingMessageService.java @@ -0,0 +1,109 @@ +package com.cm.common.dingding.service.impl; + +import com.cm.common.base.AbstractService; +import com.cm.common.dingding.config.properties.DingDingAppProperties; +import com.cm.common.dingding.manager.app.DingDingAppManager; +import com.cm.common.dingding.service.IDingDingAppUserService; +import com.cm.common.dingding.service.IDingDingMessageService; +import com.cm.common.exception.SearchException; +import com.cm.common.result.SuccessResult; +import com.dingtalk.api.DefaultDingTalkClient; +import com.dingtalk.api.DingTalkClient; +import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request; +import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response; +import com.taobao.api.ApiException; +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.parameters.P; +import org.springframework.stereotype.Service; + +import java.util.ArrayList; +import java.util.List; + +/** + * When you feel like quitting. Think about why you started + * 当你想要放弃的时候,想想当初你为何开始 + * + * @ClassName: DingDingMessageService + * @Description: 钉钉消息 + * @Author: WangGeng + * @Date: 2020/9/24 13:39 + * @Version: 1.0 + **/ +@Service +public class DingDingMessageService extends AbstractService implements IDingDingMessageService { + + @Autowired + private DingDingAppProperties dingDingAppProperties; + @Autowired + private IDingDingAppUserService dingDingAppUserService; + + @Override + public SuccessResult send(String phone, String content) throws SearchException { + String userId = dingDingAppUserService.getUserIdByPhone(phone); + if (StringUtils.isBlank(userId)) { + throw new SearchException("获取钉钉用户ID失败"); + } + sendMessage(userId, content); + return new SuccessResult(); + } + + @Override + public SuccessResult sendByPhones(List phones, String content) throws SearchException { + List userIds = new ArrayList<>(); + for (String phone : phones) { + String userId = dingDingAppUserService.getUserIdByPhone(phone); + if (!StringUtils.isBlank(userId)) { + continue; + } + userIds.add(userId); + } + if (userIds.isEmpty()) { + return new SuccessResult(); + } + int sendCount = userIds.size() / 100 + 1; + for (int i = 0; i < sendCount; i++) { + StringBuilder userIdSB = new StringBuilder(); + int lastUserLength = userIds.size() - i * 100; + int userLength = lastUserLength > 100 ? 100 : lastUserLength; + for (int j = 0; j < userLength; j++) { + if (userIdSB.length() != 0) { + userIdSB.append(","); + } + userIdSB.append(userIds.get(i * 100 + j)); + } + sendMessage(userIdSB.toString(), content); + } + return new SuccessResult(); + } + + /** + * 发送消息 + * + * @param userIds + * @param content + */ + private void sendMessage(String userIds, String content) { + try { + DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2"); + OapiMessageCorpconversationAsyncsendV2Request oapiMessageCorpconversationAsyncsendV2Request = new OapiMessageCorpconversationAsyncsendV2Request(); + oapiMessageCorpconversationAsyncsendV2Request.setUseridList(userIds); + oapiMessageCorpconversationAsyncsendV2Request.setAgentId(dingDingAppProperties.getAgentId()); + oapiMessageCorpconversationAsyncsendV2Request.setToAllUser(false); + + OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg(); + msg.setMsgtype("text"); + msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text()); + msg.getText().setContent(content); + oapiMessageCorpconversationAsyncsendV2Request.setMsg(msg); + + OapiMessageCorpconversationAsyncsendV2Response oapiMessageCorpconversationAsyncsendV2Response = client.execute(oapiMessageCorpconversationAsyncsendV2Request, DingDingAppManager.getInstance().getAccessToken()); + if (oapiMessageCorpconversationAsyncsendV2Response.getErrcode() != 0) { + throw new SearchException(oapiMessageCorpconversationAsyncsendV2Response.getMsg()); + } + } catch (ApiException e) { + e.printStackTrace(); + } + } + +}