live.action.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  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($allow){
  349. //-----事务开始-----
  350. $pInTrans = $GLOBALS['db']->StartTrans();
  351. try
  352. {
  353. //扣除观众的钻石,增加经验
  354. $total_score = intval($video_info['live_fee']*floatval($m_config['uesddiamonds_to_score']));
  355. if($live_time==0){
  356. $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;
  357. $GLOBALS['db']->query($sql);
  358. }
  359. if($GLOBALS['db']->affected_rows()||intval($live_time)>0){
  360. if((defined('PUBLIC_PAY')&&PUBLIC_PAY==1)&&$m_config['switch_public_pay']==1&&$m_config['public_pay']>0){
  361. $podcast_user_id = $video_info['user_id'];
  362. $public_screen = $GLOBALS['db']->getOne("SELECT public_screen FROM " . DB_PREFIX . "video WHERE user_id=" . $podcast_user_id . " and live_in=1");
  363. }
  364. if (intval($public_screen)!= 1) {
  365. if (defined('CHILD_ROOM') && CHILD_ROOM == 1) {
  366. fanwe_require(APP_ROOT_PATH . 'mapi/lib/ChildRoom.class.php');
  367. $child_room = new child_room();
  368. $parent_id = $child_room->parent_id($room_id);
  369. if ($room_id != $parent_id) {
  370. $podcast_user_id = $GLOBALS['db']->getOne("SELECT user_id FROM " . DB_PREFIX . "video WHERE id=" . $parent_id . " and live_in=1");
  371. $room_id = $parent_id;
  372. }
  373. }
  374. //主播获得的印票
  375. $total_ticket = round($video_info['live_fee'] * floatval($m_config['ticket_to_rate']), 2);
  376. if ($total_ticket > 0 && $live_time == 0) {
  377. $sql = "update " . DB_PREFIX . "user set ticket = ticket + " . $total_ticket . " where id = " . $podcast_user_id;
  378. $GLOBALS['db']->query($sql);
  379. //更新主播直播间获得印票
  380. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoContributionRedisService.php');
  381. $videoCont_redis = new VideoContributionRedisService();
  382. $videoCont_redis->insert_db($user_id, $podcast_user_id, $room_id, $total_ticket);
  383. }
  384. $room_id = $video_info['id'];
  385. }
  386. //第一次付费
  387. if(intval($live_pay_log_info['id'])==0){
  388. $live_pay_log = array();
  389. $live_pay_log['total_diamonds'] = $video_info['live_fee'];//钻石(from_user_id减少的钻石)合计
  390. $live_pay_log['from_user_id'] = $user_id;//观众
  391. $live_pay_log['to_user_id'] = $video_info['user_id'];//主播
  392. $live_pay_log['create_time'] = $now_time;//时间
  393. $live_pay_log['create_date'] = to_date($now_time);//日期字段,按日期归档;
  394. $live_pay_log['create_ym'] = to_date($now_time,'Ym');//年月 如:201610
  395. $live_pay_log['create_d'] = to_date($now_time,'d');//日
  396. $live_pay_log['create_w'] = to_date($now_time,'w');//周
  397. $live_pay_log['live_fee'] = $video_info['live_fee'];//收取费用(钻石/分钟
  398. if(intval($live_time)){
  399. $live_pay_time = $video_info['live_pay_time'] + intval($live_time);
  400. }else{
  401. $live_pay_time = $video_info['live_pay_time'];
  402. }
  403. $live_pay_log['live_pay_time'] = $live_pay_time;//直播间开始收费时间
  404. $live_pay_log['live_pay_date'] = to_date($live_pay_time);//直播间开始收费 日期字段
  405. $live_pay_log['video_id'] = $room_id;//直播ID
  406. $live_pay_log['group_id'] = $video_info['group_id'];//群组ID
  407. $live_pay_log['pay_time_end'] = $live_pay_time;//最后一次扣款时间
  408. $live_pay_log['live_pay_type'] = intval($video_info['live_pay_type']);//直播类型
  409. $live_pay_log['total_ticket'] = $total_ticket;//主播获得的印票
  410. $live_pay_log['total_score'] = $total_score;//积分(from_user_id可获得的积分)合计
  411. $live_pay_log['uesddiamonds_to_score'] = intval($m_config['uesddiamonds_to_score']);//观众(from_user_id)获得积分的转换比例
  412. $live_pay_log['ticket_to_rate'] = intval($m_config['ticket_to_rate']);//主播(to_user_id)获得的印票转换比例
  413. if($live_time>0){
  414. $live_pay_log['total_diamonds'] = 0;//钻石(from_user_id减少的钻石)合计
  415. $live_pay_log['total_ticket'] = 0;//主播获得的印票
  416. }
  417. if((defined('PUBLIC_PAY')&&PUBLIC_PAY==1)&&$m_config['switch_public_pay']==1&&$m_config['public_pay']>0)
  418. {
  419. if($public_screen==1){
  420. $live_pay_log['pay_type'] = 1;//是否为公屏收费记录
  421. }
  422. }
  423. if(intval($video_info['live_pay_type'])==0){
  424. if(intval($live_time)){
  425. $now_live_time = intval($live_time);
  426. $live_pay_log['pay_time_next'] = $now_time+$now_live_time;//下次扣款时间
  427. }else{
  428. $live_pay_log['pay_time_next'] = $now_time+60*intval($m_config['pay_interval']);//下次扣款时间
  429. }
  430. }
  431. if(intval($live_time)==0)$live_pay_log['total_time'] = 60;//第一次扣费累计时间(from_user_id付费观看的时间)合计
  432. $GLOBALS['db']->autoExecute(DB_PREFIX."live_pay_log", $live_pay_log,"INSERT");
  433. $count_down = $video_info['live_pay_time']-$now_time>0?$video_info['live_pay_time']-$now_time:0;
  434. //更新主播扣费人数
  435. $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;
  436. $GLOBALS['db']->query($sql);
  437. }else{
  438. $pay_time_end = $live_pay_log_info['pay_time_next'];
  439. $pay_time_next = $pay_time_end+60;//下次扣款时间
  440. if($now_time-$pay_time_end>=30){
  441. $pay_time_next = $now_time+60;
  442. }
  443. if($video_info['live_is_mention']){
  444. //提档
  445. $live_is_mention_time = $now_time;//提档开始时间
  446. $live_is_mention_pay = $video_info['live_fee'];//提档前扣费
  447. $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;
  448. }else{
  449. $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;
  450. }
  451. $GLOBALS['db']->query($sql);
  452. }
  453. if($live_time==0){
  454. //写入用户日志
  455. $log_data = array();
  456. $log_data['diamonds'] = intval($video_info['live_fee']);
  457. $log_data['log_admin_id'] = 0;
  458. $param['type'] = 6;//类型 0表示充值 1表示提现 2赠送道具 3 兑换印票 4 分享获得印票 5 登录赠送积分 6 观看付费直播
  459. $log_msg = '观看付费直播间' . $room_id . ',消费' . $video_info['live_fee'] .$m_config['diamonds_name'];
  460. account_log_com($log_data, $user_id, $log_msg, $param);
  461. }
  462. //获取用户当前的钻石数量
  463. $sql = "select diamonds from ".DB_PREFIX."user where id = ".$user_id;
  464. $diamonds = $GLOBALS['db']->getOne($sql);
  465. $data['msg'] = "";
  466. //判断用户是否足够观看5分钟
  467. if(intval($video_info['live_pay_type'])==0){
  468. if($diamonds>$video_info['live_fee']*5){
  469. $data['is_diamonds_low'] = 0;
  470. $data['is_recharge'] = 0;
  471. }else{
  472. $data['is_diamonds_low'] = 1;
  473. $data['is_recharge'] = 0;
  474. $surplus_time = intval($diamonds/$video_info['live_fee']);
  475. $data['msg'] = "您当前的".$m_config['diamonds_name']."数量过少,剩余观看".$surplus_time."分钟";
  476. }
  477. }else{
  478. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserFollwRedisService.php');
  479. $user_follw_redis = new UserFollwRedisService($user_id);
  480. if(!$user_follw_redis->is_following($video_info['user_id'])){
  481. if(intval($video_info['pay_room_id'])>0){
  482. $room_id = $video_info['id'];
  483. }
  484. //自动关注主播
  485. $root = redis_set_follow($user_id,$video_info['user_id'],false,$room_id);
  486. clear_auto_cache("playback_list",array("user_id"=>$user_id));
  487. }
  488. }
  489. }else{
  490. $data['is_diamonds_low'] = 1;
  491. $data['is_recharge'] = 1;
  492. $data['msg'] = "您当前的".$m_config['diamonds_name']."数量过少,是否前往充值?";
  493. $data['count_down'] = 0;
  494. }
  495. //同步信息
  496. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserRedisService.php');
  497. $user_redis = new UserRedisService();
  498. $user_infos = $user_redis->getRow_db($user_id,array('id','score','online_time','user_level'));
  499. user_leverl_syn($user_infos);//更新用户等级
  500. user_deal_to_reids(array($user_id,$video_info['user_id']));//同步mysql的user信息到redis
  501. sync_video_to_redis($room_id,'*',false);//将mysql的video数据,同步一份到redis
  502. //提交事务
  503. $GLOBALS['db']->Commit($pInTrans);
  504. }catch(Exception $e){
  505. //异常回滚
  506. $root['error'] = $e->getMessage();
  507. $root['status'] = 0;
  508. $GLOBALS['db']->Rollback($pInTrans);
  509. }
  510. //-----事务结束-----
  511. //获取支付信息
  512. $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";
  513. $live_pay_log_infos = $GLOBALS['db']->getRow($sql);
  514. if($live_pay_log_infos){
  515. $total_times = intval($live_pay_log_infos['total_time']-60);//累计观看时间
  516. }else{
  517. $total_times = 0;//累计观看时间
  518. }
  519. $total_diamonds = $video_info['live_fee']+intval($live_pay_log_info['total_diamonds']);//累计消费钻石
  520. }else{
  521. $total_times = intval($live_pay_log_info['total_time']-$total_time);//累计观看时间
  522. $total_diamonds = intval($live_pay_log_info['total_diamonds']);//累计消费钻石
  523. }
  524. $data['total_time'] = intval($total_times)<=0?0:intval($total_times);//累计观看时间
  525. $data['total_diamonds'] = intval($total_diamonds);//累计消费钻石
  526. }else{
  527. $data['is_popup'] = 1;
  528. }
  529. $data['count_down'] = $live_time;//倒计时
  530. if($live_time==0){
  531. $data['on_live_pay'] = 1;//倒计时结束;已经开始收费
  532. }else{
  533. $data['on_live_pay'] = 0;//倒计时未结束;未实际收费
  534. }
  535. $sql = "select diamonds from ".DB_PREFIX."user where id = ".$user_id;
  536. $diamonds = $GLOBALS['db']->getOne($sql);
  537. $data['diamonds'] = intval($diamonds);
  538. //直播间主播获得的印票和钻石
  539. $sql = "select ticket from ".DB_PREFIX."user where id = ".$video_info['user_id'];
  540. $ticket = $GLOBALS['db']->getOne($sql);
  541. $data['ticket'] = intval($ticket);
  542. return $data;
  543. }
  544. /*
  545. * 付费直播 消费榜
  546. */
  547. public function pay_cont(){
  548. $root = array('status'=>1,'error'=>'');
  549. if(!$GLOBALS['user_info']){
  550. $root['error'] = "用户未登陆,请先登陆.";
  551. $root['status'] = 0;
  552. $root['user_login_status'] = 0;//有这个参数: user_login_status = 0 时,表示服务端未登陆、要求登陆,操作
  553. }else{
  554. $user_id = intval($GLOBALS['user_info']['id']);//用户ID
  555. $type = intval($_REQUEST['type']);//类型 0消费,1收入
  556. $page = intval($_REQUEST['p']);//取第几页数据
  557. $live_pay_type = intval($_REQUEST['live_pay_type']);//付费直播 类型 0按时 1按场
  558. if(intval(LIVE_PAY_TIME)==0)$live_pay_type = 1;//兼容客户端
  559. if($page==0){
  560. $page = 1;
  561. }
  562. $page_size=30;
  563. $limit = (($page-1)*$page_size).",".$page_size;
  564. $where = " and live_pay_type = ".$live_pay_type;
  565. if($type){
  566. $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 ;
  567. }else{
  568. //$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 ;
  569. $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 ;
  570. }
  571. $list = $GLOBALS['db']->getAll($sql,true,true);
  572. foreach($list as $k=>$v){
  573. $list[$k]['head_image'] = get_spec_image($v['head_image'],150,150);
  574. $total_time_format = get_live_time_len($v['total_time']);
  575. $total_time_format = $total_time_format?$total_time_format:"0分钟";
  576. $list[$k]['total_time_format'] =$total_time_format;
  577. if($live_pay_type==1){
  578. unset($list[$k]['total_time']);
  579. unset($list[$k]['total_time_format']);
  580. }
  581. $list[$k]['start_time'] =to_date($v['create_time']);
  582. $list[$k]['end_time'] =to_date($v['create_time']);
  583. $list[$k]['nick_name'] =emoji_decode($v['nick_name']);
  584. }
  585. $rs_count = count($list);
  586. if ($page == 1) {
  587. $root['page'] = array('page' => $page, 'has_next' => 0);
  588. }else {
  589. $has_next = ($rs_count > $page * $page_size) ? '1' : '0';
  590. $root['page'] = array('page' => $page, 'has_next' => $has_next);
  591. }
  592. $root['data'] = $list;
  593. }
  594. ajax_return($root);
  595. }
  596. public function get_pay_info(){
  597. //付费开关
  598. $live_pay_info= $GLOBALS['db']->getAll("SELECT id,class FROM ".DB_PREFIX."plugin WHERE is_effect=1 and type = 1");
  599. $live_pay = array();
  600. if($live_pay_info){
  601. foreach($live_pay_info as $k=>$v){
  602. $live_pay[$v['class']] = $v['id'];
  603. }
  604. }
  605. return $live_pay;
  606. }
  607. }
  608. ?>