mirror of
https://gitee.com/hhyykk/ipms-sjy.git
synced 2025-07-25 00:15:06 +08:00
【优化】SYSTEM: 根据代码评审优化订阅消息
This commit is contained in:
@ -2,12 +2,10 @@ package cn.iocoder.yudao.module.system.api.social;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.system.api.social.dto.*;
|
||||
import cn.iocoder.yudao.module.system.convert.social.SocialUserConvert;
|
||||
import cn.iocoder.yudao.module.system.service.social.SocialClientService;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@ -18,7 +16,9 @@ import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.hutool.core.collection.CollUtil.findOne;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
|
||||
|
||||
/**
|
||||
* 社交应用的 API 实现类
|
||||
@ -65,80 +65,36 @@ public class SocialClientApiImpl implements SocialClientApi {
|
||||
@Override
|
||||
public List<SocialWxSubscribeTemplateRespDTO> getSubscribeTemplateList(Integer userType) {
|
||||
List<TemplateInfo> subscribeTemplate = socialClientService.getSubscribeTemplateList(userType);
|
||||
return SocialUserConvert.INSTANCE.convertList(subscribeTemplate);
|
||||
return convertList(subscribeTemplate, item -> BeanUtils.toBean(item, SocialWxSubscribeTemplateRespDTO.class)
|
||||
.setId(item.getPriTmplId()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSubscribeMessage(SocialWxSubscribeMessageSendReqDTO reqDTO, Integer userType) {
|
||||
socialClientService.sendSubscribeMessage(reqDTO, userType);
|
||||
}
|
||||
|
||||
public void sendSubscribeMessage(String templateTitle, Map<String, String> messages, Integer userType, Long userId,
|
||||
Integer socialType, String path) {
|
||||
// TODO @puhui999:建议是,先不拆小方法。因为逻辑的复杂度其实不高哈。合在一个方法里,因为咱写了 1.1 1.2 2. 这样的逻辑,也能一下子看懂。
|
||||
// 1.1 获得订阅模版
|
||||
SocialWxSubscribeTemplateRespDTO template = getTemplate(templateTitle, userType);
|
||||
if (template == null) {
|
||||
return;
|
||||
}
|
||||
// 1.2 获得发送对象的 openId
|
||||
String openId = getUserOpenId(userType, userId, socialType);
|
||||
if (StrUtil.isBlankIfStr(openId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 发送消息
|
||||
sendSubscribeMessage(buildMessageSendReqDTO(openId, path, template).setMessages(messages), userType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建发送消息请求参数
|
||||
*
|
||||
* @param openId 接收者(用户)的 openid
|
||||
* @param path 点击模板卡片后的跳转页面,仅限本小程序内的页面
|
||||
* @param template 订阅模版
|
||||
* @return 微信小程序订阅消息发送
|
||||
*/
|
||||
private SocialWxSubscribeMessageSendReqDTO buildMessageSendReqDTO(String openId, String path,
|
||||
SocialWxSubscribeTemplateRespDTO template) {
|
||||
return new SocialWxSubscribeMessageSendReqDTO().setLang("zh_CN").setMiniprogramState(envVersion)
|
||||
.setTemplateId(template.getId()).setToUser(openId).setPage(path);
|
||||
}
|
||||
|
||||
// TODO @puhui999:建议下沉到 service 实现。
|
||||
/**
|
||||
* 获得小程序订阅消息模版
|
||||
*
|
||||
* @param templateTitle 模版标题
|
||||
* @param userType 用户类型
|
||||
* @return 小程序订阅消息模版
|
||||
*/
|
||||
private SocialWxSubscribeTemplateRespDTO getTemplate(String templateTitle, Integer userType) {
|
||||
List<SocialWxSubscribeTemplateRespDTO> templateList = getSubscribeTemplateList(userType);
|
||||
public void sendSubscribeMessage(SocialWxSubscribeMessageSendReqDTO reqDTO) {
|
||||
// 1.1 获得订阅模版列表
|
||||
List<SocialWxSubscribeTemplateRespDTO> templateList = getSubscribeTemplateList(reqDTO.getUserType());
|
||||
if (CollUtil.isEmpty(templateList)) {
|
||||
log.warn("[getTemplate][templateTitle({}) userType({}) 没有找到订阅模板]", templateTitle, userType);
|
||||
return null;
|
||||
log.warn("[sendSubscribeMessage][reqDTO({}) 发送订阅消息失败,原因:没有找到订阅模板]", reqDTO);
|
||||
return;
|
||||
}
|
||||
// 1.2 获得需要使用的模版
|
||||
SocialWxSubscribeTemplateRespDTO template = findOne(templateList, item ->
|
||||
ObjUtil.equal(item.getTitle(), reqDTO.getTemplateTitle()));
|
||||
if (template == null) {
|
||||
log.warn("[sendSubscribeMessage][reqDTO({}) 发送订阅消息失败,原因:没有找到订阅模板]", reqDTO);
|
||||
return;
|
||||
}
|
||||
return CollectionUtil.findOne(templateList, item -> ObjUtil.equal(item.getTitle(), templateTitle));
|
||||
}
|
||||
|
||||
// TODO @puhui999:建议下沉到 service 实现。
|
||||
/**
|
||||
* 获得用户 openId
|
||||
*
|
||||
* @param userType 用户类型
|
||||
* @param userId 用户编号
|
||||
* @param socialType 社交类型
|
||||
* @return 用户 openId
|
||||
*/
|
||||
private String getUserOpenId(Integer userType, Long userId, Integer socialType) {
|
||||
SocialUserRespDTO socialUser = socialUserApi.getSocialUserByUserId(userType, userId, socialType);
|
||||
// 2. 获得社交用户
|
||||
SocialUserRespDTO socialUser = socialUserApi.getSocialUserByUserId(reqDTO.getUserType(), reqDTO.getUserId(),
|
||||
reqDTO.getSocialType());
|
||||
if (StrUtil.isBlankIfStr(socialUser.getOpenid())) {
|
||||
log.warn("[getUserOpenId][userType({}) userId({}) socialType({}) 会员 openid 缺失]",
|
||||
userType, userId, socialType);
|
||||
return null;
|
||||
log.warn("[sendSubscribeMessage][reqDTO({}) 发送订阅消息失败,原因:会员 openid 缺失]", reqDTO);
|
||||
return;
|
||||
}
|
||||
return socialUser.getOpenid();
|
||||
|
||||
// 3. 发送订阅消息
|
||||
socialClientService.sendSubscribeMessage(reqDTO, template.getId(), socialUser.getOpenid());
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -6,14 +6,15 @@ Content-Type: application/json
|
||||
tenant-id: {{adminTenentId}}
|
||||
|
||||
{
|
||||
"toUser": "oKNkb4xxw2H135-MVPKtEMkumK08",
|
||||
"templateId": "W4ybDTIwCfKHtMKR7fSfx83DtmVKEeXQo3Ti7GCw4_4",
|
||||
"miniprogramState": "developer",
|
||||
"lang": "zh_CN",
|
||||
"userId": 247,
|
||||
"userType": 1,
|
||||
"socialType": 34,
|
||||
"templateTitle": "充值成功通知",
|
||||
"page": "",
|
||||
"messages": {
|
||||
"character_string1":"5616122165165",
|
||||
"amount2":"1000.00",
|
||||
"time3":"2024-01-01 10:10:10",
|
||||
"phrase4":"成功"
|
||||
"phrase4": "充值成功"
|
||||
}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
package cn.iocoder.yudao.module.system.controller.admin.socail;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.system.api.social.SocialClientApi;
|
||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeMessageSendReqDTO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientRespVO;
|
||||
@ -29,6 +29,8 @@ public class SocialClientController {
|
||||
|
||||
@Resource
|
||||
private SocialClientService socialClientService;
|
||||
@Resource
|
||||
private SocialClientApi socialClientApi;
|
||||
|
||||
@PostMapping("/create")
|
||||
@Operation(summary = "创建社交客户端")
|
||||
@ -75,7 +77,7 @@ public class SocialClientController {
|
||||
@Operation(summary = "发送订阅消息") // 用于测试
|
||||
@PreAuthorize("@ss.hasPermission('system:social-client:query')")
|
||||
public void sendSubscribeMessage(@RequestBody SocialWxSubscribeMessageSendReqDTO reqDTO) {
|
||||
socialClientService.sendSubscribeMessage(reqDTO, UserTypeEnum.MEMBER.getValue());
|
||||
socialClientApi.sendSubscribeMessage(reqDTO);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,23 +1,11 @@
|
||||
package cn.iocoder.yudao.module.system.convert.social;
|
||||
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialUserBindReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeMessageSendReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeTemplateRespDTO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.user.SocialUserBindReqVO;
|
||||
import me.chanjar.weixin.common.bean.subscribemsg.TemplateInfo;
|
||||
import org.mapstruct.Mapper;
|
||||
import org.mapstruct.Mapping;
|
||||
import org.mapstruct.factory.Mappers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;
|
||||
|
||||
@Mapper
|
||||
public interface SocialUserConvert {
|
||||
|
||||
@ -26,25 +14,4 @@ public interface SocialUserConvert {
|
||||
@Mapping(source = "reqVO.type", target = "socialType")
|
||||
SocialUserBindReqDTO convert(Long userId, Integer userType, SocialUserBindReqVO reqVO);
|
||||
|
||||
// TODO @puhui999:要不 convert 直接放到 service 里。
|
||||
default WxMaSubscribeMessage convert(SocialWxSubscribeMessageSendReqDTO reqDTO) {
|
||||
WxMaSubscribeMessage message = BeanUtils.toBean(reqDTO, WxMaSubscribeMessage.class);
|
||||
Map<String, String> messages = reqDTO.getMessages();
|
||||
if (CollUtil.isNotEmpty(messages)) {
|
||||
messages.keySet().forEach(key -> findAndThen(messages, key, value -> message.addData(new WxMaSubscribeMessage.MsgData(key, value))));
|
||||
}
|
||||
return message;
|
||||
}
|
||||
|
||||
// TODO @puhui999:要不 convert 直接放到 service 里。其实可以 BeanUtils.toBean(reqDTO, WxMaSubscribeMessage.class) 来搞的呀。
|
||||
@Mapping(target = "id", source = "priTmplId")
|
||||
SocialWxSubscribeTemplateRespDTO convert(TemplateInfo templateInfo);
|
||||
|
||||
// TODO @puhui999:是不是用 CollectionUtils.convertList 就 ok 啦。
|
||||
default List<SocialWxSubscribeTemplateRespDTO> convertList(List<TemplateInfo> subscribeTemplate) {
|
||||
List<SocialWxSubscribeTemplateRespDTO> list = new ArrayList<>();
|
||||
subscribeTemplate.forEach(templateInfo -> list.add(convert(templateInfo)));
|
||||
return list;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -83,9 +83,11 @@ public interface SocialClientService {
|
||||
/**
|
||||
* 发送微信小程序订阅消息
|
||||
*
|
||||
* @param reqDTO 请求
|
||||
* @param reqDTO 请求
|
||||
* @param templateId 模版编号
|
||||
* @param openId 会员 openId
|
||||
*/
|
||||
void sendSubscribeMessage(SocialWxSubscribeMessageSendReqDTO reqDTO, Integer userType);
|
||||
void sendSubscribeMessage(SocialWxSubscribeMessageSendReqDTO reqDTO, String templateId, String openId);
|
||||
|
||||
// =================== 客户端管理 ===================
|
||||
|
||||
|
@ -4,8 +4,10 @@ import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
|
||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo;
|
||||
import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
|
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
|
||||
import cn.hutool.core.bean.BeanUtil;
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.hutool.core.util.ReflectUtil;
|
||||
@ -19,7 +21,6 @@ import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
|
||||
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxSubscribeMessageSendReqDTO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.socail.vo.client.SocialClientSaveReqVO;
|
||||
import cn.iocoder.yudao.module.system.convert.social.SocialUserConvert;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO;
|
||||
import cn.iocoder.yudao.module.system.dal.mysql.social.SocialClientMapper;
|
||||
import cn.iocoder.yudao.module.system.enums.social.SocialTypeEnum;
|
||||
@ -51,9 +52,11 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;
|
||||
import static cn.iocoder.yudao.framework.common.util.json.JsonUtils.toJsonString;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
|
||||
@ -273,17 +276,39 @@ public class SocialClientServiceImpl implements SocialClientService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSubscribeMessage(SocialWxSubscribeMessageSendReqDTO reqDTO, Integer userType) {
|
||||
WxMaService service = getWxMaService(userType);
|
||||
public void sendSubscribeMessage(SocialWxSubscribeMessageSendReqDTO reqDTO, String templateId, String openId) {
|
||||
WxMaService service = getWxMaService(reqDTO.getUserType());
|
||||
try {
|
||||
WxMaSubscribeService subscribeService = service.getSubscribeService();
|
||||
subscribeService.sendSubscribeMsg(SocialUserConvert.INSTANCE.convert(reqDTO));
|
||||
subscribeService.sendSubscribeMsg(buildMessageSendReqDTO(reqDTO, templateId, openId));
|
||||
} catch (WxErrorException e) {
|
||||
log.error("[sendSubscribeMessage][reqVO({}) userType({}) 发送小程序订阅消息]", reqDTO, userType, e);
|
||||
log.error("[sendSubscribeMessage][reqVO({}) templateId({}) openId({}) 发送小程序订阅消息失败]", reqDTO, templateId, openId, e);
|
||||
throw exception(SOCIAL_CLIENT_WEIXIN_MINI_APP_SUBSCRIBE_MESSAGE_ERROR);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建发送消息请求参数
|
||||
*
|
||||
* @param openId 接收者(用户)的 openid
|
||||
* @param path 点击模板卡片后的跳转页面,仅限本小程序内的页面
|
||||
* @param template 订阅模版
|
||||
* @return 微信小程序订阅消息发送
|
||||
*/
|
||||
private WxMaSubscribeMessage buildMessageSendReqDTO(SocialWxSubscribeMessageSendReqDTO reqDTO,
|
||||
String templateId, String openId) {
|
||||
// 1.1 设置订阅消息基本参数
|
||||
WxMaSubscribeMessage subscribeMessage = new WxMaSubscribeMessage().setLang("zh_CN").setMiniprogramState(envVersion)
|
||||
.setTemplateId(templateId).setToUser(openId).setPage(reqDTO.getPage());
|
||||
// 1.2 设置具体消息参数
|
||||
Map<String, String> messages = reqDTO.getMessages();
|
||||
if (CollUtil.isNotEmpty(messages)) {
|
||||
messages.keySet().forEach(key -> findAndThen(messages, key, value ->
|
||||
subscribeMessage.addData(new WxMaSubscribeMessage.MsgData(key, value))));
|
||||
}
|
||||
return subscribeMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获得 clientId + clientSecret 对应的 WxMpService 对象
|
||||
*
|
||||
|
Reference in New Issue
Block a user