mirror of
https://codeup.aliyun.com/67762337eccfc218f6110e0e/per-boe/java-servers.git
synced 2025-12-10 11:26:50 +08:00
802bug修复
This commit is contained in:
@@ -153,4 +153,6 @@ public interface CacheName {
|
|||||||
* 总点赞排行key
|
* 总点赞排行key
|
||||||
* */
|
* */
|
||||||
String CASE_RANK_PRAISE_ALL="case:rank:praise:all";
|
String CASE_RANK_PRAISE_ALL="case:rank:praise:all";
|
||||||
|
|
||||||
|
String STUDY_KEY = "StudyKey:";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
package com.xboe.school.conf;
|
|
||||||
|
|
||||||
import java.lang.annotation.*;
|
|
||||||
|
|
||||||
@Documented
|
|
||||||
@Target(ElementType.METHOD)
|
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
|
||||||
public @interface LimitRequest {
|
|
||||||
|
|
||||||
long time() default 2000;
|
|
||||||
int count() default Integer.MAX_VALUE;
|
|
||||||
}
|
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
package com.xboe.school.conf;
|
|
||||||
|
|
||||||
|
|
||||||
import com.xboe.api.vo.Result;
|
|
||||||
import net.jodah.expiringmap.ExpirationPolicy;
|
|
||||||
import net.jodah.expiringmap.ExpiringMap;
|
|
||||||
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.springframework.stereotype.Component;
|
|
||||||
import org.springframework.web.context.request.RequestAttributes;
|
|
||||||
import org.springframework.web.context.request.RequestContextHolder;
|
|
||||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
||||||
|
|
||||||
import javax.servlet.http.HttpServletRequest;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
import java.util.concurrent.TimeUnit;
|
|
||||||
|
|
||||||
@Aspect
|
|
||||||
@Component
|
|
||||||
public class LimitRequestAspect {
|
|
||||||
private static ConcurrentHashMap<String, ExpiringMap<String, Integer>> book = new ConcurrentHashMap<>();
|
|
||||||
// 定义切点
|
|
||||||
// 让所有有@LimitRequest注解的方法都执行切面方法
|
|
||||||
@Pointcut("@annotation(limitRequest)")
|
|
||||||
public void excudeService(LimitRequest limitRequest) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Around("excudeService(limitRequest)")
|
|
||||||
public Object doAround(ProceedingJoinPoint pjp, LimitRequest limitRequest) throws Throwable {
|
|
||||||
// 获得request对象
|
|
||||||
RequestAttributes ra = RequestContextHolder.getRequestAttributes();
|
|
||||||
ServletRequestAttributes sra = (ServletRequestAttributes) ra;
|
|
||||||
HttpServletRequest request = sra.getRequest();
|
|
||||||
|
|
||||||
// 获取Map对象, 如果没有则返回默认值
|
|
||||||
// 第一个参数是key, 第二个参数是默认值
|
|
||||||
ExpiringMap<String, Integer> map = book.getOrDefault(request.getRequestURI(), ExpiringMap.builder().variableExpiration().build());
|
|
||||||
Integer uCount = map.getOrDefault(request.getRemoteAddr(), 0);
|
|
||||||
|
|
||||||
|
|
||||||
if (uCount >= limitRequest.count()) { // 超过次数,不执行目标方法
|
|
||||||
//这里的返回对象类型根据controller方法的返回方式一致
|
|
||||||
return Result.error("请求过于频繁,请稍后再试");
|
|
||||||
} else if (uCount == 0){ // 第一次请求时,设置开始有效时间
|
|
||||||
map.put(request.getRemoteAddr(), uCount + 1, ExpirationPolicy.CREATED, limitRequest.time(), TimeUnit.MILLISECONDS);
|
|
||||||
} else { // 未超过次数, 记录数据加一
|
|
||||||
map.put(request.getRemoteAddr(), uCount + 1);
|
|
||||||
}
|
|
||||||
book.put(request.getRequestURI(), map);
|
|
||||||
|
|
||||||
// result的值就是被拦截方法的返回值
|
|
||||||
Object result = pjp.proceed();
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -6,6 +6,7 @@ import java.util.concurrent.TimeUnit;
|
|||||||
|
|
||||||
import com.alibaba.nacos.shaded.com.google.common.util.concurrent.RateLimiter;
|
import com.alibaba.nacos.shaded.com.google.common.util.concurrent.RateLimiter;
|
||||||
import com.xboe.api.ThirdApi;
|
import com.xboe.api.ThirdApi;
|
||||||
|
import com.xboe.constants.CacheName;
|
||||||
import com.xboe.module.course.vo.TeacherVo;
|
import com.xboe.module.course.vo.TeacherVo;
|
||||||
import com.xboe.module.usergroup.service.IUserGroupService;
|
import com.xboe.module.usergroup.service.IUserGroupService;
|
||||||
import com.xboe.school.aspect.LimitRequest;
|
import com.xboe.school.aspect.LimitRequest;
|
||||||
@@ -295,7 +296,7 @@ public class StudyCourseApi extends ApiBaseController{
|
|||||||
* @param
|
* @param
|
||||||
* @return 返回学习条目的id
|
* @return 返回学习条目的id
|
||||||
*/
|
*/
|
||||||
@LimitRequest(count=5)
|
//@LimitRequest(count=5)
|
||||||
@PostMapping("/study")
|
@PostMapping("/study")
|
||||||
public JsonResponse<String> study(@RequestBody StudyContentDto sci, HttpServletRequest request){
|
public JsonResponse<String> study(@RequestBody StudyContentDto sci, HttpServletRequest request){
|
||||||
|
|
||||||
@@ -330,17 +331,33 @@ public class StudyCourseApi extends ApiBaseController{
|
|||||||
new Thread(() -> example.someApi()).start();
|
new Thread(() -> example.someApi()).start();
|
||||||
//检查是否已存在
|
//检查是否已存在
|
||||||
}
|
}
|
||||||
|
|
||||||
StudyCourseItem item = studyService.checkHas(sci.getStudyId(),sci.getContentId());
|
StudyCourseItem item = studyService.checkHas(sci.getStudyId(),sci.getContentId());
|
||||||
if(item!=null) {
|
if(item!=null) {
|
||||||
//如果记录存在,但是进度不100无成情况,就更新进度,一期不会有这种情况
|
String studyKey = CacheName.NAME_AUTH + ":" + CacheName.STUDY_KEY + item.getCourseId()+":"+cuser.getAccountId()+":"+item.getContentId();
|
||||||
if(item.getProgress()<100 && sci.getProgress()>item.getProgress()) {
|
String studyKey2 = CacheName.NAME_AUTH + ":" + CacheName.STUDY_KEY + sci.getCourseId()+":"+cuser.getAccountId()+":"+sci.getContentId();
|
||||||
studyService.updateProcess(item.getId(), sci.getStudyId(), sci.getCourseId(), sci.getContentTotal(), sci.getProgress(),token);
|
redisTemplate.opsForValue().set(studyKey,
|
||||||
|
String.valueOf(item.getProgress()), 2, TimeUnit.HOURS);
|
||||||
|
String progressStr = redisTemplate.opsForValue().get(studyKey2);
|
||||||
|
if (progressStr != null && !progressStr.isEmpty()) {
|
||||||
|
// 尝试将 Redis 中的字符串转换为整数
|
||||||
|
int redisProgress = Integer.parseInt(progressStr);
|
||||||
|
// 假设 item.getProgress() 返回的是 int 类型
|
||||||
|
int sciProgress = sci.getProgress();
|
||||||
|
|
||||||
|
if (redisProgress < sciProgress && redisProgress < 100) {
|
||||||
|
// 执行一些操作
|
||||||
|
// if(item.getProgress()<100 && sci.getProgress()>item.getProgress()) {
|
||||||
|
// }
|
||||||
|
studyService.updateProcess(item.getId(), sci.getStudyId(), sci.getCourseId(), sci.getContentTotal(), sci.getProgress(),token);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
//追加学习时长
|
//追加学习时长
|
||||||
studyService.appendStudyDuration(sci.getStudyId(),item.getId(),sci.getContentId(),sci.getDuration());
|
studyService.appendStudyDuration(sci.getStudyId(),item.getId(),sci.getContentId(),sci.getDuration());
|
||||||
List<StudyCourse> allUserList = thirdApi.getStudyCourseList(sci.getStudyId() ,sci.getCourseId(), token);
|
List<StudyCourse> allUserList = thirdApi.getStudyCourseList(sci.getStudyId() ,sci.getCourseId(), token);
|
||||||
log.info("在线课学习记录"+allUserList);
|
log.info("在线课学习记录"+allUserList);
|
||||||
return success(item.getId());
|
return success(item.getId());
|
||||||
|
//如果记录存在,但是进度不100无成情况,就更新进度,一期不会有这种情况
|
||||||
}
|
}
|
||||||
|
|
||||||
if(StringUtils.isBlank(sci.getCourseId())){
|
if(StringUtils.isBlank(sci.getCourseId())){
|
||||||
|
|||||||
Reference in New Issue
Block a user