From 885290e4d0d0c7fad3f538d901c616e49c3d6985 Mon Sep 17 00:00:00 2001
From: 胡仁荣 <897853850@qq.com>
Date: 星期二, 18 七月 2023 18:15:58 +0800
Subject: [PATCH] oss分块上传

---
 src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java |  126 ++++++++++++++++++++++++++++++++----------
 1 files changed, 96 insertions(+), 30 deletions(-)

diff --git a/src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java b/src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java
index 58aa137..b465015 100644
--- a/src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java
+++ b/src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java
@@ -1,13 +1,18 @@
 package com.qxueyou.scc.stucontroller;
 
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
+import java.util.concurrent.TimeUnit;
 
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import com.qxueyou.scc.base.dao.CommonDAO;
+import com.qxueyou.scc.sdk.MTCloud;
+import com.qxueyou.scc.teach.live.model.AccessLog;
+import com.qxueyou.scc.teach.live.utils.RedisCache;
+import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang3.StringUtils;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.CrossOrigin;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
@@ -27,8 +32,10 @@
 import io.swagger.annotations.ApiImplicitParams;
 import io.swagger.annotations.ApiOperation;
 
-@Api(tags="直播接口-学员端")
+@Slf4j
+@Api(tags="鐩存挱鎺ュ彛-瀛﹀憳绔�")
 @RestController
+@CrossOrigin
 @RequestMapping(value = "/stu/live")
 public class StuLiveController {
 	
@@ -40,6 +47,12 @@
 	
 	@Autowired
 	MediaLiveDAO dao;
+
+	@Autowired
+	RedisCache redisCache;
+
+	@Autowired
+	CommonDAO commonDAO;
 	
 	private final static short[] PUBLIC_LIVE_STATUS = new short[] {
 			MediaVideoLive.STATUS_LIVE_LIVE,MediaVideoLive.STATUS_LIVE_PAUSE,
@@ -47,14 +60,14 @@
 	};
 	
 	/**
-	 * 获取直播列表
-	 * @param classId 班级id
+	 * 鑾峰彇鐩存挱鍒楄〃
+	 * @param classId 鐝骇id
 	 * @return
 	 */
-	@ApiOperation(value = "获取直播列表", notes = "")
+	@ApiOperation(value = "鑾峰彇鐩存挱鍒楄〃", notes = "")
     @ApiImplicitParams({
-        @ApiImplicitParam(name = "classId", value = "班级id", required = true, paramType="query", dataType = "String"),
-        @ApiImplicitParam(name = "status", value = "直播状态(2正在直播,5直播中的暂停状态,9停止直播,3回放)", required = true, paramType="query", dataType = "String"),
+        @ApiImplicitParam(name = "classId", value = "鐝骇id", required = true, paramType="query", dataType = "String"),
+        @ApiImplicitParam(name = "status", value = "鐩存挱鐘舵�侊紙2姝e湪鐩存挱锛�5鐩存挱涓殑鏆傚仠鐘舵�侊紝9鍋滄鐩存挱锛�3鍥炴斁锛�", required = true, paramType="query", dataType = "String"),
     })
 	@GetMapping(value = "list")
 	public Result list(String classId, Short status) {
@@ -81,7 +94,7 @@
 				"previewImgUrl","imgPath"
 				));
 		
