live.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. package com.cnd3b.restcontroller.enterprise.live;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.cnd3b.common.Controller;
  5. import com.cnd3b.common.data.Rows;
  6. import com.cnd3b.common.data.SQLFactory;
  7. import com.cnd3b.common.data.db.DataPool;
  8. import com.cnd3b.utility.polyv.Polyv;
  9. import net.polyv.live.v1.entity.channel.operate.LiveChannelResponse;
  10. import p2.pao.PaoRemote;
  11. import p2.pao.PaoSetRemote;
  12. import p2.util.P2Exception;
  13. import java.util.HashMap;
  14. import java.util.Random;
  15. public class live extends Controller {
  16. public live(JSONObject content) {
  17. super(content);
  18. }
  19. /**
  20. * 获取频道分类列表
  21. *
  22. * @return
  23. */
  24. public String getLiveCategory() {
  25. Polyv polyv = new Polyv();
  26. HashMap<Integer, String> map = polyv.listCategory();
  27. JSONArray array = new JSONArray();
  28. for (int id : map.keySet()) {
  29. JSONObject object = new JSONObject();
  30. object.put("categoryId", id);
  31. object.put("categoryName", map.get(id));
  32. array.add(object);
  33. }
  34. return getSucReturnObject().setData(array).toString();
  35. }
  36. /**
  37. * 直播列表查询
  38. *
  39. * @return
  40. */
  41. public String queryChannelList() {
  42. /**
  43. *排序条件设置
  44. */
  45. String[] sortfield = {"t1.createdate desc"};
  46. String sort = getSort(sortfield, "t1.createdate desc");
  47. /**
  48. * 过滤条件设置
  49. */
  50. StringBuffer where = new StringBuffer(" 1=1 ");
  51. if (content.containsKey("where")) {
  52. JSONObject whereObject = content.getJSONObject("where");
  53. if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) {
  54. where.append(" and(");
  55. where.append("t1.channelname like'%").append(whereObject.getString("condition")).append("%' ");
  56. where.append("or t2.fagentname like'%").append(whereObject.getString("condition")).append("%' ");
  57. where.append(")");
  58. }
  59. if (whereObject.containsKey("categoryid") && !"".equals(whereObject.getString("categoryid"))) {
  60. where.append(" and(");
  61. where.append("t1.categoryid ='").append(whereObject.getString("categoryid")).append("' ");
  62. where.append(")");
  63. }
  64. }
  65. SQLFactory costhead = new SQLFactory(this, "直播列表查询", pageSize, pageNumber, sort);
  66. costhead.addParameter("siteid", siteid);
  67. costhead.addParameter_SQL("where", where);
  68. Rows rows = dbConnect.runSqlQuery(costhead.getSQL());
  69. return getSucReturnObject().setDataByPaging(rows, sortfield).setFinalDo(DataPool.defaultdatalife, 1);
  70. }
  71. /**
  72. * 直播详情查询
  73. *
  74. * @return
  75. */
  76. public String queryChannelMain() {
  77. long tliveid = content.getLong("tliveid");
  78. SQLFactory sqlFactory = new SQLFactory(this, "直播详情查询");
  79. sqlFactory.addParameter("siteid", siteid);
  80. sqlFactory.addParameter("tliveid", tliveid);
  81. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  82. return getSucReturnObject().setData(rows).toString();
  83. }
  84. /**
  85. * 创建直播
  86. *
  87. * @return
  88. */
  89. public String createLive() throws P2Exception {
  90. long tagentsid = content.getLong("tagentsid");
  91. String channelname = content.getString("channelname");
  92. int categoryid = content.getInteger("categoryid");
  93. String channelPasswd = createPassWord();
  94. Polyv polyv = new Polyv();
  95. LiveChannelResponse liveChannelResponse = polyv.createChannel(channelname, channelPasswd, categoryid);
  96. if (liveChannelResponse != null) {
  97. PaoSetRemote tliveSet = getP2ServerSystemPaoSet("tlive");
  98. PaoRemote tlive = tliveSet.addAtEnd();
  99. tlive.setValue("siteid", siteid);
  100. tlive.setValue("tagentsid", tagentsid);
  101. tlive.setValue("categoryid", categoryid);//分类ID
  102. tlive.setValue("categoryname", polyv.listCategory().get(categoryid));//分类名称
  103. tlive.setValue("channelid", liveChannelResponse.getChannelId());//频道ID
  104. tlive.setValue("channelname", liveChannelResponse.getName());//频道名称
  105. tlive.setValue("channelpasswd", channelPasswd);//开播密码
  106. tlive.setValue("livestatus", "unStart");//直播间状态
  107. tlive.setValue("fliveshowurl", "https://live.polyv.cn/watch/" + liveChannelResponse.getChannelId());//观看地址
  108. tlive.setValue("fliveurl_web", "https://live.polyv.net/web-start/login?channelId=" + liveChannelResponse.getChannelId());//网页开播地址
  109. tlive.setValue("fliveurl_client", "https://console.polyv.net/live/start-client.html?channelId=" + liveChannelResponse.getChannelId());//客户端开播地址
  110. tlive.setValue("fassistanturl", "https://console.polyv.net/live_v2/teacher.html");//助教地址
  111. tlive.setValue("channelcoverimageurl", polyv.getChannelImage(liveChannelResponse.getChannelId(), "unStart"));//封面图地址
  112. tlive.setValue("createby", username);
  113. tlive.setValue("createdate", sysdate);
  114. content.put("tliveid", tlive.getUniqueIDValue());
  115. tliveSet.save();
  116. } else {
  117. return getErrReturnObject().toString();
  118. }
  119. return queryChannelMain();
  120. }
  121. /**
  122. * 修改直播
  123. *
  124. * @return
  125. */
  126. public String modifyLive() throws P2Exception {
  127. long tliveid = content.getLong("tliveid");
  128. String channelname = content.getString("channelname");
  129. int categoryid = content.getInteger("categoryid");
  130. PaoSetRemote tliveSet = getP2ServerSystemPaoSet("tlive", "siteid='" + siteid + "' and tliveid='" + tliveid + "'");
  131. if (tliveSet.isEmpty()) {
  132. return getErrReturnObject().setErrMsg("找不到当前直播信息").toString();
  133. }
  134. PaoRemote tlive = tliveSet.getPao(0);
  135. Polyv polyv = new Polyv();
  136. boolean liveChannelResponse = polyv.updateChannelSetting(tlive.getString("channelid"), channelname, categoryid);
  137. if (liveChannelResponse) {
  138. tlive.setValue("categoryid", categoryid);//分类ID
  139. tlive.setValue("categoryname", polyv.listCategory().get(categoryid));//分类名称
  140. tlive.setValue("channelname", channelname);//频道名称
  141. tliveSet.save();
  142. }
  143. return queryChannelMain();
  144. }
  145. /**
  146. * 开启直播间外部授权设置
  147. *
  148. * @return
  149. */
  150. public String setChannelAuth() {
  151. long tliveid = content.getLong("tliveid");
  152. Rows rows = dbConnect.runSqlQuery("select channelid from tlive where siteid='" + siteid + "' and tliveid=" + tliveid);
  153. if (rows.isEmpty()) {
  154. return getErrReturnObject().setErrMsg("找不到当前直播间").toString();
  155. }
  156. String channelid = rows.get(0).getString("channelid");
  157. Polyv polyv = new Polyv();
  158. String secretKey = polyv.updateChannelAuthExternal(channelid);
  159. if (secretKey == null) {
  160. return getErrReturnObject().toString();
  161. } else {
  162. dbConnect.runSqlUpdate("update tlive set fisneedauth=1,secretkey='" + secretKey + "' where siteid='" + siteid + "' and tliveid='" + tliveid + "'");
  163. //需将改为新的直播观看地址
  164. return queryChannelMain();
  165. }
  166. }
  167. /**
  168. * 关闭直播间外部授权设置
  169. *
  170. * @return
  171. */
  172. public String closeChannelAuth() {
  173. long tliveid = content.getLong("tliveid");
  174. Rows rows = dbConnect.runSqlQuery("select channelid from tlive where siteid='" + siteid + "' and tliveid=" + tliveid);
  175. if (rows.isEmpty()) {
  176. return getErrReturnObject().setErrMsg("找不到当前直播间").toString();
  177. }
  178. String channelid = rows.get(0).getString("channelid");
  179. Polyv polyv = new Polyv();
  180. if (polyv.closeChannelAuthExternal(channelid)) {
  181. dbConnect.runSqlUpdate("update tlive set fisneedauth=0,secretkey=null where siteid='" + siteid + "' and tliveid='" + tliveid + "'");
  182. //需将改为新的直播观看地址
  183. return queryChannelMain();
  184. } else {
  185. return getErrReturnObject().toString();
  186. }
  187. }
  188. /**
  189. * 直播间删除
  190. *
  191. * @return
  192. */
  193. public String deleteLive() {
  194. String tliveid = content.getString("tliveid");
  195. Rows rows = dbConnect.runSqlQuery("select channelid from tlive where siteid='" + siteid + "' and tliveid=" + tliveid);
  196. if (rows.isEmpty()) {
  197. return getErrReturnObject().setErrMsg("找不到当前直播间").toString();
  198. }
  199. String channelid = rows.get(0).getString("channelid");
  200. Polyv polyv = new Polyv();
  201. if (polyv.deleteChannel(channelid)) {
  202. SQLFactory sqlFactory = new SQLFactory(this, "直播间删除");
  203. sqlFactory.addParameter("siteid", siteid);
  204. sqlFactory.addParameter("tliveid", tliveid);
  205. String status = dbConnect.runSqlUpdate(sqlFactory.getSQL());
  206. if ("true".equalsIgnoreCase(status)) {
  207. return getSucReturnObject().toString();
  208. } else {
  209. return getErrReturnObject().setErrMsg(status).toString();
  210. }
  211. } else {
  212. return getErrReturnObject().setErrMsg("直播间删除失败").toString();
  213. }
  214. }
  215. /**
  216. * 直播授权
  217. *
  218. * @return
  219. */
  220. public String liveauthorization() {
  221. String tliveid = content.getString("tliveid");//直播ID
  222. Rows rows = dbConnect.runSqlQuery("select channelId,secretkey from tlive where siteid='" + siteid + "' and tliveid='" + tliveid + "'");
  223. if (rows.isEmpty()) {
  224. return getErrReturnObject().toString();
  225. }
  226. return getSucReturnObject().setData(new Polyv().getSignUrl(rows.get(0).getString("channelId"), rows.get(0).getString("secretkey"), userid)).toString();
  227. }
  228. private String createPassWord() {
  229. String allChar = "1234567890";
  230. StringBuffer sb = new StringBuffer();
  231. Random random = new Random();
  232. for (int i = 0; i < 6; i++) {
  233. sb.append(allChar.charAt(random.nextInt(allChar.length())));
  234. }
  235. return sb.toString();
  236. }
  237. /**
  238. * 直播申请列表查询
  239. *
  240. * @return
  241. */
  242. public String getSYLiveApplyList() {
  243. SQLFactory sqlFactory = new SQLFactory(this, "直播申请列表查询", pageSize, pageNumber, "changedate");
  244. Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL());
  245. return getSucReturnObject().setDataByPaging(rows).preloading(1).toString();
  246. }
  247. /**
  248. * 审核私域申请,自动创建直播间
  249. *
  250. * @return
  251. */
  252. public String auditSYLiveApply() throws P2Exception {
  253. String tliveapplyid = content.getString("tliveapplyid");
  254. String tagentsid = content.getString("tagentsid");
  255. String where = "tagentsid = " + tagentsid + " AND tliveapplyid = " + tliveapplyid;
  256. PaoSetRemote paoSetRemote = getP2ServerSystemPaoSet("tliveapply", where);
  257. if (paoSetRemote.isEmpty()) {
  258. return getErrReturnObject().setErrMsg("未找到对应申请").toString();
  259. } else {
  260. String sql = "SELECT*FROM tlive WHERE categoryname='私域直播' AND tagentsid=" + tagentsid;
  261. Rows rows = dbConnect.runSqlQuery(sql);
  262. if (rows.totalRows > 0) {
  263. return getErrReturnObject().setErrMsg("已存在私域直播").toString();
  264. } else {
  265. //修改状态
  266. PaoRemote paoRemote = paoSetRemote.getPao(0);
  267. paoRemote.setValue("fstatus", "审核");
  268. paoRemote.setValue("CHECKBY", username);
  269. paoRemote.setValue("CHECKDATE", getDateTime_Str());
  270. paoSetRemote.save();
  271. //开启直播
  272. content.put("tagentsid", tagentsid);
  273. content.put("channelname", "私域直播");
  274. content.put("categoryid", getCategoryId("私域直播"));
  275. return createLive();
  276. }
  277. }
  278. }
  279. /**
  280. * 通过频道分类名称获取对应的分类id
  281. *
  282. * @param categoryName
  283. * @return
  284. */
  285. public int getCategoryId(String categoryName) {
  286. Polyv polyv = new Polyv();
  287. HashMap<Integer, String> map = polyv.listCategory();
  288. int categoryId = -1;
  289. for (int id : map.keySet()) {
  290. if (map.get(id).equals(categoryName)) {
  291. categoryId = id;
  292. }
  293. }
  294. return categoryId;
  295. }
  296. }