WeMessagePushServiceImpl.java
5.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.base.Joiner;
import com.linkwechat.common.core.domain.entity.WeCorpAccount;
import com.linkwechat.common.enums.CommunityTaskType;
import com.linkwechat.common.enums.PushType;
import com.linkwechat.common.exception.wecom.WeComException;
import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.client.WeMessagePushClient;
import com.linkwechat.wecom.domain.WeMessagePush;
import com.linkwechat.wecom.domain.dto.WeMessagePushDto;
import com.linkwechat.wecom.domain.dto.message.TextMessageDto;
import com.linkwechat.wecom.mapper.WeMessagePushMapper;
import com.linkwechat.wecom.service.IWeCorpAccountService;
import com.linkwechat.wecom.service.IWeMessagePushService;
import com.linkwechat.wecom.strategy.MessageContext;
import com.linkwechat.wecom.strategy.SendMessageToUserGroupStrategy;
import com.linkwechat.wecom.strategy.SendMessageToUserStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.URLEncoder;
import java.util.*;
/**
* 消息发送的Service接口
*
* @author KeWen
* @date 2020-10-28
*/
@Service
public class WeMessagePushServiceImpl implements IWeMessagePushService {
@Autowired
private WeMessagePushMapper weMessagePushMapper;
@Autowired
private SendMessageToUserGroupStrategy sendMessageToUserGroupStrategy;
@Autowired
private SendMessageToUserStrategy sendMessageToUserStrategy;
@Autowired
private IWeCorpAccountService corpAccountService;
@Autowired
private WeMessagePushClient messagePushClient;
@Override
public WeMessagePush selectWeMessagePushById(Long messagePushId) {
return weMessagePushMapper.selectById(messagePushId);
}
@Override
public List<WeMessagePush> selectWeMessagePushList(WeMessagePush weMessagePush) {
return weMessagePushMapper.selectList(new QueryWrapper<>());
}
@Override
public void insertWeMessagePush(WeMessagePush weMessagePush) {
sendMessgae(weMessagePush);
}
/**
* 发送消息
*
* @param weMessagePush
*/
public void sendMessgae(WeMessagePush weMessagePush) {
if (weMessagePush.getPushType() != null && weMessagePush.getPushType().equals(PushType.SEND_TO_USER.getType())) {
new MessageContext(sendMessageToUserStrategy).sendMessage(weMessagePush);
}
if (weMessagePush.getPushType() != null
&& weMessagePush.getPushType().equals(PushType.SENT_TO_USER_GROUP.getType())) {
new MessageContext(sendMessageToUserGroupStrategy).sendMessage(weMessagePush);
}
}
@Override
public int deleteWeMessagePushByIds(Long[] messagePushIds) {
return weMessagePushMapper.deleteBatchIds(Arrays.asList(messagePushIds));
}
@Override
public int deleteWeMessagePushById(Long messagePushId) {
return weMessagePushMapper.deleteById(messagePushId);
}
@Override
public void pushMessageSelfH5(List<String> toUser,String textContent,Integer taskType) {
if(CollectionUtil.isEmpty(toUser)){
throw new WeComException("接受人不可为空");
}
WeMessagePushDto pushDto = new WeMessagePushDto();
// 获取agentId
WeCorpAccount validWeCorpAccount = corpAccountService.findValidWeCorpAccount();
String agentId = validWeCorpAccount.getAgentId();
String corpId = validWeCorpAccount.getCorpId();
if (StringUtils.isEmpty(agentId)) {
throw new WeComException("当前agentId不可用或不存在");
}
if(StringUtils.isEmpty(validWeCorpAccount.getAuthorizeUrl())){
throw new WeComException("JS SDK 身份校验url不可为空");
}
pushDto.setAgentid(Integer.valueOf(agentId));
//推送给指定员工
pushDto.setTouser(Joiner.on("|").join(toUser));
// 设置消息内容
pushDto.setMsgtype("text");
TextMessageDto text = new TextMessageDto();
String REDIRECT_URI = null;
if(taskType.equals(CommunityTaskType.SEAS.getType())){ //客户公海链接
if(StringUtils.isEmpty( validWeCorpAccount.getSeasRedirectUrl())){
throw new WeComException("客户公海H5跳转链接不可为空");
}
REDIRECT_URI=URLEncoder.encode(String.format("%s?corpId=%s&agentId=%s",
validWeCorpAccount.getSeasRedirectUrl()
, corpId, agentId));
}else{//群sop与老客标签建群
if(StringUtils.isEmpty( validWeCorpAccount.getSopTagRedirectUrl())){
throw new WeComException("群SOP与老客标签建群H5跳转链接不可为空");
}
REDIRECT_URI = URLEncoder.encode(String.format("%s?corpId=%s&agentId=%s&type=%s", validWeCorpAccount.getSopTagRedirectUrl(), corpId, agentId,taskType));
}
if(StringUtils.isNotEmpty(REDIRECT_URI)){
String context = String.format(
textContent+"<br><a href='%s?appid=%s&redirect_uri=%s&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect'>请点击此链接查看</a>",
validWeCorpAccount.getAuthorizeUrl(), corpId, REDIRECT_URI);
text.setContent(context);
pushDto.setText(text);
// 请求消息推送接口,获取结果 [消息推送 - 发送应用消息]
messagePushClient.sendMessageToUser(pushDto, agentId);
}
}
}