-		//添加聊天室id
+		//娣诲姞鑱婂ぉ瀹d
 		if(lst!=null && lst.size()>0){
 			for(Map<String,Object> map:lst) {
 				map.put("chatroomId", dao.getChatRoomId((String)map.get("videoLiveId")));
@@ -92,17 +105,17 @@
 	}
 
 	/**
-	 * 获取直播id
+	 * 鑾峰彇鐩存挱id
 	 * @param classId
 	 * @return
 	 */
 	private List<String> getLiveIdLst(String classId) {
 		List<String> liveIds = new ArrayList<String>(5);
 
-		//获取班级列表及班级所在直播列表
+		//鑾峰彇鐝骇鍒楄〃鍙婄彮绾ф墍鍦ㄧ洿鎾垪琛�
 		if(StringUtils.isNotEmpty(classId)) {
 			liveIds = dao.getClassLives(classId);
-		}else { //查询学员所在的全部班级
+		}else { //鏌ヨ瀛﹀憳鎵�鍦ㄧ殑鍏ㄩ儴鐝骇
 			List<ClsClass> clsLst = classService.listMyClass();
 			Set<String> ids = new HashSet<String>();
 			for(ClsClass cls:clsLst) {
@@ -114,23 +127,76 @@
 	}
 	
 	/**
-	 * 查看直播明细
-	 * @param liveId 直播id
+	 * 鏌ョ湅鐩存挱鏄庣粏
+	 * @param couresId 鐩存挱id
 	 * @return
 	 */
 	@GetMapping(value = "view")
-	public Result view(String liveId) {
-			
-		MediaVideoLive live = liveService.read(liveId);
-		
-		return new Result(true, "success", CollectionUtils.newObjectMap(
-				"startTime",live.getStartTime(),
-				"endTime",live.getEndTime(),
-				"name",live.getName(),
-				"hlsPullUrl",live.getHlsPullUrl(),
-				"imgPath",live.getPreviewImgUrl(),
-				"chatroomId",dao.getChatRoomId(liveId),
-				"remark",live.getRemark()
-				));
+	public Result view(String couresId,String userId,String userName,String logId) throws Exception {
+		AccessLog log=new AccessLog();
+		if(StringUtils.isEmpty(logId)){
+//			String ip = IpUtils.getIpAddr(request);
+//			String cityInfo = null;
+//			try {
+//				cityInfo = IpUtils.getCityInfo(ip);
+//			} catch (Exception e) {
+//				log.error("鑾峰彇ip褰掑睘鍦颁俊鎭け璐ワ紒");
+//			}
+			log.setCourseId(couresId);
+			log.setEnterTime(new Date());
+			log.setType("guangxi");
+			log.setName(userName);
+//			log.setIp(ip);
+//			log.setIpAttribution(cityInfo);
+			commonDAO.save(log);
+		}else {
+			String liveUrl = redisCache.getCacheObject("LIVE_URL" + userId);
+			return new Result(true, "success",liveUrl);
+		}
+		MTCloud client = new MTCloud();
+		String userRole = null;
+		HashMap<Object,Object> options = new HashMap<Object, Object>();
+		if(userId.equals("")){
+			userId= randomId();
+		}
+        if(userName.equals("") || userName==null){
+            userName= "娓稿";
+            userRole=MTCloud.ROLE_GUEST;
+        }else {
+			userRole=MTCloud.ROLE_USER;
+		}
+		String res = client.courseAccess(couresId,userId,userName,userRole, 10000, options);
+
+		JSONObject jsonObject = JSON.parseObject(res);
+
+		if(jsonObject.getString("code").equals("0")){
+
+			JSONObject data = jsonObject.getJSONObject("data");
+
+			String liveUrl = (String) data.get("liveUrl");
+			StringBuffer redisLiveUrl = new StringBuffer("LIVE_URL");
+			redisLiveUrl=redisLiveUrl.append(userId);
+			redisCache.setCacheObject(redisLiveUrl.toString(),liveUrl,5, TimeUnit.MINUTES);
+			return new Result(true, "success",liveUrl);
+		}
+
+		return new Result(false, jsonObject.getString("msg"));
 	}
+
+	public  String randomId() {
+			Random random=new Random();
+			String str="";
+			for (int i = 0; i <12; i++) {
+				if(i==0){
+					//棣栦綅涓嶈兘涓�0涓旀暟瀛楀彇鍊煎尯闂翠负 [1,9]
+					str+=(random.nextInt(9)+1);
+				}else{
+					//鍏朵綑浣嶇殑鏁板瓧鐨勫彇鍊煎尯闂翠负 [0,9]
+					str+=random.nextInt(10);
+				}
+			}
+			return str;
+		}
+
+
 }

--
Gitblit v1.8.0