Skip to content
Toggle navigation
Projects
Groups
Snippets
Help
江名洋
/
linkwe-scrm
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit f0497185
authored
Nov 10, 2020
by
18356073052
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
消息发送修改
1 parent
ec1b1c83
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
44 additions
and
80 deletions
linkwe-admin/src/main/java/com/linkwechat/web/controller/wecom/WeMessagePushController.java
linkwe-wecom/src/main/java/com/linkwechat/wecom/service/impl/WeMessagePushServiceImpl.java
linkwe-admin/src/main/java/com/linkwechat/web/controller/wecom/WeMessagePushController.java
View file @
f049718
...
@@ -66,7 +66,8 @@ public class WeMessagePushController extends BaseController {
...
@@ -66,7 +66,8 @@ public class WeMessagePushController extends BaseController {
@Log
(
title
=
"消息发送的"
,
businessType
=
BusinessType
.
INSERT
)
@Log
(
title
=
"消息发送的"
,
businessType
=
BusinessType
.
INSERT
)
@PostMapping
(
value
=
"add"
)
@PostMapping
(
value
=
"add"
)
public
AjaxResult
add
(
@RequestBody
WeMessagePush
weMessagePush
)
{
public
AjaxResult
add
(
@RequestBody
WeMessagePush
weMessagePush
)
{
return
toAjax
(
weMessagePushService
.
insertWeMessagePush
(
weMessagePush
));
weMessagePushService
.
insertWeMessagePush
(
weMessagePush
);
return
AjaxResult
.
success
();
}
}
/**
/**
...
...
linkwe-wecom/src/main/java/com/linkwechat/wecom/service/impl/WeMessagePushServiceImpl.java
View file @
f049718
package
com
.
linkwechat
.
wecom
.
service
.
impl
;
package
com
.
linkwechat
.
wecom
.
service
.
impl
;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.core.collection.CollectionUtil
;
import
cn.hutool.core.date.DateUnit
;
import
cn.hutool.core.date.DateUtil
;
import
cn.hutool.json.JSONObject
;
import
cn.hutool.json.JSONObject
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
import
com.baomidou.mybatisplus.core.conditions.query.QueryWrapper
;
...
@@ -9,6 +11,7 @@ import com.google.common.collect.Maps;
...
@@ -9,6 +11,7 @@ import com.google.common.collect.Maps;
import
com.linkwechat.common.constant.WeConstans
;
import
com.linkwechat.common.constant.WeConstans
;
import
com.linkwechat.common.enums.MessageType
;
import
com.linkwechat.common.enums.MessageType
;
import
com.linkwechat.common.enums.PushType
;
import
com.linkwechat.common.enums.PushType
;
import
com.linkwechat.common.exception.wecom.WeComException
;
import
com.linkwechat.common.utils.DateUtils
;
import
com.linkwechat.common.utils.DateUtils
;
import
com.linkwechat.common.utils.ReflectUtil
;
import
com.linkwechat.common.utils.ReflectUtil
;
import
com.linkwechat.common.utils.SnowFlakeUtil
;
import
com.linkwechat.common.utils.SnowFlakeUtil
;
...
@@ -22,13 +25,13 @@ import com.linkwechat.wecom.domain.dto.WeMessagePushResultDto;
...
@@ -22,13 +25,13 @@ import com.linkwechat.wecom.domain.dto.WeMessagePushResultDto;
import
com.linkwechat.wecom.mapper.WeMessagePushMapper
;
import
com.linkwechat.wecom.mapper.WeMessagePushMapper
;
import
com.linkwechat.wecom.service.IWeGroupService
;
import
com.linkwechat.wecom.service.IWeGroupService
;
import
com.linkwechat.wecom.service.IWeMessagePushService
;
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.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.stereotype.Service
;
import
java.util.Arrays
;
import
java.util.*
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Optional
;
import
java.util.stream.Collectors
;
import
java.util.stream.Collectors
;
/**
/**
...
@@ -44,10 +47,10 @@ public class WeMessagePushServiceImpl implements IWeMessagePushService {
...
@@ -44,10 +47,10 @@ public class WeMessagePushServiceImpl implements IWeMessagePushService {
private
WeMessagePushMapper
weMessagePushMapper
;
private
WeMessagePushMapper
weMessagePushMapper
;
@Autowired
@Autowired
private
WeMessagePushClient
weMessagePushClient
;
private
SendMessageToUserGroupStrategy
sendMessageToUserGroupStrategy
;
@Autowired
@Autowired
private
IWeGroupService
weGroupService
;
private
SendMessageToUserStrategy
sendMessageToUserStrategy
;
@Override
@Override
public
WeMessagePush
selectWeMessagePushById
(
Long
messagePushId
)
{
public
WeMessagePush
selectWeMessagePushById
(
Long
messagePushId
)
{
...
@@ -60,88 +63,49 @@ public class WeMessagePushServiceImpl implements IWeMessagePushService {
...
@@ -60,88 +63,49 @@ public class WeMessagePushServiceImpl implements IWeMessagePushService {
}
}
@Override
@Override
public
int
insertWeMessagePush
(
WeMessagePush
weMessagePush
)
{
public
void
insertWeMessagePush
(
WeMessagePush
weMessagePush
)
{
//立即发送
HashMap
<
String
,
Object
>
map
=
Maps
.
newHashMap
();
if
(
null
==
weMessagePush
.
getSettingTime
())
{
JSONObject
jsonObject
=
new
JSONObject
(
weMessagePush
.
getMessageJson
());
sendMessgae
(
weMessagePush
);
Optional
<
MessageType
>
of
=
MessageType
.
of
(
weMessagePush
.
getMessageType
());
}
else
{
of
.
ifPresent
(
messageType
->
map
.
put
(
messageType
.
getMessageType
(),
jsonObject
));
//发送时间不能小于当前时间
if
(
weMessagePush
.
getPushType
()
!=
null
&&
weMessagePush
.
getPushType
().
equals
(
PushType
.
SEND_TO_USER
.
getType
()))
{
//定时发送消息(异步执行)
//保存消息体到数据库,增加一个发送状态(0 已发送 1 未发送)
//发送消息
//定义一个任务执行队列
WeMessagePushDto
weMessagePushDto
=
new
WeMessagePushDto
();
//把任务放入执行队列
weMessagePushDto
.
setTouser
(
weMessagePush
.
getToUser
());
//任务执行 sendMessgae(WeMessagePush weMessagePush) 方法
weMessagePushDto
.
setToparty
(
weMessagePush
.
getToParty
());
if
(
DateUtils
.
diffTime
(
new
Date
(),
DateUtil
.
parse
(
weMessagePush
.
getSettingTime
(),
"yyyy-MM-dd HH:mm:ss"
))
<
0
)
{
weMessagePushDto
.
setTotag
(
weMessagePush
.
getToTag
());
throw
new
WeComException
(
"发送时间不能小于当前时间"
);
weMessagePushDto
.
setMsgtype
(
weMessagePush
.
getMessageType
());
//这个先写在配置文件中
weMessagePushDto
.
setAgentid
(
1000003
);
weMessagePushDto
.
setSafe
(
0
);
weMessagePushDto
.
setEnable_id_trans
(
0
);
weMessagePushDto
.
setEnable_duplicate_check
(
0
);
weMessagePushDto
.
setDuplicate_check_interval
(
1800L
);
//动态添加微信消息体属性和属性值信息
WeMessagePushDto
target
=
(
WeMessagePushDto
)
ReflectUtil
.
getTarget
(
weMessagePushDto
,
map
);
WeMessagePushResultDto
weMessagePushResultDto
=
weMessagePushClient
.
sendMessageToUser
(
target
);
if
(
weMessagePushResultDto
.
getErrcode
().
equals
(
WeConstans
.
WE_SUCCESS_CODE
))
{
weMessagePush
.
setCreateTime
(
DateUtils
.
getNowDate
());
weMessagePush
.
setDelFlag
(
0
);
weMessagePush
.
setMessagePushId
(
SnowFlakeUtil
.
nextId
());
//存储返回结果信息
weMessagePush
.
setInvaliduser
(
weMessagePushResultDto
.
getInvaliduser
());
weMessagePush
.
setInvalidparty
(
weMessagePushResultDto
.
getInvalidparty
());
weMessagePush
.
setInvalidtag
(
weMessagePushResultDto
.
getInvalidtag
());
}
}
Timer
timer
=
new
Timer
();
timer
.
schedule
(
new
TimerTask
()
{
@Override
public
void
run
()
{
sendMessgae
(
weMessagePush
);
}
}
},
DateUtil
.
parse
(
weMessagePush
.
getSettingTime
(),
"yyyy-MM-dd HH:mm:ss"
));
if
(
weMessagePush
.
getPushType
()
!=
null
&&
weMessagePush
.
getPushType
().
equals
(
PushType
.
SENT_TO_USER_GROUP
.
getType
()))
{
//根据员工id列表查询所有的群信息
List
<
String
>
strings
=
Arrays
.
asList
(
StringUtils
.
splitByWholeSeparatorPreserveAllTokens
(
weMessagePush
.
getToUser
(),
WeConstans
.
COMMA
));
List
<
String
>
chatIds
=
Lists
.
newArrayList
();
strings
.
forEach
(
s
->
{
List
<
WeGroup
>
groups
=
weGroupService
.
list
(
new
LambdaQueryWrapper
<
WeGroup
>().
eq
(
WeGroup:
:
getOwner
,
s
));
//发送消息到群聊
if
(
CollectionUtil
.
isNotEmpty
(
groups
))
{
groups
.
forEach
(
d
->
{
WeMessagePushGroupDto
weMessagePushGroupDto
=
new
WeMessagePushGroupDto
();
weMessagePushGroupDto
.
setChatid
(
d
.
getChatId
());
weMessagePushGroupDto
.
setMsgtype
(
weMessagePush
.
getMessageType
());
weMessagePushGroupDto
.
setSafe
(
0
);
//动态添加微信消息体属性和属性值信息
WeMessagePushGroupDto
target
=
(
WeMessagePushGroupDto
)
ReflectUtil
.
getTarget
(
weMessagePushGroupDto
,
map
);
WeMessagePushResultDto
weMessagePushResultDto
=
weMessagePushClient
.
sendMessageToUserGroup
(
target
);
if
(
weMessagePushResultDto
.
getErrcode
().
equals
(
WeConstans
.
WE_SUCCESS_CODE
))
{
//保存发送的群消息
chatIds
.
add
(
d
.
getChatId
());
}
}
});
}
}
});
/**
* 发送消息
weMessagePush
.
setCreateTime
(
DateUtils
.
getNowDate
());
*
weMessagePush
.
setDelFlag
(
0
);
* @param weMessagePush
weMessagePush
.
setMessagePushId
(
SnowFlakeUtil
.
nextId
());
*/
weMessagePush
.
setChatId
(
CollectionUtil
.
isNotEmpty
(
chatIds
)
?
String
.
join
(
","
,
chatIds
)
:
null
);
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
);
}
}
return
weMessagePushMapper
.
insert
(
weMessagePush
);
}
}
@Override
@Override
public
int
deleteWeMessagePushByIds
(
Long
[]
messagePushIds
)
{
public
int
deleteWeMessagePushByIds
(
Long
[]
messagePushIds
)
{
...
@@ -153,5 +117,4 @@ public class WeMessagePushServiceImpl implements IWeMessagePushService {
...
@@ -153,5 +117,4 @@ public class WeMessagePushServiceImpl implements IWeMessagePushService {
return
weMessagePushMapper
.
deleteById
(
messagePushId
);
return
weMessagePushMapper
.
deleteById
(
messagePushId
);
}
}
}
}
Write
Preview
Markdown
is supported
Attach a file
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to post a comment