Commit d9e72060 by YaoYuHang

社群运营-企微消息推送异步化

1 parent 29600aa5
...@@ -76,7 +76,12 @@ public class WeCommunityGroupSopController extends BaseController { ...@@ -76,7 +76,12 @@ public class WeCommunityGroupSopController extends BaseController {
List<Long> materialIdList = groupSopDto.getMaterialIdList(); List<Long> materialIdList = groupSopDto.getMaterialIdList();
// 上传的图片的URl列表 // 上传的图片的URl列表
List<String> picList = groupSopDto.getPicList(); List<String> picList = groupSopDto.getPicList();
return toAjax(groupSopService.addGroupSop(weGroupSop, groupIdList, materialIdList, picList)); int affectedRows = groupSopService.addGroupSop(weGroupSop, groupIdList, materialIdList, picList);
if (affectedRows > 0) {
// 添加成功后进行异步消息推送
groupSopService.sendMessage(groupIdList);
}
return toAjax(affectedRows);
} }
/** /**
......
...@@ -80,7 +80,7 @@ public class WeCommunityH5Controller extends BaseController { ...@@ -80,7 +80,7 @@ public class WeCommunityH5Controller extends BaseController {
res.put("todo", todoList); res.put("todo", todoList);
doneList.addAll(tagGroupTaskService.getEmplTaskList(emplId, true)); doneList.addAll(tagGroupTaskService.getEmplTaskList(emplId, true));
doneList.addAll(sopService.getEmplTaskList(emplId, true)); doneList.addAll(sopService.getEmplTaskList(emplId, true));
res.put("done", todoList); res.put("done", doneList);
} }
SysUser user = userService.selectUserByUserName(emplId); SysUser user = userService.selectUserByUserName(emplId);
boolean isAdmin = user != null && user.isAdmin(); boolean isAdmin = user != null && user.isAdmin();
......
...@@ -5,6 +5,10 @@ import com.linkwechat.common.constant.HttpStatus; ...@@ -5,6 +5,10 @@ import com.linkwechat.common.constant.HttpStatus;
import com.linkwechat.common.core.controller.BaseController; import com.linkwechat.common.core.controller.BaseController;
import com.linkwechat.common.core.domain.AjaxResult; import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.core.page.TableDataInfo; import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.bean.BeanUtils;
import com.linkwechat.wecom.domain.WePresTagGroupTask;
import com.linkwechat.wecom.domain.dto.WePresTagGroupTaskDto; import com.linkwechat.wecom.domain.dto.WePresTagGroupTaskDto;
import com.linkwechat.wecom.domain.vo.WePresTagGroupTaskStatVo; import com.linkwechat.wecom.domain.vo.WePresTagGroupTaskStatVo;
import com.linkwechat.wecom.domain.vo.WePresTagGroupTaskVo; import com.linkwechat.wecom.domain.vo.WePresTagGroupTaskVo;
...@@ -56,7 +60,18 @@ public class WeCommunityPresTagGroupController extends BaseController { ...@@ -56,7 +60,18 @@ public class WeCommunityPresTagGroupController extends BaseController {
if (null == groupCodeService.selectWeGroupCodeById(wePresTagGroupTaskDto.getGroupCodeId())) { if (null == groupCodeService.selectWeGroupCodeById(wePresTagGroupTaskDto.getGroupCodeId())) {
return AjaxResult.error(HttpStatus.NOT_FOUND, "群活码不存在"); return AjaxResult.error(HttpStatus.NOT_FOUND, "群活码不存在");
} }
return toAjax(taskService.add(wePresTagGroupTaskDto)); // 创建新任务并保存
WePresTagGroupTask task = new WePresTagGroupTask();
BeanUtils.copyProperties(wePresTagGroupTaskDto, task);
task.setCreateBy(SecurityUtils.getUsername());
List<String> tagList = wePresTagGroupTaskDto.getTagList();
List<String> scopeList = wePresTagGroupTaskDto.getScopeList();
int affectedRows = taskService.add(task, tagList, scopeList);
if (affectedRows > 0) {
// 若保存成功,则进行异步消息推送
taskService.sendMessage(task);
}
return toAjax(affectedRows);
} }
/** /**
...@@ -65,7 +80,11 @@ public class WeCommunityPresTagGroupController extends BaseController { ...@@ -65,7 +80,11 @@ public class WeCommunityPresTagGroupController extends BaseController {
// @PreAuthorize("@ss.hasPermi('wecom:communitytagGroup:query')") // @PreAuthorize("@ss.hasPermi('wecom:communitytagGroup:query')")
@GetMapping(path = "/{id}") @GetMapping(path = "/{id}")
public AjaxResult getTask(@PathVariable("id") Long id) { public AjaxResult getTask(@PathVariable("id") Long id) {
return AjaxResult.success(taskService.getTaskById(id)); WePresTagGroupTaskVo taskVo = taskService.getTaskById(id);
if (StringUtils.isNull(taskVo)) {
return AjaxResult.error(HttpStatus.NOT_FOUND, "群活码不存在");
}
return AjaxResult.success();
} }
/** /**
......
...@@ -224,7 +224,7 @@ wecome: ...@@ -224,7 +224,7 @@ wecome:
# JS SDK 身份校验url # JS SDK 身份校验url
authorizeUrl: https://open.weixin.qq.com/connect/oauth2/authorize authorizeUrl: https://open.weixin.qq.com/connect/oauth2/authorize
# JS SDK身份校验成功后的redirect url # JS SDK身份校验成功后的redirect url
authorizeRedirectUrl: http://linkwechat.cn/mobile/#/task authorizeRedirectUrl: http://www.linkwechat.cn/mobile/#/task
#腾讯云对象存储 #腾讯云对象存储
cos: cos:
secretId: AKIDQRCizIeUGl13vakkDQMH0VkjhPr1rBBv secretId: AKIDQRCizIeUGl13vakkDQMH0VkjhPr1rBBv
......
...@@ -19,7 +19,7 @@ public class WePresTagGroupTask extends BaseEntity { ...@@ -19,7 +19,7 @@ public class WePresTagGroupTask extends BaseEntity {
*主键ID *主键ID
*/ */
@TableId @TableId
private Long taskId = SnowFlakeUtil.nextId();; private Long taskId = SnowFlakeUtil.nextId();
/** /**
* 任务名称 * 任务名称
......
package com.linkwechat.wecom.service; package com.linkwechat.wecom.service;
import com.linkwechat.wecom.domain.WeGroup;
import com.linkwechat.wecom.domain.WeGroupSop; import com.linkwechat.wecom.domain.WeGroupSop;
import com.linkwechat.wecom.domain.vo.WeCommunityTaskEmplVo; import com.linkwechat.wecom.domain.vo.WeCommunityTaskEmplVo;
import com.linkwechat.wecom.domain.vo.WeGroupSopVo; import com.linkwechat.wecom.domain.vo.WeGroupSopVo;
...@@ -95,4 +96,10 @@ public interface IWeGroupSopService { ...@@ -95,4 +96,10 @@ public interface IWeGroupSopService {
* @return 结果 * @return 结果
*/ */
List<WeCommunityTaskEmplVo> getScopeListByRuleId(Long ruleId); List<WeCommunityTaskEmplVo> getScopeListByRuleId(Long ruleId);
/**
* 向指定的群聊进行sop企微消息推送
* @param groupIdList 群聊id列表
*/
void sendMessage(List<String> groupIdList);
} }
package com.linkwechat.wecom.service; package com.linkwechat.wecom.service;
import com.linkwechat.wecom.domain.WePresTagGroupTask;
import com.linkwechat.wecom.domain.WeTag; import com.linkwechat.wecom.domain.WeTag;
import com.linkwechat.wecom.domain.dto.WePresTagGroupTaskDto; import com.linkwechat.wecom.domain.dto.WePresTagGroupTaskDto;
import com.linkwechat.wecom.domain.vo.WeCommunityTaskEmplVo; import com.linkwechat.wecom.domain.vo.WeCommunityTaskEmplVo;
...@@ -14,12 +15,13 @@ import java.util.List; ...@@ -14,12 +15,13 @@ import java.util.List;
public interface IWePresTagGroupTaskService { public interface IWePresTagGroupTaskService {
/** /**
* 新增建群任务 * 添加新标签建群任务
* * @param task 建群任务本体信息
* @param taskDto 建群所需数据 * @param tagIdList 标签列表
* @return 数据库新增行数 * @param emplIdList 员工列表
* @return 结果
*/ */
int add(WePresTagGroupTaskDto taskDto); int add(WePresTagGroupTask task, List<String> tagIdList, List<String> emplIdList);
/** /**
* 根据条件查询任务列表 * 根据条件查询任务列表
...@@ -108,4 +110,10 @@ public interface IWePresTagGroupTaskService { ...@@ -108,4 +110,10 @@ public interface IWePresTagGroupTaskService {
* @return 结果 * @return 结果
*/ */
int updateEmplTaskStatus(Long taskId, String emplId); int updateEmplTaskStatus(Long taskId, String emplId);
/**
* 根据标签建群任务信息发送消息
* @param task 标签建群任务
*/
void sendMessage(WePresTagGroupTask task);
} }
...@@ -18,6 +18,7 @@ import com.linkwechat.wecom.service.IWeGroupSopService; ...@@ -18,6 +18,7 @@ import com.linkwechat.wecom.service.IWeGroupSopService;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -55,15 +56,12 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup ...@@ -55,15 +56,12 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup
@Autowired @Autowired
private WeMessagePushClient messagePushClient; private WeMessagePushClient messagePushClient;
@Value("${wecome.authorizeUrl") @Value("${wecome.authorizeUrl}")
private String authorizeUrl; private String authorizeUrl;
@Value("${wecome.authorizeRedirectUrl") @Value("${wecome.authorizeRedirectUrl}")
private String authorizeRedirectUrl; private String authorizeRedirectUrl;
@Value("${wecome.callBack.appIdOrCorpId}")
private String corpId;
/** /**
* 通过规则id获取sop规则 * 通过规则id获取sop规则
* *
...@@ -73,7 +71,9 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup ...@@ -73,7 +71,9 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup
@Override @Override
public WeGroupSopVo getGroupSopById(Long ruleId) { public WeGroupSopVo getGroupSopById(Long ruleId) {
WeGroupSopVo groupSopVo = groupSopMapper.getGroupSopById(ruleId); WeGroupSopVo groupSopVo = groupSopMapper.getGroupSopById(ruleId);
this.setChatAndMaterialAndPicList(groupSopVo); if (StringUtils.isNotNull(groupSopVo)) {
setChatAndMaterialAndPicList(groupSopVo);
}
return groupSopVo; return groupSopVo;
} }
...@@ -114,7 +114,6 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup ...@@ -114,7 +114,6 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup
if (StringUtils.isNotEmpty(sopPicList)) { if (StringUtils.isNotEmpty(sopPicList)) {
sopPicMapper.batchSopPic(sopPicList); sopPicMapper.batchSopPic(sopPicList);
} }
this.sendMessage(ruleId);
return 1; return 1;
} }
return 0; return 0;
...@@ -319,17 +318,23 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup ...@@ -319,17 +318,23 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup
/** /**
* 消息推送(企微API 消息推送 - 发送应用消息) * 消息推送(企微API 消息推送 - 发送应用消息)
*/ */
private void sendMessage(Long ruleId) { @Override
@Async
public void sendMessage(List<String> groupIdList) {
// 构造请求参数 // 构造请求参数
WeMessagePushDto pushDto = new WeMessagePushDto(); WeMessagePushDto pushDto = new WeMessagePushDto();
// 根据群聊获取员工(群主)列表 // 查询群聊列表,获取群主列表
List<WeGroup> groupList = this.getGroupListByRuleId(ruleId); QueryWrapper<WeGroup> queryWrapper = new QueryWrapper<>();
queryWrapper.in("chat_id", groupIdList);
List<WeGroup> groupList = groupMapper.selectList(queryWrapper);
String toUser = groupList.stream().map(WeGroup::getOwner).collect(Collectors.joining("|")); String toUser = groupList.stream().map(WeGroup::getOwner).collect(Collectors.joining("|"));
pushDto.setTouser(toUser); pushDto.setTouser(toUser);
// 获取agentId // 获取agentId
WeCorpAccount validWeCorpAccount = corpAccountService.findValidWeCorpAccount(); WeCorpAccount validWeCorpAccount = corpAccountService.findValidWeCorpAccount();
String agentId = validWeCorpAccount.getAgentId(); String agentId = validWeCorpAccount.getAgentId();
String corpId = validWeCorpAccount.getCorpId();
if (StringUtils.isEmpty(agentId)) { if (StringUtils.isEmpty(agentId)) {
throw new WeComException("当前agentId不可用或不存在"); throw new WeComException("当前agentId不可用或不存在");
} }
...@@ -341,7 +346,7 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup ...@@ -341,7 +346,7 @@ public class WeGroupSopServiceImpl extends ServiceImpl<WeGroupSopMapper, WeGroup
TextMessageDto text = new TextMessageDto(); TextMessageDto text = new TextMessageDto();
String REDIRECT_URI = URLEncoder.encode(String.format("%s?corpId=%s&agentId=%s&type=%s", authorizeRedirectUrl, corpId, agentId, CommunityTaskType.SOP.getType())); String REDIRECT_URI = URLEncoder.encode(String.format("%s?corpId=%s&agentId=%s&type=%s", authorizeRedirectUrl, corpId, agentId, CommunityTaskType.SOP.getType()));
String context = String.format( String context = String.format(
"你有一个新任务,<a href='%s?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'>请点击此链接查看</a>", "你有新的SOP待发送,<a href='%s?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'>请点击此链接查看</a>",
authorizeUrl, corpId, REDIRECT_URI); authorizeUrl, corpId, REDIRECT_URI);
text.setContent(context); text.setContent(context);
pushDto.setText(text); pushDto.setText(text);
......
...@@ -27,6 +27,7 @@ import com.linkwechat.wecom.service.*; ...@@ -27,6 +27,7 @@ import com.linkwechat.wecom.service.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
...@@ -76,28 +77,18 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas ...@@ -76,28 +77,18 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas
@Value("${wecome.authorizeRedirectUrl}") @Value("${wecome.authorizeRedirectUrl}")
private String authorizeRedirectUrl; private String authorizeRedirectUrl;
@Value("${wecome.callBack.appIdOrCorpId}")
private String corpId;
/** /**
* 新增建群任务 * 添加新标签建群任务
* * @param task 建群任务本体信息
* @param taskDto 建群所需数据 * @param tagIdList 标签列表
* @return 数据库新增行数 * @param emplIdList 员工列表
* @return 结果
*/ */
@Override @Override
@Transactional @Transactional
public int add(WePresTagGroupTaskDto taskDto) { public int add(WePresTagGroupTask task, List<String> tagIdList, List<String> emplIdList) {
// 创建WeCommunityOldGroup对象进行存储
WePresTagGroupTask task = new WePresTagGroupTask();
BeanUtils.copyProperties(taskDto, task);
task.setCreateBy(SecurityUtils.getUsername());
int res = 0;
if (taskMapper.insertTask(task) > 0) { if (taskMapper.insertTask(task) > 0) {
// 保存标签对象 // 保存标签对象
List<String> tagIdList = taskDto.getTagList();
if (CollectionUtil.isNotEmpty(tagIdList)) { if (CollectionUtil.isNotEmpty(tagIdList)) {
List<WePresTagGroupTaskTag> taskTagList = tagIdList List<WePresTagGroupTaskTag> taskTagList = tagIdList
.stream() .stream()
...@@ -107,7 +98,6 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas ...@@ -107,7 +98,6 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas
} }
// 保存员工信息 // 保存员工信息
List<String> emplIdList = taskDto.getScopeList();
if (CollectionUtil.isNotEmpty(emplIdList)) { if (CollectionUtil.isNotEmpty(emplIdList)) {
List<WePresTagGroupTaskScope> wePresTagGroupTaskScopeList = emplIdList List<WePresTagGroupTaskScope> wePresTagGroupTaskScopeList = emplIdList
.stream() .stream()
...@@ -115,13 +105,9 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas ...@@ -115,13 +105,9 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas
.collect(Collectors.toList()); .collect(Collectors.toList());
taskScopeMapper.batchBindsTaskScopes(wePresTagGroupTaskScopeList); taskScopeMapper.batchBindsTaskScopes(wePresTagGroupTaskScopeList);
} }
return 1;
this.sendMessage(task);
res = 1;
} }
return 0;
return res;
} }
/** /**
...@@ -153,7 +139,9 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas ...@@ -153,7 +139,9 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas
@Override @Override
public WePresTagGroupTaskVo getTaskById(Long taskId) { public WePresTagGroupTaskVo getTaskById(Long taskId) {
WePresTagGroupTaskVo taskVo = taskMapper.selectTaskById(taskId); WePresTagGroupTaskVo taskVo = taskMapper.selectTaskById(taskId);
this.setGroupCodeAndScopeAndTag(taskVo); if (StringUtils.isNotNull(taskVo)) {
setGroupCodeAndScopeAndTag(taskVo);
}
return taskVo; return taskVo;
} }
...@@ -348,7 +336,9 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas ...@@ -348,7 +336,9 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas
* *
* @param task 建群任务 * @param task 建群任务
*/ */
private void sendMessage(WePresTagGroupTask task) { @Override
@Async
public void sendMessage(WePresTagGroupTask task) {
try { try {
Integer sendType = task.getSendType(); Integer sendType = task.getSendType();
...@@ -423,6 +413,7 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas ...@@ -423,6 +413,7 @@ public class WePresTagGroupTaskServiceImpl extends ServiceImpl<WePresTagGroupTas
// 获取agentId // 获取agentId
WeCorpAccount validWeCorpAccount = corpAccountService.findValidWeCorpAccount(); WeCorpAccount validWeCorpAccount = corpAccountService.findValidWeCorpAccount();
String agentId = validWeCorpAccount.getAgentId(); String agentId = validWeCorpAccount.getAgentId();
String corpId = validWeCorpAccount.getCorpId();
if (StringUtils.isEmpty(agentId)) { if (StringUtils.isEmpty(agentId)) {
throw new WeComException("当前agentId不可用或不存在"); throw new WeComException("当前agentId不可用或不存在");
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!