| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327 |
- package com.cnd3b.restcontroller.enterprise.live;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.cnd3b.common.Controller;
- import com.cnd3b.common.data.Rows;
- import com.cnd3b.common.data.SQLFactory;
- import com.cnd3b.common.data.db.DataPool;
- import com.cnd3b.utility.polyv.Polyv;
- import net.polyv.live.v1.entity.channel.operate.LiveChannelResponse;
- import p2.pao.PaoRemote;
- import p2.pao.PaoSetRemote;
- import p2.util.P2Exception;
- import java.util.HashMap;
- import java.util.Random;
- public class live extends Controller {
- public live(JSONObject content) {
- super(content);
- }
- /**
- * 获取频道分类列表
- *
- * @return
- */
- public String getLiveCategory() {
- Polyv polyv = new Polyv();
- HashMap<Integer, String> map = polyv.listCategory();
- JSONArray array = new JSONArray();
- for (int id : map.keySet()) {
- JSONObject object = new JSONObject();
- object.put("categoryId", id);
- object.put("categoryName", map.get(id));
- array.add(object);
- }
- return getSucReturnObject().setData(array).toString();
- }
- /**
- * 直播列表查询
- *
- * @return
- */
- public String queryChannelList() {
- /**
- *排序条件设置
- */
- String[] sortfield = {"t1.createdate desc"};
- String sort = getSort(sortfield, "t1.createdate desc");
- /**
- * 过滤条件设置
- */
- StringBuffer where = new StringBuffer(" 1=1 ");
- if (content.containsKey("where")) {
- JSONObject whereObject = content.getJSONObject("where");
- if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
- where.append(" and(");
- where.append("t1.channelname like'%").append(whereObject.getString("condition")).append("%' ");
- where.append("or t2.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
- where.append(")");
- }
- if (whereObject.containsKey("categoryid") && !"".equals(whereObject.getString("categoryid"))) {
- where.append(" and(");
- where.append("t1.categoryid ='").append(whereObject.getString("categoryid")).append("' ");
- where.append(")");
- }
- }
- SQLFactory costhead = new SQLFactory(this, "直播列表查询", pageSize, pageNumber, sort);
- costhead.addParameter("siteid", siteid);
- costhead.addParameter_SQL("where", where);
- Rows rows = dbConnect.runSqlQuery(costhead.getSQL());
- return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
- }
- /**
- * 直播详情查询
- *
- * @return
- */
- public String queryChannelMain() {
- long tliveid = content.getLong("tliveid");
- SQLFactory sqlFactory = new SQLFactory(this, "直播详情查询");
- sqlFactory.addParameter("siteid", siteid);
- sqlFactory.addParameter("tliveid", tliveid);
- Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
- return getSucReturnObject().setData(rows).toString();
- }
- /**
- * 创建直播
- *
- * @return
- */
- public String createLive() throws P2Exception {
- long tagentsid = content.getLong("tagentsid");
- String channelname = content.getString("channelname");
- int categoryid = content.getInteger("categoryid");
- String channelPasswd = createPassWord();
- Polyv polyv = new Polyv();
- LiveChannelResponse liveChannelResponse = polyv.createChannel(channelname, channelPasswd, categoryid);
- if (liveChannelResponse != null) {
- PaoSetRemote tliveSet = getP2ServerSystemPaoSet("tlive");
- PaoRemote tlive = tliveSet.addAtEnd();
- tlive.setValue("siteid", siteid);
- tlive.setValue("tagentsid", tagentsid);
- tlive.setValue("categoryid", categoryid);//分类ID
- tlive.setValue("categoryname", polyv.listCategory().get(categoryid));//分类名称
- tlive.setValue("channelid", liveChannelResponse.getChannelId());//频道ID
- tlive.setValue("channelname", liveChannelResponse.getName());//频道名称
- tlive.setValue("channelpasswd", channelPasswd);//开播密码
- tlive.setValue("livestatus", "unStart");//直播间状态
- tlive.setValue("fliveshowurl", "https://live.polyv.cn/watch/" + liveChannelResponse.getChannelId());//观看地址
- tlive.setValue("fliveurl_web", "https://live.polyv.net/web-start/login?channelId=" + liveChannelResponse.getChannelId());//网页开播地址
- tlive.setValue("fliveurl_client", "https://console.polyv.net/live/start-client.html?channelId=" + liveChannelResponse.getChannelId());//客户端开播地址
- tlive.setValue("fassistanturl", "https://console.polyv.net/live_v2/teacher.html");//助教地址
- tlive.setValue("channelcoverimageurl", polyv.getChannelImage(liveChannelResponse.getChannelId(), "unStart"));//封面图地址
- tlive.setValue("createby", username);
- tlive.setValue("createdate", sysdate);
- content.put("tliveid", tlive.getUniqueIDValue());
- tliveSet.save();
- } else {
- return getErrReturnObject().toString();
- }
- return queryChannelMain();
- }
- /**
- * 修改直播
- *
- * @return
- */
- public String modifyLive() throws P2Exception {
- long tliveid = content.getLong("tliveid");
- String channelname = content.getString("channelname");
- int categoryid = content.getInteger("categoryid");
- PaoSetRemote tliveSet = getP2ServerSystemPaoSet("tlive", "siteid='" + siteid + "' and tliveid='" + tliveid + "'");
- if (tliveSet.isEmpty()) {
- return getErrReturnObject().setErrMsg("找不到当前直播信息").toString();
- }
- PaoRemote tlive = tliveSet.getPao(0);
- Polyv polyv = new Polyv();
- boolean liveChannelResponse = polyv.updateChannelSetting(tlive.getString("channelid"), channelname, categoryid);
- if (liveChannelResponse) {
- tlive.setValue("categoryid", categoryid);//分类ID
- tlive.setValue("categoryname", polyv.listCategory().get(categoryid));//分类名称
- tlive.setValue("channelname", channelname);//频道名称
- tliveSet.save();
- }
- return queryChannelMain();
- }
- /**
- * 开启直播间外部授权设置
- *
- * @return
- */
- public String setChannelAuth() {
- long tliveid = content.getLong("tliveid");
- Rows rows = dbConnect.runSqlQuery("select channelid from tlive where siteid='" + siteid + "' and tliveid=" + tliveid);
- if (rows.isEmpty()) {
- return getErrReturnObject().setErrMsg("找不到当前直播间").toString();
- }
- String channelid = rows.get(0).getString("channelid");
- Polyv polyv = new Polyv();
- String secretKey = polyv.updateChannelAuthExternal(channelid);
- if (secretKey == null) {
- return getErrReturnObject().toString();
- } else {
- dbConnect.runSqlUpdate("update tlive set fisneedauth=1,secretkey='" + secretKey + "' where siteid='" + siteid + "' and tliveid='" + tliveid + "'");
- //需将改为新的直播观看地址
- return queryChannelMain();
- }
- }
- /**
- * 关闭直播间外部授权设置
- *
- * @return
- */
- public String closeChannelAuth() {
- long tliveid = content.getLong("tliveid");
- Rows rows = dbConnect.runSqlQuery("select channelid from tlive where siteid='" + siteid + "' and tliveid=" + tliveid);
- if (rows.isEmpty()) {
- return getErrReturnObject().setErrMsg("找不到当前直播间").toString();
- }
- String channelid = rows.get(0).getString("channelid");
- Polyv polyv = new Polyv();
- if (polyv.closeChannelAuthExternal(channelid)) {
- dbConnect.runSqlUpdate("update tlive set fisneedauth=0,secretkey=null where siteid='" + siteid + "' and tliveid='" + tliveid + "'");
- //需将改为新的直播观看地址
- return queryChannelMain();
- } else {
- return getErrReturnObject().toString();
- }
- }
- /**
- * 直播间删除
- *
- * @return
- */
- public String deleteLive() {
- String tliveid = content.getString("tliveid");
- Rows rows = dbConnect.runSqlQuery("select channelid from tlive where siteid='" + siteid + "' and tliveid=" + tliveid);
- if (rows.isEmpty()) {
- return getErrReturnObject().setErrMsg("找不到当前直播间").toString();
- }
- String channelid = rows.get(0).getString("channelid");
- Polyv polyv = new Polyv();
- if (polyv.deleteChannel(channelid)) {
- SQLFactory sqlFactory = new SQLFactory(this, "直播间删除");
- sqlFactory.addParameter("siteid", siteid);
- sqlFactory.addParameter("tliveid", tliveid);
- String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
- if ("true".equalsIgnoreCase(status)) {
- return getSucReturnObject().toString();
- } else {
- return getErrReturnObject().setErrMsg(status).toString();
- }
- } else {
- return getErrReturnObject().setErrMsg("直播间删除失败").toString();
- }
- }
- /**
- * 直播授权
- *
- * @return
- */
- public String liveauthorization() {
- String tliveid = content.getString("tliveid");//直播ID
- Rows rows = dbConnect.runSqlQuery("select channelId,secretkey from tlive where siteid='" + siteid + "' and tliveid='" + tliveid + "'");
- if (rows.isEmpty()) {
- return getErrReturnObject().toString();
- }
- return getSucReturnObject().setData(new Polyv().getSignUrl(rows.get(0).getString("channelId"), rows.get(0).getString("secretkey"), userid)).toString();
- }
- private String createPassWord() {
- String allChar = "1234567890";
- StringBuffer sb = new StringBuffer();
- Random random = new Random();
- for (int i = 0; i < 6; i++) {
- sb.append(allChar.charAt(random.nextInt(allChar.length())));
- }
- return sb.toString();
- }
- /**
- * 直播申请列表查询
- *
- * @return
- */
- public String getSYLiveApplyList() {
- SQLFactory sqlFactory = new SQLFactory(this, "直播申请列表查询", pageSize, pageNumber, "changedate");
- Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
- return getSucReturnObject().setDataByPaging(rows).preloading(1).toString();
- }
- /**
- * 审核私域申请,自动创建直播间
- *
- * @return
- */
- public String auditSYLiveApply() throws P2Exception {
- String tliveapplyid = content.getString("tliveapplyid");
- String tagentsid = content.getString("tagentsid");
- String where = "tagentsid = " + tagentsid + " AND tliveapplyid = " + tliveapplyid;
- PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet("tliveapply", where);
- if (paoSetRemote.isEmpty()) {
- return getErrReturnObject().setErrMsg("未找到对应申请").toString();
- } else {
- String sql = "SELECT*FROM tlive WHERE categoryname='私域直播' AND tagentsid=" + tagentsid;
- Rows rows = dbConnect.runSqlQuery(sql);
- if (rows.totalRows > 0) {
- return getErrReturnObject().setErrMsg("已存在私域直播").toString();
- } else {
- //修改状态
- PaoRemote paoRemote = paoSetRemote.getPao(0);
- paoRemote.setValue("fstatus", "审核");
- paoRemote.setValue("CHECKBY", username);
- paoRemote.setValue("CHECKDATE", getDateTime_Str());
- paoSetRemote.save();
- //开启直播
- content.put("tagentsid", tagentsid);
- content.put("channelname", "私域直播");
- content.put("categoryid", getCategoryId("私域直播"));
- return createLive();
- }
- }
- }
- /**
- * 通过频道分类名称获取对应的分类id
- *
- * @param categoryName
- * @return
- */
- public int getCategoryId(String categoryName) {
- Polyv polyv = new Polyv();
- HashMap<Integer, String> map = polyv.listCategory();
- int categoryId = -1;
- for (int id : map.keySet()) {
- if (map.get(id).equals(categoryName)) {
- categoryId = id;
- }
- }
- return categoryId;
- }
- }
|