pom.xml | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/qxueyou/scc/base/model/Result.java | 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/qxueyou/scc/callback/CallbackExampleAct.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/qxueyou/scc/config/IpUtils.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/qxueyou/scc/controller/VideoLiveController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/qxueyou/scc/teach/live/model/AccessLog.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
pom.xml
@@ -441,6 +441,11 @@ <version>2.8.3</version> </dependency> <dependency> <groupId>org.lionsoul</groupId> <artifactId>ip2region</artifactId> <version>1.7.2</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> src/main/java/com/qxueyou/scc/base/model/Result.java
src/main/java/com/qxueyou/scc/callback/CallbackExampleAct.java
New file @@ -0,0 +1,76 @@ package com.qxueyou.scc.callback; import com.qxueyou.scc.base.dao.CommonDAO; import com.qxueyou.scc.base.model.Result; import com.qxueyou.scc.base.util.CollectionUtils; import com.qxueyou.scc.sdk.MTCloud; import com.qxueyou.scc.teach.live.model.MediaVideoLive; import com.qxueyou.scc.teach.live.service.IMediaLiveService; import lombok.extern.slf4j.Slf4j; import net.sf.json.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.net.URLDecoder; import java.util.HashMap; import java.util.List; @Slf4j @Controller @RequestMapping(value = "/callback") public class CallbackExampleAct { @Autowired CommonDAO commonDAO; @RequestMapping(value = "/api", method = RequestMethod.POST, produces = "application/json;charset=utf-8") @ResponseBody public Result callbackApi(HttpServletRequest request, HttpServletResponse response) throws Exception { //获取请求参数 HashMap<Object, Object> reqParams = new HashMap<Object, Object>(); reqParams.put("openID", request.getParameter("openID")); reqParams.put("timestamp", request.getParameter("timestamp")); reqParams.put("cmd", request.getParameter("cmd")); reqParams.put("params", request.getParameter("params")); reqParams.put("ver", request.getParameter("ver")); //reqParams.put("sign", request.getParameter("sign")); String sign = request.getParameter("sign"); MTCloud talkfunSDK = new MTCloud("56407","cd8ab50fbdb53a3b0338bda46186c58a"); if(!talkfunSDK.generateSign(reqParams).equals(sign)) { //签名验证失败 return new Result(false,"sign validate fail"); } // 解析params String jsonStr = URLDecoder.decode((String) reqParams.get("params"), "UTF-8"); JSONObject params = JSONObject.fromObject(jsonStr); //签名验证成功 if(reqParams.get("cmd").equals("live.start")){//直播开始 //路演直播状态结束业务处理 Integer courseId =(Integer) params.get("course_id"); StringBuffer hql=new StringBuffer("from MediaVideoLive where deleteFlag is false and courseId=?"); List<Object> args = CollectionUtils.newList(courseId); MediaVideoLive unique = commonDAO.findUnique(hql.toString(), args, MediaVideoLive.class); unique.setStatus(MediaVideoLive.STATUS_LIVE_LIVE); commonDAO.update(unique); }else if(reqParams.get("cmd").equals("live.stop")){//直播结束 //路演直播状态结束业务处理 Integer courseId =(Integer) params.get("course_id"); StringBuffer hql=new StringBuffer("from MediaVideoLive where deleteFlag is false and courseId=?"); List<Object> args = CollectionUtils.newList(courseId); MediaVideoLive unique = commonDAO.findUnique(hql.toString(), args, MediaVideoLive.class); unique.setStatus(MediaVideoLive.STATUS_LIVE_STOP); commonDAO.update(unique); } return new Result(true,"suc"); } } src/main/java/com/qxueyou/scc/config/IpUtils.java
New file @@ -0,0 +1,95 @@ package com.qxueyou.scc.config; import lombok.extern.slf4j.Slf4j; import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.lionsoul.ip2region.DataBlock; import org.lionsoul.ip2region.DbConfig; import org.lionsoul.ip2region.DbSearcher; import org.lionsoul.ip2region.Util; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import javax.servlet.http.HttpServletRequest; import java.io.File; import java.io.InputStream; /** * @author: liuliu * @date: 2023/07/03 10:28 * @description: ip工具类 */ @Slf4j public class IpUtils { /** * 本地环回地址 */ private static final String LOCAL_IP = "127.0.0.1"; /** * 未知 */ private static final String UNKNOWN = "unknown"; public static String getIpAddr(HttpServletRequest request) { if (request == null) { return UNKNOWN; } String ip = request.getHeader("x-forwarded-for"); if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("X-Forwarded-For"); } if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } if (StringUtils.isBlank(ip) || UNKNOWN.equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } return "0:0:0:0:0:0:0:1".equals(ip) ? LOCAL_IP : ip; } public static String getCityInfo(String ip) throws Exception { if (!Util.isIpAddress(ip)) { log.error("错误: 无效的ip地址"); return null; } InputStream is = new PathMatchingResourcePatternResolver().getResources("ip2region.db")[0].getInputStream(); File target = new File("ip2region.db"); FileUtils.copyInputStreamToFile(is, target); is.close(); if (StringUtils.isEmpty(String.valueOf(target))) { log.error("错误: 无效的ip2region.db文件"); return null; } DbSearcher searcher = new DbSearcher(new DbConfig(), String.valueOf(target)); try { DataBlock dataBlock = (DataBlock) searcher.getClass().getMethod("btreeSearch", String.class).invoke(searcher, ip); String ipInfo = dataBlock.getRegion(); if (!StringUtils.isEmpty(ipInfo)) { ipInfo = ipInfo.replace("|0", ""); ipInfo = ipInfo.replace("0|", ""); } return ipInfo; } catch (Exception e) { e.printStackTrace(); } return null; } } src/main/java/com/qxueyou/scc/controller/VideoLiveController.java
@@ -2,16 +2,22 @@ import java.text.SimpleDateFormat; import java.util.*; import java.util.concurrent.TimeUnit; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.hankcs.hanlp.corpus.tag.Nature; import com.qxueyou.scc.base.dao.CommonDAO; 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 io.swagger.models.auth.In; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpRequest; import org.springframework.web.bind.annotation.*; import com.qxueyou.scc.admin.classes.model.ClsClass; @@ -31,11 +37,14 @@ import io.swagger.annotations.ApiOperation; import springfox.documentation.spring.web.json.Json; import javax.servlet.http.HttpServletRequest; import static com.hankcs.hanlp.corpus.tag.Nature.r; import static com.hankcs.hanlp.corpus.tag.Nature.s; @Api(tags="直播管理接口") @RestController @Slf4j @CrossOrigin(origins="*",maxAge=3600) @RequestMapping(value = "/admin/videoLive") public class VideoLiveController { @@ -59,6 +68,8 @@ @Autowired CommonDAO commonDAO; @Autowired private RedisCache redisCache; /** * 获取直播列表 * @@ -157,21 +168,43 @@ @ApiOperation(value = "开启直播") @GetMapping(value = "OpenLive") public Result OpenLive(int courseId) throws Exception { public Result OpenLive(Integer courseId, String logId, HttpServletRequest request) throws Exception { MTCloud client = new MTCloud(); String redisUrl = redisCache.getCacheObject("ZCR" + courseId); 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(courseId.toString()); accessLog.setEnterTime(new Date()); accessLog.setType("videoLive"); accessLog.setName(ClientUtils.getUserName()); accessLog.setIp(ip); accessLog.setIpAttribution(cityInfo); accessLog.setCreateTime(new Date()); accessLog.setUpdateTime(new Date()); //访问留痕 commonDAO.save(accessLog); } if (StringUtils.isEmpty(redisUrl)) { HashMap<Object,Object> options = new HashMap<Object, Object>(); options.put("ssl", false); String res = client.courseLogin(ClientUtils.getUserId(),MTCloud.ACCOUNT_TYPE_THIRD, options); System.out.println(res); JSONObject jsonObject = JSON.parseObject(res); if(jsonObject.getString("code").equals("0")){ JSONObject data = jsonObject.getJSONObject("data"); String protocol = (String) data.get("url"); return new Result(true,"开启成功",protocol); redisCache.setCacheObject("ZCR" + courseId, protocol, 1, TimeUnit.DAYS); return new Result(true,"suc", CollectionUtils.newObjectMap("url", protocol, "log", accessLog.getLogId())); } return new Result(false,jsonObject.getString("msg")); } return new Result(true,"suc",CollectionUtils.newObjectMap("url", redisUrl, "log", accessLog.getLogId())); } @ApiOperation(value = "修改直播状态") src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java
@@ -6,9 +6,12 @@ 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; @@ -31,6 +34,8 @@ import io.swagger.annotations.ApiImplicitParam; import io.swagger.annotations.ApiImplicitParams; import io.swagger.annotations.ApiOperation; import javax.servlet.http.HttpServletRequest; @Slf4j @Api(tags="直播接口-学员端") @@ -132,26 +137,29 @@ * @return */ @GetMapping(value = "view") public Result view(String couresId,String userId,String userName,String logId) throws Exception { AccessLog log=new AccessLog(); 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归属地信息失败!"); // } 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); 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; @@ -170,19 +178,35 @@ 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); 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())); } 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=""; src/main/java/com/qxueyou/scc/teach/live/model/AccessLog.java
@@ -1,50 +1,161 @@ package com.qxueyou.scc.teach.live.model; import io.swagger.annotations.ApiModelProperty; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.hibernate.annotations.GenericGenerator; import javax.persistence.Table; import javax.persistence.*; import java.io.Serializable; import java.util.Date; /** * 访问日志 */ @Data @NoArgsConstructor//无参构造方法 @AllArgsConstructor//全参构造方法 @Entity //@NoArgsConstructor//无参构造方法 //@AllArgsConstructor//全参构造方法 //@EqualsAndHashCode(callSuper = true) @Table(name="sa_access_log") public class AccessLog implements Serializable { private static final long serialVersionUID = -1979488824632702882L; private static final long serialVersionUID = - 1979488824632702882L; public String getLogId() { return logId; } public void setLogId(String logId) { this.logId = logId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCourseId() { return courseId; } public void setCourseId(String courseId) { this.courseId = courseId; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public Date getEnterTime() { return enterTime; } public void setEnterTime(Date enterTime) { this.enterTime = enterTime; } public Date getLeaveTime() { return leaveTime; } public void setLeaveTime(Date leaveTime) { this.leaveTime = leaveTime; } public String getUserId() { return userId; } public void setUserId(String userId) { this.userId = userId; } public String getIpAttribution() { return ipAttribution; } public void setIpAttribution(String ipAttribution) { this.ipAttribution = ipAttribution; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } public Integer getDeleteFlag() { return deleteFlag; } public void setDeleteFlag(Integer deleteFlag) { this.deleteFlag = deleteFlag; } @ApiModelProperty(value = "编号") @Id @GeneratedValue(generator = "hibernate-uuid") @GenericGenerator(name = "hibernate-uuid", strategy = "uuid") @Column(name="log_id") private String logId; @ApiModelProperty(value = "姓名") private String name; @ApiModelProperty(value = "直播间id") @Column(name="course_id") private String courseId; @ApiModelProperty(value = "访问ip") private String ip; @ApiModelProperty(value = "进入直播开始时间") @Column(name="enter_time") private Date enterTime; @ApiModelProperty(value = "离开直播时间") @Column(name="leave_time") private Date leaveTime; @ApiModelProperty(value = "如果是userId在平台查询不到则是游客") @Column(name="user_id") private String userId; @ApiModelProperty(value = "ip归属地信息") @Column(name="ip_attribution") private String ipAttribution; @ApiModelProperty(value = "roadShow:路演,conference:会议") private String type; @ApiModelProperty(value = "创建时间") @Column(name="create_time") private Date createTime; @ApiModelProperty(value = "修改时间") @Column(name="update_time") private Date updateTime; //是否删除:0未删除 1删除 @ApiModelProperty(value = "删除标识") @Column(name="delete_flag") private Integer deleteFlag; }