Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gxpt_ht
Overview
Overview
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
pseer
gxpt_ht
Commits
03543ae4
Commit
03543ae4
authored
Jun 03, 2024
by
吕明尚
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
微信公众号的模版消息推送
parent
9c2288c3
Hide whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
130 additions
and
30 deletions
+130
-30
WeChatConstants.java
.../src/main/java/share/common/constant/WeChatConstants.java
+3
-1
WeChatController.java
...in/java/share/web/controller/system/WeChatController.java
+24
-0
RedisTask.java
share-quartz/src/main/java/share/quartz/task/RedisTask.java
+19
-0
Miniprogram.java
...tem/src/main/java/share/system/domain/vo/Miniprogram.java
+8
-8
OfficialAccountTemplate.java
.../java/share/system/domain/vo/OfficialAccountTemplate.java
+1
-7
WachatUser.java
...stem/src/main/java/share/system/domain/vo/WachatUser.java
+9
-0
WechatNewService.java
.../src/main/java/share/system/service/WechatNewService.java
+3
-1
SOrderServiceImpl.java
...ain/java/share/system/service/impl/SOrderServiceImpl.java
+1
-1
WechatNewServiceImpl.java
.../java/share/system/service/impl/WechatNewServiceImpl.java
+62
-12
No files found.
share-common/src/main/java/share/common/constant/WeChatConstants.java
View file @
03543ae4
...
@@ -239,7 +239,9 @@ public class WeChatConstants {
...
@@ -239,7 +239,9 @@ public class WeChatConstants {
/** 公众号发送模板消息的url */
/** 公众号发送模板消息的url */
public
static
final
String
WECHAT_PUBLIC_SEND_TEMPLATE_URL
=
"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}"
;
public
static
final
String
WECHAT_PUBLIC_SEND_TEMPLATE_URL
=
"https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}"
;
//微信公众号的获取用户列表
//微信公众号的获取用户列表
public
static
final
String
WECHAT_PUBLIC_USER_GET_URL
=
"https://api.weixin.qq.com/cgi-bin/user/get?access_token={}}&next_openid={}"
;
public
static
final
String
WECHAT_PUBLIC_USER_GET_URL
=
"https://api.weixin.qq.com/cgi-bin/user/get?access_token={}&next_openid={}"
;
//微信公众账号的用户信息
public
static
final
String
WECHAT_PUBLIC_USER_INFO_URL
=
"https://api.weixin.qq.com/cgi-bin/user/info/batchget?access_token={}"
;
...
...
share-front/src/main/java/share/web/controller/system/WeChatController.java
View file @
03543ae4
package
share
.
web
.
controller
.
system
;
package
share
.
web
.
controller
.
system
;
import
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.Api
;
import
io.swagger.annotations.ApiImplicitParam
;
import
io.swagger.annotations.ApiImplicitParam
;
import
io.swagger.annotations.ApiOperation
;
import
io.swagger.annotations.ApiOperation
;
...
@@ -10,15 +11,19 @@ import org.springframework.web.bind.annotation.*;
...
@@ -10,15 +11,19 @@ import org.springframework.web.bind.annotation.*;
import
share.common.core.domain.R
;
import
share.common.core.domain.R
;
import
share.common.core.redis.RedisUtil
;
import
share.common.core.redis.RedisUtil
;
import
share.common.utils.JsonConvertUtil
;
import
share.common.utils.JsonConvertUtil
;
import
share.system.domain.SConsumerToken
;
import
share.system.domain.TemplateMessage
;
import
share.system.domain.TemplateMessage
;
import
share.system.request.RegisterThirdSConsumerRequest
;
import
share.system.request.RegisterThirdSConsumerRequest
;
import
share.system.request.WxBindingPhoneRequest
;
import
share.system.request.WxBindingPhoneRequest
;
import
share.system.request.WxRegisterPhoneRequest
;
import
share.system.request.WxRegisterPhoneRequest
;
import
share.system.response.LoginResponse
;
import
share.system.response.LoginResponse
;
import
share.system.service.SConsumerCenterService
;
import
share.system.service.SConsumerCenterService
;
import
share.system.service.SConsumerTokenService
;
import
share.system.service.WechatNewService
;
import
java.util.ArrayList
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.List
;
import
java.util.Map
;
/**
/**
...
@@ -39,6 +44,12 @@ public class WeChatController {
...
@@ -39,6 +44,12 @@ public class WeChatController {
@Autowired
@Autowired
private
RedisUtil
redisUtil
;
private
RedisUtil
redisUtil
;
@Autowired
private
WechatNewService
wechatNewService
;
@Autowired
private
SConsumerTokenService
sConsumerTokenService
;
/**
/**
* 微信登录小程序授权登录
* 微信登录小程序授权登录
...
@@ -104,6 +115,19 @@ public class WeChatController {
...
@@ -104,6 +115,19 @@ public class WeChatController {
return
R
.
ok
(
miniTempList
);
return
R
.
ok
(
miniTempList
);
}
}
@GetMapping
(
"/test"
)
public
void
getWechatUSERList
()
{
List
<
String
>
list
=
new
ArrayList
<>();
List
<
String
>
wechatUSERList
=
wechatNewService
.
getWechatUSERList
(
list
,
""
);
Map
<
String
,
String
>
stringStringMap
=
wechatNewService
.
WechatUsetListInfo
(
wechatUSERList
);
System
.
out
.
println
(
stringStringMap
);
List
<
SConsumerToken
>
sConsumerTokenList
=
sConsumerTokenService
.
list
(
new
LambdaQueryWrapper
<
SConsumerToken
>().
isNotNull
(
SConsumerToken:
:
getUnionId
).
isNull
(
SConsumerToken:
:
getOpenId
));
sConsumerTokenList
.
forEach
(
item
->
{
item
.
setOpenId
(
stringStringMap
.
get
(
item
.
getUnionId
()));
sConsumerTokenService
.
updateById
(
item
);
});
System
.
out
.
println
(
sConsumerTokenList
);
}
}
}
...
...
share-quartz/src/main/java/share/quartz/task/RedisTask.java
View file @
03543ae4
...
@@ -87,6 +87,9 @@ public class RedisTask {
...
@@ -87,6 +87,9 @@ public class RedisTask {
@Autowired
@Autowired
private
WechatNewService
wechatNewService1
;
private
WechatNewService
wechatNewService1
;
@Autowired
private
SConsumerTokenService
sConsumerTokenService
;
//15分钟的常量
//15分钟的常量
final
long
FIFTEEN_MINUTES
=
60
*
15
;
final
long
FIFTEEN_MINUTES
=
60
*
15
;
...
@@ -495,4 +498,20 @@ public class RedisTask {
...
@@ -495,4 +498,20 @@ public class RedisTask {
if
(!
CollectionUtils
.
isEmpty
(
roomLabels
))
roomLabelService
.
saveBatch
(
roomLabels
);
if
(!
CollectionUtils
.
isEmpty
(
roomLabels
))
roomLabelService
.
saveBatch
(
roomLabels
);
}
}
//自动更新用户unionid
public
void
AutoUpdateOpenid
()
{
List
<
SConsumerToken
>
sConsumerTokenList
=
sConsumerTokenService
.
list
(
new
LambdaQueryWrapper
<
SConsumerToken
>().
isNotNull
(
SConsumerToken:
:
getUnionId
).
isNull
(
SConsumerToken:
:
getOpenId
));
if
(
CollectionUtils
.
isEmpty
(
sConsumerTokenList
))
{
return
;
}
List
<
String
>
list
=
new
ArrayList
<>();
List
<
String
>
wechatUSERList
=
wechatNewService
.
getWechatUSERList
(
list
,
""
);
Map
<
String
,
String
>
stringStringMap
=
wechatNewService
.
WechatUsetListInfo
(
wechatUSERList
);
List
<
SConsumerToken
>
updateList
=
new
ArrayList
<>();
sConsumerTokenList
.
forEach
(
item
->
{
item
.
setOpenId
(
stringStringMap
.
get
(
item
.
getUnionId
()));
updateList
.
add
(
item
);
});
if
(!
CollectionUtils
.
isEmpty
(
updateList
))
sConsumerTokenService
.
updateBatchById
(
updateList
);
}
}
}
share-system/src/main/java/share/system/domain/vo/
UserOpenIdVo
.java
→
share-system/src/main/java/share/system/domain/vo/
Miniprogram
.java
View file @
03543ae4
package
share
.
system
.
domain
.
vo
;
package
share
.
system
.
domain
.
vo
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModel
;
import
io.swagger.annotations.ApiModelProperty
;
import
lombok.Data
;
import
lombok.Data
;
import
lombok.EqualsAndHashCode
;
import
lombok.EqualsAndHashCode
;
import
lombok.experimental.Accessors
;
import
lombok.experimental.Accessors
;
import
java.util.List
;
@Data
@Data
@EqualsAndHashCode
(
callSuper
=
false
)
@EqualsAndHashCode
(
callSuper
=
false
)
@Accessors
(
chain
=
true
)
@Accessors
(
chain
=
true
)
@ApiModel
(
value
=
"WeChatAccessTokenVo"
,
description
=
"公众号用户列表"
)
@ApiModel
(
value
=
"miniprogram对象"
,
description
=
"微信模板发送类"
)
public
class
UserOpenIdVo
{
public
class
Miniprogram
{
private
Integer
total
;
@ApiModelProperty
(
value
=
"所需跳转到的小程序appid"
,
required
=
true
)
private
Integer
count
;
private
String
appid
;
private
List
<
String
>
data
;
private
String
next_openid
;
@ApiModelProperty
(
value
=
"所需跳转到小程序的具体页面路径"
)
private
String
pagepath
;
}
}
share-system/src/main/java/share/system/domain/vo/OfficialAccountTemplate.java
View file @
03543ae4
...
@@ -20,13 +20,7 @@ public class OfficialAccountTemplate {
...
@@ -20,13 +20,7 @@ public class OfficialAccountTemplate {
private
String
template_id
;
private
String
template_id
;
@ApiModelProperty
(
value
=
"跳小程序所需数据,不需跳小程序可不用传该数据"
)
@ApiModelProperty
(
value
=
"跳小程序所需数据,不需跳小程序可不用传该数据"
)
private
String
miniprogram
;
private
Miniprogram
miniprogram
;
@ApiModelProperty
(
value
=
"所需跳转到的小程序appid"
,
required
=
true
)
private
String
appid
;
@ApiModelProperty
(
value
=
"所需跳转到小程序的具体页面路径"
)
private
String
pagepath
;
@ApiModelProperty
(
value
=
"发送内容"
)
@ApiModelProperty
(
value
=
"发送内容"
)
private
HashMap
<
String
,
SendTemplateMessageItemVo
>
data
;
private
HashMap
<
String
,
SendTemplateMessageItemVo
>
data
;
...
...
share-system/src/main/java/share/system/domain/vo/WachatUser.java
0 → 100644
View file @
03543ae4
package
share
.
system
.
domain
.
vo
;
import
lombok.Data
;
@Data
public
class
WachatUser
{
private
String
openid
;
private
String
lang
=
"zh_CN"
;
}
share-system/src/main/java/share/system/service/WechatNewService.java
View file @
03543ae4
...
@@ -91,7 +91,9 @@ public interface WechatNewService {
...
@@ -91,7 +91,9 @@ public interface WechatNewService {
Boolean
sendPublicTemplateMessage
(
SOrder
byOrderNo
,
MessageReminderEnum
messageReminderEnum
,
Long
consumerId
);
Boolean
sendPublicTemplateMessage
(
SOrder
byOrderNo
,
MessageReminderEnum
messageReminderEnum
,
Long
consumerId
);
UserOpenIdVo
getWechatUSERList
();
List
<
String
>
getWechatUSERList
(
List
<
String
>
openIds
,
String
next_openid
);
Map
<
String
,
String
>
WechatUsetListInfo
(
List
<
String
>
openIds
);
/**
/**
* 微信小程序发送订阅消息
* 微信小程序发送订阅消息
* @param templateMessage 消息对象
* @param templateMessage 消息对象
...
...
share-system/src/main/java/share/system/service/impl/SOrderServiceImpl.java
View file @
03543ae4
...
@@ -952,7 +952,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
...
@@ -952,7 +952,7 @@ public class SOrderServiceImpl extends ServiceImpl<SOrderMapper, SOrder> impleme
consumers
.
forEach
(
o
->
{
consumers
.
forEach
(
o
->
{
phones
.
add
(
o
.
getPhone
());
phones
.
add
(
o
.
getPhone
());
//公众号发送订单预订成功通知
//公众号发送订单预订成功通知
// wechatNewService.sendPublicTemplateMessage(sOrder,MessageReminderEnum.ORDER_RESERVE,
o.getId());
wechatNewService
.
sendPublicTemplateMessage
(
sOrder
,
MessageReminderEnum
.
ORDER_RESERVE
,
o
.
getId
());
});
});
smsService
.
sendSmsOrderPredetermine
(
phones
,
sOrder
,
sStore
,
room
);
smsService
.
sendSmsOrderPredetermine
(
phones
,
sOrder
,
sStore
,
room
);
}
}
...
...
share-system/src/main/java/share/system/service/impl/WechatNewServiceImpl.java
View file @
03543ae4
...
@@ -35,10 +35,7 @@ import share.system.service.*;
...
@@ -35,10 +35,7 @@ import share.system.service.*;
import
java.io.UnsupportedEncodingException
;
import
java.io.UnsupportedEncodingException
;
import
java.net.URLDecoder
;
import
java.net.URLDecoder
;
import
java.util.Date
;
import
java.util.*
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.concurrent.TimeUnit
;
import
java.util.concurrent.TimeUnit
;
/**
/**
...
@@ -521,7 +518,9 @@ public class WechatNewServiceImpl implements WechatNewService {
...
@@ -521,7 +518,9 @@ public class WechatNewServiceImpl implements WechatNewService {
if
(
data
.
containsKey
(
"errcode"
)
&&
!
data
.
getString
(
"errcode"
).
equals
(
"0"
)
&&
!
data
.
getString
(
"errcode"
).
equals
(
"40014"
))
{
if
(
data
.
containsKey
(
"errcode"
)
&&
!
data
.
getString
(
"errcode"
).
equals
(
"0"
)
&&
!
data
.
getString
(
"errcode"
).
equals
(
"40014"
))
{
if
(
data
.
containsKey
(
"errmsg"
))
{
if
(
data
.
containsKey
(
"errmsg"
))
{
wxExceptionDispose
(
data
,
"微信公众号发送模板消息异常"
);
wxExceptionDispose
(
data
,
"微信公众号发送模板消息异常"
);
throw
new
BaseException
(
"微信接口调用失败:"
+
data
.
getString
(
"errcode"
)
+
data
.
getString
(
"errmsg"
));
// throw new BaseException("微信接口调用失败:" + data.getString("errcode") + data.getString("errmsg"));
logger
.
error
(
"微信接口调用失败:"
+
data
.
getString
(
"errcode"
)
+
data
.
getString
(
"errmsg"
));
}
}
}
else
if
(
data
.
getString
(
"errcode"
).
equals
(
"40014"
))
{
}
else
if
(
data
.
getString
(
"errcode"
).
equals
(
"40014"
))
{
return
sendPublicTemplateMessage
(
templateMessage
);
return
sendPublicTemplateMessage
(
templateMessage
);
...
@@ -579,9 +578,10 @@ public class WechatNewServiceImpl implements WechatNewService {
...
@@ -579,9 +578,10 @@ public class WechatNewServiceImpl implements WechatNewService {
@Override
@Override
public
Boolean
sendPublicTemplateMessage
(
SOrder
byOrderNo
,
MessageReminderEnum
messageReminderEnum
,
Long
consumerId
)
{
public
Boolean
sendPublicTemplateMessage
(
SOrder
byOrderNo
,
MessageReminderEnum
messageReminderEnum
,
Long
consumerId
)
{
OfficialAccountTemplate
officialAccountTemplate
=
new
OfficialAccountTemplate
();
OfficialAccountTemplate
officialAccountTemplate
=
new
OfficialAccountTemplate
();
officialAccountTemplate
.
setAppid
(
weChatConfig
.
getAppId
());
Miniprogram
miniprogram
=
new
Miniprogram
();
String
pagepath
=
"/pages/orderResult/orderResult?orderNo="
+
byOrderNo
.
getOrderNo
();
miniprogram
.
setAppid
(
weChatConfig
.
getAppId
());
officialAccountTemplate
.
setPagepath
(
pagepath
);
miniprogram
.
setPagepath
(
"/pages/orderResult/orderResult?orderNo="
+
byOrderNo
.
getOrderNo
());
officialAccountTemplate
.
setMiniprogram
(
miniprogram
);
HashMap
<
String
,
SendTemplateMessageItemVo
>
data
=
new
HashMap
<>();
HashMap
<
String
,
SendTemplateMessageItemVo
>
data
=
new
HashMap
<>();
SStore
sStore
=
storeService
.
getById
(
byOrderNo
.
getStoreId
());
SStore
sStore
=
storeService
.
getById
(
byOrderNo
.
getStoreId
());
SRoomVo
sRoomVo
=
roomService
.
selectSRoomById
(
byOrderNo
.
getRoomId
());
SRoomVo
sRoomVo
=
roomService
.
selectSRoomById
(
byOrderNo
.
getRoomId
());
...
@@ -623,18 +623,68 @@ public class WechatNewServiceImpl implements WechatNewService {
...
@@ -623,18 +623,68 @@ public class WechatNewServiceImpl implements WechatNewService {
//商品名称
//商品名称
data
.
put
(
"thing21"
,
new
SendTemplateMessageItemVo
(
msg
));
data
.
put
(
"thing21"
,
new
SendTemplateMessageItemVo
(
msg
));
}
}
officialAccountTemplate
.
setData
(
data
);
return
sendPublicTemplateMessage
(
officialAccountTemplate
);
return
sendPublicTemplateMessage
(
officialAccountTemplate
);
}
}
@Override
@Override
public
UserOpenIdVo
getWechatUSERList
()
{
public
List
<
String
>
getWechatUSERList
(
List
<
String
>
openIds
,
String
next_openid
)
{
String
accessToken
=
getMiniAccessToken
();
String
accessToken
=
getPublicAccessToken
();
String
url
=
StrUtil
.
format
(
WeChatConstants
.
WECHAT_PUBLIC_USER_GET_URL
,
accessToken
,
""
);
String
url
;
if
(
StringUtils
.
isEmpty
(
next_openid
))
{
url
=
StrUtil
.
format
(
WeChatConstants
.
WECHAT_PUBLIC_USER_GET_URL
,
accessToken
,
""
);
}
else
{
url
=
StrUtil
.
format
(
WeChatConstants
.
WECHAT_PUBLIC_USER_GET_URL
,
accessToken
,
next_openid
);
}
JSONObject
data
=
restTemplateUtil
.
getData
(
url
);
JSONObject
data
=
restTemplateUtil
.
getData
(
url
);
if
(
ObjectUtil
.
isNull
(
data
))
{
if
(
ObjectUtil
.
isNull
(
data
))
{
throw
new
BaseException
(
"微信平台接口异常,没任何数据返回!"
);
throw
new
BaseException
(
"微信平台接口异常,没任何数据返回!"
);
}
}
return
JSONObject
.
parseObject
(
data
.
toJSONString
(),
UserOpenIdVo
.
class
);
JSONObject
dataJSONArray
=
data
.
getJSONObject
(
"data"
);
JSONArray
openids
=
dataJSONArray
.
getJSONArray
(
"openid"
);
//转list
List
<
String
>
openIdList
=
openids
.
toJavaList
(
String
.
class
);
openIds
.
addAll
(
openIdList
);
if
(
data
.
getInteger
(
"total"
)
>
openIds
.
size
())
{
return
getWechatUSERList
(
openIds
,
data
.
getString
(
"next_openid"
));
}
return
openIds
;
}
@Override
public
Map
<
String
,
String
>
WechatUsetListInfo
(
List
<
String
>
openIds
)
{
Map
<
String
,
String
>
map
=
new
HashMap
<>();
String
accessToken
=
getPublicAccessToken
();
String
url
=
StrUtil
.
format
(
WeChatConstants
.
WECHAT_PUBLIC_USER_INFO_URL
,
accessToken
);
List
<
WachatUser
>
list
=
new
ArrayList
<>();
openIds
.
stream
().
forEach
(
o
->
{
WachatUser
wachatUser
=
new
WachatUser
();
wachatUser
.
setOpenid
(
o
);
list
.
add
(
wachatUser
);
});
Map
<
String
,
Object
>
listMap
=
new
HashMap
<>();
listMap
.
put
(
"user_list"
,
list
);
JSONObject
jsonObject
=
new
JSONObject
(
listMap
);
String
result
=
restTemplateUtil
.
postJsonData
(
url
,
jsonObject
);
JSONObject
data
=
JSONObject
.
parseObject
(
result
);
if
(
ObjectUtil
.
isNull
(
data
))
{
throw
new
BaseException
(
"微信平台接口异常,没任何数据返回!"
);
}
if
(
data
.
containsKey
(
"errcode"
)
&&
!
data
.
getString
(
"errcode"
).
equals
(
"0"
))
{
if
(
data
.
containsKey
(
"errmsg"
))
{
// 保存到微信异常表
// wxExceptionDispose(data, "微信小程序登录凭证校验异常");
throw
new
BaseException
(
"微信接口调用失败:"
+
data
.
getString
(
"errcode"
)
+
data
.
getString
(
"errmsg"
));
}
}
JSONArray
userInfoList
=
data
.
getJSONArray
(
"user_info_list"
);
//转list
List
<
JSONObject
>
userInfos
=
userInfoList
.
toJavaList
(
JSONObject
.
class
);
userInfos
.
stream
().
forEach
(
o
->
{
map
.
put
(
o
.
getString
(
"unionid"
),
o
.
getString
(
"openid"
));
});
return
map
;
}
}
@Override
@Override
...
...
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