Commit a35eee80 by 吕明尚

验券增加幂等

parent b491437e
......@@ -176,6 +176,12 @@
<artifactId>xstream</artifactId>
<version>1.4.9</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
package share.common.utils.aop;
import java.lang.annotation.*;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface RepeatSubmit {
String key() default "";
}
package share.common.utils.aop;
import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
@Aspect
@Component
public class RepeatSubmitAspect {
private static final Cache<String, Object> CACHES = CacheBuilder.newBuilder()
// 最大缓存 设置为1000个
.maximumSize(1000)
// 设置写缓存后1s过期
.expireAfterWrite(1, TimeUnit.SECONDS)
.build();
@Around("execution(public * *(..)) && @annotation(share.common.utils.aop.RepeatSubmit))")
public Object interceptor(ProceedingJoinPoint pjp) {
MethodSignature signature = (MethodSignature) pjp.getSignature();
Method method = signature.getMethod();
RepeatSubmit repeatSubmit = method.getAnnotation(RepeatSubmit.class);
String key = getKey(repeatSubmit.key(), pjp.getArgs());
if (StringUtils.isNotBlank(key)) {
if (CACHES.getIfPresent(key) != null) {
throw new RuntimeException("请勿重复请求");
}
// 如果是第一次请求,就将key 当前对象压入缓存中
CACHES.put(key, key);
}
try {
return pjp.proceed();
} catch (Throwable throwable) {
throw new RuntimeException("服务器异常!");
}
}
/**
* key 的生成策略
*
* @param keyExpress 表达式
* @param args 参数
* @return 生成的key
*/
private String getKey(String keyExpress, Object[] args) {
for (int i = 0; i < args.length; i++) {
keyExpress = keyExpress.replace("arg[" + i + "]", args[i].toString());
}
return keyExpress;
}
}
......@@ -32,6 +32,7 @@ import share.common.core.redis.RedisUtil;
import share.common.enums.*;
import share.common.exception.base.BaseException;
import share.common.utils.DateUtils;
import share.common.utils.aop.RepeatSubmit;
import share.system.domain.SConsumer;
import share.system.domain.SConsumerCoupon;
import share.system.domain.SCoupon;
......@@ -100,6 +101,7 @@ public class QPServiceImpl implements QPService {
* 用户验卷接口
*/
@Override
@RepeatSubmit(key = "美团验卷")
public String consumeByUser(String code, String storeId, String status) throws Exception {
//获取用户信息
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
......@@ -206,7 +208,7 @@ public class QPServiceImpl implements QPService {
}
isConsumerCouponService.insertSConsumerCoupon(sConsumerCoupon);
//核销美团券
qpService.consume(code.trim(), 1, sStore.getOpenShopUuid(), status);
// qpService.consume(code.trim(), 1, sStore.getOpenShopUuid(), status);
return "验劵成功";
}
......
......@@ -20,6 +20,7 @@ import org.springframework.util.CollectionUtils;
import share.common.core.redis.RedisUtil;
import share.common.enums.*;
import share.common.exception.base.BaseException;
import share.common.utils.aop.RepeatSubmit;
import share.system.domain.SConsumer;
import share.system.domain.SConsumerCoupon;
import share.system.domain.SCoupon;
......@@ -351,6 +352,7 @@ public class TiktokServiceImpl implements TiktokService {
}
@Override
@RepeatSubmit(key = "抖音验卷")
public String consumeByUser(String code, String storeId) throws Exception {
SConsumer user = FrontTokenComponent.getWxSConsumerEntry();
if (ObjectUtil.isNull(user)) {
......
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="AdditionalModuleElements">
<content url="file://$MODULE_DIR$" dumb="true">
<excludeFolder url="file://$MODULE_DIR$/target" />
</content>
</component>
</module>
\ 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