From 2174b22bbbb45284765a23b8189df59583c65d29 Mon Sep 17 00:00:00 2001 From: 胡仁荣 <897853850@qq.com> Date: 星期五, 04 八月 2023 14:33:47 +0800 Subject: [PATCH] 统计人数 --- src/main/java/com/qxueyou/scc/config/IpUtils.java | 95 +++++++++++ src/main/java/com/qxueyou/scc/callback/CallbackExampleAct.java | 76 +++++++++ src/main/java/com/qxueyou/scc/base/model/Result.java | 4 src/main/java/com/qxueyou/scc/teach/live/model/AccessLog.java | 121 ++++++++++++++ pom.xml | 5 src/main/java/com/qxueyou/scc/controller/VideoLiveController.java | 61 +++++- src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java | 74 ++++++--- 7 files changed, 390 insertions(+), 46 deletions(-) diff --git a/pom.xml b/pom.xml index ac828c5..312cfca 100644 --- a/pom.xml +++ b/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> diff --git a/src/main/java/com/qxueyou/scc/base/model/Result.java b/src/main/java/com/qxueyou/scc/base/model/Result.java index d1188a2..d719e72 100644 --- a/src/main/java/com/qxueyou/scc/base/model/Result.java +++ b/src/main/java/com/qxueyou/scc/base/model/Result.java @@ -80,8 +80,8 @@ this.msg = success ? MSG_SUCCESS : MSG_FAIL; this.data = CollectionUtils.newObjectMap(objs); } - - public boolean isSuccess() { + + public boolean isSuccess() { return success; } diff --git a/src/main/java/com/qxueyou/scc/callback/CallbackExampleAct.java b/src/main/java/com/qxueyou/scc/callback/CallbackExampleAct.java new file mode 100644 index 0000000..2e1a47c --- /dev/null +++ b/src/main/java/com/qxueyou/scc/callback/CallbackExampleAct.java @@ -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"); + + } + // 瑙f瀽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"); + } +} diff --git a/src/main/java/com/qxueyou/scc/config/IpUtils.java b/src/main/java/com/qxueyou/scc/config/IpUtils.java new file mode 100644 index 0000000..093b0a9 --- /dev/null +++ b/src/main/java/com/qxueyou/scc/config/IpUtils.java @@ -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("閿欒: 鏃犳晥鐨刬p鍦板潃"); + 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("閿欒: 鏃犳晥鐨刬p2region.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; + } +} diff --git a/src/main/java/com/qxueyou/scc/controller/VideoLiveController.java b/src/main/java/com/qxueyou/scc/controller/VideoLiveController.java index 58b4d3a..5e75019 100644 --- a/src/main/java/com/qxueyou/scc/controller/VideoLiveController.java +++ b/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 { @@ -58,7 +67,9 @@ @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(); - 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); - } - return new Result(false,jsonObject.getString("msg")); + 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); + JSONObject jsonObject = JSON.parseObject(res); + if (jsonObject.getString("code").equals("0")) { + JSONObject data = jsonObject.getJSONObject("data"); + String protocol = (String) data.get("url"); + redisCache.setCacheObject("ZCR" + courseId, protocol, 1, TimeUnit.DAYS); + return new Result(true,"suc", CollectionUtils.newObjectMap("url", protocol, "log", accessLog.getLogId())); + } + } + return new Result(true,"suc",CollectionUtils.newObjectMap("url", redisUrl, "log", accessLog.getLogId())); } @ApiOperation(value = "淇敼鐩存挱鐘舵��") diff --git a/src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java b/src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java index b465015..75c3cf6 100644 --- a/src/main/java/com/qxueyou/scc/stucontroller/StuLiveController.java +++ b/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=""; diff --git a/src/main/java/com/qxueyou/scc/teach/live/model/AccessLog.java b/src/main/java/com/qxueyou/scc/teach/live/model/AccessLog.java index eb8c195..0549857 100644 --- a/src/main/java/com/qxueyou/scc/teach/live/model/AccessLog.java +++ b/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 = "鐩存挱闂磇d") + @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 = "濡傛灉鏄痷serId鍦ㄥ钩鍙版煡璇笉鍒板垯鏄父瀹�") + @Column(name="user_id") private String userId; @ApiModelProperty(value = "ip褰掑睘鍦颁俊鎭�") + @Column(name="ip_attribution") private String ipAttribution; @ApiModelProperty(value = "roadShow:璺紨锛宑onference:浼氳") 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; } -- Gitblit v1.8.0