添加事件中心发送短信&优化代码&添加单测

This commit is contained in:
feng.li
2023-10-24 13:56:54 +08:00
parent 9884c2f3e6
commit 5311eb94d2
8 changed files with 120 additions and 38 deletions

View File

@@ -6,41 +6,39 @@ package com.cool.store.enums;
* @Description:
*/
public enum SMSMsgEnum {
INTENTION_APPLY_PASS("意向申请通过","【沪上阿姨】恭喜您通过了加盟意向申请审核,请务必于{$var}前登录沪姨合伙人小程序完成面试预约时间申请,感谢您对沪上阿姨的关注与支持!点击转跳沪姨合伙人小程序:{$var}"),
INTENTION_APPLY_PASS("意向申请通过","【沪上阿姨】恭喜您通过了加盟意向申请审核,请务必于{$var}前登录沪姨合伙人小程序完成面试预约时间申请,感谢您对沪上阿姨的关注与支持!点击转跳沪姨合伙人小程序:{$var}", "c8e0770d78d61a832c49afa828f683ee"),
INTERVIEW_APPOINTMENT_PASS("预约面试通过通知","【沪上阿姨】你已成功预约{$var}进行沪上阿姨加盟资格面试届时请通过沪姨合伙人小程序进行面试面试时间约40分钟请提前仔细阅读面试准备材料以及观看加盟说明视频做好面试相应准备。请务必提前安排好您的时间。点击转跳沪姨合伙人小程序{$var}"),
INTERVIEW_APPOINTMENT_PASS("预约面试通过通知","【沪上阿姨】你已成功预约{$var}进行沪上阿姨加盟资格面试届时请通过沪姨合伙人小程序进行面试面试时间约40分钟请提前仔细阅读面试准备材料以及观看加盟说明视频做好面试相应准备。请务必提前安排好您的时间。点击转跳沪姨合伙人小程序{$var}", "9a0f2fd5d594047515f8ce6ce8d92b74"),
INTERVIEW_PASS("面试通过通知","【沪上阿姨】恭喜您通过了加盟资格面试,接下来我们会为您安排专业的选址开发顾问协助您进行选址,请保持电话畅通,祝您早日选址成功!点击转跳沪姨合伙人小程序:{$var}"),
INTERVIEW_PASS("面试通过通知","【沪上阿姨】恭喜您通过了加盟资格面试,接下来我们会为您安排专业的选址开发顾问协助您进行选址,请保持电话畅通,祝您早日选址成功!点击转跳沪姨合伙人小程序:{$var}", "00a005aa218c2f3320cd27244b97efb9"),
INTERVIEW_BEGIN_IN_DAY("面试开始前一天", "【沪上阿姨】您预约的沪上阿姨加盟资格面试将于明天 {$var}{$var})开始,为保证您能够更加顺利的通过面试,请提前通过沪上阿姨合伙人小程序仔细阅读面试准备材料并做好相应准备。点击转跳小程序:{$var}"),
INTERVIEW_BEGIN_IN_DAY("面试开始前一天", "【沪上阿姨】您预约的沪上阿姨加盟资格面试将于明天 {$var}{$var})开始,为保证您能够更加顺利的通过面试,请提前通过沪上阿姨合伙人小程序仔细阅读面试准备材料并做好相应准备。点击转跳小程序:{$var}", "657880fb2614fe70e579a06883dff57b"),
INTERVIEW_BEGIN_IN_MINUTES("面试开始前30分钟", "【沪上阿姨】您预约的沪上阿姨加盟资格面试将在30分钟后开始。面试预计持续40分钟请务必提前安排好您的时间。点击跳转沪上阿姨小程序快速进入面试房间{$var}"),
INTERVIEW_BEGIN_IN_MINUTES("面试开始前30分钟", "【沪上阿姨】您预约的沪上阿姨加盟资格面试将在30分钟后开始。面试预计持续40分钟请务必提前安排好您的时间。点击跳转沪上阿姨小程序快速进入面试房间{$var}", "07790f743a22d961afb8a3902e691404"),
SMS_INVATE("短信邀约", "【沪上阿姨】感谢您对沪上阿姨品牌的关注与支持,您可通过链接登记加盟申请信息,我们的客户经理将在第一时间与您联系。{$var}"),
SMS_INVATE("短信邀约", "【沪上阿姨】感谢您对沪上阿姨品牌的关注与支持,您可通过链接登记加盟申请信息,我们的客户经理将在第一时间与您联系。{$var}", "d7772108bb7d9767494818bcd39d2ec1"),
;
private String title;
private String content;
private String templateCode;
SMSMsgEnum(String title, String content) {
SMSMsgEnum(String title, String content, String templateCode) {
this.title = title;
this.content = content;
this.templateCode = templateCode;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
public String getTemplateCode() {
return templateCode;
}
}

View File

@@ -29,16 +29,6 @@ public class CreateEventResponse {
*/
private List<EventEntry> entryList;
/**
* 响应码200 为成功
*/
private Integer code;
/**
* 响应信息
*/
private String message;
@Data
private static class EventEntry {

View File

@@ -2,17 +2,22 @@ package com.cool.store.http;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSON;
import com.cool.store.dto.response.ResultDTO;
import com.cool.store.enums.FeiShuNoticeMsgEnum;
import com.cool.store.enums.SMSMsgEnum;
import com.cool.store.exception.ApiException;
import com.cool.store.mq.util.HttpRestTemplateService;
import com.cool.store.request.event.CreateSingleEventRequest;
import com.cool.store.request.event.SendMsgRequest;
import com.cool.store.response.event.CreateEventResponse;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
@@ -28,27 +33,37 @@ public class EventCenterHttpRequest {
@Resource
private HttpRestTemplateService httpRestTemplateService;
/**
* 事件中心 url
*/
@Value("${hsay.event.url}")
private String eventUrl;
/**
* pc 端飞书应用地址
*/
@Value("${feishu.notice.link.url}")
private String PCLinkUrl;
/**
* 移动端飞书应用地址
*/
@Value("${feishu.notice.link.url.mobile}")
private String mobileLinkUrl;
/**
* 本系统来源
*/
private final static String source = "hsay_partner";
@Value("${hsay.event.systemsource}")
private String source;
/**
* 创建单个事件
* @param request {@link com.cool.store.request.event.CreateSingleEventRequest}
* @return
*/
private CreateEventResponse createSingleEvent(CreateSingleEventRequest request) throws ApiException {
CreateEventResponse response = httpRestTemplateService.postForObject(eventUrl + "/event/create", request, CreateEventResponse.class);
private ResultDTO<CreateEventResponse> createSingleEvent(CreateSingleEventRequest request) throws ApiException {
ResultDTO<CreateEventResponse> response = httpRestTemplateService.postForObject(eventUrl + "/event/create", request, ResultDTO.class);
log.info("创建沪上阿姨事件中心事件request: {}, response: {}", JSON.toJSONString(request), JSON.toJSONString(response));
if (response.getCode() != 200) {
throw new ApiException("创建事件失败!");
@@ -56,11 +71,17 @@ public class EventCenterHttpRequest {
return response;
}
public void sendFeiShuCardMessage(FeiShuNoticeMsgEnum msgEnum, List<String> receiverIds) throws ApiException {
//1. 组织消息参数SendMsgRequest 的 cardParams
/**
* 发送飞书通知
* @param msgEnum {@link com.cool.store.enums.FeiShuNoticeMsgEnum}
* @param receiverIds 接收者 id飞书为 user_id不是 open_id
* @param objects 消息参数,飞书消息卡片为 参数名:参数
*/
public void sendFeiShuNotice(FeiShuNoticeMsgEnum msgEnum, List<String> receiverIds, Object... objects) throws ApiException {
//1. 组织消息参数SendMsgRequest 的 cardParams需要填入的参数为模板中 ${} 中的参数
HashMap<String, String> cardParams = new HashMap<>();
cardParams.put("title", msgEnum.getTitle());
cardParams.put("content", msgEnum.getContent());
cardParams.put("content", MessageFormat.format(msgEnum.getContent(),objects));
cardParams.put("img_key", msgEnum.getImageMediaId());
cardParams.put("common_url", PCLinkUrl);
cardParams.put("pc_url", PCLinkUrl);
@@ -79,8 +100,40 @@ public class EventCenterHttpRequest {
String randomNum = RandomUtil.randomNumbers(32);
CreateSingleEventRequest createEventRequest = CreateSingleEventRequest.builder()
.thirdPartyId(randomNum)
.bizSource(source)
.eventType("push_msg")
.content(JSON.toJSONString(sendFeiShuMsgRequest))
.build();
//4. 创建事件
createSingleEvent(createEventRequest);
}
/**
* 发送短信通知
* @param phone 接收者手机号
* @param smsMsgEnum {@link com.cool.store.enums.SMSMsgEnum}
* @param objects 短信参数,短信的消息参数为 手机号: 参数1, 参数2, 参数3, 参数4, 参数5
*/
public void sendSmsVariable(String phone, SMSMsgEnum smsMsgEnum, String... objects) throws ApiException {
//1. 组织消息参数,短信的消息参数为 手机号: 参数1, 参数2, 参数3, 参数4, 参数5需要填入的参数为模板中 ${} 中的参数
HashMap<String, String> smsParams = new HashMap<>();
String params = String.join(",", Arrays.asList(objects));
smsParams.put(phone, params);
//2. 组织消息发送请求体,即 CreateSingleEventRequest 的 content 字段
SendMsgRequest sendFeiShuMsgRequest = SendMsgRequest.builder()
.platform("SHORT_MESSAGE")
.templateId(smsMsgEnum.getTemplateCode())
.cardParams(smsParams)
.build();
//3. 组织创建事件参数
String randomNum = RandomUtil.randomNumbers(32);
CreateSingleEventRequest createEventRequest = CreateSingleEventRequest.builder()
.thirdPartyId(randomNum)
.bizSource(source)
.eventType("push_msg")
.bizSource("hsay_partner")
.content(JSON.toJSONString(sendFeiShuMsgRequest))
.build();

View File

@@ -27,6 +27,10 @@ public class NoticeService {
@Autowired
private ISVHttpRequest isvHttpRequest;
/**
* @deprecated 替换为 {@link com.cool.store.http.EventCenterHttpRequest#sendFeiShuNotice(FeiShuNoticeMsgEnum, List, Object...)}
*/
@Deprecated
public void sendFeiShuNotice(FeiShuNoticeMsgEnum feiShuNoticeMsgEnum, List<String> userIds, Object... objects) {
try{
if (CollectionUtils.isEmpty(userIds)){

View File

@@ -20,13 +20,11 @@ import com.cool.store.request.SmsSendRequest;
import com.cool.store.response.SmsSendResponse;
import com.cool.store.service.SmsService;
import com.cool.store.service.WechatMiniAppService;
import com.cool.store.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.io.IOException;
import java.text.MessageFormat;
@@ -102,13 +100,10 @@ public class SmsServiceImpl implements SmsService {
}
/**
* 发送变量短信
* @param params 手机号,参数 多个使用;隔开 例: 手机号1,参数A,参数B;手机号2,参数C,参数D
* @param msg 短信内容,变量使用{$var}标识
* @return
* @throws IOException
* @deprecated 替换为 {@link com.cool.store.http.EventCenterHttpRequest#sendSmsVariable(String, SMSMsgEnum, String...)}
*/
@Override
@Deprecated
@Async
public SmsSendResponse sendSmsVariable(String phone, SMSMsgEnum smsMsgEnum,String... objects){
//
@@ -142,7 +137,6 @@ public class SmsServiceImpl implements SmsService {
throw new ServiceException(ErrorCodeEnum.LINE_ALREADY_EXISTS,msg);
}
}
}
LoginUserInfo operator = CurrentUserHolder.getUser();
StringBuffer stringBuffer = new StringBuffer();

View File

@@ -21,6 +21,10 @@
<groupId>com.coolstore</groupId>
<artifactId>coolstore-partner-service</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>

View File

@@ -0,0 +1,35 @@
package com.cool.store.service;
import com.cool.store.enums.FeiShuNoticeMsgEnum;
import com.cool.store.exception.ApiException;
import com.cool.store.http.EventCenterHttpRequest;
import org.junit.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;
import javax.annotation.Resource;
import java.util.Arrays;
/**
* @author Fun Li 2023/10/24 10:48
* @version 1.0
*/
//SpringBootTest 默认不启动 web 环境,会导致 websocket 发现 web 容器不可用而报错,所以需要指定 web 环境
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class EventRequestTest extends AbstractJUnit4SpringContextTests {
@Resource
private EventCenterHttpRequest eventCenterHttpRequest;
@Test
public void test() {
System.out.println("test");
}
@Test
public void testSendFeiShuNotice() throws ApiException {
//eventCenterHttpRequest.sendFeiShuNotice(FeiShuNoticeMsgEnum.common_notice, Arrays.asList("9818f98c"), "测试");
eventCenterHttpRequest.sendFeiShuNotice(FeiShuNoticeMsgEnum.ALLOCATION_INVESTMENT_MANAGER, Arrays.asList("661c6cfg"), "2023-10-24 10:09:04", "测试", "1008611");
}
}

View File

@@ -73,6 +73,10 @@
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.6</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>