live.action.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2011 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: shaoyv(172231343@qq.com)
  8. // +----------------------------------------------------------------------
  9. class liveModule extends baseModule
  10. {
  11. /**
  12. * 切换付费(主播端)
  13. */
  14. public function live_pay(){
  15. $root = array('status'=>1,'error'=>'');
  16. $data = array();
  17. if(!$GLOBALS['user_info']){
  18. $root['error'] = "用户未登陆,请先登陆.";
  19. $root['status'] = 0;
  20. $root['user_login_status'] = 0;//有这个参数: user_login_status = 0 时,表示服务端未登陆、要求登陆,操作
  21. }else{
  22. $m_config = load_auto_cache("m_config");//初始化手机端配置
  23. if(!isset($m_config)){
  24. $root['error'] = "初始化手机端配置错误";
  25. $root['status'] = 0;
  26. ajax_return($root);
  27. }
  28. $user_id = intval($GLOBALS['user_info']['id']);//用户ID
  29. $room_id = intval($_REQUEST['room_id']);//直播ID 也是room_id
  30. $live_fee = intval($_REQUEST['live_fee']);//直播收取的费用 (钻石/分钟)
  31. $pay_type = intval($_REQUEST['live_pay_type']);//收费类型 0按时收费,1按场次收费
  32. //提档
  33. $is_mention = intval($_REQUEST['is_mention']);//提档 0不提档 1 提档
  34. //按时
  35. $live_pay_max = intval($m_config['live_pay_max']);//付费直播收费最高
  36. $live_pay_min = intval($m_config['live_pay_min']);//付费直播收费最低
  37. //按场
  38. $live_pay_scene_max = intval($m_config['live_pay_scene_max']);//付费直播收费最高
  39. $live_pay_scene_min = intval($m_config['live_pay_scene_min']);//付费直播收费最低
  40. if($pay_type ==0 &&(defined('LIVE_PAY')&&LIVE_PAY==0)){
  41. $root['error'] = "按时付费未开启";
  42. $root['status'] = 0;
  43. ajax_return($root);
  44. }
  45. if($pay_type == 1 && (defined('LIVE_PAY_SCENE')&&LIVE_PAY_SCENE==0)){
  46. $root['error'] = "按场付费未开启";
  47. $root['status'] = 0;
  48. ajax_return($root);
  49. }
  50. if($pay_type==0&&$live_pay_max<$live_fee&&$is_mention==0&&$live_pay_max>0){
  51. $root['error'] = "按时收费不能高于".$live_pay_max.$m_config['diamonds_name'];
  52. $root['status'] = 0;
  53. ajax_return($root);
  54. }
  55. if($pay_type==0&&$live_pay_min>$live_fee&&$is_mention==0){
  56. $root['error'] = "按时收费不能低于".$live_pay_min.$m_config['diamonds_name'];
  57. $root['status'] = 0;
  58. ajax_return($root);
  59. }
  60. if($pay_type==1&&$live_pay_scene_max<$live_fee&&$is_mention==0&&$live_pay_scene_max>0){
  61. $root['error'] = "按场收费不能高于".$live_pay_scene_max.$m_config['diamonds_name'];
  62. $root['status'] = 0;
  63. ajax_return($root);
  64. }
  65. if($pay_type==1&&$live_pay_scene_min>$live_fee&&$is_mention==0){
  66. $root['error'] = "按场收费不能低于".$live_pay_scene_min.$m_config['diamonds_name'];
  67. $root['status'] = 0;
  68. ajax_return($root);
  69. }
  70. //判断付费是否开启
  71. $pay_info = $this->get_pay_info();
  72. if($pay_type==0&&intval($pay_info['live_pay'])==0){
  73. $root['error'] = "按时付费未开启";
  74. $root['status'] = 0;
  75. ajax_return($root);
  76. }
  77. if($pay_type==1&&intval($pay_info['live_pay_scene'])==0){
  78. $root['error'] = "按付费未开启";
  79. $root['status'] = 0;
  80. ajax_return($root);
  81. }
  82. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/VideoRedisService.php');
  83. $video_redis = new VideoRedisService();
  84. $fields = array('group_id','live_fee','live_is_mention','is_live_pay','user_id');
  85. $video_info = $video_redis->getRow_db($room_id,$fields);
  86. $live_pay_type = intval($video_info['live_pay_type']);
  87. //实际付费人数
  88. if($live_pay_type==0){
  89. $times = get_gmtime()-60;
  90. $sql ="select count(*) from ".DB_PREFIX."live_pay_log where video_id =".$room_id." and pay_time_end>=".$times;
  91. $live_viewer = $GLOBALS['db']->getOne($sql,true,true);
  92. }else{
  93. $live_viewer = $GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."live_pay_log where video_id =".$room_id,true,true);
  94. }
  95. $group_id = $video_info['group_id'];
  96. if(!$room_id){
  97. $root['error'] = "直播ID不存在";
  98. $root['status'] = 0;
  99. ajax_return($root);
  100. }
  101. $m_config['live_count_down'] = intval($m_config['live_count_down'])?intval($m_config['live_count_down']):120;
  102. $live_pay_time = intval(NOW_TIME+$m_config['live_count_down']);
  103. //加入直播意外终止的问题
  104. $p_user_id = $video_info['user_id'];
  105. //获取支付信息
  106. $sql = "select id,user_id,live_pay_type,live_fee from ".DB_PREFIX."video where user_id = ".$p_user_id." and live_pay_type=".$pay_type." and is_live_pay =1 and is_aborted = 1 and live_in !=3";
  107. $live_old_info = $GLOBALS['db']->getRow($sql);
  108. $live_old_id = intval($live_old_info['id']);//被服务器异常终止结束(主要是心跳超时)
  109. $pay_room_id = 0;
  110. if($live_old_id>0){
  111. $pay_room_id = $live_old_id;
  112. }
  113. //提档流程
  114. if($is_mention==1&&$pay_type==0){
  115. if(intval($video_info['is_live_pay'])==1&&intval($video_info['live_fee'])>0&&intval($video_info['live_fee'])==0){
  116. $root['status'] = 0;
  117. ajax_return($root);
  118. }
  119. if(intval($m_config['live_pay_fee'])==0){
  120. $root['status'] = 0;
  121. $root['error'] = "提档参数不存在";
  122. ajax_return($root);
  123. }
  124. $live_fee = intval($m_config['live_pay_fee']+$video_info['live_fee']);
  125. //更新付费信息
  126. $sql = "update ".DB_PREFIX."video set live_is_mention =1,live_fee = ".$live_fee.",live_pay_type = ".$pay_type.",pay_room_id = ".$pay_room_id." where live_pay_type=0 and is_live_pay =1 and live_in =1 and id = ".$room_id." and user_id = ".$user_id;
  127. }else{
  128. if((defined('PUBLIC_PAY')&&PUBLIC_PAY==1)&&$m_config['switch_public_pay']==1&&$m_config['public_pay']>0){
  129. }else{
  130. if(intval($video_info['is_live_pay'])==1&&intval($video_info['live_fee'])>0){
  131. $root['status'] = 0;
  132. ajax_return($root);
  133. }
  134. }
  135. if((defined('PUBLIC_PAY')&&PUBLIC_PAY==1)&&$m_config['switch_public_pay']==1&&$m_config['public_pay']>0){
  136. $sql = "update ".DB_PREFIX."video set live_fee = '".$live_fee."',public_screen = 0,live_pay_type = '".$pay_type."',is_live_pay =1,live_pay_time = '".$live_pay_time."',pay_room_id = ".$pay_room_id." where is_live_pay =1 and live_in =1 and public_screen=1 and id = ".$room_id." and user_id = ".$user_id;
  137. $public_screen = $GLOBALS['db']->getOne("SELECT public_screen FROM ".DB_PREFIX."video WHERE user_id=" . $user_id . " and live_in=1");
  138. }else{
  139. $sql = "update ".DB_PREFIX."video set live_fee = '".$live_fee."',live_pay_type = '".$pay_type."',is_live_pay =1,live_pay_time = '".$live_pay_time."',pay_room_id = ".$pay_room_id." where is_live_pay =0 and live_in =1 and id = ".$room_id." and user_id = ".$user_id;
  140. }
  141. }
  142. $GLOBALS['db']->query($sql);
  143. if($GLOBALS['db']->affected_rows()){
  144. $data['live_fee'] = $live_fee;
  145. $data['live_viewer']=intval($live_viewer);
  146. $data['pay_type'] = $pay_type;
  147. $data['count_down'] = $m_config['live_count_down'];
  148. if($is_mention){
  149. $data['count_down'] = 0;
  150. }
  151. $root = $data;
  152. $root['status'] = 1;
  153. $root['error'] = '';
  154. sync_video_to_redis($room_id,'*',false);
  155. if($is_mention==0){
  156. //=========================================================
  157. //广播:开始进入收费直播
  158. //发送广播:收费直播
  159. fanwe_require(APP_ROOT_PATH.'system/tim/TimApi.php');
  160. $api = createTimAPI();
  161. $ext = array();
  162. if($pay_type==1){
  163. $ext['type'] = 40; //40:按场收费直播(全体推送的,用于通知用户即将进入收费直播)
  164. }else{
  165. $ext['type'] = 32; //32:按时收费直播(全体推送的,用于通知用户即将进入收费直播)
  166. }
  167. $ext['room_id'] = $room_id;//直播ID 也是room_id;只有与当前房间相同时,收到消息才响应
  168. #构造高级接口所需参数
  169. $msg_content = array();
  170. //创建array 所需元素
  171. $msg_content_elem = array(
  172. 'MsgType' => 'TIMCustomElem', //自定义类型
  173. 'MsgContent' => array(
  174. 'Data' => json_encode($ext),
  175. )
  176. );
  177. array_push($msg_content, $msg_content_elem);
  178. //32:收费直播
  179. //$api->group_send_group_msg2($group_id, $msg_content);
  180. //$api->openim_push($user_id, $msg_content,0);
  181. $ret = $api->group_send_group_msg2($user_id, $group_id, $msg_content);
  182. //=========================================================
  183. if ($ret['ActionStatus'] == 'FAIL' && $ret['ErrorCode'] == 10002){
  184. //10002 系统错误,请再次尝试或联系技术客服。
  185. log_err_file(array(__FILE__,__LINE__,__METHOD__,$ret));
  186. $ret = $api->group_send_group_msg2($user_id, $group_id, $msg_content);
  187. }
  188. if ($ret['ActionStatus'] == 'FAIL'){
  189. $root['error'] = $ret['ErrorInfo'].":".$ret['ErrorCode'];
  190. }else{
  191. if($pay_type==1){
  192. $root['error'] = '已切换为付费直播,'.$live_fee.$m_config['diamonds_name'].'/场';
  193. }else{
  194. $root['error'] = '已切换为付费直播,'.$live_fee.$m_config['diamonds_name'].'/分钟';
  195. }
  196. }
  197. }else{
  198. if($pay_type==0&&$is_mention==1){
  199. $root['error'] = '提档成功,'.$live_fee.$m_config['diamonds_name'].'/分钟';
  200. }
  201. }
  202. }else{
  203. $root['status'] = 0;
  204. $root['error'] = '切换失败';
  205. }
  206. }
  207. ajax_return($root);
  208. }
  209. /**
  210. * 扣费(观众端)
  211. */
  212. public function live_pay_deduct(){
  213. $root = array('status'=>1,'error'=>'');
  214. if(!$GLOBALS['user_info']){
  215. $root['error'] = "用户未登陆,请先登陆.";
  216. $root['status'] = 0;
  217. $root['user_login_status'] = 0;//有这个参数: user_login_status = 0 时,表示服务端未登陆、要求登陆,操作
  218. }else{
  219. $user_id = intval($GLOBALS['user_info']['id']);//用户ID
  220. $room_id = intval($_REQUEST['room_id']);//直播ID 也是room_id
  221. $is_agree = intval($_REQUEST['is_agree']);//是否同意
  222. if(!$room_id){
  223. $root['error'] = "直播ID不存在";
  224. $root['status'] = 0;
  225. ajax_return($root);
  226. }
  227. $user_info = $GLOBALS['user_info'];
  228. /*fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/VideoRedisService.php');
  229. $video_redis = new VideoRedisService();
  230. $fields = array('id','is_aborted','end_time','live_in','user_id','group_id','live_pay_time','live_pay_type','live_fee','live_is_mention','pay_room_id');
  231. $video_info = $video_redis->getRow_db($room_id,$fields);*/
  232. $sql = "select id,is_aborted,end_time,live_in,user_id,group_id,live_pay_time,live_pay_type,live_fee,live_is_mention,pay_room_id from ".DB_PREFIX."video where id =".$room_id;
  233. $video_info = $GLOBALS['db']->getRow($sql);
  234. //判断直播结束
  235. if(empty($video_info)){
  236. $video_info = $GLOBALS['db']->getRow($sql);
  237. }
  238. if(intval($video_info['live_in'])==0&&$video_info['end_time']==''&&$video_info['is_aborted']!=1){
  239. $root['error'] = "直播已结束不存在";
  240. $root['status'] = 0;
  241. ajax_return($root);
  242. }
  243. //判断付费是否开启
  244. $pay_info = $this->get_pay_info();
  245. if($video_info['live_pay_type']==0&&intval($pay_info['live_pay'])==0){
  246. $root['error'] = "按时付费未开启";
  247. $root['status'] = 0;
  248. ajax_return($root);
  249. }
  250. if($video_info['live_pay_type']==1&&intval($pay_info['live_pay_scene'])==0){
  251. $root['error'] = "按场付费未开启";
  252. $root['status'] = 0;
  253. ajax_return($root);
  254. }
  255. //观众在直播间内
  256. if(0){
  257. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/VideoViewerRedisService.php');
  258. $video_viewer_redis = new VideoViewerRedisService();
  259. $video_viewer = $video_viewer_redis->existence_viewer_list($room_id,$user_id);
  260. $in_room = $video_viewer['video_viewer_level_score'];
  261. }else{
  262. $in_room = 1;
  263. }
  264. if(intval($video_info['live_fee'])!=0&&$in_room&&$video_info['user_id']!=$user_id){
  265. $data = $this->pay_deduct($video_info,$user_info,$is_agree);
  266. $root = $data;
  267. }
  268. }
  269. ajax_return($root);
  270. }
  271. /**
  272. * 扣费
  273. */
  274. public function pay_deduct($video_info,$user_info,$is_agree){
  275. $data = array('status'=>1,'error'=>'');
  276. $user_id = $user_info['id'];
  277. $room_id = $video_info['id'];
  278. $new_room_id = 0;
  279. if(intval($video_info['pay_room_id'])>0){
  280. $room_id = $video_info['pay_room_id'];
  281. $new_room_id = $video_info['id'];
  282. }
  283. //
  284. //扣费开始
  285. $m_config = load_auto_cache("m_config");//初始化手机端配置
  286. $m_config['pay_interval'] = 1;//扣费间隔时间 (分)
  287. //开始扣费剩余时间
  288. $data['is_live_pay'] = 1;
  289. $data['live_pay_type'] = intval($video_info['live_pay_type']);
  290. $data['is_agree'] = $is_agree;
  291. $data['live_fee'] = $video_info['live_fee'];
  292. if($data['live_pay_type']==0){
  293. $live_time = $video_info['live_pay_time']-NOW_TIME;
  294. $live_time = $live_time>0?intval($live_time):0;
  295. }else{
  296. $live_time = 0;
  297. }
  298. $now_time = NOW_TIME;
  299. //倒计时结束 and 同意扣费
  300. if(floatval($m_config['uesddiamonds_to_score'])<=0){
  301. $data = array('status'=>0,'error'=>'uesddiamonds_to_score参数错误');
  302. return $data;
  303. }
  304. //增加:主播印票
  305. if(floatval($m_config['ticket_to_rate'])<=0){
  306. $data = array('status'=>0,'error'=>'ticket_to_rate参数错误');
  307. return $data;
  308. }
  309. if($is_agree){//
  310. $data['on_live_pay'] = 1; //收费中
  311. $data['count_down'] = 0;
  312. $total_time = intval($m_config['pay_interval']*60);//本次观看时间
  313. if((defined('PUBLIC_PAY')&&PUBLIC_PAY==1)&&$m_config['switch_public_pay']==1&&$m_config['public_pay']>0){
  314. $sql = "select id,pay_time_next,total_time,total_diamonds,pay_type from ".DB_PREFIX."live_pay_log where from_user_id = ".$user_id." and to_user_id = ".$video_info['user_id']." and video_id=".$room_id;
  315. $live_pay_log_info = $GLOBALS['db']->getRow($sql);
  316. if(intval($live_pay_log_info['id'])==0){
  317. $sql = "select id,pay_time_next,total_time,total_diamonds,pay_type from ".DB_PREFIX."live_pay_log_history where from_user_id = ".$user_id." and to_user_id = ".$video_info['user_id']." and video_id=".$room_id;
  318. $live_pay_log_info = $GLOBALS['db']->getRow($sql);
  319. }
  320. }else{
  321. //获取支付信息
  322. $sql = "select id,pay_time_next,total_time,total_diamonds from ".DB_PREFIX."live_pay_log where from_user_id = ".$user_id." and to_user_id = ".$video_info['user_id']." and video_id=".$room_id;
  323. $live_pay_log_info = $GLOBALS['db']->getRow($sql);
  324. if(intval($live_pay_log_info['id'])==0){
  325. $sql = "select id,pay_time_next,total_time,total_diamonds from ".DB_PREFIX."live_pay_log_history where from_user_id = ".$user_id." and to_user_id = ".$video_info['user_id']." and video_id=".$room_id;
  326. $live_pay_log_info = $GLOBALS['db']->getRow($sql);
  327. }
  328. }
  329. //开始扣费的条件
  330. //一、按时扣费 => 1.未扣费过;2.扣费过 and 达到扣费时间
  331. //二、按场收费 => 1.未扣费过;
  332. $allow = 0;
  333. //可扣费 =》 按时扣费 AND 扣费过 AND 达到扣费时间
  334. if(intval($live_pay_log_info['id'])>0&&$live_pay_log_info['pay_time_next'] <= $now_time&&intval($video_info['live_pay_type'])==0){
  335. $allow = 1;
  336. }elseif(intval($live_pay_log_info['id'])==0){
  337. //可扣费 =》 未扣费过
  338. $allow = 1;
  339. }else{
  340. if((defined('PUBLIC_PAY')&&PUBLIC_PAY==1)&&$m_config['switch_public_pay']==1&&$m_config['public_pay']>0)
  341. {
  342. $public_screen = $GLOBALS['db']->getOne("SELECT public_screen FROM ".DB_PREFIX."video WHERE user_id=" . $video_info['user_id']. " and live_in=1");
  343. if($live_pay_log_info['pay_type']==1&&$public_screen==0&&$video_info['live_pay_type']==1){
  344. $allow = 1;
  345. }
  346. }
  347. }
  348. if(intval($video_info['live_in'])==0){
  349. $root['error'] = "直播已被关闭";
  350. $root['status'] = 0;
  351. ajax_return($root);
  352. }
  353. if($allow){
  354. //-----事务开始-----
  355. $pInTrans = $GLOBALS['db']->StartTrans();
  356. try
  357. {
  358. //扣除观众的钻石,增加经验
  359. $total_score = intval($video_info['live_fee']*floatval($m_config['uesddiamonds_to_score']));
  360. if($live_time==0){
  361. $sql = "update ".DB_PREFIX."user set diamonds = diamonds-".$video_info['live_fee'].",use_diamonds = use_diamonds+".$video_info['live_fee'].", score = score + ".$total_score." where diamonds >= ".$video_info['live_fee']." and id = ".$user_id;
  362. $GLOBALS['db']->query($sql);
  363. }
  364. if($GLOBALS['db']->affected_rows()||intval($live_time)>0){
  365. $podcast_user_id = intval($video_info['user_id']);
  366. if((defined('PUBLIC_PAY')&&PUBLIC_PAY==1)&&$m_config['switch_public_pay']==1&&$m_config['public_pay']>0){
  367. $public_screen = $GLOBALS['db']->getOne("SELECT public_screen FROM " . DB_PREFIX . "video WHERE user_id=" . $podcast_user_id . " and live_in=1");
  368. }
  369. if (intval($public_screen)!= 1) {
  370. if (defined('CHILD_ROOM') && CHILD_ROOM == 1) {
  371. fanwe_require(APP_ROOT_PATH . 'mapi/lib/ChildRoom.class.php');
  372. $child_room = new child_room();
  373. $parent_id = $child_room->parent_id($room_id);
  374. if ($room_id != $parent_id) {
  375. $podcast_user_id = $GLOBALS['db']->getOne("SELECT user_id FROM " . DB_PREFIX . "video WHERE id=" . $parent_id . " and live_in=1");
  376. $room_id = $parent_id;
  377. }
  378. }
  379. //主播获得的印票
  380. $total_ticket = round($video_info['live_fee'] * floatval($m_config['ticket_to_rate']), 2);
  381. if ($total_ticket > 0 && $live_time == 0) {
  382. $sql = "update " . DB_PREFIX . "user set ticket = ticket + " . $total_ticket . " where id = " . $podcast_user_id;
  383. $GLOBALS['db']->query($sql);
  384. //更新主播直播间获得印票
  385. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoContributionRedisService.php');
  386. $videoCont_redis = new VideoContributionRedisService();
  387. $videoCont_redis->insert_db($user_id, $podcast_user_id, $room_id, $total_ticket);
  388. }
  389. $room_id = $video_info['id'];
  390. }
  391. //第一次付费
  392. if(intval($live_pay_log_info['id'])==0){
  393. $live_pay_log = array();
  394. $live_pay_log['total_diamonds'] = $video_info['live_fee'];//钻石(from_user_id减少的钻石)合计
  395. $live_pay_log['from_user_id'] = $user_id;//观众
  396. $live_pay_log['to_user_id'] = $video_info['user_id'];//主播
  397. $live_pay_log['create_time'] = $now_time;//时间
  398. $live_pay_log['create_date'] = to_date($now_time);//日期字段,按日期归档;
  399. $live_pay_log['create_ym'] = to_date($now_time,'Ym');//年月 如:201610
  400. $live_pay_log['create_d'] = to_date($now_time,'d');//日
  401. $live_pay_log['create_w'] = to_date($now_time,'w');//周
  402. $live_pay_log['live_fee'] = $video_info['live_fee'];//收取费用(钻石/分钟
  403. if(intval($live_time)){
  404. $live_pay_time = $video_info['live_pay_time'] + intval($live_time);
  405. }else{
  406. $live_pay_time = $video_info['live_pay_time'];
  407. }
  408. $live_pay_log['live_pay_time'] = $live_pay_time;//直播间开始收费时间
  409. $live_pay_log['live_pay_date'] = to_date($live_pay_time);//直播间开始收费 日期字段
  410. $live_pay_log['video_id'] = $room_id;//直播ID
  411. $live_pay_log['group_id'] = $video_info['group_id'];//群组ID
  412. $live_pay_log['pay_time_end'] = $live_pay_time;//最后一次扣款时间
  413. $live_pay_log['live_pay_type'] = intval($video_info['live_pay_type']);//直播类型
  414. $live_pay_log['total_ticket'] = $total_ticket;//主播获得的印票
  415. $live_pay_log['total_score'] = $total_score;//积分(from_user_id可获得的积分)合计
  416. $live_pay_log['uesddiamonds_to_score'] = intval($m_config['uesddiamonds_to_score']);//观众(from_user_id)获得积分的转换比例
  417. $live_pay_log['ticket_to_rate'] = intval($m_config['ticket_to_rate']);//主播(to_user_id)获得的印票转换比例
  418. if($live_time>0){
  419. $live_pay_log['total_diamonds'] = 0;//钻石(from_user_id减少的钻石)合计
  420. $live_pay_log['total_ticket'] = 0;//主播获得的印票
  421. }
  422. if((defined('PUBLIC_PAY')&&PUBLIC_PAY==1)&&$m_config['switch_public_pay']==1&&$m_config['public_pay']>0)
  423. {
  424. if($public_screen==1){
  425. $live_pay_log['pay_type'] = 1;//是否为公屏收费记录
  426. }
  427. }
  428. if(intval($video_info['live_pay_type'])==0){
  429. if(intval($live_time)){
  430. $now_live_time = intval($live_time);
  431. $live_pay_log['pay_time_next'] = $now_time+$now_live_time;//下次扣款时间
  432. }else{
  433. $live_pay_log['pay_time_next'] = $now_time+60*intval($m_config['pay_interval']);//下次扣款时间
  434. }
  435. }
  436. if(intval($live_time)==0)$live_pay_log['total_time'] = 60;//第一次扣费累计时间(from_user_id付费观看的时间)合计
  437. $GLOBALS['db']->autoExecute(DB_PREFIX."live_pay_log", $live_pay_log,"INSERT");
  438. $count_down = $video_info['live_pay_time']-$now_time>0?$video_info['live_pay_time']-$now_time:0;
  439. //更新主播扣费人数
  440. $sql = "update ".DB_PREFIX."video set live_pay_count = live_pay_count + 1 where is_live_pay =1 and id = ".$room_id." and user_id = ".$user_id;
  441. $GLOBALS['db']->query($sql);
  442. }else{
  443. $pay_time_end = $live_pay_log_info['pay_time_next'];
  444. $pay_time_next = $pay_time_end+60;//下次扣款时间
  445. if($now_time-$pay_time_end>=30){
  446. $pay_time_next = $now_time+60;
  447. }
  448. if($video_info['live_is_mention']){
  449. //提档
  450. $live_is_mention_time = $now_time;//提档开始时间
  451. $live_is_mention_pay = $video_info['live_fee'];//提档前扣费
  452. $sql = "update ".DB_PREFIX."live_pay_log set ticket_to_rate = ".intval($m_config['ticket_to_rate']).",uesddiamonds_to_score = ".intval($m_config['uesddiamonds_to_score']).",total_score = total_score + ".$total_score.", total_ticket = total_ticket + ".$total_ticket.", new_room_id=".$new_room_id.",live_is_mention_time =".$live_is_mention_time.",live_is_mention_pay =".$live_is_mention_pay.",total_diamonds=total_diamonds+".$live_is_mention_pay.",total_time=total_time+".$total_time.", pay_time_end = ".$pay_time_end.",pay_time_next = ".$pay_time_next." where from_user_id = ".$user_id." and to_user_id = ".$video_info['user_id']." and video_id=".$room_id;
  453. }else{
  454. $sql = "update ".DB_PREFIX."live_pay_log set ticket_to_rate = ".intval($m_config['ticket_to_rate']).",uesddiamonds_to_score = ".intval($m_config['uesddiamonds_to_score']).",total_score = total_score + ".$total_score.", total_ticket = total_ticket + ".$total_ticket.", new_room_id=".$new_room_id.",total_diamonds=total_diamonds+".$video_info['live_fee'].",total_time=total_time+".$total_time.", pay_time_end = ".$pay_time_end.",pay_time_next = ".$pay_time_next." where from_user_id = ".$user_id." and to_user_id = ".$video_info['user_id']." and video_id=".$room_id;
  455. }
  456. $GLOBALS['db']->query($sql);
  457. }
  458. if($live_time==0){
  459. //写入用户日志
  460. $log_data = array();
  461. $log_data['diamonds'] = intval($video_info['live_fee']);
  462. $log_data['log_admin_id'] = 0;
  463. $param['type'] = 6;//类型 0表示充值 1表示提现 2赠送道具 3 兑换印票 4 分享获得印票 5 登录赠送积分 6 观看付费直播
  464. $log_msg = '观看付费直播间' . $room_id . ',消费' . $video_info['live_fee'] .$m_config['diamonds_name'];
  465. account_log_com($log_data, $user_id, $log_msg, $param);
  466. }
  467. //获取用户当前的钻石数量
  468. $sql = "select diamonds from ".DB_PREFIX."user where id = ".$user_id;
  469. $diamonds = $GLOBALS['db']->getOne($sql);
  470. $data['msg'] = "";
  471. //判断用户是否足够观看5分钟
  472. if(intval($video_info['live_pay_type'])==0){
  473. if($diamonds>$video_info['live_fee']*5){
  474. $data['is_diamonds_low'] = 0;
  475. $data['is_recharge'] = 0;
  476. }elseif($diamonds<$video_info['live_fee']){
  477. $data['is_diamonds_low'] = 1;
  478. $data['is_recharge'] = 1;
  479. $data['msg'] = "您当前的".$m_config['diamonds_name']."数量过少,是否前往充值?";
  480. }else{
  481. $data['is_diamonds_low'] = 1;
  482. $data['is_recharge'] = 0;
  483. $surplus_time = intval($diamonds/$video_info['live_fee']);
  484. $data['msg'] = "您当前的".$m_config['diamonds_name']."数量过少,剩余观看".$surplus_time."分钟";
  485. }
  486. }else{
  487. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserFollwRedisService.php');
  488. $user_follw_redis = new UserFollwRedisService($user_id);
  489. if(!$user_follw_redis->is_following($video_info['user_id'])){
  490. if(intval($video_info['pay_room_id'])>0){
  491. $room_id = $video_info['id'];
  492. }
  493. //自动关注主播
  494. $root = redis_set_follow($user_id,$video_info['user_id'],false,$room_id);
  495. clear_auto_cache("playback_list",array("user_id"=>$user_id));
  496. }
  497. }
  498. }else{
  499. $data['is_diamonds_low'] = 1;
  500. $data['is_recharge'] = 1;
  501. $data['msg'] = "您当前的".$m_config['diamonds_name']."数量过少,是否前往充值?";
  502. $data['count_down'] = 0;
  503. }
  504. //同步信息
  505. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserRedisService.php');
  506. $user_redis = new UserRedisService();
  507. $user_infos = $user_redis->getRow_db($user_id,array('id','score','online_time','user_level'));
  508. user_leverl_syn($user_infos);//更新用户等级
  509. user_deal_to_reids(array($user_id,$video_info['user_id']));//同步mysql的user信息到redis
  510. sync_video_to_redis($room_id,'*',false);//将mysql的video数据,同步一份到redis
  511. //提交事务
  512. $GLOBALS['db']->Commit($pInTrans);
  513. }catch(Exception $e){
  514. //异常回滚
  515. $root['error'] = $e->getMessage();
  516. $root['status'] = 0;
  517. $GLOBALS['db']->Rollback($pInTrans);
  518. }
  519. //-----事务结束-----
  520. //获取支付信息
  521. $sql = "select id,pay_time_next,total_time,total_diamonds from ".DB_PREFIX."live_pay_log where from_user_id = ".$user_id." and to_user_id = ".$video_info['user_id']." and video_id=".$room_id." and total_time>60";
  522. $live_pay_log_infos = $GLOBALS['db']->getRow($sql);
  523. if($live_pay_log_infos){
  524. $total_times = intval($live_pay_log_infos['total_time']-60);//累计观看时间
  525. }else{
  526. $total_times = 0;//累计观看时间
  527. }
  528. $total_diamonds = $video_info['live_fee']+intval($live_pay_log_info['total_diamonds']);//累计消费钻石
  529. }else{
  530. $total_times = intval($live_pay_log_info['total_time']-$total_time);//累计观看时间
  531. $total_diamonds = intval($live_pay_log_info['total_diamonds']);//累计消费钻石
  532. }
  533. $data['total_time'] = intval($total_times)<=0?0:intval($total_times);//累计观看时间
  534. $data['total_diamonds'] = intval($total_diamonds);//累计消费钻石
  535. }else{
  536. $data['is_popup'] = 1;
  537. }
  538. $data['count_down'] = $live_time;//倒计时
  539. if($live_time==0){
  540. $data['on_live_pay'] = 1;//倒计时结束;已经开始收费
  541. }else{
  542. $data['on_live_pay'] = 0;//倒计时未结束;未实际收费
  543. }
  544. $sql = "select diamonds from ".DB_PREFIX."user where id = ".$user_id;
  545. $diamonds = $GLOBALS['db']->getOne($sql);
  546. $data['diamonds'] = intval($diamonds);
  547. //直播间主播获得的印票和钻石
  548. $sql = "select ticket from ".DB_PREFIX."user where id = ".$video_info['user_id'];
  549. $ticket = $GLOBALS['db']->getOne($sql);
  550. $data['ticket'] = intval($ticket);
  551. return $data;
  552. }
  553. /*
  554. * 付费直播 消费榜
  555. */
  556. public function pay_cont(){
  557. $root = array('status'=>1,'error'=>'');
  558. if(!$GLOBALS['user_info']){
  559. $root['error'] = "用户未登陆,请先登陆.";
  560. $root['status'] = 0;
  561. $root['user_login_status'] = 0;//有这个参数: user_login_status = 0 时,表示服务端未登陆、要求登陆,操作
  562. }else{
  563. $user_id = intval($GLOBALS['user_info']['id']);//用户ID
  564. $type = intval($_REQUEST['type']);//类型 0消费,1收入
  565. $page = intval($_REQUEST['p']);//取第几页数据
  566. $live_pay_type = intval($_REQUEST['live_pay_type']);//付费直播 类型 0按时 1按场
  567. if(intval(LIVE_PAY_TIME)==0)$live_pay_type = 1;//兼容客户端
  568. if($page==0){
  569. $page = 1;
  570. }
  571. $page_size=30;
  572. $limit = (($page-1)*$page_size).",".$page_size;
  573. $where = " and live_pay_type = ".$live_pay_type;
  574. if($type){
  575. $sql = "select lp.video_id as room_id,lp.total_time,lp.create_time,lp.total_diamonds,lp.from_user_id as user_id,u.head_image,u.user_level,u.sex,u.nick_name,lp.live_pay_type from ".DB_PREFIX."live_pay_log_history lp left join ".DB_PREFIX."user as u on u.id = lp.from_user_id where lp.total_ticket>0 and lp.to_user_id = ".$user_id.$where." order by lp.create_time desc limit ".$limit ;
  576. }else{
  577. //$sql = "select lp.video_id as room_id,lp.total_time,lp.total_diamonds,lp.to_user_id as user_id,u.head_image,u.user_level,u.sex,u.nick_name from ".DB_PREFIX."live_pay_log lp left join ".DB_PREFIX."user as u on u.id = lp.from_user_id where lp.from_user_id = ".$user_id." group by lp.video_id order by lp.total_diamonds limit ".$limit ;
  578. $sql = "select lp.video_id as room_id,lp.total_time,lp.create_time,lp.total_diamonds,lp.to_user_id as user_id,u.head_image,u.user_level,u.sex,u.nick_name,lp.live_pay_type from ".DB_PREFIX."live_pay_log_history lp left join ".DB_PREFIX."user as u on u.id = lp.to_user_id where lp.total_ticket>0 and lp.from_user_id = ".$user_id.$where." order by lp.create_time desc limit ".$limit ;
  579. }
  580. $list = $GLOBALS['db']->getAll($sql,true,true);
  581. foreach($list as $k=>$v){
  582. $list[$k]['head_image'] = get_spec_image($v['head_image'],150,150);
  583. $total_time_format = get_live_time_len($v['total_time']);
  584. $total_time_format = $total_time_format?$total_time_format:"0分钟";
  585. $list[$k]['total_time_format'] =$total_time_format;
  586. if($live_pay_type==1){
  587. unset($list[$k]['total_time']);
  588. unset($list[$k]['total_time_format']);
  589. }
  590. $list[$k]['start_time'] =to_date($v['create_time']);
  591. $list[$k]['end_time'] =to_date($v['create_time']);
  592. $list[$k]['nick_name'] =emoji_decode($v['nick_name']);
  593. }
  594. $rs_count = count($list);
  595. if ($page == 1) {
  596. $root['page'] = array('page' => $page, 'has_next' => 0);
  597. }else {
  598. $has_next = ($rs_count > $page * $page_size) ? '1' : '0';
  599. $root['page'] = array('page' => $page, 'has_next' => $has_next);
  600. }
  601. $root['data'] = $list;
  602. }
  603. ajax_return($root);
  604. }
  605. public function get_pay_info(){
  606. //付费开关
  607. $live_pay_info= $GLOBALS['db']->getAll("SELECT id,class FROM ".DB_PREFIX."plugin WHERE is_effect=1 and type = 1");
  608. $live_pay = array();
  609. if($live_pay_info){
  610. foreach($live_pay_info as $k=>$v){
  611. $live_pay[$v['class']] = $v['id'];
  612. }
  613. }
  614. return $live_pay;
  615. }
  616. }
  617. ?>