钉钉备份

This commit is contained in:
WenG 2020-10-09 09:15:34 +08:00
parent 10e88967be
commit 76d073ee84
7 changed files with 285 additions and 1 deletions

View File

@ -17,6 +17,7 @@ import org.springframework.context.annotation.Configuration;
@ConfigurationProperties(prefix = "open-platform.dingding.app") @ConfigurationProperties(prefix = "open-platform.dingding.app")
public class DingDingAppProperties { public class DingDingAppProperties {
private Long agentId;
private Boolean active; private Boolean active;
private String appKey; private String appKey;
private String appSecret; private String appSecret;
@ -24,6 +25,14 @@ public class DingDingAppProperties {
private String useridByUnionidUrl; private String useridByUnionidUrl;
private String userinfoByIdUrl; private String userinfoByIdUrl;
public Long getAgentId() {
return agentId;
}
public void setAgentId(Long agentId) {
this.agentId = agentId;
}
public Boolean getActive() { public Boolean getActive() {
return active; return active;
} }
@ -75,7 +84,9 @@ public class DingDingAppProperties {
@Override @Override
public String toString() { public String toString() {
final StringBuilder sb = new StringBuilder("{"); final StringBuilder sb = new StringBuilder("{");
sb.append("\"active\":") sb.append("\"agentId\":")
.append(agentId);
sb.append(",\"active\":")
.append(active); .append(active);
sb.append(",\"appKey\":\"") sb.append(",\"appKey\":\"")
.append(appKey).append('\"'); .append(appKey).append('\"');

View File

@ -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, "测试");
}
}

View File

@ -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);
}
}

View File

@ -33,5 +33,12 @@ public interface IDingDingAppUserService {
*/ */
OapiUserGetResponse getUserByUserId(String userId) throws SearchException; OapiUserGetResponse getUserByUserId(String userId) throws SearchException;
/**
* 通过手机获取用户ID
*
* @param phone
* @return
*/
String getUserIdByPhone(String phone);
} }

View File

@ -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<String> phones, String content) throws SearchException;
}

View File

@ -7,8 +7,10 @@ import com.cm.common.dingding.service.IDingDingAppUserService;
import com.cm.common.exception.SearchException; import com.cm.common.exception.SearchException;
import com.dingtalk.api.DefaultDingTalkClient; import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient; import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserGetByMobileRequest;
import com.dingtalk.api.request.OapiUserGetRequest; import com.dingtalk.api.request.OapiUserGetRequest;
import com.dingtalk.api.request.OapiUserGetUseridByUnionidRequest; import com.dingtalk.api.request.OapiUserGetUseridByUnionidRequest;
import com.dingtalk.api.response.OapiUserGetByMobileResponse;
import com.dingtalk.api.response.OapiUserGetResponse; import com.dingtalk.api.response.OapiUserGetResponse;
import com.dingtalk.api.response.OapiUserGetUseridByUnionidResponse; import com.dingtalk.api.response.OapiUserGetUseridByUnionidResponse;
import com.taobao.api.ApiException; 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;
}
}
} }

View File

@ -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<String> phones, String content) throws SearchException {
List<String> 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();
}
}
}