Commit 22dd17ce authored by alex yao's avatar alex yao

feat:收藏冪等

parent 24c5e077
......@@ -16,6 +16,7 @@ import cn.com.poc.agent_application.service.BizAgentApplicationCategoryService;
import cn.com.poc.agent_application.service.BizAgentApplicationMallService;
import cn.com.poc.agent_application.service.BizAgentApplicationPublishService;
import cn.com.poc.agent_application.service.BizMemberAgentApplicationCollectService;
import cn.com.poc.common.annotation.RedisLimit;
import cn.com.poc.common.constant.CommonConstant;
import cn.com.poc.common.constant.XLangConstant;
import cn.com.poc.common.utils.StringUtils;
......@@ -28,6 +29,7 @@ import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
@Component
public class BizAgentApplicationMallRestImpl implements BizAgentApplicationMallRest {
......@@ -115,14 +117,14 @@ public class BizAgentApplicationMallRestImpl implements BizAgentApplicationMallR
for (MallAgentApplicationQueryItem item : items) {
BizAgentApplicationMallDto mallDto = BizAgentApplicationMallConvert.itemToDto(item);
// 如果应用上架了
if (CommonConstant.IsDeleted.Y.equals(mallDto.getIsSale())) {
if (CommonConstant.YOrN.Y.equals(mallDto.getIsSale())) {
// 设置当前用户是否收藏了
if (mallDto.getBaseInfo() != null) {
BizMemberAgentApplicationCollectEntity collect = bizMemberAgentApplicationCollectService.getByAgentId(mallDto.getBaseInfo().getAgentId());
if (collect != null) {
mallDto.setIsCollect(collect.getIsCollect());
} else {
mallDto.setIsCollect(CommonConstant.IsDeleted.N);
mallDto.setIsCollect(CommonConstant.YOrN.N);
}
}
resultList.add(mallDto);
......@@ -143,6 +145,7 @@ public class BizAgentApplicationMallRestImpl implements BizAgentApplicationMallR
}
@Override
@RedisLimit(currentUser = true, key = "collect:agent", count = 1, timeout = 1, exceptionInfo = "exception/collect.limit.message")
public void collectOrCancelAgentInMall(Integer id) throws Exception {
Assert.notNull(id);
agentApplicationMallService.collectOrCancelAgentInMall(id);
......
package cn.com.poc.common.annotation;
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
/**
* @author Ken
*/
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface CurrentUserRolePermission {
String[] permissionCodeArr();
String prompt() default "无权限,请联系管理员";
}
package cn.com.poc.common.annotation;
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
/**
* 限流
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Component
@Documented
public @interface RedisLimit {
/**
* 限流key
*/
String key();
/**
* 限流数量 [单位时间内次数]
*/
int count();
/**
* 限流时间 默认60秒
*/
long timeout() default 60;
/**
* 限流时间单位 [默认秒]
*/
LimitTimeUnit timeUnit() default LimitTimeUnit.SECONDS;
/**
* 是否针对当前用户
*
* @return
*/
boolean currentUser() default false;
/**
* 异常信息
*/
String exceptionInfo() default "";
enum LimitTimeUnit {
/**
* 秒
*/
SECONDS,
/**
* 分钟
*/
MINUTES,
/**
* 小时
*/
HOURS,
/**
* 天
*/
DAYS,
/**
* 当日
*/
DAY_OF_MONTH,
/**
* 当月
*/
MONTH_OF_YEAR;
LimitTimeUnit(){}
}
}
package cn.com.poc.common.aspect;
import cn.com.poc.common.annotation.RedisLimit;
import cn.com.poc.common.utils.BlContext;
import cn.com.poc.common.utils.DateUtils;
import cn.com.poc.support.security.oauth.entity.UserBaseEntity;
import cn.com.yict.framemax.core.exception.BusinessException;
import cn.com.yict.framemax.core.i18n.I18nMessageException;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
* 限流切面
*/
@Aspect
@Component
public class RedisLimitAspect {
private final Logger logger = LoggerFactory.getLogger(RedisLimitAspect.class);
@Resource
private RedisTemplate<String, Integer> redisTemplate;
@Pointcut("@annotation( cn.com.poc.common.annotation.RedisLimit)")
public void redisLimitAnnotation() {
}
@Around(value = "redisLimitAnnotation()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
RedisLimit annotation = method.getAnnotation(RedisLimit.class);
StringBuilder redisKey = new StringBuilder();
String key = annotation.key();
redisKey.append("Limit_");
redisKey.append(key);
redisKey.append(":");
if (annotation.currentUser()) {
UserBaseEntity currentUser = BlContext.getCurrentUserNotException();
redisKey.append(currentUser.getUserId());
}
if (Boolean.FALSE.equals(redisTemplate.hasKey(redisKey.toString()))) {
redisTemplate.opsForValue().increment(redisKey.toString(), 1);
redisTemplate.expire(redisKey.toString(), expireTime(annotation.timeout(), annotation.timeUnit()), TimeUnit.MILLISECONDS);
} else if (redisTemplate.opsForValue().get(redisKey.toString()).intValue() >= annotation.count()) {
throw new I18nMessageException(annotation.exceptionInfo());
} else {
redisTemplate.opsForValue().increment(redisKey.toString(), 1);
}
return joinPoint.proceed();
}
private Long expireTime(Long timeout, RedisLimit.LimitTimeUnit limitTimeUnit) {
switch (limitTimeUnit) {
case SECONDS:
return timeout * 1000;
case MINUTES:
return timeout * 60 * 1000;
case HOURS:
return timeout * 60 * 60 * 1000;
case DAYS:
Date date = new Date();
return DateUtils.diffTwoDate(DateUtils.addDays(date, timeout.intValue()), date);
case DAY_OF_MONTH:
Date dayBegin = DateUtils.getDayBegin(DateUtils.addDays(DateUtils.getToday(), timeout.intValue()));
return DateUtils.diffTwoDate(dayBegin, DateUtils.getToday());
case MONTH_OF_YEAR:
Date monthBegin = DateUtils.getMonthBegin(DateUtils.getMonthAfter(DateUtils.getToday(), timeout.intValue()));
return DateUtils.diffTwoDate(monthBegin, DateUtils.getToday());
default:
throw new BusinessException("不支持的单位");
}
}
}
......@@ -67,4 +67,5 @@ call.failure=Call failure
model.not.exist=Model not exist
upload.more.than.10m=The uploaded file cannot exceed 10M file name
error.file.content.is.null=Incorrect file, file contents cannot be empty, file name
file.content.more.than.100w=The number of characters in a file cannot exceed 100w
\ No newline at end of file
file.content.more.than.100w=The number of characters in a file cannot exceed 100w
collect.limit.message=Click too fast, do not repeat the operation
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment