新增视频播放地址api

This commit is contained in:
none 2023-03-20 15:33:20 +08:00
parent d19aa8d2ee
commit 71fcb008ed
7 changed files with 179 additions and 4 deletions

View File

@ -0,0 +1,40 @@
package xyz.playedu.api.bus;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.playedu.api.domain.Course;
import xyz.playedu.api.domain.CourseHour;
import xyz.playedu.api.domain.User;
import xyz.playedu.api.service.CourseService;
import xyz.playedu.api.service.UserService;
import java.util.List;
/**
* @Author 杭州白书科技有限公司
* @create 2023/3/20 14:56
*/
@Component
public class UserBus {
@Autowired
private CourseService courseService;
@Autowired
private UserService userService;
public boolean canSeeCourse(User user, Course course) {
List<Integer> courseDepIds = courseService.getDepIdsByCourseId(course.getId());
if (courseDepIds == null || courseDepIds.size() == 0) {
//线上课无所属部门=>公开课=>任何学员都可以学习
return true;
}
List<Integer> userDepIds = userService.getDepIdsByUserId(user.getId());
if (userDepIds == null || userDepIds.size() == 0) {
return false;
}
return CollectionUtils.intersection(courseDepIds, userDepIds).size() > 0;
}
}

View File

@ -0,0 +1,47 @@
package xyz.playedu.api.caches;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import xyz.playedu.api.bus.UserBus;
import xyz.playedu.api.domain.Course;
import xyz.playedu.api.domain.User;
import xyz.playedu.api.exception.ServiceException;
import xyz.playedu.api.util.RedisUtil;
/**
* @Author 杭州白书科技有限公司
* @create 2023/3/20 15:20
*/
@Component
public class UserCanSeeCourseCache {
@Autowired
private UserBus userBus;
private final static String keyTemplate = "c:%d-u:%d";
private final static int expire = 3600;//s
public boolean check(User user, Course course, boolean isThrow) throws ServiceException {
boolean result;
if (RedisUtil.exists(key(user, course))) {
result = "1".equals(RedisUtil.get(key(user, course)));
} else {
result = userBus.canSeeCourse(user, course);
put(user, course, result);
}
if (isThrow) {
throw new ServiceException("无权限观看");
}
return result;
}
public void put(User user, Course course, boolean result) {
RedisUtil.set(key(user, course), result ? "1" : "0", expire);
}
private String key(User user, Course course) {
return String.format(keyTemplate, course.getId(), user.getId());
}
}

View File

@ -4,13 +4,17 @@ import org.apache.commons.collections4.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.domain.Course;
import xyz.playedu.api.domain.CourseHour;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.service.CourseChapterService;
import xyz.playedu.api.service.CourseHourService;
import xyz.playedu.api.service.CourseService;
import xyz.playedu.api.types.JsonResponse;
import xyz.playedu.api.types.paginate.CoursePaginateFiler;
import xyz.playedu.api.types.paginate.PaginationResult;
import java.util.HashMap;
import java.util.stream.Collectors;
/**
* @Author 杭州白书科技有限公司
@ -23,6 +27,12 @@ public class CourseController {
@Autowired
private CourseService courseService;
@Autowired
private CourseChapterService chapterService;
@Autowired
private CourseHourService hourService;
@GetMapping("/index")
public JsonResponse index(@RequestParam HashMap<String, Object> params) {
Integer page = MapUtils.getInteger(params, "page", 1);
@ -41,12 +51,11 @@ public class CourseController {
@GetMapping("/{id}")
public JsonResponse detail(@PathVariable(name = "id") Integer id) throws NotFoundException {
Course course = courseService.findOrFail(id);
if (course.getIsShow().equals(0)) {
throw new NotFoundException("课程不存在");
}
HashMap<String, Object> data = new HashMap<>();
data.put("course", course);
data.put("course", course);//线上课
data.put("chapters", chapterService.getChaptersByCourseId(course.getId()));//章节
data.put("hours", hourService.getHoursByCourseId(course.getId()).stream().collect(Collectors.groupingBy(CourseHour::getChapterId)));//课时
return JsonResponse.data(data);
}

View File

@ -0,0 +1,62 @@
package xyz.playedu.api.controller.frontend;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xyz.playedu.api.PlayEduFCtx;
import xyz.playedu.api.caches.UserCanSeeCourseCache;
import xyz.playedu.api.domain.Course;
import xyz.playedu.api.domain.CourseHour;
import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.exception.ServiceException;
import xyz.playedu.api.service.CourseHourService;
import xyz.playedu.api.service.CourseService;
import xyz.playedu.api.service.ResourceService;
import xyz.playedu.api.types.JsonResponse;
import java.util.HashMap;
/**
* @Author 杭州白书科技有限公司
* @create 2023/3/20 14:59
*/
@RestController
@RequestMapping("/api/v1/course/{courseId}/hour")
public class HourController {
@Autowired
private CourseService courseService;
@Autowired
private CourseHourService hourService;
@Autowired
private UserCanSeeCourseCache userCanSeeCourseCache;
@Autowired
private ResourceService resourceService;
@GetMapping("/{id}/play")
public JsonResponse play(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException, ServiceException {
Course course = courseService.findOrFail(courseId);
userCanSeeCourseCache.check(PlayEduFCtx.getUser(), course, true);
CourseHour hour = hourService.findOrFail(id, courseId);
Resource resource = resourceService.findOrFail(hour.getRid());
HashMap<String, Object> data = new HashMap<>();
data.put("url", resource.getUrl());//视频播放地址
data.put("extension", resource.getExtension());//视频格式
data.put("duration", resourceService.duration(resource.getId()));//视频时长
return JsonResponse.data(data);
}
@PostMapping("/{id}/record")
public JsonResponse record(@PathVariable(name = "courseId") Integer courseId, @PathVariable(name = "id") Integer id) throws NotFoundException, ServiceException {
Course course = courseService.findOrFail(courseId);
userCanSeeCourseCache.check(PlayEduFCtx.getUser(), course, true);
CourseHour hour = hourService.findOrFail(id, courseId);
return JsonResponse.success();
}
}

View File

@ -49,6 +49,11 @@ public class UserController {
return JsonResponse.data(data);
}
@PutMapping("/avatar")
public JsonResponse changeAvatar() {
return JsonResponse.success();
}
@PutMapping("/password")
public JsonResponse changePassword(@RequestBody @Validated ChangePasswordRequest req) throws ServiceException {
userService.passwordChange(PlayEduFCtx.getUser(), req.getOldPassword(), req.getNewPassword());

View File

@ -33,4 +33,6 @@ public interface ResourceService extends IService<Resource> {
Map<Integer, Integer> getCategoryCount(String type);
Integer total(String type);
Integer duration(Integer id);
}

View File

@ -5,6 +5,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import xyz.playedu.api.domain.Resource;
import xyz.playedu.api.domain.ResourceCategoryRelation;
import xyz.playedu.api.domain.ResourceVideo;
import xyz.playedu.api.exception.NotFoundException;
import xyz.playedu.api.service.ResourceService;
import xyz.playedu.api.mapper.ResourceMapper;
@ -122,6 +123,15 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, Resource> i
public Integer total(String type) {
return Math.toIntExact(count(query().getWrapper().eq("type", type).eq("is_hidden", 0)));
}
@Override
public Integer duration(Integer id) {
ResourceVideo resourceVideo = resourceVideoService.getOne(resourceVideoService.query().getWrapper().eq("rid", id));
if (resourceVideo == null) {
return null;
}
return resourceVideo.getDuration();
}
}