提交 a05e24d0 authored 作者: liupengfei's avatar liupengfei

钉钉通知消息

上级 72e0e579
......@@ -12,4 +12,9 @@ public class Constant {
* 开发者后台->企业自建应用->选择您创建的E应用->查看->AppSecret
*/
public static final String APP_SECRET="qkRYMu5MFh3M_croU4asvAIh1XM7QOm_-gtNjTWCwNx_y4_xoxVLYo7D9zl28GGk";
/**
* 应用agentId
*/
public static final long AGENT_ID = 616502456l;
}
......@@ -25,4 +25,9 @@ public class URLConstant {
* 获取管理员列表
*/
public static final String URL_ADMIN_LIST = "https://oapi.dingtalk.com/user/get_admin";
/**
* 发送工作通知消息
*/
public static final String URL_MESSAGE_SEND = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
}
package com.controller.customer;
import java.util.Date;
import java.util.Map;
import com.util.StringUtils;
import com.message.consumer.CustomerSaveConsumer;
import com.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
......@@ -13,9 +15,6 @@ import com.excetion.BaseException;
import com.form.customer.QueryCustomerForm;
import com.model.customer.Customer;
import com.service.customer.CustomerService;
import com.util.CallBack;
import com.util.PageInfo;
import com.util.PageParam;
import com.vo.customer.CustomerVo;
import io.swagger.annotations.Api;
......@@ -29,6 +28,9 @@ public class CustomerController {
@Autowired
private CustomerService service;
@Autowired
private SpringContextUtils springContextUtils;
@ApiOperation(value = "保存客户信息", notes = "保存客户信息")
@PostMapping("/save")
public CallBack<?> save(Customer entity){
......@@ -48,6 +50,7 @@ public class CustomerController {
entity.setAllotTime(old.getAllotTime());
}
service.updateByPrimaryKey(entity);
this.saveAppendHandler(old, entity);
return CallBack.success();
}
if (StringUtils.isNotEmpty(entity.getUserId())) {
......@@ -56,12 +59,14 @@ public class CustomerController {
}
entity.setCreateTime(new Date());
service.insert(entity);
this.saveAppendHandler(null, entity);
return CallBack.success(entity.getId());
}
@ApiOperation(value = "查询客户信息", notes = "查询全部客户信息")
@GetMapping("/page")
public CallBack<PageInfo<CustomerVo>> page(PageParam pageParam, QueryCustomerForm form) {
// TODO 访问权限验证 查看当前的用户 是否为本公司的用户
return CallBack.success(service.page(pageParam, form));
}
......@@ -74,4 +79,22 @@ public class CustomerController {
}
return CallBack.success(service.selectByPrimaryKey(id));
}
/**
* Description: 增强 保存客户信息 逻辑,不影响操作结果
* @param
* @return void
* @author lpf
* @date 2020-04-29 16:07
*/
private void saveAppendHandler(Customer old, Customer newObj) {
Map<String,CustomerSaveConsumer> map = springContextUtils.getContext().getBeansOfType(CustomerSaveConsumer.class);
if (map.isEmpty()) {
// 无增强逻辑
return;
}
map.keySet().parallelStream().map(map :: get).forEach(c -> {
// 执行增强逻辑
c.accept(old, newObj);
});
}
}
package com.message.consumer;
import com.model.customer.Customer;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import java.util.function.BiConsumer;
/**
* Title: CustomerSaveConsumer
* Description: 客户信息保存,附加逻辑
* (oldObj, newObj) -> {}
* @author lpf
* @version V1.0
* @date 2020-04-29
*/
public interface CustomerSaveConsumer extends BiConsumer<Customer, Customer>, Ordered {
}
package com.message.consumer;
import com.model.customer.Customer;
import org.springframework.stereotype.Component;
/**
* Title: CustomerSaveLog
* Description: 客户信息保存,保存更改负责人的提交记录
*
* @author lpf
* @version V1.0
* @date 2020-04-29
*/
@Component
public class CustomerSaveLog implements CustomerSaveConsumer{
@Override
public void accept(Customer old, Customer newObj) {
// TODO
System.out.println(" 客户信息保存,保存更改负责人的提交记录");
}
@Override
public int getOrder() {
return 1;
}
}
package com.message.consumer;
import com.config.Constant;
import com.config.URLConstant;
import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiMessageCorpconversationAsyncsendV2Request;
import com.dingtalk.api.response.OapiMessageCorpconversationAsyncsendV2Response;
import com.model.customer.Customer;
import com.taobao.api.ApiException;
import com.util.AccessTokenUtil;
import com.util.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
* Title: CustomerSaveSendDingTalk
* Description: 客户信息保存发送钉钉 工作通知消息
*
* @author lpf
* @version V1.0
* @date 2020-04-29
*/
@Component
public class CustomerSaveSendDingTalk implements CustomerSaveConsumer{
private static final Logger log = LoggerFactory.getLogger(CustomerSaveSendDingTalk.class);
/** 0217504529598819 池总 */
private final String ADMIN_ID_LIST = "0217504529598819";
/** 526150051021491847 lpf 测试 */
// private final String ADMIN_ID_LIST = "526150051021491847";
/**
* Description: 钉钉发送工作通知消息
* 同一个应用相同消息的内容同一个用户一天只能接收一次。
* 同一个应用给同一个用户发送消息,企业内部开发方式一天不得超过500次
* 通过设置to_all_user参数全员推送消息,一天最多3次
* 超出以上限制次数后,接口返回成功,但用户无法接收到
* 该接口是异步发送消息,接口返回成功并不表示用户一定会收到消息
* @param old
* @param newObj
* @return void
* @author lpf
* @date 2020-04-30 10:23
*/
@Override
public void accept(Customer old, Customer newObj) {
String title;
String msg = "客户:" + newObj.getCompanyName() + "; \n ";
if (old == null) {
title = "客户新增";
if (StringUtils.isNotEmpty(newObj.getUserId())) {
// 负责人id不为空, 给负责人发送工作通知消息
msg = msg + "由 **" + newObj.getUserName() + "** 负责; \n ";
this.sendMessage(newObj.getUserId(), title, msg);
}
} else {
title = "客户负责人更改";
// 修改指派时间标记,修改的用户和旧用户不一致的情况,则更新指派时间
boolean upFlag = StringUtils.isNotEmpty(newObj.getUserId())
&& !newObj.getUserId().equals(old.getUserId());
boolean upFlag1 = StringUtils.isNotEmpty(old.getUserId())
&& !old.getUserId().equals(newObj.getUserId());
if (!(upFlag || upFlag1)) {
// 指派人未发生改变的情况下不需要发送通知
log.info("指派人未发生改变,不需要发送消息通知");
return ;
}
if (StringUtils.isNotEmpty(old.getUserId())) {
// 旧负责人存在,消息拼接
msg = msg + "old负责人:" + old.getUserName() + "; \n ";
} else {
msg = msg + "公共资源; \n ";
}
if (StringUtils.isNotEmpty(newObj.getUserId())) {
// 新负责人存在,消息拼接
msg = msg + "现由 **" + newObj.getUserName() + "** 负责; \n ";
} else {
msg = msg + "无负责人,已存放到公共资源中; \n ";
}
if (StringUtils.isNotEmpty(old.getUserId())) {
// 旧对象的 负责人id不为空 给旧的负责人发送工作通知短信
this.sendMessage(old.getUserId(), title, msg);
}
if (StringUtils.isNotEmpty(newObj.getUserId())) {
// 新的负责人id不为空 给新负责人发送 工作通知短信
this.sendMessage(newObj.getUserId(), title, msg);
}
}
// 给管理员发送通知短信
this.sendMessage(ADMIN_ID_LIST, title, msg);
log.info(newObj.getId() + ";"+ title + msg + ";消息通知完毕");
}
@Override
public int getOrder() {
return 6;
}
/**
* Description: 发送markdown文本消息
* @param userIdList
* @param title 标题
* @param text 内容
* @return void
* @author lpf
* @date 2020-04-30 9:26
*/
private void sendMessage(String userIdList, String title, String text) {
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setMsgtype("markdown");
msg.setMarkdown(new OapiMessageCorpconversationAsyncsendV2Request.Markdown());
title = "## " + title;
text = title + " \n #### " + text;
log.info("钉钉markdown消息 title: " + title);
log.info("钉钉markdown消息 text: " + text);
msg.getMarkdown().setTitle(title);
msg.getMarkdown().setText(text);
this.sendMessage(userIdList, msg);
}
/**
* Description: 发送文本消息
* @param userIdList
* @param text
* @return void
* @author lpf
* @date 2020-04-30 9:12
*/
private void sendMessage(String userIdList,String text) {
OapiMessageCorpconversationAsyncsendV2Request.Msg msg = new OapiMessageCorpconversationAsyncsendV2Request.Msg();
msg.setMsgtype("text");
msg.setText(new OapiMessageCorpconversationAsyncsendV2Request.Text());
msg.getText().setContent(text);
this.sendMessage(userIdList, msg);
}
private void sendMessage(String userIdList, OapiMessageCorpconversationAsyncsendV2Request.Msg msg) {
log.info("给负责人发送短消息,消息内容:" + msg );
String accessToken = AccessTokenUtil.getToken();
DingTalkClient client = new DefaultDingTalkClient(URLConstant.URL_MESSAGE_SEND);
OapiMessageCorpconversationAsyncsendV2Request request = new OapiMessageCorpconversationAsyncsendV2Request();
request.setUseridList(userIdList);
request.setAgentId(Constant.AGENT_ID);
request.setMsg(msg);
OapiMessageCorpconversationAsyncsendV2Response response;
try {
response = client.execute(request,accessToken);
log.info("发送短消息响应:" + response.getBody());
} catch (ApiException e) {
e.printStackTrace();
log.error("发送工作通知消息失败:" + e.toString());
// TODO 消息没有被消费wo
}
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论