新增通过手机号批量发送钉钉消息

yun-zuoyi
nies 3 years ago
parent 8b337e10c5
commit aec7e5d709

@ -29,12 +29,10 @@ import cn.estsh.i3plus.pojo.model.common.ImppEmail;
import cn.estsh.i3plus.pojo.model.common.ImppSmsContent;
import cn.estsh.i3plus.pojo.model.common.UserModel;
import cn.estsh.i3plus.pojo.model.license.ImppLicense;
import cn.estsh.i3plus.pojo.model.platform.SysLoginModel;
import cn.estsh.i3plus.pojo.model.platform.SysRoleModel;
import cn.estsh.i3plus.pojo.model.platform.UserDetailModel;
import cn.estsh.i3plus.pojo.model.platform.UserDetailPagerModel;
import cn.estsh.i3plus.pojo.model.platform.*;
import cn.estsh.i3plus.pojo.platform.bean.*;
import cn.estsh.i3plus.sdk.dingtalk.cn.estsh.i3plus.sdk.service.IDingTalkService;
import cn.estsh.i3plus.sdk.dingtalk.dingrobot.DingRobotRequestBody;
import cn.estsh.i3plus.sdk.dingtalk.service.IDingTalkService;
import cn.estsh.impp.framework.base.controller.CoreBaseController;
import cn.estsh.impp.framework.boot.auth.AuthUtil;
import cn.estsh.impp.framework.boot.exception.ImppBusiException;
@ -42,10 +40,24 @@ import cn.estsh.impp.framework.boot.exception.ImppExceptionBuilder;
import cn.estsh.impp.framework.boot.license.ImppLicenseTool;
import cn.estsh.impp.framework.boot.license.serviceimpl.ImppLicenseDecoder;
import cn.estsh.impp.framework.boot.util.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenRequest;
import com.aliyun.dingtalkoauth2_1_0.models.GetAccessTokenResponse;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOHeaders;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTORequest;
import com.aliyun.dingtalkrobot_1_0.models.BatchSendOTOResponse;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiV2UserGetbymobileRequest;
import com.dingtalk.api.response.OapiV2UserGetResponse;
import com.dingtalk.api.response.OapiV2UserGetbymobileResponse;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.taobao.api.ApiException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
@ -320,6 +332,7 @@ public class WhiteController extends CoreBaseController {
return ResultBean.success("查询成功").setResultList(menuList);
}
@GetMapping(value = "/auth/dingtalk/login")
@ApiOperation(value = "登录", notes = "登录")
public ResultBean loginByPhoneNumber(HttpServletRequest request, String dingTalkTmpAuthCode,
@ -956,6 +969,7 @@ public class WhiteController extends CoreBaseController {
/**
*
*
* @param sysUser
* @return
*/
@ -1054,6 +1068,179 @@ public class WhiteController extends CoreBaseController {
}
private static final Long ACCESS_TOKEN_EXPIRE_TIME = 7080L;
private static final String ACCESS_TOKEN_KEY = "ding_talk:access_token";
/**
*
* @param dingSendBatchRequestModel
* @return
*/
@PostMapping(value = "/dingRobot/sendBatchRequest")
@ApiOperation(value = "根据手机号 钉钉单聊机器人批量发送消息", notes = "根据手机号 钉钉单聊机器人批量发送消息")
public ResultBean singleRobotsendBatchRequest(@RequestBody DingSendBatchRequestModel dingSendBatchRequestModel ) {
if(ObjectUtils.isEmpty(dingSendBatchRequestModel)){
return ResultBean.fail("消息内容不能为空");
}
List<String> phoneNumberList = dingSendBatchRequestModel.getPhoneNumberList();
String title = dingSendBatchRequestModel.getTitle();
String content = dingSendBatchRequestModel.getContent();
if (StringUtils.isBlank(content)) {
return ResultBean.fail("消息内容不能为空");
}
if (ObjectUtils.isEmpty(phoneNumberList)) {
return ResultBean.fail("手机号不能为空");
}
phoneNumberList = phoneNumberList.stream().filter(StringUtils::isNotBlank).collect(Collectors.toList());
if (ObjectUtils.isEmpty(phoneNumberList)) {
return ResultBean.fail("手机号不能为空");
}
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkrobot_1_0.Client client = null;
try {
client = new com.aliyun.dingtalkrobot_1_0.Client(config);
} catch (Exception e) {
e.printStackTrace();
return ResultBean.fail(e).setMsg("发送失败");
}
BatchSendOTOHeaders batchSendOTOHeaders = new BatchSendOTOHeaders();
batchSendOTOHeaders.xAcsDingtalkAccessToken = getAccessToken();
BatchSendOTORequest batchSendOTORequest = new BatchSendOTORequest();
List<String> userIdList = new ArrayList<String>();
phoneNumberList.forEach(el -> {
ResultBean result = getDingUserIdByPhoneNumber(el, batchSendOTOHeaders.xAcsDingtalkAccessToken);
if (result.isSuccess()) {
userIdList.add((String) result.getResultObject());
}
});
if (userIdList.isEmpty()) {
return ResultBean.fail("发送失败,无法根据手机号获取用户id");
}
// userIdList.add("27202755621041923");
// userIdList.add("070537336424114523");
String dingTalkAppKey = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.DINGTALK_APP_KEY);
batchSendOTORequest.setRobotCode(dingTalkAppKey);
batchSendOTORequest.setUserIds(userIdList);
// markdown officialImageMsg
DingRobotRequestBody requestBody = generateMarkdown(title, content);
batchSendOTORequest.setMsgKey("sampleMarkdown");
batchSendOTORequest.setMsgParam(JSON.toJSONString(requestBody.getMarkdown()));
try {
BatchSendOTOResponse response = client.batchSendOTOWithOptions(batchSendOTORequest, batchSendOTOHeaders, new RuntimeOptions());
System.out.println(JSON.toJSONString(response));
} catch (TeaException err) {
err.printStackTrace();
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
}
return ResultBean.fail(err).setMsg("发送失败");
} catch (Exception _err) {
_err.printStackTrace();
TeaException err = new TeaException(_err.getMessage(), _err);
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
}
return ResultBean.fail(err).setMsg("发送失败");
}
return ResultBean.success("发送成功");
}
private DingRobotRequestBody generateMarkdown(String title, String content) {
DingRobotRequestBody dingRobotRequestBody = new DingRobotRequestBody();
DingRobotRequestBody.MarkDown markDown = new DingRobotRequestBody.MarkDown();
markDown.setTitle(title);
dingRobotRequestBody.setMsgType("markdown");
// markDown.setText(" # Andon 通知" +
// "\n"+
// "\n"+
// " [![qGTEi6.png](https://s1.ax1x.com/2022/03/24/qGTEi6.png)](https://imgtu.com/i/qGTEi6) EP产线-01工位物料缺料请及时处理 "
// );
// markDown.setText(" # Andon 通知" +
// "\n"+
// "\n"+
// " [![qGTEi6.png](https://s1.ax1x.com/2022/03/24/qGTEi6.png)](https://imgtu.com/i/qGTEi6) EP产线-01工位物料缺料请及时处理 "
// );
// markDown.setText(" ## Andon 通知" +
// "\n"+
// "\n"+
// " [![qGTEi6.png](https://s1.ax1x.com/2022/03/24/qGTEi6.png)](https://imgtu.com/i/qGTEi6) EP产线-01工位物料缺料处理措施:联络供应商送货"
// );
markDown.setText(content);
dingRobotRequestBody.setMarkDown(markDown);
return dingRobotRequestBody;
}
/**
* uid
*
* @param phoneNumber
* @return
*/
public ResultBean getDingUserIdByPhoneNumber(String phoneNumber, String accessToken) {
try {
DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/v2/user/getbymobile");
OapiV2UserGetbymobileRequest req = new OapiV2UserGetbymobileRequest();
req.setMobile(phoneNumber);
// req.setMobile("13298408382");
OapiV2UserGetbymobileResponse rsp = client.execute(req, accessToken);
return ResultBean.success("获取用户id成功").setResultObject(rsp.getResult().getUserid());
} catch (ApiException e) {
e.printStackTrace();
return ImppExceptionBuilder.newInstance().buildExceptionResult(e);
}
}
/**
* accessToken
*
* @return
*/
private String getAccessToken() {
Object accessTokenCached = redisCore.getObject(ACCESS_TOKEN_KEY);
if (accessTokenCached != null) {
return String.valueOf(accessTokenCached);
}
Config config = new Config();
config.protocol = "https";
config.regionId = "central";
com.aliyun.dingtalkoauth2_1_0.Client client = null;
String dingTalkAppKey = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.DINGTALK_APP_KEY);
String dingTalkAppSecret = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.DINGTALK_APP_SECRET);
try {
client = new com.aliyun.dingtalkoauth2_1_0.Client(config);
} catch (Exception e) {
e.printStackTrace();
}
GetAccessTokenRequest getAccessTokenRequest = new GetAccessTokenRequest()
.setAppKey(dingTalkAppKey)
.setAppSecret(dingTalkAppSecret);
try {
GetAccessTokenResponse response = client.getAccessToken(getAccessTokenRequest);
if (!StringUtils.isEmpty(response.body.accessToken)) {
redisCore.putObject(ACCESS_TOKEN_KEY, response.body.accessToken, ACCESS_TOKEN_EXPIRE_TIME);
}
return response.body.accessToken;
} catch (TeaException err) {
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
}
} catch (Exception _err) {
TeaException err = new TeaException(_err.getMessage(), _err);
if (!com.aliyun.teautil.Common.empty(err.code) && !com.aliyun.teautil.Common.empty(err.message)) {
// err 中含有 code 和 message 属性,可帮助开发定位问题
}
}
return "";
}
private OapiV2UserGetResponse getDingTalkUserInfo(String dingTalkTmpAuthCode) {
String dingTalkUrl = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.DINGTALK_URL);
String dingTalkAppKey = RedisCacheTool.getSysConfigStrVal(PlatformConstWords.DINGTALK_APP_KEY);

Loading…
Cancel
Save