VideoClassifiedAction.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: 云淡风轻(1956838968@qq.com)
  8. // +----------------------------------------------------------------------
  9. class VideoClassifiedAction extends CommonAction{
  10. public function update_class(){
  11. clear_auto_cache("m_config");
  12. $sql = $GLOBALS['db']->query( "update ".DB_PREFIX."m_config set val=val+1 where code ='init_version'");
  13. return $sql;
  14. }
  15. public function index()
  16. {
  17. if(strim($_REQUEST['title'])!=''){
  18. $map['title'] = array('like','%'.strim($_REQUEST['title']).'%');
  19. }
  20. if (method_exists ( $this, '_filter' )) {
  21. $this->_filter ( $map );
  22. }
  23. $name=$this->getActionName();
  24. $model = D ($name);
  25. if (! empty ( $model )) {
  26. $this->_list ( $model, $map );
  27. }
  28. $this->display ();
  29. }
  30. public function add()
  31. {
  32. $this->assign("new_sort", M("VideoClassified")->max("sort")+1);
  33. $this->display();
  34. }
  35. public function edit() {
  36. $id = intval($_REQUEST ['id']);
  37. $condition['id'] = $id;
  38. $vo = M(MODULE_NAME)->where($condition)->find();
  39. $this->assign ( 'vo', $vo );
  40. $this->display ();
  41. }
  42. public function foreverdelete() {
  43. //彻底删除指定记录
  44. $ajax = intval($_REQUEST['ajax']);
  45. $id = $_REQUEST ['id'];
  46. if (isset ( $id )) {
  47. $id = explode ( ',', $id );
  48. if(in_array(1,$id)){
  49. $this->error ("分类为课程的不能删除,请重新选择",$ajax);
  50. }
  51. //分类下有直播时不删除
  52. $id_count = count($id);
  53. $unset_num = '编号';
  54. foreach($id as $ki=>$vi){
  55. $video = M('Video')->where("classified_id=".$vi." and live_in<>0")->findAll();
  56. if($video){
  57. $unset_num .= ($vi.',');
  58. unset($id[$ki]);
  59. }
  60. }
  61. if(count($id)<$id_count){
  62. $id = array_values($id);
  63. $unset_num = substr($unset_num,0,-1);
  64. $unset_num .= '分类下有直播不能删除';
  65. }else {
  66. $unset_num = '';
  67. }
  68. $condition = array ('id' => array ('in', $id ) );
  69. $rel_data = M(MODULE_NAME)->where($condition)->findAll();
  70. foreach($rel_data as $data)
  71. {
  72. $info[] = $data['title'];
  73. }
  74. if($info) $info = implode(",",$info);
  75. $list = M(MODULE_NAME)->where ( $condition )->delete();
  76. if ($list!==false) {
  77. $this->update_class();
  78. clear_auto_cache("video_classified");
  79. save_log($info.l("FOREVER_DELETE_SUCCESS"),1);
  80. if($unset_num==''){
  81. $this->success (l("FOREVER_DELETE_SUCCESS"));
  82. }else{
  83. $this->success ($unset_num);
  84. }
  85. } else {
  86. save_log($info.l("FOREVER_DELETE_FAILED"),0);
  87. if($unset_num == ''){
  88. $this->error (l("FOREVER_DELETE_FAILED"));
  89. }else{
  90. $this->error ($unset_num);
  91. }
  92. }
  93. } else {
  94. $this->error (l("INVALID_OPERATION"));
  95. }
  96. }
  97. public function insert() {
  98. B('FilterString');
  99. $data = M(MODULE_NAME)->create();
  100. //开始验证有效性
  101. $this->assign("jumpUrl",u(MODULE_NAME."/add"));
  102. $data['title'] = strim($data['title']);
  103. if(!check_empty($data['title']))
  104. {
  105. $this->error("请输入分类名称");
  106. }
  107. if(mb_strlen($data['title'],'utf-8')>10){
  108. $this->error("分类名称不能大于10个字符");
  109. }
  110. $cate_id = $GLOBALS['db']->getOne("select id from ".DB_PREFIX."video_classified where title = '".$data['title']."'");
  111. if($cate_id){
  112. $this->error("分类名称已存在");
  113. }
  114. // 更新数据
  115. $log_info = $data['title'];
  116. $list=M(MODULE_NAME)->add($data);
  117. $m_config = load_auto_cache("m_config");//初始化手机端配置
  118. $init_version = intval($m_config['init_version']);//手机端配置版本号
  119. $info['init_version']=$init_version+1;
  120. if (false !== $list) {
  121. //成功提示
  122. $this->update_class();
  123. clear_auto_cache("video_classified");
  124. save_log($log_info.L("INSERT_SUCCESS"),1);
  125. $this->success(L("INSERT_SUCCESS"));
  126. } else {
  127. //错误提示
  128. save_log($log_info.L("INSERT_FAILED"),0);
  129. $this->error(L("INSERT_FAILED"));
  130. }
  131. }
  132. public function update() {
  133. B('FilterString');
  134. $data = M(MODULE_NAME)->create ();
  135. $log_info = M(MODULE_NAME)->where("id=".intval($data['id']))->getField("title");
  136. //开始验证有效性
  137. $this->assign("jumpUrl",u(MODULE_NAME."/edit",array("id"=>$data['id'])));
  138. if(!check_empty($data['title']))
  139. {
  140. $this->error("请输入分类名称");
  141. }
  142. if(mb_strlen($data['title'],'utf-8')>10){
  143. $this->error("分类名称不能大于10个字符");
  144. }
  145. $cate_id = $GLOBALS['db']->getOne("select id from ".DB_PREFIX."video_classified where title = '".$data['title']."'");
  146. if($cate_id && $cate_id!=$data['id']){
  147. $this->error("分类名称已存在,请重新填写!");
  148. }
  149. if(intval($data['is_effect'])==0){
  150. $video = M('Video')->where("classified_id=".intval($data['id'])." and live_in<>0")->findAll();
  151. if($video){
  152. $this->error("该分类下面有直播,不能设置无效");
  153. }
  154. }
  155. $list=M(MODULE_NAME)->save ($data);
  156. if (false !== $list) {
  157. $this->update_class();
  158. clear_auto_cache("video_classified");
  159. save_log($log_info.L("UPDATE_SUCCESS"),1);
  160. $this->success(L("UPDATE_SUCCESS"));
  161. } else {
  162. //错误提示
  163. save_log($log_info.L("UPDATE_FAILED"),0);
  164. $this->error(L("UPDATE_FAILED"),0,$log_info.L("UPDATE_FAILED"));
  165. }
  166. }
  167. public function upload_video(){
  168. $region_lv2 = $GLOBALS['db']->getAll("select * from ".DB_PREFIX."region_conf where region_level = 2 order by py asc"); //二级地址
  169. $this->assign("region_lv2",$region_lv2);
  170. $classified_id = intval($_REQUEST['classified_id']);
  171. if(!$classified_id){
  172. $this->error("分类编号错误");
  173. }
  174. $cate_list = M("VideoCate")->where("is_delete = 0 and is_effect = 1")->findAll();
  175. $this->assign('cate_list', $cate_list);
  176. $user_list = M("User")->where("is_effect = 1 and is_robot =0")->findAll();
  177. $this->assign ( 'classified_id', $classified_id);
  178. $this->assign ( 'user_list', $user_list);
  179. $m_config = load_auto_cache("m_config");
  180. $this->assign('secret_id', $m_config['qcloud_secret_id']);
  181. $this->display();
  182. }
  183. public function video_insert(){
  184. require_once APP_ROOT_PATH."/mapi/lib/core/common.php";
  185. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/BaseRedisService.php');
  186. $m_config = load_auto_cache("m_config");
  187. $result = array('status'=>1,'error'=>'视频上传成功,视频转码需要几分钟,请耐心等待');
  188. $user_id = intval($_REQUEST['user_id']);
  189. $cate_id = intval($_REQUEST['cate_id']);
  190. $title = trim($_REQUEST['title']);
  191. $file_id = trim($_REQUEST['file_id']);
  192. $is_live_pay = intval($_REQUEST['is_live_pay']);
  193. $live_fee = intval($_REQUEST['live_fee']);
  194. $live_image = trim($_REQUEST['live_image']);
  195. $classified_id = intval($_REQUEST['classified_id']);
  196. if($user_id == 0)
  197. {
  198. admin_ajax_return(array('status'=>'0','error'=>'请选择视频所属主播'));
  199. }
  200. if(!check_empty($title))
  201. {
  202. admin_ajax_return(array('status'=>'0','error'=>'请输入话题'));
  203. }
  204. if(!check_empty($file_id))
  205. {
  206. admin_ajax_return(array('status'=>'0','error'=>'请上传视频'));
  207. }
  208. $province = strim($_REQUEST['province']);//省
  209. $city = strim($_REQUEST['city']);//市
  210. //obs 推流延长首次心跳时间
  211. $obs_monitor_time = intval($m_config['obs_monitor_time']) ? intval($m_config['obs_monitor_time']) : 300;
  212. $monitor_time = to_date(NOW_TIME + $obs_monitor_time, 'Y-m-d H:i:s');//主播心跳监听
  213. //添加位置
  214. if ($province == 'null') {
  215. $province = '';
  216. }
  217. if ($city == 'null') {
  218. $city = '';
  219. }
  220. $province = str_replace("省", "", $province);
  221. $city = str_replace("市", "", $city);
  222. if ($province == '' || $city == '') {
  223. /*
  224. //服务端则用ip再定位一次
  225. fanwe_require APP_ROOT_PATH . "system/extend/ip.php";
  226. $ip = new iplocate ();
  227. $area = $ip->getaddress ( CLIENT_IP );
  228. $location = $area ['area1'];
  229. */
  230. $ipinfo = get_ip_info();
  231. $province = $ipinfo['province'];
  232. $city = $ipinfo['city'];
  233. }
  234. //
  235. $is_private = false;
  236. $share_type = '';
  237. $data = $this->create_video($user_id,$classified_id,$live_image, $title, $is_private, $monitor_time, $cate_id, $province, $city, $share_type,1,0,$is_live_pay,$live_fee);
  238. fanwe_require(APP_ROOT_PATH . 'mapi/lib/core/video_factory.php');
  239. $video_factory = new VideoFactory();
  240. $ret = $video_factory->ModifyVodInfo($file_id, $data);
  241. if (!$ret['status']) {
  242. admin_ajax_return($ret);
  243. }
  244. // 新上传的视频未生成地址
  245. $data['room_title']=$GLOBALS['user_info']['nick_name']."直播间";
  246. $data['is_del_vod'] = 1;
  247. $data['video_vid'] = $file_id;
  248. $data['end_time'] = NOW_TIME;//'结束时间'
  249. $GLOBALS['db']->autoExecute(DB_PREFIX . "video", $data, 'INSERT');
  250. if($GLOBALS['db']->insert_id()){
  251. save_log($data['id'].L("INSERT_SUCCESS"),1);
  252. sync_video_to_redis($data['id'], '*', false);
  253. }else{
  254. $result['status'] = 0;
  255. $result['error'] = "视频上传失败";
  256. save_log($data['id'].L("INSERT_FAILED"),0);
  257. }
  258. admin_ajax_return($result);
  259. }
  260. /**
  261. * 上传视频签名接口
  262. *
  263. * @return 签名
  264. */
  265. public function sign()
  266. {
  267. $args = $_REQUEST['args'];
  268. fanwe_require(APP_ROOT_PATH . 'mapi/lib/core/video_factory.php');
  269. $video_factory = new VideoFactory();
  270. $result = $video_factory->Sign($args);
  271. $root = array('status' => 1, 'result' => $result);
  272. ajax_file_return($root);
  273. }
  274. public function create_video($user_id,$classified_id,$live_image, $title, $is_private, $monitor_time, $cate_id = '', $province = '', $city = '', $share_type = '',$is_upload =0,$is_preparation = 0,$is_live_pay=0,$live_fee=0)
  275. {
  276. require_once APP_ROOT_PATH."/mapi/lib/core/common.php";
  277. //话题
  278. if ($cate_id) {
  279. //$cate_title = $GLOBALS['db']->getOne("select title from ".DB_PREFIX."video_cate where id=".$cate_id,true,true);
  280. $cate = load_auto_cache("cate_id", array('id' => $cate_id));
  281. $cate_title = $cate['title'];
  282. if ($cate_title != $title) {
  283. $cate_id = 0;
  284. }
  285. }
  286. if ($cate_id == 0 && $title != '') {
  287. $cate_id = $GLOBALS['db']->getOne("select id from " . DB_PREFIX . "video_cate where title='" . $title . "'", true, true);
  288. if ($cate_id) {
  289. $is_newtitle = 0;
  290. } else {
  291. $is_newtitle = 1;
  292. }
  293. }
  294. if ($is_newtitle) {
  295. $data_cate = array();
  296. $data_cate['title'] = $title;
  297. $data_cate['is_effect'] = 1;
  298. $data_cate['is_delete'] = 0;
  299. $data_cate['create_time'] = NOW_TIME;
  300. $GLOBALS['db']->autoExecute(DB_PREFIX . "video_cate", $data_cate, 'INSERT');
  301. $cate_id = $GLOBALS['db']->insert_id();
  302. }
  303. if ($province == '') {
  304. $province = '火星';
  305. }
  306. if ($city == '') {
  307. $city = '火星';
  308. }
  309. $video_id = get_max_room_id(0);
  310. $data = array();
  311. $data['id'] = $video_id;
  312. //room_type 房间类型 : 1私有群(Private),0公开群(Public),2聊天室(ChatRoom),3互动直播聊天室(AVChatRoom)
  313. if ($is_private == 1) {
  314. $data['room_type'] = 1;
  315. $data['private_key'] = md5($video_id . rand(1, 9999999));//私密直播key
  316. } else {
  317. $data['room_type'] = 3;
  318. }
  319. $data['classified_id'] = 0;
  320. if($classified_id){
  321. $data['classified_id'] = $classified_id;
  322. }
  323. $data['is_upload'] = 0;
  324. if($is_upload){
  325. $data['is_upload'] = 1;
  326. }
  327. $data['is_preparation'] = 0;
  328. if($is_preparation){
  329. $data['is_preparation'] = 1;
  330. }
  331. $data['is_live_pay'] = 0;
  332. if($is_live_pay){
  333. $data['is_live_pay'] = $is_live_pay;
  334. $data['live_pay_type'] = 1;
  335. $data['live_fee'] = $live_fee;
  336. }
  337. $m_config = load_auto_cache("m_config");
  338. $data['virtual_number'] = intval($m_config['virtual_number']);
  339. $data['max_robot_num'] = intval($m_config['robot_num']);//允许添加的最大机器人数;
  340. $sql = "select sex,ticket,refund_ticket,user_level,fans_count,head_image,thumb_head_image from " . DB_PREFIX . "user where id = " . $user_id;
  341. $user = $GLOBALS['db']->getRow($sql, true, true);
  342. $info = origin_image_info($user['head_image']);
  343. $data['head_image'] = get_spec_image($info['file_name']);
  344. $data['thumb_head_image'] = $user['thumb_head_image'];
  345. $data['live_image'] = $live_image;
  346. $data['sex'] = intval($user['sex']);//性别 0:未知, 1-男,2-女
  347. $data['video_type'] = $m_config['video_type'];//0:腾讯云互动直播;1:腾讯云直播;2:方维云直播
  348. require_once(APP_ROOT_PATH . 'system/tim/TimApi.php');
  349. $api = createTimAPI();
  350. $ret = $api->group_create_group('AVChatRoom', (string)$user_id, (string)$user_id, (string)$video_id);
  351. if ($ret['ActionStatus'] != 'OK') {
  352. api_ajax_return(array(
  353. 'status' => 0,
  354. 'error' => $ret['ErrorCode'] . $ret['ErrorInfo']
  355. ));
  356. }
  357. $data['group_id'] = $ret['GroupId'];
  358. $data['monitor_time'] = $monitor_time;
  359. $data['create_type'] = 1;// 0:APP端创建的直播;1:PC端创建的直播
  360. $data['push_url'] = '';//video_type=1;1:腾讯云直播推流地址
  361. $data['play_url'] = '';//video_type=1;1:腾讯云直播播放地址(rmtp,flv)
  362. $data['share_type'] = $share_type;
  363. $data['title'] = $title;
  364. $data['cate_id'] = $cate_id;
  365. $data['user_id'] = $user_id;
  366. $data['live_in'] = 3;//live_in:是否直播中 1-直播中 0-已停止;2:正在创建直播;
  367. $data['watch_number'] = '';//'当前观看人数';
  368. $data['vote_number'] = '';//'获得票数';
  369. $data['province'] = $province;//'省';
  370. $data['city'] = $city;//'城市';
  371. $data['create_time'] = NOW_TIME;//'创建时间';
  372. $data['begin_time'] = NOW_TIME;//'开始时间';
  373. $data['end_time'] = '';//'结束时间';
  374. $data['is_hot'] = 1;//'1热门; 0:非热门';
  375. $data['is_new'] = 1; //'1新的; 0:非新的,直播结束时把它标识为:0?'
  376. $data['online_status'] = 1;//主播在线状态;1:在线(默认); 0:离开
  377. //sort_init(初始排序权重) = (用户可提现印票:fanwe_user.ticket - fanwe_user.refund_ticket) * 保留印票权重+ 直播/回看[回看是:0; 直播:9000000000 直播,需要排在最上面 ]+ fanwe_user.user_level * 等级权重+ fanwe_user.fans_count * 当前有的关注数权重
  378. $sort_init = (intval($user['ticket']) - intval($user['refund_ticket'])) * floatval($m_config['ticke_weight']);
  379. $sort_init += intval($user['user_level']) * floatval($m_config['level_weight']);
  380. $sort_init += intval($user['fans_count']) * floatval($m_config['focus_weight']);
  381. $data['sort_init'] = 200000000 + $sort_init;
  382. $data['sort_num'] = $data['sort_init'];
  383. return $data;
  384. }
  385. public function set_effect()
  386. {
  387. $id = intval($_REQUEST['id']);
  388. $info = M(MODULE_NAME)->where("id=".$id)->getField("title");
  389. if($info == '课程'){
  390. $this->error("课程不能设置无效");
  391. }
  392. $c_is_effect = M(MODULE_NAME)->where("id=".$id)->getField("is_effect"); //当前状态
  393. $n_is_effect = $c_is_effect == 0 ? 1 : 0; //需设置的状态
  394. if($n_is_effect==0){
  395. $video = M('Video')->where("classified_id=".$id." and live_in<>0")->findAll();
  396. if($video){
  397. $this->error("该分类下面有直播,不能设置无效");
  398. }
  399. }
  400. M(MODULE_NAME)->where("id=".$id)->setField("is_effect",$n_is_effect);
  401. save_log($info.l("SET_EFFECT_".$n_is_effect),1);
  402. $this->update_class();
  403. clear_auto_cache("video_classified");
  404. $this->ajaxReturn($n_is_effect,l("SET_EFFECT_".$n_is_effect),1) ;
  405. }
  406. public function set_sort()
  407. {
  408. $id = intval($_REQUEST['id']);
  409. $sort = intval($_REQUEST['sort']);
  410. $log_info = M("VideoCate")->where("id=".$id)->getField("title");
  411. if(!check_sort($sort))
  412. {
  413. $this->error(l("SORT_FAILED"),1);
  414. }
  415. M(MODULE_NAME)->where("id=".$id)->setField("sort",$sort);
  416. save_log($log_info.l("SORT_SUCCESS"),1);
  417. $this->update_class();
  418. clear_auto_cache("video_classified");
  419. $this->success(l("SORT_SUCCESS"),1);
  420. }
  421. }
  422. ?>