| | |
| | | package com.qxueyou.scc.stucontroller; |
| | | |
| | | 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.base.model.Pager; |
| | | import com.qxueyou.scc.config.IpUtils; |
| | | import com.qxueyou.scc.sdk.MTCloud; |
| | | import com.qxueyou.scc.teach.live.model.AccessLog; |
| | | import com.qxueyou.scc.teach.live.utils.RedisCache; |
| | | import com.qxueyou.scc.teach.student.model.StuStudent; |
| | | 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; |
| | |
| | | import io.swagger.annotations.ApiImplicitParams; |
| | | import io.swagger.annotations.ApiOperation; |
| | | |
| | | import javax.servlet.http.HttpServletRequest; |
| | | |
| | | @Slf4j |
| | | @Api(tags="直播接口-学员端") |
| | | @RestController |
| | | @CrossOrigin |
| | | @RequestMapping(value = "/stu/live") |
| | | public class StuLiveController { |
| | | |
| | |
| | | |
| | | @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, |
| | |
| | | * @return |
| | | */ |
| | | @GetMapping(value = "view") |
| | | public Result view(String couresId) throws Exception { |
| | | |
| | | |
| | | public Result view(String couresId, String userId, String userName, String logId, HttpServletRequest request) throws Exception { |
| | | String redisLiveUrl = redisCache.getCacheObject("LIVE_URL" + userId); |
| | | AccessLog accessLog=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归属地信息失败!"); |
| | | } |
| | | accessLog.setCourseId(couresId); |
| | | accessLog.setEnterTime(new Date()); |
| | | accessLog.setType("guangxi"); |
| | | accessLog.setName(userName); |
| | | accessLog.setIp(ip); |
| | | accessLog.setIpAttribution(cityInfo); |
| | | accessLog.setCreateTime(new Date()); |
| | | accessLog.setUpdateTime(new Date()); |
| | | commonDAO.save(accessLog); |
| | | } |
| | | if (!StringUtils.isEmpty(redisLiveUrl)){ |
| | | return new Result(true, "success",CollectionUtils.newObjectMap("url",redisLiveUrl,"log",accessLog.getLogId())); |
| | | } |
| | | MTCloud client = new MTCloud(); |
| | | |
| | | String userRole = null; |
| | | HashMap<Object,Object> options = new HashMap<Object, Object>(); |
| | | |
| | | String res = client.courseAccess(couresId, ClientUtils.getUserId(), ClientUtils.getUserName(), MTCloud.ROLE_USER, 10000, options); |
| | | 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); |
| | | |
| | | JSONObject data = jsonObject.getJSONObject("data"); |
| | | if(jsonObject.getString("code").equals("0")){ |
| | | JSONObject data = jsonObject.getJSONObject("data"); |
| | | String liveUrl = (String) data.get("liveUrl"); |
| | | StringBuffer redisLiveKey = new StringBuffer("LIVE_URL"); |
| | | redisLiveKey.append(userId); |
| | | redisCache.setCacheObject(redisLiveKey.toString(),liveUrl,5, TimeUnit.MINUTES); |
| | | return new Result(true, "success",CollectionUtils.newObjectMap("url",liveUrl,"log",accessLog.getLogId())); |
| | | } |
| | | |
| | | String liveUrl = (String) data.get("liveUrl"); |
| | | |
| | | System.out.println(liveUrl); |
| | | |
| | | return new Result(true, "success",liveUrl); |
| | | return new Result(false, jsonObject.getString("msg")); |
| | | } |
| | | |
| | | @ApiOperation(value = "离开路演直播间") |
| | | @GetMapping(value = "leaveRoadShow") |
| | | public Result enterIntoRoadShow(String logId) { |
| | | // System.out.println(logId); |
| | | if(!StringUtils.isEmpty(logId)){ |
| | | StringBuffer hql=new StringBuffer("from AccessLog where logId=?"); |
| | | List<Object> args = CollectionUtils.newList(logId); |
| | | AccessLog accessLogs = commonDAO.findUnique(hql.toString(), args, AccessLog.class); |
| | | // System.out.println(accessLogs); |
| | | accessLogs.setLeaveTime(new Date()); |
| | | accessLogs.setUpdateTime(new Date()); |
| | | commonDAO.update(accessLogs); |
| | | return new Result(true,"退出成功"); |
| | | } |
| | | return new Result(false,"logId不能为空"); |
| | | } |
| | | |
| | | |
| | | 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; |
| | | } |
| | | |
| | | |
| | | } |