Commit 63e319e6639c4ac387f98fb85d5e6835b3c2af2c
1 parent
02f4d382
【新增】SYSTEM: 发送微信小程序订阅消息
Showing
6 changed files
with
128 additions
and
27 deletions
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApi.java
| 1 | 1 | package cn.iocoder.yudao.module.system.api.social; |
| 2 | 2 | |
| 3 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO; | |
| 4 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO; | |
| 5 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO; | |
| 6 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeTemplateRespDTO; | |
| 3 | +import cn.iocoder.yudao.module.system.api.social.dto.*; | |
| 7 | 4 | import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum; |
| 8 | 5 | import jakarta.validation.Valid; |
| 9 | 6 | |
| ... | ... | @@ -59,4 +56,11 @@ public interface SocialClientApi { |
| 59 | 56 | */ |
| 60 | 57 | List<SocialWxSubscribeTemplateRespDTO> getSubscribeTemplate(); |
| 61 | 58 | |
| 59 | + /** | |
| 60 | + * 发送订阅消息 | |
| 61 | + * | |
| 62 | + * @param reqDTO 请求 | |
| 63 | + */ | |
| 64 | + void sendSubscribeMessage(SocialWxSubscribeMessageReqDTO reqDTO); | |
| 65 | + | |
| 62 | 66 | } | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/api/social/dto/SocialWxSubscribeMessageReqDTO.java
0 → 100644
| 1 | +package cn.iocoder.yudao.module.system.api.social.dto; | |
| 2 | + | |
| 3 | +import cn.iocoder.yudao.framework.common.core.KeyValue; | |
| 4 | +import lombok.Data; | |
| 5 | + | |
| 6 | +import java.util.List; | |
| 7 | + | |
| 8 | +/** | |
| 9 | + * 微信小程序订阅消息 Request DTO | |
| 10 | + * | |
| 11 | + * @author HUIHUI | |
| 12 | + */ | |
| 13 | +@Data | |
| 14 | +public class SocialWxSubscribeMessageReqDTO { | |
| 15 | + | |
| 16 | + /** | |
| 17 | + * 接收者(用户)的 openid. | |
| 18 | + * <pre> | |
| 19 | + * 参数:touser | |
| 20 | + * 是否必填: 是 | |
| 21 | + * 描述: 接收者(用户)的 openid | |
| 22 | + * </pre> | |
| 23 | + */ | |
| 24 | + private String toUser; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * 所需下发的模板消息的id. | |
| 28 | + * <pre> | |
| 29 | + * 参数:template_id | |
| 30 | + * 是否必填: 是 | |
| 31 | + * 描述: 所需下发的模板消息的id | |
| 32 | + * </pre> | |
| 33 | + */ | |
| 34 | + private String templateId; | |
| 35 | + | |
| 36 | + /** | |
| 37 | + * 点击模板卡片后的跳转页面,仅限本小程序内的页面. | |
| 38 | + * <pre> | |
| 39 | + * 参数:page | |
| 40 | + * 是否必填: 否 | |
| 41 | + * 描述: 点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,(示例index?foo=bar)。该字段不填则模板无跳转。 | |
| 42 | + * </pre> | |
| 43 | + */ | |
| 44 | + private String page; | |
| 45 | + | |
| 46 | + /** | |
| 47 | + * 跳转小程序类型:developer为开发版;trial为体验版;formal为正式版;默认为正式版 | |
| 48 | + * | |
| 49 | + * 枚举 WxMaConstants.MiniProgramState | |
| 50 | + */ | |
| 51 | + private String miniprogramState; | |
| 52 | + | |
| 53 | + /** | |
| 54 | + * 进入小程序查看的语言类型,支持zh_CN(简体中文)、en_US(英文)、zh_HK(繁体中文)、zh_TW(繁体中文),默认为zh_CN | |
| 55 | + * | |
| 56 | + * 枚举 WxMaConstants.MiniProgramLang | |
| 57 | + */ | |
| 58 | + private String lang; | |
| 59 | + | |
| 60 | + /** | |
| 61 | + * 模板内容,不填则下发空模板. | |
| 62 | + * <pre> | |
| 63 | + * 参数:data | |
| 64 | + * 是否必填: 是 | |
| 65 | + * 描述: 模板内容,不填则下发空模板 | |
| 66 | + * </pre> | |
| 67 | + */ | |
| 68 | + private List<KeyValue<String, String>> data; | |
| 69 | + | |
| 70 | +} | ... | ... |
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
| ... | ... | @@ -122,6 +122,7 @@ public interface ErrorCodeConstants { |
| 122 | 122 | ErrorCode SOCIAL_CLIENT_WEIXIN_MINI_APP_PHONE_CODE_ERROR = new ErrorCode(1_002_018_200, "获得手机号失败"); |
| 123 | 123 | ErrorCode SOCIAL_CLIENT_WEIXIN_MINI_APP_QRCODE_ERROR = new ErrorCode(1_002_018_201, "获得小程序码失败"); |
| 124 | 124 | ErrorCode SOCIAL_CLIENT_WEIXIN_MINI_APP_SUBSCRIBE_TEMPLATE_ERROR = new ErrorCode(1_002_018_202, "获得小程序订阅消息模版失败"); |
| 125 | + ErrorCode SOCIAL_CLIENT_WEIXIN_MINI_APP_SUBSCRIBE_MESSAGE_ERROR = new ErrorCode(1_002_018_203, "发送小程序订阅消息失败"); | |
| 125 | 126 | ErrorCode SOCIAL_CLIENT_NOT_EXISTS = new ErrorCode(1_002_018_210, "社交客户端不存在"); |
| 126 | 127 | ErrorCode SOCIAL_CLIENT_UNIQUE = new ErrorCode(1_002_018_211, "社交客户端已存在配置"); |
| 127 | 128 | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/api/social/SocialClientApiImpl.java
| ... | ... | @@ -2,10 +2,7 @@ package cn.iocoder.yudao.module.system.api.social; |
| 2 | 2 | |
| 3 | 3 | import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 5 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialWxJsapiSignatureRespDTO; | |
| 6 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialWxPhoneNumberInfoRespDTO; | |
| 7 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO; | |
| 8 | -import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeTemplateRespDTO; | |
| 5 | +import cn.iocoder.yudao.module.system.api.social.dto.*; | |
| 9 | 6 | import cn.iocoder.yudao.module.system.service.social.SocialClientService; |
| 10 | 7 | import jakarta.annotation.Resource; |
| 11 | 8 | import me.chanjar.weixin.common.bean.WxJsapiSignature; |
| ... | ... | @@ -55,4 +52,9 @@ public class SocialClientApiImpl implements SocialClientApi { |
| 55 | 52 | return BeanUtils.toBean(subscribeTemplate, SocialWxSubscribeTemplateRespDTO.class); |
| 56 | 53 | } |
| 57 | 54 | |
| 55 | + @Override | |
| 56 | + public void sendSubscribeMessage(SocialWxSubscribeMessageReqDTO reqDTO) { | |
| 57 | + socialClientService.sendSubscribeMessage(reqDTO); | |
| 58 | + } | |
| 59 | + | |
| 58 | 60 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientService.java
| ... | ... | @@ -3,6 +3,7 @@ package cn.iocoder.yudao.module.system.service.social; |
| 3 | 3 | import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; |
| 4 | 4 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 5 | 5 | import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO; |
| 6 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeMessageReqDTO; | |
| 6 | 7 | import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO; |
| 7 | 8 | import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientSaveReqVO; |
| 8 | 9 | import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO; |
| ... | ... | @@ -72,6 +73,20 @@ public interface SocialClientService { |
| 72 | 73 | */ |
| 73 | 74 | byte[] getWxaQrcode(SocialWxQrcodeReqDTO reqVO); |
| 74 | 75 | |
| 76 | + /** | |
| 77 | + * 获得微信小程订阅模板 | |
| 78 | + * | |
| 79 | + * @return 微信小程订阅模板 | |
| 80 | + */ | |
| 81 | + List<TemplateInfo> getSubscribeTemplate(); | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * 发送订阅消息 | |
| 85 | + * | |
| 86 | + * @param reqDTO 请求 | |
| 87 | + */ | |
| 88 | + void sendSubscribeMessage(SocialWxSubscribeMessageReqDTO reqDTO); | |
| 89 | + | |
| 75 | 90 | // =================== 客户端管理 =================== |
| 76 | 91 | |
| 77 | 92 | /** |
| ... | ... | @@ -112,11 +127,4 @@ public interface SocialClientService { |
| 112 | 127 | */ |
| 113 | 128 | PageResult<SocialClientDO> getSocialClientPage(SocialClientPageReqVO pageReqVO); |
| 114 | 129 | |
| 115 | - /** | |
| 116 | - * 获得微信小程订阅模板 | |
| 117 | - * | |
| 118 | - * @return 微信小程订阅模板 | |
| 119 | - */ | |
| 120 | - List<TemplateInfo> getSubscribeTemplate(); | |
| 121 | - | |
| 122 | 130 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/social/SocialClientServiceImpl.java
| ... | ... | @@ -4,6 +4,7 @@ import cn.binarywang.wx.miniapp.api.WxMaService; |
| 4 | 4 | import cn.binarywang.wx.miniapp.api.WxMaSubscribeService; |
| 5 | 5 | import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl; |
| 6 | 6 | import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; |
| 7 | +import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage; | |
| 7 | 8 | import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl; |
| 8 | 9 | import cn.hutool.core.bean.BeanUtil; |
| 9 | 10 | import cn.hutool.core.lang.Assert; |
| ... | ... | @@ -16,6 +17,7 @@ import cn.iocoder.yudao.framework.common.util.cache.CacheUtils; |
| 16 | 17 | import cn.iocoder.yudao.framework.common.util.http.HttpUtils; |
| 17 | 18 | import cn.iocoder.yudao.framework.common.util.object.BeanUtils; |
| 18 | 19 | import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO; |
| 20 | +import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeMessageReqDTO; | |
| 19 | 21 | import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO; |
| 20 | 22 | import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientSaveReqVO; |
| 21 | 23 | import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO; |
| ... | ... | @@ -258,6 +260,32 @@ public class SocialClientServiceImpl implements SocialClientService { |
| 258 | 260 | } |
| 259 | 261 | } |
| 260 | 262 | |
| 263 | + @Override | |
| 264 | + public List<TemplateInfo> getSubscribeTemplate() { | |
| 265 | + WxMaService service = getWxMaService(UserTypeEnum.MEMBER.getValue()); | |
| 266 | + try { | |
| 267 | + WxMaSubscribeService subscribeService = service.getSubscribeService(); | |
| 268 | + return subscribeService.getTemplateList(); | |
| 269 | + }catch (WxErrorException e) { | |
| 270 | + log.error("[getSubscribeTemplate][获得小程序订阅消息模版]", e); | |
| 271 | + throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_SUBSCRIBE_TEMPLATE_ERROR); | |
| 272 | + } | |
| 273 | + } | |
| 274 | + | |
| 275 | + @Override | |
| 276 | + public void sendSubscribeMessage(SocialWxSubscribeMessageReqDTO reqDTO) { | |
| 277 | + WxMaService service = getWxMaService(UserTypeEnum.MEMBER.getValue()); | |
| 278 | + try { | |
| 279 | + WxMaSubscribeService subscribeService = service.getSubscribeService(); | |
| 280 | + WxMaSubscribeMessage message = BeanUtils.toBean(reqDTO, WxMaSubscribeMessage.class); | |
| 281 | + reqDTO.getData().forEach(item-> message.addData(new WxMaSubscribeMessage.MsgData(item.getKey(), item.getValue()))); | |
| 282 | + subscribeService.sendSubscribeMsg(message); | |
| 283 | + }catch (WxErrorException e) { | |
| 284 | + log.error("[sendSubscribeMessage][发送小程序订阅消息]", e); | |
| 285 | + throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_SUBSCRIBE_MESSAGE_ERROR); | |
| 286 | + } | |
| 287 | + } | |
| 288 | + | |
| 261 | 289 | /** |
| 262 | 290 | * 获得 clientId + clientSecret 对应的 WxMpService 对象 |
| 263 | 291 | * |
| ... | ... | @@ -367,16 +395,4 @@ public class SocialClientServiceImpl implements SocialClientService { |
| 367 | 395 | return socialClientMapper.selectPage(pageReqVO); |
| 368 | 396 | } |
| 369 | 397 | |
| 370 | - @Override | |
| 371 | - public List<TemplateInfo> getSubscribeTemplate() { | |
| 372 | - WxMaService service = getWxMaService(UserTypeEnum.MEMBER.getValue()); | |
| 373 | - try { | |
| 374 | - WxMaSubscribeService subscribeService = service.getSubscribeService(); | |
| 375 | - return subscribeService.getTemplateList(); | |
| 376 | - }catch (WxErrorException e) { | |
| 377 | - log.error("[getSubscribeTemplate][获得小程序订阅消息模版]", e); | |
| 378 | - throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_SUBSCRIBE_TEMPLATE_ERROR); | |
| 379 | - } | |
| 380 | - } | |
| 381 | - | |
| 382 | 398 | } | ... | ... |