This commit is contained in:
none 2023-03-22 14:49:33 +08:00
parent 39f46d5ace
commit d621cc5937
19 changed files with 57 additions and 59 deletions

View File

@ -6,7 +6,7 @@ import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
public class PlayEduBCtx {
public class BCtx {
private static final java.lang.ThreadLocal<LinkedHashMap<String, Object>> THREAD_LOCAL = new java.lang.ThreadLocal<>();
@ -15,7 +15,7 @@ public class PlayEduBCtx {
public final static String KEY_ADMIN_PER = "admin_per";
public final static String KEY_CONFIG = "config";
public PlayEduBCtx() {
public BCtx() {
}
private static void put(String key, Object val) {

View File

@ -8,14 +8,14 @@ import java.util.LinkedHashMap;
* @Author 杭州白书科技有限公司
* @create 2023/3/13 09:24
*/
public class PlayEduFCtx {
public class FCtx {
private static final java.lang.ThreadLocal<LinkedHashMap<String, Object>> THREAD_LOCAL = new java.lang.ThreadLocal<>();
private static final String KEY_USER_ID = "user_id";
private static final String KEY_USER = "user";
private static final String KEY_JWT_JTI = "jwt_jti";
public PlayEduFCtx() {
public FCtx() {
}
private static void put(String key, Object val) {

View File

@ -2,7 +2,7 @@ package xyz.playedu.api.bus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.AdminRole;
import xyz.playedu.api.service.AdminPermissionService;
@ -54,7 +54,7 @@ public class BackendBus {
}
public static String valueHidden(String permissionSlug, String type, String value) {
HashMap<String, Boolean> permissions = PlayEduBCtx.getAdminPer();
HashMap<String, Boolean> permissions = BCtx.getAdminPer();
if (permissions.get(permissionSlug) != null) {
return value;
}
@ -75,7 +75,7 @@ public class BackendBus {
if (superRole == null) {
return false;
}
List<Integer> roleIds = adminUserService.getRoleIdsByUserId(PlayEduBCtx.getAdminUserID());
List<Integer> roleIds = adminUserService.getRoleIdsByUserId(BCtx.getAdminUserID());
if (roleIds.size() == 0) {
return false;
}

View File

@ -1,17 +1,15 @@
package xyz.playedu.api.bus;
import lombok.SneakyThrows;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import xyz.playedu.api.PlayEduFCtx;
import xyz.playedu.api.FCtx;
import xyz.playedu.api.caches.UserLastLearnTimeCache;
import xyz.playedu.api.domain.Course;
import xyz.playedu.api.domain.CourseHour;
import xyz.playedu.api.domain.User;
import xyz.playedu.api.event.UserLearnCourseUpdateEvent;
import xyz.playedu.api.exception.ServiceException;
import xyz.playedu.api.service.CourseService;
import xyz.playedu.api.service.UserService;
@ -53,7 +51,7 @@ public class UserBus {
Long curTime = System.currentTimeMillis();
// 最近一次学习时间
Long lastTime = userLastLearnTimeCache.get(PlayEduFCtx.getUserId());
Long lastTime = userLastLearnTimeCache.get(FCtx.getUserId());
// 最大周期为10s
if (lastTime == null || curTime - lastTime > 10000) {
lastTime = curTime - 10000;

View File

@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.domain.CourseChapter;
import xyz.playedu.api.event.CourseChapterDestroyEvent;
@ -63,7 +63,7 @@ public class CourseChapterController {
return JsonResponse.error("当前章节下面存在课时无法删除");
}
chapterService.removeById(chapter.getId());
ctx.publishEvent(new CourseChapterDestroyEvent(this, PlayEduBCtx.getAdminUserID(), chapter.getCourseId(), chapter.getId()));
ctx.publishEvent(new CourseChapterDestroyEvent(this, BCtx.getAdminUserID(), chapter.getCourseId(), chapter.getId()));
return JsonResponse.success();
}

View File

@ -7,7 +7,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.domain.*;
import xyz.playedu.api.event.CourseDestroyEvent;
@ -191,7 +191,7 @@ public class CourseController {
@DeleteMapping("/{id}")
public JsonResponse destroy(@PathVariable(name = "id") Integer id) {
courseService.removeById(id);
ctx.publishEvent(new CourseDestroyEvent(this, PlayEduBCtx.getAdminUserID(), id));
ctx.publishEvent(new CourseDestroyEvent(this, BCtx.getAdminUserID(), id));
return JsonResponse.success();
}

View File

@ -6,7 +6,7 @@ import org.springframework.context.ApplicationContext;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.CourseChapter;
@ -87,7 +87,7 @@ public class CourseHourController {
}
CourseHour courseHour = hourService.create(courseId, chapterId, req.getSort(), req.getTitle(), type, req.getRid(), req.getDuration());
ctx.publishEvent(new CourseHourCreatedEvent(this, PlayEduBCtx.getAdminUserID(), courseHour.getCourseId(), courseHour.getChapterId(), courseHour.getId()));
ctx.publishEvent(new CourseHourCreatedEvent(this, BCtx.getAdminUserID(), courseHour.getCourseId(), courseHour.getChapterId(), courseHour.getId()));
return JsonResponse.success();
}
@ -125,7 +125,7 @@ public class CourseHourController {
// 只需要发布一次event就可以了
CourseHour firstHour = hours.get(0);
ctx.publishEvent(new CourseHourCreatedEvent(this, PlayEduBCtx.getAdminUserID(), firstHour.getCourseId(), firstHour.getChapterId(), firstHour.getId()));
ctx.publishEvent(new CourseHourCreatedEvent(this, BCtx.getAdminUserID(), firstHour.getCourseId(), firstHour.getChapterId(), firstHour.getId()));
return JsonResponse.success();
}
@ -154,7 +154,7 @@ public class CourseHourController {
public JsonResponse destroy(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException {
CourseHour courseHour = hourService.findOrFail(id, courseId);
hourService.removeById(courseHour.getId());
ctx.publishEvent(new CourseHourDestroyEvent(this, PlayEduBCtx.getAdminUserID(), courseHour.getCourseId(), courseHour.getChapterId(), courseHour.getId()));
ctx.publishEvent(new CourseHourDestroyEvent(this, BCtx.getAdminUserID(), courseHour.getCourseId(), courseHour.getChapterId(), courseHour.getId()));
return JsonResponse.success();
}

View File

@ -5,7 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.domain.Department;
import xyz.playedu.api.event.DepartmentDestroyEvent;
@ -117,7 +117,7 @@ public class DepartmentController {
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
Department department = departmentService.findOrFail(id);
departmentService.deleteById(department.getId());
ctx.publishEvent(new DepartmentDestroyEvent(this, PlayEduBCtx.getAdminUserID(), department.getId()));
ctx.publishEvent(new DepartmentDestroyEvent(this, BCtx.getAdminUserID(), department.getId()));
return JsonResponse.success();
}

View File

@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.bus.BackendBus;
import xyz.playedu.api.constant.SystemConstant;
import xyz.playedu.api.domain.AdminUser;
@ -74,7 +74,7 @@ public class LoginController {
@GetMapping("/detail")
public JsonResponse detail() {
AdminUser user = PlayEduBCtx.getAdminUser();
AdminUser user = BCtx.getAdminUser();
HashMap<String, Boolean> permissions = backendBus.adminUserPermissions(user.getId());
HashMap<String, Object> data = new HashMap<>();
@ -86,7 +86,7 @@ public class LoginController {
@PutMapping("/password")
public JsonResponse changePassword(@RequestBody @Validated PasswordChangeRequest req) {
AdminUser user = PlayEduBCtx.getAdminUser();
AdminUser user = BCtx.getAdminUser();
String password = HelperUtil.MD5(req.getOldPassword() + user.getSalt());
if (!password.equals(user.getPassword())) {
return JsonResponse.error("原密码不正确");

View File

@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.Resource;
@ -124,7 +124,7 @@ public class ResourceCategoryController {
public JsonResponse destroy(@PathVariable Integer id) throws NotFoundException {
ResourceCategory category = categoryService.findOrFail(id);
categoryService.deleteById(category.getId());
ctx.publishEvent(new ResourceCategoryDestroyEvent(this, PlayEduBCtx.getAdminUserID(), category.getId()));
ctx.publishEvent(new ResourceCategoryDestroyEvent(this, BCtx.getAdminUserID(), category.getId()));
return JsonResponse.success();
}

View File

@ -4,7 +4,7 @@ import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.bus.BackendBus;
import xyz.playedu.api.constant.BPermissionConstant;
import xyz.playedu.api.constant.BackendConstant;
@ -70,7 +70,7 @@ public class ResourceController {
filter.setName(name);
if (!backendBus.isSuperAdmin()) {// 非超管只能读取它自己上传的资源
filter.setAdminId(PlayEduBCtx.getAdminUserID());
filter.setAdminId(BCtx.getAdminUserID());
}
PaginationResult<Resource> result = resourceService.paginate(page, size, filter);

View File

@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.service.ImageCaptchaService;
import xyz.playedu.api.types.ImageCaptchaResult;
import xyz.playedu.api.types.JsonResponse;
@ -33,7 +33,7 @@ public class SystemController {
@GetMapping("/config")
public JsonResponse config() {
Map<String, String> data = PlayEduBCtx.getConfig();
Map<String, String> data = BCtx.getConfig();
return JsonResponse.data(data);
}

View File

@ -6,7 +6,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.constant.BackendConstant;
import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.exception.ServiceException;
@ -40,7 +40,7 @@ public class UploadController {
@PostMapping("/minio")
public JsonResponse uploadMinio(@RequestParam HashMap<String, Object> params, MultipartFile file) throws ServiceException {
String categoryIds = MapUtils.getString(params, "category_ids");
Resource res = uploadService.storeMinio(PlayEduBCtx.getAdminUserID(), file, categoryIds);
Resource res = uploadService.storeMinio(BCtx.getAdminUserID(), file, categoryIds);
return JsonResponse.data(res);
}
@ -92,7 +92,7 @@ public class UploadController {
// 视频素材保存
Resource videoResource = resourceService.create(
PlayEduBCtx.getAdminUserID(),
BCtx.getAdminUserID(),
req.getCategoryIds(),
type,
req.getOriginalFilename(),
@ -104,7 +104,7 @@ public class UploadController {
url
);
// 视频封面素材保存
Resource posterResource = uploadService.storeBase64Image(PlayEduBCtx.getAdminUserID(), req.getPoster(), null);
Resource posterResource = uploadService.storeBase64Image(BCtx.getAdminUserID(), req.getPoster(), null);
// 视频的封面素材改为[隐藏 && 属于视频的子素材]
resourceService.changeParentId(posterResource.getId(), videoResource.getId());
// 视频信息

View File

@ -4,7 +4,7 @@ import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduFCtx;
import xyz.playedu.api.FCtx;
import xyz.playedu.api.bus.UserBus;
import xyz.playedu.api.caches.CourseCache;
import xyz.playedu.api.caches.UserCanSeeCourseCache;
@ -48,7 +48,7 @@ public class HourController {
@SneakyThrows
public JsonResponse play(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) {
Course course = courseCache.findOrFail(courseId);
userCanSeeCourseCache.check(PlayEduFCtx.getUser(), course, true);
userCanSeeCourseCache.check(FCtx.getUser(), course, true);
CourseHour hour = hourService.findOrFail(id, courseId);
Resource resource = resourceService.findOrFail(hour.getRid());
@ -67,7 +67,7 @@ public class HourController {
if (duration <= 0) {
return JsonResponse.error("duration参数错误");
}
User user = PlayEduFCtx.getUser();
User user = FCtx.getUser();
// 线上课检测
Course course = courseCache.findOrFail(courseId);
// 权限校验
@ -85,8 +85,8 @@ public class HourController {
public JsonResponse ping(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) {
Course course = courseCache.findOrFail(courseId);
CourseHour hour = hourService.findOrFail(id, courseId);
userCanSeeCourseCache.check(PlayEduFCtx.getUser(), course, true);
userBus.userLearnDurationRecord(PlayEduFCtx.getUser(), course, hour);
userCanSeeCourseCache.check(FCtx.getUser(), course, true);
userBus.userLearnDurationRecord(FCtx.getUser(), course, hour);
return JsonResponse.success();
}

View File

@ -7,7 +7,7 @@ import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xyz.playedu.api.PlayEduFCtx;
import xyz.playedu.api.FCtx;
import xyz.playedu.api.constant.SystemConstant;
import xyz.playedu.api.domain.User;
import xyz.playedu.api.event.UserLoginEvent;
@ -71,7 +71,7 @@ public class LoginController {
@PostMapping("/logout")
public JsonResponse logout() throws JwtLogoutException {
jwtService.userLogout(RequestUtil.token());
ctx.publishEvent(new UserLogoutEvent(this, PlayEduFCtx.getUserId(), PlayEduFCtx.getJwtJti()));
ctx.publishEvent(new UserLogoutEvent(this, FCtx.getUserId(), FCtx.getJwtJti()));
return JsonResponse.success();
}

View File

@ -4,7 +4,7 @@ import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduFCtx;
import xyz.playedu.api.FCtx;
import xyz.playedu.api.domain.Course;
import xyz.playedu.api.domain.Department;
import xyz.playedu.api.domain.User;
@ -39,7 +39,7 @@ public class UserController {
@GetMapping("/detail")
public JsonResponse detail() {
User user = PlayEduFCtx.getUser();
User user = FCtx.getUser();
List<Department> departments = departmentService.listByIds(userService.getDepIdsByUserId(user.getId()));
HashMap<String, Object> data = new HashMap<>();
@ -56,7 +56,7 @@ public class UserController {
@PutMapping("/password")
public JsonResponse changePassword(@RequestBody @Validated ChangePasswordRequest req) throws ServiceException {
userService.passwordChange(PlayEduFCtx.getUser(), req.getOldPassword(), req.getNewPassword());
userService.passwordChange(FCtx.getUser(), req.getOldPassword(), req.getNewPassword());
return JsonResponse.success();
}
@ -67,7 +67,7 @@ public class UserController {
return JsonResponse.error("请选择部门");
}
List<Integer> userJoinDepIds = userService.getDepIdsByUserId(PlayEduFCtx.getUserId());
List<Integer> userJoinDepIds = userService.getDepIdsByUserId(FCtx.getUserId());
if (userJoinDepIds == null) {
return JsonResponse.error("当前学员未加入任何部门");
}

View File

@ -6,7 +6,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.bus.AppBus;
import xyz.playedu.api.bus.BackendBus;
import xyz.playedu.api.constant.SystemConstant;
@ -49,7 +49,7 @@ public class AdminMiddleware implements HandlerInterceptor {
// 读取全局配置
Map<String, String> systemConfig = configService.keyValues();
PlayEduBCtx.setConfig(systemConfig);
BCtx.setConfig(systemConfig);
if (BackendBus.inUnAuthWhitelist(request.getRequestURI())) {
return HandlerInterceptor.super.preHandle(request, response, handler);
@ -71,9 +71,9 @@ public class AdminMiddleware implements HandlerInterceptor {
return responseTransform(response, 403, "当前管理员禁止登录");
}
PlayEduBCtx.setAdminUserId(payload.getSub());
PlayEduBCtx.setAdminUser(adminUser);
PlayEduBCtx.setAdminPer(backendBus.adminUserPermissions(adminUser.getId()));
BCtx.setAdminUserId(payload.getSub());
BCtx.setAdminUser(adminUser);
BCtx.setAdminPer(backendBus.adminUserPermissions(adminUser.getId()));
return HandlerInterceptor.super.preHandle(request, response, handler);
} catch (Exception e) {
@ -93,7 +93,7 @@ public class AdminMiddleware implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
PlayEduBCtx.remove();
BCtx.remove();
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}

View File

@ -6,7 +6,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import xyz.playedu.api.PlayEduFCtx;
import xyz.playedu.api.FCtx;
import xyz.playedu.api.constant.FrontendConstant;
import xyz.playedu.api.constant.SystemConstant;
import xyz.playedu.api.domain.User;
@ -59,10 +59,10 @@ public class FrontMiddleware implements HandlerInterceptor {
return responseTransform(response, 403, "当前学员已锁定无法登录");
}
PlayEduFCtx.setUserId(user.getId());
PlayEduFCtx.setUser(user);
PlayEduFCtx.setJWtJti(token);
PlayEduFCtx.setJWtJti(payload.getJti());
FCtx.setUserId(user.getId());
FCtx.setUser(user);
FCtx.setJWtJti(token);
FCtx.setJWtJti(payload.getJti());
return HandlerInterceptor.super.preHandle(request, response, handler);
} catch (Exception e) {
@ -80,7 +80,7 @@ public class FrontMiddleware implements HandlerInterceptor {
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
PlayEduFCtx.remove();
FCtx.remove();
HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
}
}

View File

@ -8,7 +8,7 @@ import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.playedu.api.PlayEduBCtx;
import xyz.playedu.api.BCtx;
import xyz.playedu.api.bus.BackendBus;
import xyz.playedu.api.middleware.BackendPermissionMiddleware;
import xyz.playedu.api.types.JsonResponse;
@ -35,7 +35,7 @@ public class BackendPermissionMiddlewareImpl {
public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
BackendPermissionMiddleware middleware = signature.getMethod().getAnnotation(BackendPermissionMiddleware.class);
Integer adminUserId = PlayEduBCtx.getAdminUserID();
Integer adminUserId = BCtx.getAdminUserID();
HashMap<String, Boolean> permissions = backendBus.adminUserPermissions(adminUserId);
if (permissions.get(middleware.slug()) == null) {
return JsonResponse.error("权限不足", 403);