派生自 projectDept/qhighschool

yn147
2023-10-26 3970ced88b5b456f03fe277c254ca761f05492e0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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.AccessLog;
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.Date;
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")){//直播开始
            //路演直播状态结束业务处理
            String courseId = (String) params.get("course_id");
            StringBuffer hql=new StringBuffer("from MediaVideoLive where deleteFlag is false and courseId=?");
            List<Object> args = CollectionUtils.newList(Integer.valueOf(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")){//直播结束
            //路演直播状态结束业务处理
            String courseId = (String) params.get("course_id");
            StringBuffer hql=new StringBuffer("from MediaVideoLive where deleteFlag is false and courseId=?");
            List<Object> args = CollectionUtils.newList(Integer.valueOf(courseId));
            MediaVideoLive unique = commonDAO.findUnique(hql.toString(), args, MediaVideoLive.class);
            unique.setStatus(MediaVideoLive.STATUS_LIVE_STOP);
            StringBuffer accessLog=new StringBuffer("from AccessLog where courseId=? and type='videoLive'");
            List<Object> arglog = CollectionUtils.newList(courseId);
            AccessLog unique1 = commonDAO.findUnique(accessLog.toString(), arglog, AccessLog.class);
            unique1.setLeaveTime(new Date());
            commonDAO.update(unique1);
            commonDAO.update(unique);
        }
        return  new Result(true,"suc");
    }
}