Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
O
on-site-service
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
external
on-site-service
Commits
7e72c62b
Commit
7e72c62b
authored
Jul 08, 2022
by
shangtx
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 公众号消息
parent
3765b47f
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
113 additions
and
1 deletion
+113
-1
SysConstants.java
...ava/com/onsiteservice/constant/constant/SysConstants.java
+2
-1
pom.xml
mini-app/pom.xml
+6
-0
OrderListener.java
...onsiteservice/miniapp/service/listener/OrderListener.java
+27
-0
WeixinMessageService.java
...eservice/miniapp/service/weixin/WeixinMessageService.java
+78
-0
No files found.
constant/src/main/java/com/onsiteservice/constant/constant/SysConstants.java
View file @
7e72c62b
...
...
@@ -20,7 +20,8 @@ public class SysConstants {
public
class
Queue
{
// 管理端订单
public
static
final
String
ADMIN_ORDER
=
"admin-order"
;
public
static
final
String
SMS
=
"sms"
;
// 用户订单消息
public
static
final
String
USER_ORDER
=
"user-order"
;
}
}
mini-app/pom.xml
View file @
7e72c62b
...
...
@@ -25,6 +25,12 @@
<artifactId>
wx-java-miniapp-spring-boot-starter
</artifactId>
</dependency>
<!--微信公众号-->
<dependency>
<groupId>
com.github.binarywang
</groupId>
<artifactId>
weixin-java-mp
</artifactId>
</dependency>
</dependencies>
<build>
...
...
mini-app/src/main/java/com/onsiteservice/miniapp/service/listener/OrderListener.java
0 → 100644
View file @
7e72c62b
package
com
.
onsiteservice
.
miniapp
.
service
.
listener
;
import
com.onsiteservice.constant.constant.SysConstants
;
import
com.onsiteservice.miniapp.service.weixin.WeixinMessageService
;
import
org.springframework.amqp.rabbit.annotation.Queue
;
import
org.springframework.amqp.rabbit.annotation.RabbitHandler
;
import
org.springframework.amqp.rabbit.annotation.RabbitListener
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.socket.TextMessage
;
import
javax.annotation.Resource
;
@Component
@RabbitListener
(
queuesToDeclare
=
@Queue
(
SysConstants
.
Queue
.
USER_ORDER
))
public
class
OrderListener
{
@Resource
private
WeixinMessageService
weixinMessageService
;
/* TODO 给用户发送订单的新情况 */
@RabbitHandler
public
void
notice
()
{
weixinMessageService
.
sendMsg
(
null
);
}
}
mini-app/src/main/java/com/onsiteservice/miniapp/service/weixin/WeixinMessageService.java
0 → 100644
View file @
7e72c62b
package
com
.
onsiteservice
.
miniapp
.
service
.
weixin
;
import
com.alibaba.fastjson.JSON
;
import
com.onsiteservice.util.DateUtils
;
import
lombok.extern.slf4j.Slf4j
;
import
me.chanjar.weixin.mp.api.WxMpService
;
import
me.chanjar.weixin.mp.bean.template.WxMpTemplateData
;
import
me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Service
;
import
org.springframework.transaction.annotation.Transactional
;
import
java.util.Date
;
import
java.util.LinkedList
;
import
java.util.List
;
/**
* 微信公众号消息服务
*/
@Service
@Slf4j
@Transactional
(
rollbackFor
=
Exception
.
class
)
public
class
WeixinMessageService
{
@Autowired
private
WxMpService
wxService
;
/**
* 发送微信模板消息 批量发送处理
*/
public
void
sendMsg
(
List
<
WxMpTemplateMessage
>
wxMpTemplateMessages
)
{
for
(
WxMpTemplateMessage
msg
:
wxMpTemplateMessages
)
{
try
{
wxService
.
getTemplateMsgService
().
sendTemplateMsg
(
msg
);
}
catch
(
Exception
e
)
{
e
.
printStackTrace
();
String
errorMsg
=
e
.
getMessage
();
log
.
error
(
"微信模板推送异常:"
+
errorMsg
);
}
}
}
public
WxMpTemplateMessage
wxMpTemplateMessages
()
{
List
<
WxMpTemplateMessage
>
msgList
=
new
LinkedList
<>();
// 示例
WxMpTemplateData
keyword1
=
new
WxMpTemplateData
();
keyword1
.
setName
(
"keyword1"
);
// 报警来源
keyword1
.
setValue
(
""
);
keyword1
.
setColor
(
"#ff3334"
);
WxMpTemplateData
keyword2
=
new
WxMpTemplateData
();
keyword2
.
setName
(
"keyword2"
);
// 发生时间
keyword2
.
setValue
(
DateUtils
.
formatDate
(
new
Date
(),
"yyyy-MM-dd HH:mm:ss"
));
WxMpTemplateData
keyword3
=
new
WxMpTemplateData
();
keyword3
.
setName
(
"keyword3"
);
// 报警内容
keyword3
.
setValue
(
"dsadsd"
);
keyword3
.
setColor
(
"#ff3334"
);
WxMpTemplateData
remark
=
new
WxMpTemplateData
();
remark
.
setName
(
"remark"
);
remark
.
setValue
(
"请及时处理"
);
List
<
WxMpTemplateData
>
dataList
=
List
.
of
(
keyword1
,
keyword2
,
keyword3
,
remark
);
String
alarmNoticeTemplateId
=
""
;
// 消息模板id
String
openId
=
""
;
// 微信用户的公众号openId
String
appId
=
""
;
// appid
WxMpTemplateMessage
.
MiniProgram
miniProgram
=
new
WxMpTemplateMessage
.
MiniProgram
();
miniProgram
.
setAppid
(
appId
);
WxMpTemplateMessage
wxMpTemplateMessage
=
WxMpTemplateMessage
.
builder
()
.
templateId
(
alarmNoticeTemplateId
).
toUser
(
openId
).
miniProgram
(
miniProgram
)
.
data
(
dataList
).
build
();
// TODO
return
null
;
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
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 comment