fix:模板频控功能

This commit is contained in:
zhongzb
2023-07-07 00:04:55 +08:00
parent 65f8735d28
commit c80a48e85c
6 changed files with 68 additions and 15 deletions

View File

@@ -16,6 +16,19 @@
<groupId>com.abin.mallchat</groupId>
<artifactId>mallchat-common</artifactId>
</dependency>
<!-- Used for unit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.19</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>

View File

@@ -5,14 +5,11 @@ import com.abin.mallchat.common.common.domain.dto.FrequencyControlDTO;
import com.abin.mallchat.common.common.exception.FrequencyControlException;
import com.abin.mallchat.common.common.handler.GlobalUncaughtExceptionHandler;
import com.abin.mallchat.common.common.service.frequencycontrol.FrequencyControlUtil;
import com.abin.mallchat.common.common.utils.JsonUtils;
import com.abin.mallchat.common.user.domain.entity.User;
import com.abin.mallchat.common.user.service.cache.UserCache;
import com.abin.mallchat.custom.chat.service.WeChatMsgOperationService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.beans.factory.annotation.Autowired;
@@ -37,7 +34,7 @@ public class WeChatMsgOperationServiceImpl implements WeChatMsgOperationService
new GlobalUncaughtExceptionHandler()));
// at消息的微信推送模板id
private final String atMsgPublishTemplateId = "";
private final String atMsgPublishTemplateId = "Xd7sWPZsuWa0UmpvLaZPvaJVjNj1KjEa0zLOm5_Z7IU";
private final String WE_CHAT_MSG_COLOR = "#A349A4";
@@ -96,8 +93,8 @@ public class WeChatMsgOperationServiceImpl implements WeChatMsgOperationService
private List<WxMpTemplateData> generateAtMsgData(User sender, String msg) {
List dataList = new ArrayList<WxMpTemplateData>();
// todo: 没有消息模板,暂不实现
// dataList.add(new WxMpTemplateData("senderName", sender.getName() , WE_CHAT_MSG_COLOR));
// dataList.add(new WxMpTemplateData("content", msg , WE_CHAT_MSG_COLOR));
dataList.add(new WxMpTemplateData("name", sender.getName(), WE_CHAT_MSG_COLOR));
dataList.add(new WxMpTemplateData("content", msg, WE_CHAT_MSG_COLOR));
return dataList;
}
@@ -107,12 +104,12 @@ public class WeChatMsgOperationServiceImpl implements WeChatMsgOperationService
* @param templateMsg 微信模板消息
*/
protected void publishTemplateMsg(WxMpTemplateMessage templateMsg) {
WxMpTemplateMsgService wxMpTemplateMsgService = wxMpService.getTemplateMsgService();
try {
wxMpTemplateMsgService.sendTemplateMsg(templateMsg);
} catch (WxErrorException e) {
log.error("publish we chat msg failed! open id is {}, msg is {}.",
templateMsg.getToUser(), JsonUtils.toStr(templateMsg.getData()));
}
// WxMpTemplateMsgService wxMpTemplateMsgService = wxMpService.getTemplateMsgService();todo 等审核通过
// try {
// wxMpTemplateMsgService.sendTemplateMsg(templateMsg);
// } catch (WxErrorException e) {
// log.error("publish we chat msg failed! open id is {}, msg is {}.",
// templateMsg.getToUser(), JsonUtils.toStr(templateMsg.getData()));
// }
}
}

View File

@@ -0,0 +1,65 @@
package com.abin.mallchat.custom.ac;
import com.abin.mallchat.common.common.algorithm.ac.ACTrie;
import com.abin.mallchat.common.common.algorithm.ac.MatchResult;
import com.google.common.collect.Lists;
import org.junit.Test;
import java.util.List;
import static org.junit.Assert.assertEquals;
/**
* Created by berg on 2023/6/18.
*/
public class ACTrieTest {
private final static List<String> ALPHABET = Lists.newArrayList("abc", "bcd", "cde");
private static ACTrie trie(List<String> keywords) {
return new ACTrie(keywords);
}
@Test
public void test_TextIsLongerThanKeyword() {
final ACTrie trie = trie(ALPHABET);
final String text = " " + ALPHABET.get(0);
List<MatchResult> matchResults = trie.matches(text);
checkResult(matchResults.get(0), 1, 4, ALPHABET.get(0), text);
}
@Test
public void test_VariousKeywordsOneMatch() {
final ACTrie trie = trie(ALPHABET);
final String text = "bcd";
List<MatchResult> matchResults = trie.matches(text);
checkResult(matchResults.get(0), 0, 3, ALPHABET.get(1), text);
}
@Test
public void test_VariousKeywordsMultiMatch() {
final ACTrie trie = trie(ALPHABET);
final String text = "abcd";
List<MatchResult> matchResults = trie.matches(text);
assertEquals(2, matchResults.size());
checkResult(matchResults.get(0), 0, 3, ALPHABET.get(0), text);
checkResult(matchResults.get(1), 1, 4, ALPHABET.get(1), text);
}
@Test
public void test_VariousKeywordsMultiMatch2() {
final ACTrie trie = trie(ALPHABET);
final String text = "abcde";
List<MatchResult> matchResults = trie.matches(text);
assertEquals(3, matchResults.size());
checkResult(matchResults.get(0), 0, 3, ALPHABET.get(0), text);
checkResult(matchResults.get(1), 1, 4, ALPHABET.get(1), text);
checkResult(matchResults.get(2), 2, 5, ALPHABET.get(2), text);
}
private void checkResult(MatchResult matchResult, int expectedStart, int expectedEnd, String expectedKeyword, String text) {
assertEquals("Start of match should have been " + expectedStart, expectedStart, matchResult.getStartIndex());
assertEquals("End of match should have been " + expectedEnd, expectedEnd, matchResult.getEndIndex());
assertEquals(expectedKeyword, text.substring(expectedStart, expectedEnd));
}
}

View File

@@ -0,0 +1,34 @@
package com.abin.mallchat.custom.ac;
import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;
import com.auth0.jwt.interfaces.JWTVerifier;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import java.util.Date;
@Slf4j
public class CreateTokenTest {
@Test
public void create(){
String token = JWT.create()
.withClaim("uid", 10004L) // 只存一个uid信息其他的自己去redis查
.withClaim("createTime", new Date())
.sign(Algorithm.HMAC256("dsfsdfsdfsdfsd")); // signature
log.info("生成的token为 {}",token);
try {
JWTVerifier verifier = JWT.require(Algorithm.HMAC256("dsfsdfsdfsdfsc")).build();
DecodedJWT jwt = verifier.verify(token);
log.info(jwt.getClaims().toString());
} catch (Exception e) {
log.info("decode error,token:{}", token, e);
}
}
}

View File

@@ -0,0 +1,31 @@
package com.abin.mallchat.custom.spring;
import com.abin.mallchat.custom.chat.service.WeChatMsgOperationService;
import com.abin.mallchat.custom.chat.service.impl.ChatServiceImpl;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Collections;
/**
* Description: 微信模板测试
* Author: <a href="https://github.com/zongzibinbin">abin</a>
* Date: 2023-07-06
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class WXTemplate {
@Autowired
private WeChatMsgOperationService chatMsgOperationService;
@Autowired
private ChatServiceImpl chatService;
@Test
public void test() {
chatMsgOperationService.publishChatMsgToWeChatUser(1L, Collections.singletonList(10008L), "你家规下去");
}
}