deal.action.php 85 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  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 dealModule
  10. {
  11. /**
  12. * 送礼物
  13. */
  14. public function pop_prop(){
  15. $root = array();
  16. if(!$GLOBALS['user_info']){
  17. $root['error'] = "用户未登陆,请先登陆.";
  18. $root['status'] = 0;
  19. $root['user_login_status'] = 0;//有这个参数: user_login_status = 0 时,表示服务端未登陆、要求登陆,操作
  20. }else{
  21. $user_id = intval($GLOBALS['user_info']['id']);
  22. $prop_id = intval($_REQUEST['prop_id']);//礼物id
  23. $num = intval($_REQUEST['num']);//礼物数量
  24. $is_plus = intval($_REQUEST['is_plus']);//1显示连续;
  25. $video_id = strim($_REQUEST['room_id']);//直播ID 也是room_id
  26. $from=strim($_REQUEST['from']);//判断发送来源 pc或者app
  27. $is_coins = intval($_REQUEST['is_coins']);//0是钻石,1是游戏币
  28. $child_id = isset($_REQUEST['child_id'])?intval($_REQUEST['child_id']):$video_id;//子房间
  29. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/VideoRedisService.php');
  30. $video_redis = new VideoRedisService();
  31. $group = $video_redis->getRow_db($video_id,array('group_id'));
  32. //子房间模块
  33. if(defined('CHILD_ROOM') && CHILD_ROOM == 1){
  34. fanwe_require(APP_ROOT_PATH.'mapi/lib/ChildRoom.class.php');
  35. $child_room = new child_room();
  36. $video_id = $child_room->child_pop_prop($user_id,$video_id);
  37. }
  38. //
  39. //$sql = "select id,user_id,group_id from ".DB_PREFIX."video where id = ".$video_id;
  40. //$video = $GLOBALS['db']->getRow($sql);
  41. $video = $video_redis->getRow_db($video_id,array('id','user_id','group_id','prop_table','room_type'));
  42. if($group){
  43. $group_id = strim($group['group_id']);//群组ID
  44. }else{
  45. $group_id = strim($video['group_id']);//群组ID
  46. }
  47. $podcast_id = intval($video['user_id']);//送给谁,有群组ID(group_id),除了红包外其它的都是送给:群主
  48. $room_type = intval($video['room_type']);//直播间类型 房间类型 : 1私有群(Private),0公开群(Public),2聊天室(ChatRoom),3互动直播聊天室(AVChatRoom)
  49. //子房间
  50. if(defined('CHILD_ROOM') && CHILD_ROOM == 1){
  51. fanwe_require(APP_ROOT_PATH.'mapi/lib/ChildRoom.class.php');
  52. $child_room = new child_room();
  53. $parent_id = $child_room->parent_id($video_id);
  54. $p_video = $video_redis->getRow_db($parent_id, array('id', 'user_id', 'group_id', 'prop_table'));
  55. if(empty($group_id)){
  56. $group_id = $p_video['group_id'];
  57. }
  58. }
  59. $is_nospeaking = $GLOBALS['db']->getOne("SELECT is_nospeaking FROM ".DB_PREFIX."user WHERE id=".$user_id,true,true);
  60. if($is_nospeaking){
  61. $root['status'] = 0;
  62. $root['error'] = "被im全局禁言,不能发礼物";
  63. ajax_return($root);
  64. }
  65. if($user_id == $podcast_id){
  66. $root['error'] = "不能发礼物给自己";
  67. $root['status'] = 0;
  68. ajax_return($root);
  69. }
  70. //检查测试账号不能发礼物给真实主播
  71. $sql = "select mobile from ".DB_PREFIX."user where id = '".$podcast_id."'";
  72. $podcast_mobile = $GLOBALS['db']->getOne($sql);
  73. if(($GLOBALS['user_info']['mobile'] == '13888888888'&&$podcast_mobile!='13999999999')||$GLOBALS['user_info']['mobile'] == '13999999999'&&$podcast_mobile!='13888888888'){
  74. $root['error'] = "测试账号不能发礼物给真实主播";
  75. $root['status'] = 0;
  76. ajax_return($root);
  77. }
  78. //以后需要从缓存中读取
  79. //$sql = "select id,name,score,diamonds,icon,ticket,is_much,sort,is_red_envelope,is_animated from ".DB_PREFIX."prop where id = '".$prop_id."'";
  80. //$prop = $GLOBALS['db']->getRow($sql);
  81. $prop = load_auto_cache("prop_id",array('id'=>$prop_id));
  82. if ($num <= 0){
  83. $num = 1;
  84. }
  85. if($is_coins == 0){
  86. $total_diamonds = $num * $prop['diamonds'];
  87. }else{
  88. $total_diamonds = $num * $prop['coins'];
  89. }
  90. $total_score = $num * $prop['score'];
  91. $total_ticket = intval($num * $prop['ticket']);
  92. $robot_diamonds = intval($prop['robot_diamonds']);
  93. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserRedisService.php');
  94. $user_redis = new UserRedisService();
  95. $root = $this->pack_prop($video['prop_table'],$video_redis,$total_diamonds,$total_score,$total_ticket,$num,$prop,$is_plus,$video_id,$user_id,$prop_id,$podcast_id,$group_id,$room_type,$from,$robot_diamonds,$is_coins,$child_id);
  96. }
  97. ajax_return($root);
  98. }
  99. /**
  100. * 邀请码分销
  101. * @param $user_id
  102. * @param $total_ticket
  103. * @param $prop_id
  104. */
  105. private function check_invite($user_id, $total_ticket, $prop_id)
  106. {
  107. if (!defined('OPEN_INVITE_CODE') || OPEN_INVITE_CODE != 1) {
  108. return;
  109. }
  110. $m_config = load_auto_cache('m_config');
  111. if ($m_config['invite_ratio'] <= 0 || $m_config['invite_ratio'] > 1) {
  112. return;
  113. }
  114. $ratio = round($total_ticket * $m_config['invite_ratio'], 2);
  115. if ($ratio <= 0) {
  116. return;
  117. }
  118. $invite_by_user_id = $GLOBALS['db']->getOne("select invite_by_user_id from fanwe_user_invite where user_id = {$user_id}");
  119. if ($invite_by_user_id <= 0) {
  120. return;
  121. }
  122. $invite_user = $GLOBALS['db']->getRow("select is_effect,is_authentication from " . DB_PREFIX . "user where id = {$invite_by_user_id}");
  123. if (!$invite_user['is_effect'] && $invite_user['is_authentication'] == 2) {
  124. return;
  125. }
  126. $GLOBALS['db']->query("update " . DB_PREFIX . "user set ticket = ticket + " . $ratio . " where id = " . $invite_by_user_id);
  127. $GLOBALS['db']->autoExecute(DB_PREFIX . "invite_distribution_log", array(
  128. 'from_user_id' => $user_id,
  129. 'to_user_id' => $invite_by_user_id,
  130. 'create_date' => "'".to_date(NOW_TIME,'Y-m-d')."'",
  131. 'prop_id' => $prop_id,
  132. 'ticket' => $ratio,
  133. 'create_time' => NOW_TIME,
  134. 'create_ym' => to_date(NOW_TIME, 'Ym'),
  135. 'create_d' => to_date(NOW_TIME, 'd'),
  136. 'create_w' => to_date(NOW_TIME, 'W'),
  137. ));
  138. user_deal_to_reids(array($invite_by_user_id));
  139. }
  140. /**
  141. * 送礼物封装
  142. */
  143. public function pack_prop($table,$video_redis,$total_diamonds,$total_score,$total_ticket,$num,$prop,$is_plus,$video_id,$user_id,$prop_id,$podcast_id,$group_id,$room_type,$from,$robot_diamonds,$is_coins,$child_id=''){
  144. $pInTrans = $GLOBALS['db']->StartTrans();
  145. try
  146. {
  147. $m_config = load_auto_cache("m_config");//初始化手机端配置
  148. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/UserRedisService.php');
  149. $user_redis = new UserRedisService();
  150. //免费礼物
  151. if($total_diamonds==0&&$total_score==0&&$total_ticket==0){
  152. $type = 1;
  153. //普通会员收到的提示内容;
  154. $desc = "我送了".$num."个".$prop['name'];
  155. //礼物接收人(主播)收到的提示内容
  156. $desc2 = $desc;
  157. $ext = array();
  158. $ext['type'] = $type; //0:普通消息;1:礼物;2:弹幕消息;3:主播退出;4:禁言;5:观众进入房间;6:观众退出房间;7:直播结束; 8:红包
  159. $ext['num'] = $num;
  160. $ext['is_plus'] = $is_plus;//1:数量连续叠加显示;0:不叠加;这个值是从客户端上传过来的
  161. $ext['is_much'] = $prop['is_much'];//1:可以连续发送多个;用于小金额礼物
  162. $ext['room_id'] = $video_id;//直播ID 也是room_id;只有与当前房间相同时,收到消息才响应
  163. $ext['is_animated'] = $prop['is_animated'];//1:动画;0:未动画
  164. //消息发送者
  165. $sender = array();
  166. $user_info = $user_redis->getRow_db($user_id,array('nick_name','head_image','user_level','v_icon'));
  167. $sender['user_id'] = $user_id;//发送人昵称
  168. $sender['nick_name'] = emoji_decode($user_info['nick_name']);//发送人昵称
  169. $sender['head_image'] = get_spec_image($user_info['head_image']);//发送人头像
  170. $sender['user_level'] = $user_info['user_level'];//用户等级
  171. $sender['v_icon'] = $user_info['v_icon'];//认证图标
  172. $ext['sender'] = $sender;
  173. if ($type == 1){
  174. $ext['prop_id'] = $prop_id; //礼物id
  175. $ext['icon'] = get_spec_image($prop['icon']);//图片,是否要: 大中小格式?
  176. $ext['total_ticket'] = intval($user_redis->getOne_db($podcast_id,'ticket'));//用户总的:印票数
  177. $ext['to_user_id'] = $podcast_id;//礼物接收人(主播)
  178. $ext['fonts_color'] = '';//字体颜色
  179. $ext['desc'] = $desc;//普通群员收到的提示内容;
  180. $ext['desc2'] = $desc2;//礼物接收人(主播)收到的提示内容;
  181. $ext['anim_type'] = $prop['anim_type'];//大型道具类型;
  182. $ext['top_title'] = $sender['nick_name']."送了".$prop['name'];//大型道具类型,标题;
  183. $ext['anim_cfg'] = $prop['anim_cfg'];
  184. }
  185. #构造高级接口所需参数
  186. $msg_content = array();
  187. //创建array 所需元素
  188. $msg_content_elem = array(
  189. 'MsgType' => 'TIMCustomElem', //自定义类型
  190. 'MsgContent' => array(
  191. 'Data' => json_encode($ext),
  192. 'Desc' => '',
  193. // 'Ext' => $ext,
  194. // 'Sound' => '',
  195. )
  196. );
  197. //将创建的元素$msg_content_elem, 加入array $msg_content
  198. array_push($msg_content, $msg_content_elem);
  199. if (isset($_REQUEST['is_debug'])){
  200. $root['error'] = '';
  201. $root['status'] = 1;
  202. }else{
  203. fanwe_require(APP_ROOT_PATH.'system/tim/TimApi.php');
  204. $api = createTimAPI();
  205. $ret = $api->group_send_group_msg2($user_id, $group_id, $msg_content);
  206. if ($ret['ActionStatus'] == 'FAIL' && $ret['ErrorCode'] == 10002){
  207. //10002 系统错误,请再次尝试或联系技术客服。
  208. log_err_file(array(__FILE__,__LINE__,__METHOD__,$ret));
  209. $ret = $api->group_send_group_msg2($user_id, $group_id, $msg_content);
  210. }
  211. //$videoGift_redis->update_db($user_prop_id, $ret);
  212. if ($ret['ActionStatus'] == 'FAIL'){
  213. $root['error'] = $ret['ErrorInfo'].":".$ret['ErrorCode'];
  214. $root['status'] = 0;
  215. }else{
  216. $root['error'] = '';
  217. $root['status'] = 1;
  218. }
  219. }
  220. }else{
  221. //私密直播间送红包不加经验
  222. if ($prop['is_red_envelope'] ==1&&$room_type==1){
  223. $total_score = 0;
  224. }
  225. if($is_coins == 0){
  226. //减少用户钻石
  227. $sql = "update ".DB_PREFIX."user set diamonds = diamonds - ".$total_diamonds.", use_diamonds = use_diamonds + ".$total_diamonds.", score = score + ".$total_score." where id = '".$user_id."' and diamonds >= ".$total_diamonds;
  228. }else{
  229. //减少用户游戏币
  230. $sql = "update ".DB_PREFIX."user set coin = coin - ".$total_diamonds.", score = score + ".$total_score." where id = '".$user_id."' and coin >= ".$total_diamonds;
  231. }
  232. $GLOBALS['db']->query($sql);
  233. if($GLOBALS['db']->affected_rows()){
  234. if($is_coins){
  235. fanwe_require(APP_ROOT_PATH . 'mapi/lib/core/Model.class.php');
  236. Model::$lib = dirname(__FILE__);
  237. $user_model = Model::build('user');
  238. $account_diamonds = $user_model->coin($user_id);
  239. Model::build('coin_log')->addLog($user_id, -1, -$total_diamonds, $account_diamonds, '送礼物消费游戏币');
  240. }
  241. //记录:钻石 减少日志
  242. if ($total_ticket > 0){
  243. if ($prop['is_red_envelope'] == 1){
  244. //主播增加:钻石 数量
  245. //$user_redis->lock_diamonds($podcast_id,$total_ticket);
  246. $sql = "update ".DB_PREFIX."user set diamonds = diamonds + ".$total_ticket." where id = ".$podcast_id;
  247. $GLOBALS['db']->query($sql);
  248. }else{
  249. $m_config = load_auto_cache("m_config");//初始化手机端配置
  250. if(intval(OPEN_REWARD_GIFT)){
  251. if(file_exists(APP_ROOT_PATH.'mapi/lib/core/award_function.php')) {
  252. fanwe_require(APP_ROOT_PATH . 'mapi/lib/core/award_function.php');
  253. $list_award = get_award_info();
  254. }
  255. }
  256. if(defined("robot_gifts") && robot_gifts ==1){
  257. $roboter = $GLOBALS['db']->getOne("select roboter from ".DB_PREFIX."user where roboter=1 and id=".$user_id);//查询是否特殊权限用户
  258. if($roboter){
  259. //增加:不可提现印票
  260. $sql = "update ".DB_PREFIX."user set no_ticket = no_ticket + ".$total_ticket." where id = ".$podcast_id;
  261. $GLOBALS['db']->query($sql);
  262. }else{
  263. //增加:用户印票
  264. $sql = "update ".DB_PREFIX."user set ticket = ticket + ".$total_ticket." where id = ".$podcast_id;
  265. $GLOBALS['db']->query($sql);
  266. }
  267. }else{
  268. //增加:用户印票
  269. $sql = "update ".DB_PREFIX."user set ticket = ticket + ".$total_ticket." where id = ".$podcast_id;
  270. $GLOBALS['db']->query($sql);
  271. }
  272. }
  273. // 邀请码分销
  274. $this->check_invite($user_id, $total_ticket, $prop_id);
  275. /*
  276. * 记录在redis中
  277. //当前直播获得印票数
  278. $sql = "update ".DB_PREFIX."video set vote_number = vote_number + ".$total_ticket." where id =".$video_id;
  279. $GLOBALS['db']->query($sql);
  280. */
  281. }
  282. //=========数据库更新成功后,处理redis数据==========
  283. //插入:送礼物表 修改礼物直接写入 mysql @by slf
  284. $video_prop = array();
  285. $video_prop['prop_id'] = $prop_id;
  286. $video_prop['prop_name'] = "'".$prop['name']."'";
  287. $video_prop['is_red_envelope'] = $prop['is_red_envelope'];
  288. $video_prop['total_score'] = $total_score;
  289. $video_prop['total_diamonds'] = $total_diamonds;
  290. $video_prop['total_ticket'] = intval($total_ticket);//is_red_envelope=1时,为主播获得的:钻石 数量
  291. $video_prop['from_user_id'] = $user_id;
  292. $video_prop['to_user_id'] = $podcast_id;
  293. $video_prop['create_time'] = NOW_TIME;
  294. $video_prop['create_date'] = "'".to_date(NOW_TIME,'Y-m-d')."'";
  295. $video_prop['num'] = $num;
  296. $video_prop['video_id'] = $video_id;
  297. $video_prop['group_id'] = "'".$group_id."'";
  298. $video_prop['create_ym'] = to_date($video_prop['create_time'],'Ym');
  299. $video_prop['create_d'] = to_date($video_prop['create_time'],'d');
  300. $video_prop['create_w'] = to_date($video_prop['create_time'],'W');
  301. $video_prop['from_ip'] = "'".get_client_ip()."'";
  302. $video_prop['is_coin'] = 0;
  303. if($is_coins == 1){
  304. $video_prop['is_coin'] = 1;
  305. }
  306. $table_info = $GLOBALS['db']->getRow("Describe ".$table." from_ip",true,true);
  307. if(!$table_info){
  308. $GLOBALS['db']->query("ALTER TABLE ".$table." ADD COLUMN `from_ip` varchar(255) NOT NULL COMMENT '送礼物人IP'");
  309. }
  310. $table_version = $GLOBALS['db']->getRow("Describe ".$table." is_coin",true,true);
  311. if(!$table_version){
  312. $GLOBALS['db']->query("ALTER TABLE ".$table." ADD COLUMN `is_coin` varchar(255) NOT NULL COMMENT '双币礼物,0是钻石,1是游戏币'");
  313. }
  314. if(intval(OPEN_REWARD_GIFT)&&intval($list_award['is_open_award'])==1){
  315. $table_version_is_award = $GLOBALS['db']->getRow("Describe ".$table." is_award",true,true);
  316. if(!$table_version_is_award){
  317. $GLOBALS['db']->query("ALTER TABLE ".$table." ADD COLUMN `is_award` varchar(255) NOT NULL COMMENT '是否为可中奖礼物 1为 是、0为否'");
  318. }
  319. }
  320. //将礼物写入mysql表中
  321. $field_arr = array('prop_id','prop_name','is_red_envelope', 'total_score', 'total_diamonds', 'total_ticket','from_user_id', 'to_user_id', 'create_time','create_date','num','video_id','group_id','create_ym','create_d','create_w','from_ip','is_coin');
  322. if(intval(OPEN_REWARD_GIFT)&&intval($list_award['is_open_award'])==1){
  323. $field_arr[] = 'is_award';
  324. $video_prop['is_award'] = intval($prop['is_award']);
  325. }
  326. $fields = implode(",",$field_arr);
  327. $valus = implode(",",$video_prop);
  328. fanwe_require (APP_ROOT_PATH.'mapi/lib/core/common.php');
  329. //$table = createPropTable();
  330. $sql = "insert into ".$table." (".$fields.") VALUES (".$valus.")";
  331. $GLOBALS['db']->query($sql);
  332. $user_prop_id = $GLOBALS['db']->insert_id();
  333. //写入总表
  334. if(file_exists(APP_ROOT_PATH.'mapi/lib/core/award_function.php')){
  335. fanwe_require(APP_ROOT_PATH.'mapi/lib/core/award_function.php');
  336. $table_all = createPropAllTable();
  337. $table_version = $GLOBALS['db']->getRow("Describe ".$table_all." is_coin",true,true);
  338. if(!$table_version){
  339. $GLOBALS['db']->query("ALTER TABLE ".$table_all." ADD COLUMN `is_coin` varchar(255) NOT NULL COMMENT '双币礼物,0是钻石,1是游戏币'");
  340. }
  341. $sql = "insert into ".$table_all." (".$fields.") VALUES (".$valus.")";
  342. $GLOBALS['db']->query($sql);
  343. $award_res = array('status'=>0,'is_award'=>0);
  344. if(intval(OPEN_REWARD_GIFT)&&intval($prop['is_award'])==1&&intval($list_award['is_open_award'])==1){
  345. //礼物中奖逻辑处理
  346. $award_info = array();
  347. $award_info['video_id'] = $video_id;
  348. $award_info['group_id'] = $group_id;
  349. $award_info['from_ip'] = "'".get_client_ip()."'";
  350. $award_info['user_id'] = $user_id;
  351. $award_result = gift_award($award_info,$prop);
  352. if($award_result['status']){
  353. $award_res = $award_result;// 是否中奖
  354. }
  355. }
  356. }
  357. //提交事务,不等 消息推送,防止锁太久
  358. $GLOBALS['db']->Commit($pInTrans);
  359. $pInTrans = false;//防止,下面异常时,还调用:Rollback
  360. //子房间
  361. if(defined('CHILD_ROOM') && CHILD_ROOM == 1){
  362. fanwe_require(APP_ROOT_PATH.'mapi/lib/ChildRoom.class.php');
  363. $child_room = new child_room();
  364. $child_room->child_room_prop($child_id,$video_id,$video_prop,$fields,$user_id,$total_ticket);
  365. }
  366. if ($prop['is_red_envelope'] == 0 && $total_ticket > 0){
  367. //贡献榜
  368. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/VideoContributionRedisService.php');
  369. $videoCont_redis = new VideoContributionRedisService();
  370. $videoCont_redis->insert_db($user_id, $podcast_id, $video_id, $total_ticket);
  371. }
  372. //分销功能 计算抽成
  373. if(defined('OPEN_DISTRIBUTION')&&OPEN_DISTRIBUTION==1&&$prop['is_red_envelope'] == 0&&$total_ticket>0){
  374. $this->distribution_calculate($user_id,$total_ticket);
  375. }
  376. if ($prop['is_red_envelope'] == 0&&$total_ticket>0) {
  377. $this->distribution($podcast_id, $video_id, $total_ticket);
  378. }
  379. user_deal_to_reids(array($user_id,$podcast_id));
  380. //更新用户等级
  381. $user_info = $user_redis->getRow_db($user_id,array('id','score','online_time','user_level'));
  382. user_leverl_syn($user_info);
  383. /*
  384. $sql = "select diamonds,use_diamonds,score,ticket,user_level,refund_ticket from ".DB_PREFIX."user where id = ".$user_id;
  385. $user_data = $GLOBALS['db']->getRow($sql);
  386. $user_redis->update_db($user_id, $user_data);
  387. $sql = "select diamonds,use_diamonds,score,ticket,user_level,refund_ticket from ".DB_PREFIX."user where id = ".$podcast_id;
  388. $user_data = $GLOBALS['db']->getRow($sql);
  389. $user_redis->update_db($podcast_id, $user_data);
  390. */
  391. //=================发送:礼物=================================
  392. $type = 1;
  393. //普通会员收到的提示内容;
  394. if ($prop['is_red_envelope'] == 1){
  395. $desc = '我给大家送了一个红包';
  396. $type = 8;
  397. }else{
  398. $desc = "我送了".$num."个".$prop['name'];
  399. }
  400. //礼物接收人(主播)收到的提示内容
  401. $desc2 = $desc;
  402. $ext = array();
  403. $ext['type'] = $type; //0:普通消息;1:礼物;2:弹幕消息;3:主播退出;4:禁言;5:观众进入房间;6:观众退出房间;7:直播结束; 8:红包
  404. $ext['num'] = $num;
  405. $ext['is_plus'] = $is_plus;//1:数量连续叠加显示;0:不叠加;这个值是从客户端上传过来的
  406. $ext['is_much'] = $prop['is_much'];//1:可以连续发送多个;用于小金额礼物
  407. $ext['room_id'] = $video_id;//直播ID 也是room_id;只有与当前房间相同时,收到消息才响应
  408. $ext['award'] = $award_res;//
  409. if($prop['is_much']) {
  410. // 计算连发次数,兼容 PC 端
  411. $key = "user:prop:{$user_id}:{$video_id}:{$prop_id}";
  412. $user_prop = $GLOBALS['cache']->get($key, true);
  413. if($user_prop && $user_prop['time'] > NOW_TIME){
  414. $plus_num = $user_prop['num'] + 1;
  415. if($from=='pc' && $plus_num > 1){
  416. $is_plus=1;
  417. }
  418. } else {
  419. $plus_num = 1;
  420. }
  421. $ext['plus_num'] = $plus_num;
  422. // app 上传 is_plus 视为连发
  423. $GLOBALS['cache']->set($key, array('time' => NOW_TIME + 5, 'num' => $plus_num), 5, true);
  424. //APP
  425. $key = "user:prop:{'app'}{$user_id}:{$video_id}:{$prop_id}";
  426. $app_user_prop = $GLOBALS['cache']->get($key, true);
  427. if($app_user_prop && $is_plus==1){
  428. $app_plus_num = $app_user_prop['num'] + 1;
  429. } else {
  430. $app_plus_num = 1;
  431. }
  432. $ext['app_plus_num'] = $app_plus_num;
  433. // app 上传 is_plus 视为连发
  434. $GLOBALS['cache']->set($key, array('num' => $app_plus_num), 86400, true);
  435. }else{
  436. $ext['app_plus_num'] = 1;
  437. }
  438. $ext['is_animated'] = $prop['is_animated'];//1:动画;0:未动画
  439. //消息发送者
  440. $sender = array();
  441. $user_info = $user_redis->getRow_db($user_id,array('nick_name','head_image','user_level','v_icon'));
  442. $sender['user_id'] = $user_id;//发送人昵称
  443. $sender['nick_name'] = emoji_decode($user_info['nick_name']);//发送人昵称
  444. $sender['head_image'] = get_spec_image($user_info['head_image']);//发送人头像
  445. $sender['user_level'] = $user_info['user_level'];//用户等级
  446. $sender['v_icon'] = $user_info['v_icon'];//认证图标
  447. $ext['sender'] = $sender;
  448. if ($type == 1){
  449. $ext['prop_id'] = $prop_id; //礼物id
  450. //$ext['animated_url'] = $prop['animated_url'];//动画播放url
  451. $ext['icon'] = get_spec_image($prop['icon']);//图片,是否要: 大中小格式?
  452. //$ext['is_red_envelope'] = $prop['is_red_envelope'];//是否是:红包;1:红包
  453. $ext['user_prop_id'] = $user_prop_id; //红包时用到,抢红包的id
  454. //$ext['show_num'] = $show_num;//显示连续送的礼物数量;
  455. $fields = array('ticket','no_ticket','refund_ticket');
  456. $user_info = $user_redis->getRow_db($podcast_id,$fields);//用户总的:印票数
  457. $ext['total_ticket'] =$user_info['ticket']+$user_info['no_ticket'];//用户总的:印票数
  458. //直播间显示主播实际可提现印票(客户定制,标准版保留此功能)
  459. if(0){
  460. $ext['total_ticket'] = $ext['total_ticket'] - intval($user_info['refund_ticket']);
  461. }
  462. $ext['to_user_id'] = $podcast_id;//礼物接收人(主播)
  463. $ext['fonts_color'] = '';//字体颜色
  464. $ext['desc'] = $desc;//普通群员收到的提示内容;
  465. $ext['desc2'] = $desc2;//礼物接收人(主播)收到的提示内容;
  466. $ext['anim_type'] = $prop['anim_type'];//大型道具类型;
  467. $ext['top_title'] = $sender['nick_name']."送了".$prop['name'];//大型道具类型,标题;
  468. /*
  469. if ($ext['is_animated'] == 1){
  470. //要缓存getAllCached
  471. $sql = "select id,url,play_count,delay_time,duration,show_user,type from ".DB_PREFIX."prop_animated where prop_id = ".$prop_id." order by sort desc";
  472. $anim_list = $GLOBALS['db']->getAll($sql);
  473. $ext['anim_cfg'] = $anim_list;
  474. //$ext['sql'] = $sql;
  475. }else{
  476. $ext['anim_cfg'] = array();
  477. }
  478. */
  479. $ext['anim_cfg'] = $prop['anim_cfg'];
  480. }else{
  481. $ext['prop_id'] = $prop_id; //礼物id
  482. //$ext['animated_url'] = $prop['animated_url'];//动画播放url
  483. $ext['icon'] = get_spec_image($prop['icon']);//图片,是否要: 大中小格式?
  484. //$ext['is_red_envelope'] = $prop['is_red_envelope'];//是否是:红包;1:红包
  485. $ext['user_prop_id'] = $user_prop_id; //红包时用到,抢红包的id
  486. //$ext['show_num'] = $show_num;//显示连续送的礼物数量;
  487. $ext['total_ticket'] = intval($user_redis->getOne_db($podcast_id,'ticket'));//用户总的:印票数
  488. $ext['to_user_id'] = $podcast_id;//礼物接收人(主播)
  489. $ext['to_diamonds'] = $total_ticket;//礼物接收人(主播),获得的:钻石 数量
  490. $ext['fonts_color'] = '';//字体颜色
  491. $ext['desc'] = $desc;//普通群员收到的提示内容;
  492. $ext['desc2'] = $desc2;//礼物接收人(主播)收到的提示内容;
  493. $allot_diamonds = 0;
  494. if ($prop['is_red_envelope'] ==1&&$room_type==1){
  495. }else{
  496. if ($robot_diamonds > 0){
  497. //优先分配给:观众列表中的机器人
  498. $robot_list = $video_redis->get_robot($video_id);
  499. $robot_num = count($robot_list);
  500. if ($robot_num > 0){
  501. //给一半以上的机器 人分配
  502. $robot_num = mt_rand(ceil($robot_num/2), $robot_num);
  503. //可分配的钻石小于机器人数1.3倍时,减少分配人数
  504. if ($robot_num * 1.3 > $robot_diamonds) $robot_num = ceil($robot_diamonds/2);
  505. $diamonds_list = $this->red_rand_list2($robot_diamonds,$robot_num);
  506. while(count($diamonds_list) > 0){
  507. $money = $diamonds_list[0];
  508. array_splice($diamonds_list,0,1);
  509. $robot_num = count($robot_list) - 1;
  510. $r = mt_rand(0, $robot_num);
  511. $robot_userid = $robot_list[$r];
  512. array_splice($robot_list,$r,1);
  513. //实际分配的
  514. $allot_diamonds = $allot_diamonds + $money;
  515. allot_red_to_user($user_prop_id,$robot_userid,$money);
  516. };
  517. }
  518. }
  519. }
  520. //生成一个随机红包队列(观众可抢钻石=diamonds-ticket-robot_diamods)
  521. $money_list = $this->red_rand_list($total_diamonds - $total_ticket-$allot_diamonds);
  522. fanwe_require(APP_ROOT_PATH.'mapi/lib/redis/VideoRedRedisService.php');
  523. $videoRed_redis = new VideoRedRedisService();
  524. $videoRed_redis->push_red($user_prop_id, $money_list);
  525. //记录直播间发的:红包 记录; 主要用于,直播结束后,处理还未被领取的红包
  526. $video_redis->add_red($video_id, $user_prop_id);
  527. }
  528. #构造高级接口所需参数
  529. $msg_content = array();
  530. //创建array 所需元素
  531. $msg_content_elem = array(
  532. 'MsgType' => 'TIMCustomElem', //自定义类型
  533. 'MsgContent' => array(
  534. 'Data' => json_encode($ext),
  535. 'Desc' => '',
  536. // 'Ext' => $ext,
  537. // 'Sound' => '',
  538. )
  539. );
  540. //将创建的元素$msg_content_elem, 加入array $msg_content
  541. array_push($msg_content, $msg_content_elem);
  542. if (isset($_REQUEST['is_debug'])){
  543. $root['error'] = '';
  544. $root['status'] = 1;
  545. }else{
  546. fanwe_require(APP_ROOT_PATH.'system/tim/TimApi.php');
  547. $api = createTimAPI();
  548. $ret = $api->group_send_group_msg2($user_id, $group_id, $msg_content);
  549. if ($ret['ActionStatus'] == 'FAIL' && $ret['ErrorCode'] == 10002){
  550. //10002 系统错误,请再次尝试或联系技术客服。
  551. log_err_file(array(__FILE__,__LINE__,__METHOD__,$ret));
  552. $ret = $api->group_send_group_msg2($user_id, $group_id, $msg_content);
  553. }
  554. $GLOBALS['db']->autoExecute($table, $ret,'UPDATE','id='.$user_prop_id);
  555. //$videoGift_redis->update_db($user_prop_id, $ret);
  556. if ($ret['ActionStatus'] == 'FAIL'){
  557. $root['error'] = $ret['ErrorInfo'].":".$ret['ErrorCode'];
  558. $root['status'] = 0;
  559. }else{
  560. if (defined('PROP_NOTIFY') && PROP_NOTIFY == 1 && $type==1 && $prop['anim_type']!='') //判断开关是否开启,是否为大型礼物
  561. {
  562. $m_config = load_auto_cache('m_config');
  563. $root['is_notify'] = intval($m_config['is_prop_notify']); //判断后台是否开启这个功能
  564. if ($root['is_notify'])
  565. {
  566. //获取房间类型
  567. $sql1 = "SELECT room_type FROM ".DB_PREFIX."video WHERE id=".$video_id;
  568. $room_type = $GLOBALS['db']->getOne($sql1,true,true);
  569. $sql2 = "SELECT live_in FROM ".DB_PREFIX."video WHERE id=".$video_id;
  570. $live_type = $GLOBALS['db']->getOne($sql2,true,true);
  571. if($room_type!=1 && $live_type!=3) //判定房间是否为私有房间或回播的房间
  572. {
  573. fanwe_require(APP_ROOT_PATH.'mapi/lib/core/prop_notify.php');
  574. $root[] = propNotify($sender,$ext,$prop); //执行全服通告
  575. }
  576. }
  577. $root['is_notify'] = intval($m_config['is_prop_notify']);
  578. }
  579. $root['error'] = '';
  580. $root['status'] = 1;
  581. }
  582. }
  583. }else{
  584. $GLOBALS['db']->Rollback($pInTrans);
  585. $root['error'] = $is_coins ? "用户游戏币不足" : "用户".$m_config['diamonds_name']."不足";
  586. $root['status'] = 0;
  587. }
  588. }
  589. }catch(Exception $e){
  590. //异常回滚
  591. $root['error'] = $e->getMessage();
  592. $root['status'] = 0;
  593. $GLOBALS['db']->Rollback($pInTrans);
  594. }
  595. if (defined('OPEN_MISSION') && OPEN_MISSION) {
  596. require_once APP_ROOT_PATH . 'mapi/lib/core/Model.class.php';
  597. Model::$lib = dirname(__FILE__);
  598. Model::build('mission')->incProgress($user_id,2);
  599. }
  600. return $root;
  601. }
  602. /**
  603. * 弹幕消息接口
  604. */
  605. public function pop_msg()
  606. {
  607. $root = array();
  608. if (!$GLOBALS['user_info']) {
  609. $root['error'] = "用户未登陆,请先登陆.";
  610. $root['status'] = 0;
  611. $root['user_login_status'] = 0;//有这个参数: user_login_status = 0 时,表示服务端未登陆、要求登陆,操作
  612. } else {
  613. $user_id = intval($GLOBALS['user_info']['id']);
  614. $room_id = strim($_REQUEST['room_id']);//直播ID 也是room_id
  615. $msg = strim($_REQUEST['msg']);//消息内容8J+UkQ==
  616. //$to_user_id = intval($_REQUEST['to_user_id']);//群主ID
  617. $is_nospeaking = $GLOBALS['db']->getOne("SELECT is_nospeaking FROM " . DB_PREFIX . "user WHERE id=" . $user_id,
  618. true, true);
  619. if ($is_nospeaking) {
  620. $root['status'] = 0;
  621. $root['error'] = "被im全局禁言,不能发消息";
  622. ajax_return($root);
  623. }
  624. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedisService.php');
  625. $video_redis = new VideoRedisService();
  626. $video = $video_redis->getRow_db($room_id, array('id', 'user_id', 'group_id', 'prop_table'));
  627. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
  628. $user_redis = new UserRedisService();
  629. $group_id = strim($video['group_id']);//群组ID
  630. $podcast_id = intval($video['user_id']);//送给谁,有群组ID(group_id),除了红包外其它的都是送给:群主
  631. //子房间
  632. if(defined('CHILD_ROOM') && CHILD_ROOM == 1){
  633. fanwe_require(APP_ROOT_PATH.'mapi/lib/ChildRoom.class.php');
  634. $child_room = new child_room();
  635. $parent_id = $child_room->parent_id($room_id);
  636. $p_video = $video_redis->getRow_db($parent_id, array('id', 'user_id', 'group_id', 'prop_table'));
  637. if(empty($group_id)){
  638. $group_id = $p_video['group_id'];
  639. }
  640. }
  641. $m_config = load_auto_cache("m_config");//初始化手机端配置
  642. $is_podcast = false;
  643. //主播,自己发送 弹幕 消息,不扣钻石
  644. if ($podcast_id == $user_id) {
  645. $ext = array();
  646. $ext['type'] = 2; //0:普通消息;1:礼物;2:弹幕消息;3:主播退出;4:禁言;5:观众进入房间;6:观众退出房间;7:直播结束
  647. $ext['room_id'] = $room_id;//直播ID 也是room_id;只有与当前房间相同时,收到消息才响应
  648. $ext['num'] = 1;
  649. $ext['prop_id'] = 0; //礼物id
  650. //s$ext['animated_url'] = '';//动画播放url
  651. $ext['icon'] = '';//图片,是否要: 大中小格式?
  652. //$ext['is_red_envelope'] = 0;//是否是:红包;1:红包
  653. $ext['user_prop_id'] = 0; //红包时用到,抢红包的id
  654. //$ext['show_num'] = 1;//显示连续送的礼物数量;
  655. $fields = array('ticket','no_ticket','refund_ticket');
  656. $user_info = $user_redis->getRow_db($podcast_id,$fields);//用户总的:印票数
  657. $ext['total_ticket'] = intval($user_info['ticket'])+ intval($user_info['no_ticket']);//用户总的:印票数
  658. //直播间显示主播实际可提现印票(客户定制,标准版保留此功能)
  659. if(0){
  660. $ext['total_ticket'] = $ext['total_ticket'] - intval($user_info['refund_ticket']);
  661. }
  662. $ext['to_user_id'] = 0;//礼物接收人(主播)
  663. $ext['fonts_color'] = '';//字体颜色
  664. $ext['desc'] = $msg;//弹幕消息;
  665. $ext['desc2'] = $msg;//弹幕消息;
  666. //消息发送者
  667. $sender = array();
  668. $user_info = $user_redis->getRow_db($user_id, array('nick_name', 'head_image', 'user_level', 'v_icon'));
  669. $sender['user_id'] = $user_id;//发送人昵称
  670. $sender['nick_name'] = emoji_decode($user_info['nick_name']);//发送人昵称
  671. $sender['head_image'] = get_spec_image($user_info['head_image']);//发送人头像
  672. $sender['user_level'] = $user_info['user_level'];//用户等级
  673. $sender['v_icon'] = $user_info['v_icon'];//认证图标
  674. $ext['sender'] = $sender;
  675. #构造高级接口所需参数
  676. $msg_content = array();
  677. //创建array 所需元素
  678. $msg_content_elem = array(
  679. 'MsgType' => 'TIMCustomElem', //自定义类型
  680. 'MsgContent' => array(
  681. 'Data' => json_encode($ext),
  682. 'Desc' => '',
  683. // 'Ext' => $ext,
  684. // 'Sound' => '',
  685. )
  686. );
  687. if (intval($m_config['has_dirty_words']) == 1) {
  688. //文档内容,用来过滤脏字
  689. $msg_text_elem = array(
  690. 'MsgType' => 'TIMTextElem', //
  691. 'MsgContent' => array(
  692. 'Text' => $msg,
  693. )
  694. );
  695. array_push($msg_content, $msg_text_elem, $msg_content_elem);
  696. } else {
  697. //将创建的元素$msg_content_elem, 加入array $msg_content
  698. array_push($msg_content, $msg_content_elem);
  699. }
  700. if (isset($_REQUEST['is_debug'])) {
  701. $root['error'] = '';
  702. $root['status'] = 1;
  703. } else {
  704. fanwe_require(APP_ROOT_PATH . 'system/tim/TimApi.php');
  705. $api = createTimAPI();
  706. $ret = $api->group_send_group_msg2($user_id, $group_id, $msg_content);
  707. }
  708. if ($ret['ActionStatus'] == 'FAIL') {
  709. log_err_file(array(__FILE__, __LINE__, __METHOD__, $ret));
  710. if ($ret['ErrorCode'] == 80001) {
  711. $root['error'] = '该词已被禁用';
  712. } else {
  713. $root['error'] = $ret['ErrorInfo'] . ":" . $ret['ErrorCode'];
  714. }
  715. $root['status'] = 0;
  716. } else {
  717. $root['error'] = '';
  718. $root['status'] = 0;//app端通过这个判断是否扣除钻石,1为扣除
  719. }
  720. } else {
  721. //$sql = "select id from ".DB_PREFIX."video_forbid_send_msg where group_id='".$group_id."' and user_id = ".$user_id;
  722. //$has_forbid = $GLOBALS['db']->getOne($sql,true,true) > 0;
  723. $has_forbid = $video_redis->has_forbid_msg2($group_id, $user_id);
  724. if ($has_forbid) {
  725. $root['error'] = "被禁言,不能发送消息";
  726. $root['status'] = 0;
  727. } else {
  728. //file_put_contents(APP_ROOT_PATH.'mapi/lib/msg.txt', $msg);
  729. //$msg2 = unserialize(file_get_contents(APP_ROOT_PATH.'mapi/lib/msg2.txt'));
  730. //$msg =$msg .'【'.base64_decode("8J+UkQ==").'】';
  731. //子房间收益归主房间主播
  732. $p_id = $podcast_id;
  733. if(defined('CHILD_ROOM') && CHILD_ROOM == 1){
  734. fanwe_require(APP_ROOT_PATH.'mapi/lib/ChildRoom.class.php');
  735. $child_room = new child_room();
  736. $parent_id = $child_room->parent_id($room_id);
  737. $p_video = $video_redis->getRow_db($parent_id, array('id', 'user_id', 'group_id', 'prop_table'));
  738. $p_id = $p_video['user_id'];
  739. }
  740. $total_diamonds = 1;
  741. $total_score = 1;
  742. $total_ticket = 1;
  743. $pInTrans = $GLOBALS['db']->StartTrans();
  744. try {
  745. $sql = "update " . DB_PREFIX . "user set diamonds = diamonds - " . $total_diamonds . ",use_diamonds = use_diamonds + " . $total_diamonds . ", score = score + " . $total_score . " where id = '" . $user_id . "' and diamonds >= " . $total_diamonds;
  746. $GLOBALS['db']->query($sql);
  747. if ($GLOBALS['db']->affected_rows()) {
  748. if ($total_ticket > 0) {
  749. if(defined("robot_gifts") && robot_gifts ==1){
  750. $roboter = $GLOBALS['db']->getOne("select roboter from ".DB_PREFIX."user where roboter=1 and id=".$user_id);//查询是否特殊权限用户
  751. if($roboter){
  752. //增加:不可提现印票
  753. $sql = "update ".DB_PREFIX."user set no_ticket = no_ticket + ".$total_ticket." where id = ".$p_id;
  754. $GLOBALS['db']->query($sql);
  755. }else{
  756. //增加:用户印票
  757. $sql = "update ".DB_PREFIX."user set ticket = ticket + ".$total_ticket." where id = ".$p_id;
  758. $GLOBALS['db']->query($sql);
  759. }
  760. }else{
  761. //增加:用户印票
  762. $sql = "update " . DB_PREFIX . "user set ticket = ticket + " . $total_ticket . " where id = " . $p_id;
  763. $GLOBALS['db']->query($sql);
  764. }
  765. /*
  766. //当前直播获得印票数
  767. $sql = "update ".DB_PREFIX."video set vote_number = vote_number + ".$total_ticket." where id =".$room_id;
  768. $GLOBALS['db']->query($sql);
  769. */
  770. //记录:用户印票增加日志
  771. }
  772. $video_prop = array();
  773. $video_prop['prop_id'] = 0;
  774. $video_prop['prop_name'] = "'" . '弹幕' . "'";
  775. $video_prop['is_red_envelope'] = 0;
  776. $video_prop['total_score'] = $total_score;
  777. $video_prop['total_diamonds'] = $total_diamonds;
  778. $video_prop['total_ticket'] = intval($total_ticket);
  779. $video_prop['from_user_id'] = $user_id;
  780. $video_prop['to_user_id'] = $podcast_id;
  781. $video_prop['create_time'] = NOW_TIME;
  782. $video_prop['create_date'] = "'" . to_date(NOW_TIME, 'Y-m-d') . "'";
  783. $video_prop['num'] = 1;
  784. $video_prop['video_id'] = $room_id;
  785. $video_prop['group_id'] = "'" . $group_id . "'";
  786. $video_prop['msg'] = "'" . $msg . "'";
  787. $video_prop['create_ym'] = to_date($video_prop['create_time'], 'Ym');
  788. $video_prop['create_d'] = to_date($video_prop['create_time'], 'd');
  789. $video_prop['create_w'] = to_date($video_prop['create_time'], 'W');
  790. $video_prop['from_ip'] = "'".get_client_ip()."'";
  791. //将礼物写入mysql表中
  792. $field_arr = array(
  793. 'prop_id',
  794. 'prop_name',
  795. 'is_red_envelope',
  796. 'total_score',
  797. 'total_diamonds',
  798. 'total_ticket',
  799. 'from_user_id',
  800. 'to_user_id',
  801. 'create_time',
  802. 'create_date',
  803. 'num',
  804. 'video_id',
  805. 'group_id',
  806. 'msg',
  807. 'create_ym',
  808. 'create_d',
  809. 'create_w',
  810. 'from_ip'
  811. );
  812. $fields = implode(",", $field_arr);
  813. $valus = implode(",", $video_prop);
  814. $table = $video['prop_table'];
  815. $table_info = $GLOBALS['db']->getRow("Describe ".$table." from_ip",true,true);
  816. if(!$table_info){
  817. $GLOBALS['db']->query("ALTER TABLE ".$table." ADD COLUMN `from_ip` varchar(255) NOT NULL COMMENT '送礼物人IP'");
  818. }
  819. $sql = "insert into " . $table . "(" . $fields . ") VALUES (" . $valus . ")";
  820. $GLOBALS['db']->query($sql);
  821. $user_prop_id = $GLOBALS['db']->insert_id();
  822. //写入总表
  823. if(file_exists(APP_ROOT_PATH.'mapi/lib/core/award_function.php')){
  824. fanwe_require(APP_ROOT_PATH.'mapi/lib/core/award_function.php');
  825. $table_all = createPropAllTable();
  826. $sql = "insert into ".$table_all." (".$fields.") VALUES (".$valus.")";
  827. $GLOBALS['db']->query($sql);
  828. }
  829. //提交事务,不等 消息推送,防止锁太久
  830. $GLOBALS['db']->Commit($pInTrans);
  831. $pInTrans = false;//防止,下面异常时,还调用:Rollback
  832. //子房间
  833. if(defined('CHILD_ROOM') && CHILD_ROOM == 1){
  834. fanwe_require(APP_ROOT_PATH.'mapi/lib/ChildRoom.class.php');
  835. $child_room = new child_room();
  836. $child_room->child_room_pop_msg($room_id,$video_prop,$fields,$user_id,$total_ticket);
  837. }
  838. if ($total_ticket > 0) {
  839. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoContributionRedisService.php');
  840. $videoCont_redis = new VideoContributionRedisService();
  841. $videoCont_redis->insert_db($user_id, $podcast_id, $room_id, $total_ticket);
  842. }
  843. user_deal_to_reids(array($user_id, $podcast_id));
  844. //更新用户等级
  845. $user_info = $user_redis->getRow_db($user_id,
  846. array('id', 'score', 'online_time', 'user_level'));
  847. user_leverl_syn($user_info);
  848. //分销功能 计算抽成
  849. if (defined('OPEN_DISTRIBUTION') && OPEN_DISTRIBUTION == 1 && $total_ticket > 0) {
  850. $this->distribution_calculate($user_id, $total_ticket);
  851. }
  852. $this->distribution($podcast_id, $room_id, $total_ticket);
  853. //发送:礼物
  854. $ext = array();
  855. $ext['type'] = 2; //0:普通消息;1:礼物;2:弹幕消息;3:主播退出;4:禁言;5:观众进入房间;6:观众退出房间;7:直播结束
  856. $ext['room_id'] = $room_id;//直播ID 也是room_id;只有与当前房间相同时,收到消息才响应
  857. $ext['num'] = 1;
  858. $ext['prop_id'] = 0; //礼物id
  859. //s$ext['animated_url'] = '';//动画播放url
  860. $ext['icon'] = '';//图片,是否要: 大中小格式?
  861. //$ext['is_red_envelope'] = 0;//是否是:红包;1:红包
  862. $ext['user_prop_id'] = $user_prop_id; //红包时用到,抢红包的id
  863. //$ext['show_num'] = 1;//显示连续送的礼物数量;
  864. $fields = array('ticket','no_ticket');
  865. $user_info = $user_redis->getRow_db($podcast_id,$fields);//用户总的:印票数
  866. $ext['total_ticket'] =$user_info['ticket']+$user_info['no_ticket'];//用户总的:印票数
  867. // $ext['total_ticket'] = intval($user_redis->getOne_db($podcast_id, 'ticket'));//用户总的:印票数
  868. $ext['to_user_id'] = 0;//礼物接收人(主播)
  869. $ext['fonts_color'] = '';//字体颜色
  870. $ext['desc'] = $msg;//弹幕消息;
  871. $ext['desc2'] = $msg;//弹幕消息;
  872. //消息发送者
  873. $sender = array();
  874. $user_info = $user_redis->getRow_db($user_id,
  875. array('nick_name', 'head_image', 'user_level'));
  876. $sender['user_id'] = $user_id;//发送人昵称
  877. $sender['nick_name'] = emoji_decode($user_info['nick_name']);//发送人昵称
  878. $sender['head_image'] = get_spec_image($user_info['head_image']);//发送人头像
  879. $sender['user_level'] = $user_info['user_level'];//用户等级
  880. $ext['sender'] = $sender;
  881. #构造高级接口所需参数
  882. $msg_content = array();
  883. //创建array 所需元素
  884. $msg_content_elem = array(
  885. 'MsgType' => 'TIMCustomElem', //自定义类型
  886. 'MsgContent' => array(
  887. 'Data' => json_encode($ext),
  888. 'Desc' => '',
  889. )
  890. );
  891. if (intval($m_config['has_dirty_words']) == 1) {
  892. //文档内容,用来过滤脏字
  893. $msg_text_elem = array(
  894. 'MsgType' => 'TIMTextElem', //
  895. 'MsgContent' => array(
  896. 'Text' => $msg,
  897. )
  898. );
  899. array_push($msg_content, $msg_text_elem, $msg_content_elem);
  900. } else {
  901. //将创建的元素$msg_content_elem, 加入array $msg_content
  902. array_push($msg_content, $msg_content_elem);
  903. }
  904. if (isset($_REQUEST['is_debug'])) {
  905. $root['error'] = '';
  906. $root['status'] = 1;
  907. } else {
  908. fanwe_require(APP_ROOT_PATH . 'system/tim/TimApi.php');
  909. $api = createTimAPI();
  910. $ret = $api->group_send_group_msg2($user_id, $group_id, $msg_content);
  911. if ($ret['ActionStatus'] == 'FAIL' && $ret['ErrorCode'] == 10002) {
  912. //10002 系统错误,请再次尝试或联系技术客服。
  913. log_err_file(array(__FILE__, __LINE__, __METHOD__, $ret));
  914. $ret = $api->group_send_group_msg2($user_id, $group_id, $msg_content);
  915. }
  916. $GLOBALS['db']->autoExecute($table, $ret, 'UPDATE', 'id=' . $user_prop_id);
  917. //$videoGift_redis->update_db($user_prop_id, $ret);
  918. if ($ret['ActionStatus'] == 'FAIL') {
  919. if ($ret['ErrorCode'] == 80001) {
  920. $root['error'] = '该词已被禁用';
  921. } else {
  922. $root['error'] = $ret['ErrorInfo'] . ":" . $ret['ErrorCode'];
  923. }
  924. $root['status'] = 0;
  925. } else {
  926. $root['error'] = '';
  927. $root['status'] = 1;
  928. }
  929. }
  930. } else {
  931. $GLOBALS['db']->Rollback($pInTrans);
  932. $root['error'] = "用户".$m_config['diamonds_name']."不足";
  933. $root['status'] = 0;
  934. }
  935. } catch (Exception $e) {
  936. //异常回滚
  937. $root['error'] = $e->getMessage();
  938. $root['status'] = 0;
  939. $GLOBALS['db']->Rollback($pInTrans);
  940. }
  941. }
  942. }
  943. }
  944. ajax_return($root);
  945. }
  946. /**
  947. * 预生成好,红包随机队列
  948. * @param unknown_type $total_diamonds
  949. *
  950. * 0 1
  951. * 1 20
  952. * 2 3
  953. *
  954. */
  955. function red_rand_list($total_diamonds)
  956. {
  957. $list = array();
  958. while ($total_diamonds > 0) {
  959. $diamonds = mt_rand(1, 20);//随机取:1至20中的一个数字
  960. if ($total_diamonds >= $diamonds) {
  961. $total_diamonds = $total_diamonds - $diamonds;
  962. $list[] = $diamonds;
  963. } else {
  964. if ($total_diamonds >= 1) {
  965. $diamonds = 1;
  966. $total_diamonds = $total_diamonds - $diamonds;
  967. $list[] = $diamonds;
  968. }
  969. }
  970. }
  971. return $list;
  972. }
  973. /**
  974. * 把$total_diamonds 生成指定数量$num的,随机列表数
  975. * @param unknown_type $total_diamonds
  976. * @param unknown_type $num
  977. * @return multitype:number
  978. */
  979. function red_rand_list2($total_diamonds, $num)
  980. {
  981. $list = array();
  982. if ($num > $total_diamonds) {
  983. $num = $total_diamonds;
  984. }
  985. //先生成一批为:1 的
  986. for ($x = 0; $x < $num; $x++) {
  987. $list[] = 1;
  988. $total_diamonds = $total_diamonds - 1;
  989. }
  990. while ($total_diamonds > 0) {
  991. foreach ($list as $k => $v) {
  992. $diamonds = mt_rand(1, 19);//随机取:1至20中的一个数字
  993. if ($total_diamonds >= $diamonds) {
  994. $total_diamonds = $total_diamonds - $diamonds;
  995. } else {
  996. if ($total_diamonds >= 1) {
  997. $diamonds = 1;
  998. $total_diamonds = $total_diamonds - $diamonds;
  999. }
  1000. }
  1001. $list[$k] = $v + $diamonds;
  1002. if ($total_diamonds == 0) {
  1003. break;
  1004. }
  1005. }
  1006. };
  1007. return $list;
  1008. }
  1009. /**
  1010. * 抢红包
  1011. */
  1012. public function red_envelope()
  1013. {
  1014. $root = array();
  1015. $root['status'] = 1;
  1016. if (!$GLOBALS['user_info']) {
  1017. $root['error'] = "用户未登陆,请先登陆.";
  1018. $root['status'] = 0;
  1019. $root['user_login_status'] = 0;//有这个参数: user_login_status = 0 时,表示服务端未登陆、要求登陆,操作
  1020. } else {
  1021. $user_id = intval($GLOBALS['user_info']['id']);
  1022. $user_prop_id = intval($_REQUEST['user_prop_id']);//红包id
  1023. $m_config = load_auto_cache("m_config");//初始化手机端配置
  1024. //============================redis================================================
  1025. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedRedisService.php');
  1026. $videoRed_redis = new VideoRedRedisService();
  1027. //判断该用户没有抢过;
  1028. $ret = $videoRed_redis->get_user_winning($user_prop_id, $user_id);
  1029. if ($ret == false) {
  1030. //判断是否还有可以抢的红包
  1031. if ($videoRed_redis->red_exists($user_prop_id)) {
  1032. $money = $videoRed_redis->pop_red($user_prop_id);
  1033. if ($money > 0) {
  1034. allot_red_to_user($user_prop_id, $user_id, $money);
  1035. $root['diamonds'] = $money;
  1036. $root['error'] = "恭喜您抢到" . $money . "个".$m_config['diamonds_name'];
  1037. } else {
  1038. $root['status'] = 0;
  1039. $root['error'] = "手慢了,未捡到";
  1040. }
  1041. } else {
  1042. $root['status'] = 0;
  1043. $root['error'] = "手慢了,未捡到!";
  1044. }
  1045. } else {
  1046. $root['diamonds'] = $ret;
  1047. $root['error'] = "恭喜您抢到" . $ret . "个".$m_config['diamonds_name'];
  1048. }
  1049. }
  1050. ajax_return($root);
  1051. }
  1052. /**
  1053. * 抢红包---》看看大家的手气
  1054. */
  1055. public function user_red_envelope()
  1056. {
  1057. $root = array();
  1058. $root['status'] = 1;
  1059. //$GLOBALS['user_info']['id'] = 278;
  1060. if (!$GLOBALS['user_info']) {
  1061. $root['error'] = "用户未登陆,请先登陆.";
  1062. $root['status'] = 0;
  1063. $root['user_login_status'] = 0;//有这个参数: user_login_status = 0 时,表示服务端未登陆、要求登陆,操作
  1064. } else {
  1065. $user_prop_id = intval($_REQUEST['user_prop_id']);//红包id
  1066. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoRedRedisService.php');
  1067. $videoRed_redis = new VideoRedRedisService();
  1068. $list = $videoRed_redis->get_winnings($user_prop_id);
  1069. foreach ($list as $k => $v) {
  1070. $list[$k]['nick_name'] = emoji_decode($v['nick_name']);
  1071. }
  1072. $root['status'] = 1;
  1073. $root['list'] = $list;
  1074. }
  1075. ajax_return($root);
  1076. }
  1077. /**
  1078. * 送礼物给某人
  1079. */
  1080. public function send_prop()
  1081. {
  1082. $root = array();
  1083. if (!$GLOBALS['user_info']) {
  1084. $root['error'] = "用户未登陆,请先登陆." . print_r($_COOKIE, 1);
  1085. $root['status'] = 0;
  1086. $root['user_login_status'] = 0;//有这个参数: user_login_status = 0 时,表示服务端未登陆、要求登陆,操作
  1087. } else {
  1088. $user_id = intval($GLOBALS['user_info']['id']);
  1089. $prop_id = intval($_REQUEST['prop_id']);//礼物id
  1090. $num = intval($_REQUEST['num']);//礼物数量
  1091. $to_user_id = strim($_REQUEST['to_user_id']);//送给谁
  1092. $is_nospeaking = $GLOBALS['db']->getOne("SELECT is_nospeaking FROM " . DB_PREFIX . "user WHERE id=" . $user_id,
  1093. true, true);
  1094. if ($is_nospeaking) {
  1095. $root['status'] = 0;
  1096. $root['error'] = "被im全局禁言,不能发礼物";
  1097. ajax_return($root);
  1098. }
  1099. if ($user_id == $to_user_id) {
  1100. $root['error'] = "不能发礼物给自己";
  1101. $root['status'] = 0;
  1102. ajax_return($root);
  1103. }
  1104. $m_config = load_auto_cache("m_config");//初始化手机端配置
  1105. //检查测试账号不能发礼物给真实主播
  1106. $sql = "select mobile from " . DB_PREFIX . "user where id = '" . $to_user_id . "'";
  1107. $podcast_mobile = $GLOBALS['db']->getOne($sql);
  1108. if (($GLOBALS['user_info']['mobile'] == '13888888888' && $podcast_mobile != '13999999999') || $GLOBALS['user_info']['mobile'] == '13999999999' && $podcast_mobile != '13888888888') {
  1109. $root['error'] = "测试账号不能发礼物给真实主播";
  1110. $root['status'] = 0;
  1111. ajax_return($root);
  1112. }
  1113. $prop = load_auto_cache("prop_id", array('id' => $prop_id));
  1114. if ($num <= 0) {
  1115. $num = 1;
  1116. }
  1117. $total_diamonds = $num * $prop['diamonds'];
  1118. $total_score = $num * $prop['score'];
  1119. $total_ticket = intval($num * $prop['ticket']);
  1120. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
  1121. $user_redis = new UserRedisService();
  1122. $pInTrans = $GLOBALS['db']->StartTrans();
  1123. try {
  1124. //免费礼物
  1125. if ($total_diamonds == 0 && $total_score == 0 && $total_ticket == 0) {
  1126. $m_config = load_auto_cache("m_config");//初始化手机端配置
  1127. $root['to_msg'] = "收到一个" . $prop['name'] . ",获得" . $total_ticket . $m_config['ticket_name'] . ",可以去个人主页>我的收益 查看哦";
  1128. $to_diamonds = 0;
  1129. $to_ticket = intval($total_ticket);
  1130. $root['from_msg'] = "送给你一个" . $prop['name'];
  1131. $root['from_score'] = "你的经验值+" . $total_score;
  1132. $root['to_ticket'] = intval($to_ticket);
  1133. $root['to_diamonds'] = $to_diamonds;//可获得的:钻石数;只有红包时,才有
  1134. $root['to_user_id'] = $to_user_id;
  1135. $root['prop_icon'] = $prop['icon'];
  1136. $root['status'] = 1;
  1137. $root['prop_id'] = $prop_id;
  1138. $root['total_ticket'] = intval($user_redis->getOne_db($to_user_id, 'ticket'));//用户总的:印票数
  1139. } else {
  1140. //私聊送红包礼物没有经验
  1141. if ($prop['is_red_envelope'] == 1) {
  1142. $total_score = 0;
  1143. }
  1144. $sql = "update " . DB_PREFIX . "user set diamonds = diamonds - " . $total_diamonds . ", use_diamonds = use_diamonds + " . $total_diamonds . ", score = score + " . $total_score . " where id = '" . $user_id . "' and diamonds >= " . $total_diamonds;
  1145. $GLOBALS['db']->query($sql);
  1146. if ($GLOBALS['db']->affected_rows()) {
  1147. //将红包的钻石,直接加给被送用户
  1148. if ($prop['is_red_envelope'] == 1) {
  1149. //$desc = '我给大家送了一个红包';
  1150. //$user_redis->lock_diamonds($to_user_id,$total_diamonds);
  1151. $sql = "update " . DB_PREFIX . "user set diamonds = diamonds + " . $total_diamonds . " where id = " . $to_user_id;
  1152. $GLOBALS['db']->query($sql);
  1153. $root['to_msg'] = "收到一个" . $prop['name'] . ",获得" . $total_diamonds .$m_config['diamonds_name'].",可以去个人主页 查看哦";
  1154. $to_diamonds = $total_diamonds;//用户添加的:钻石 数;
  1155. $to_ticket = 0;
  1156. } else {
  1157. $m_config = load_auto_cache("m_config");//初始化手机端配置
  1158. $society_info = $GLOBALS['db']->getRow("select society_id,society_chieftain from " . DB_PREFIX . "user where id = " . $to_user_id);
  1159. if(defined('OPEN_SOCIETY_MODULE') && OPEN_SOCIETY_MODULE){
  1160. if($m_config['society_pattern'] == 2){
  1161. //收益归公会长所有
  1162. if($society_info['society_id']){
  1163. $this->soceity_rake($to_user_id, $total_ticket ,$user_id);
  1164. }else{
  1165. $sql = "update " . DB_PREFIX . "user set ticket = ticket + " . $total_ticket . " where id = " . $to_user_id;
  1166. $GLOBALS['db']->query($sql);
  1167. //写入用户日志
  1168. earnings_log($total_ticket, $society_info['society_id'], 0, 13, '私信获取收益:'.$total_ticket.'印票', $to_user_id);
  1169. }
  1170. }elseif($m_config['society_pattern'] == 1){
  1171. //有公会需要增加公会等级积分
  1172. if($society_info['society_id']){
  1173. society_level_syn($total_ticket, 0, $society_info['society_id']);
  1174. }
  1175. $sql = "update " . DB_PREFIX . "user set ticket = ticket + " . $total_ticket . " where id = " . $to_user_id;
  1176. $GLOBALS['db']->query($sql);
  1177. if($society_info['society_chieftain']){
  1178. $GLOBALS['db']->query("update ".DB_PREFIX."society set contribution = contribution + $total_ticket where id=".$society_info['society_id']);
  1179. //写入会长日志
  1180. earnings_log($total_ticket, $society_info['society_id'], $user_id, 13, '私信获取收益:'.$total_ticket.'印票', $to_user_id);
  1181. }else{
  1182. //写入用户日志
  1183. earnings_log($total_ticket, $society_info['society_id'], 0, 13, '私信获取收益:'.$total_ticket.'印票', $to_user_id);
  1184. }
  1185. }else{
  1186. $sql = "update " . DB_PREFIX . "user set ticket = ticket + " . $total_ticket . " where id = " . $to_user_id;
  1187. $GLOBALS['db']->query($sql);
  1188. //写入用户日志
  1189. earnings_log($total_ticket, $society_info['society_id'], 0, 13, '私信获取收益:'.$total_ticket.'印票', $to_user_id);
  1190. }
  1191. }else{
  1192. $sql = "update " . DB_PREFIX . "user set ticket = ticket + " . $total_ticket . " where id = " . $to_user_id;
  1193. $GLOBALS['db']->query($sql);
  1194. //写入用户日志
  1195. earnings_log($total_ticket, $society_info['society_id'], 0, 13, '私信获取收益:'.$total_ticket.'印票', $to_user_id);
  1196. }
  1197. $root['to_msg'] = "收到一个" . $prop['name'] . ",获得" . $total_ticket . $m_config['ticket_name'] . ",可以去个人主页>我的收益 查看哦";
  1198. $to_diamonds = 0;
  1199. $to_ticket = intval($total_ticket);
  1200. }
  1201. $this->check_invite($user_id, $total_ticket, $prop_id);
  1202. //插入:送礼物表
  1203. $video_prop = array();
  1204. $video_prop['prop_id'] = $prop_id;
  1205. $video_prop['prop_name'] = "'" . $prop['name'] . "'";
  1206. $video_prop['is_red_envelope'] = $prop['is_red_envelope'];
  1207. $video_prop['total_score'] = $total_score;
  1208. $video_prop['total_diamonds'] = $total_diamonds;
  1209. if ($prop['is_red_envelope'] == 1) {
  1210. $video_prop['total_ticket'] = intval($total_diamonds);
  1211. } else {
  1212. $video_prop['total_ticket'] = intval($total_ticket);
  1213. }
  1214. $video_prop['from_user_id'] = $user_id;
  1215. $video_prop['to_user_id'] = $to_user_id;
  1216. $video_prop['create_time'] = NOW_TIME;
  1217. $video_prop['create_date'] = "'".to_date(NOW_TIME,'Y-m-d')."'";
  1218. $video_prop['num'] = $num;
  1219. $video_prop['create_ym'] = to_date($video_prop['create_time'], 'Ym');
  1220. $video_prop['create_d'] = to_date($video_prop['create_time'], 'd');
  1221. $video_prop['create_w'] = to_date($video_prop['create_time'], 'W');
  1222. $video_prop['from_ip'] = "'".get_client_ip()."'";
  1223. $video_prop['is_private'] = 1;
  1224. //将礼物写入mysql表中
  1225. $field_arr = array(
  1226. 'prop_id',
  1227. 'prop_name',
  1228. 'is_red_envelope',
  1229. 'total_score',
  1230. 'total_diamonds',
  1231. 'total_ticket',
  1232. 'from_user_id',
  1233. 'to_user_id',
  1234. 'create_time',
  1235. 'create_date',
  1236. 'num',
  1237. 'create_ym',
  1238. 'create_d',
  1239. 'create_w',
  1240. 'from_ip',
  1241. 'is_private'
  1242. );
  1243. if(intval(OPEN_REWARD_GIFT)){
  1244. if(file_exists(APP_ROOT_PATH.'mapi/lib/core/award_function.php')) {
  1245. fanwe_require(APP_ROOT_PATH . 'mapi/lib/core/award_function.php');
  1246. $list_award = get_award_info();
  1247. }
  1248. }
  1249. if(intval(OPEN_REWARD_GIFT)&&intval($list_award['is_open_award'])==1){
  1250. $field_arr[] = 'is_award';
  1251. $video_prop['is_award'] = intval($prop['is_award']);
  1252. }
  1253. $fields = implode(",", $field_arr);
  1254. $valus = implode(",", $video_prop);
  1255. $table = createPropTable();
  1256. $table_info = $GLOBALS['db']->getRow("Describe ".$table." from_ip",true,true);
  1257. if(!$table_info){
  1258. $GLOBALS['db']->query("ALTER TABLE ".$table." ADD COLUMN `from_ip` varchar(255) NOT NULL COMMENT '送礼物人IP'");
  1259. }
  1260. $sql = "insert into " . $table . "(" . $fields . ") VALUES (" . $valus . ")";
  1261. $GLOBALS['db']->query($sql);
  1262. $user_prop_id = $GLOBALS['db']->insert_id();
  1263. //写入总表
  1264. if(file_exists(APP_ROOT_PATH.'mapi/lib/core/award_function.php')){
  1265. fanwe_require(APP_ROOT_PATH.'mapi/lib/core/award_function.php');
  1266. $table_all = createPropAllTable();
  1267. $sql = "insert into ".$table_all." (".$fields.") VALUES (".$valus.")";
  1268. $GLOBALS['db']->query($sql);
  1269. }
  1270. //提交事务
  1271. $GLOBALS['db']->Commit($pInTrans);
  1272. $pInTrans = false;
  1273. if ($prop['is_red_envelope'] == 0) {
  1274. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/VideoContributionRedisService.php');
  1275. $videoCont_redis = new VideoContributionRedisService();
  1276. $videoCont_redis->insert_db($user_id, $to_user_id, 0, $total_ticket);
  1277. }
  1278. //分销功能 计算抽成
  1279. if (defined('OPEN_DISTRIBUTION') && OPEN_DISTRIBUTION == 1 && $prop['is_red_envelope'] == 0 && $total_ticket > 0) {
  1280. $this->distribution_calculate($user_id, $total_ticket);
  1281. }
  1282. $this->distribution($to_user_id, 0, $total_ticket);
  1283. user_deal_to_reids(array($user_id, $to_user_id));
  1284. //更新用户等级
  1285. $user_info = $user_redis->getRow_db($user_id,
  1286. array('id', 'score', 'online_time', 'user_level'));
  1287. user_leverl_syn($user_info);
  1288. $root['from_msg'] = "送给你一个" . $prop['name'];
  1289. $root['from_score'] = "你的经验值+" . $total_score;
  1290. $root['to_ticket'] = intval($to_ticket);
  1291. $root['to_diamonds'] = $to_diamonds;//可获得的:钻石数;只有红包时,才有
  1292. $root['to_user_id'] = $to_user_id;
  1293. $root['prop_icon'] = $prop['icon'];
  1294. $root['status'] = 1;
  1295. $root['prop_id'] = $prop_id;
  1296. $root['total_ticket'] = intval($user_redis->getOne_db($to_user_id, 'ticket'));//用户总的:印票数
  1297. } else {
  1298. $GLOBALS['db']->Rollback($pInTrans);
  1299. $root['error'] = "用户".$m_config['diamonds_name']."不足";
  1300. $root['status'] = 0;
  1301. }
  1302. }
  1303. //减少用户钻石
  1304. } catch (Exception $e) {
  1305. //异常回滚
  1306. $root['error'] = $e->getMessage();
  1307. $root['status'] = 0;
  1308. $GLOBALS['db']->Rollback($pInTrans);
  1309. }
  1310. }
  1311. ajax_return($root);
  1312. }
  1313. /**
  1314. * 游戏分销
  1315. * @param $podcast_id
  1316. * @param $video_id
  1317. * @param $total_ticket
  1318. */
  1319. private function distribution($podcast_id, $video_id, $total_ticket)
  1320. {
  1321. require_once APP_ROOT_PATH . 'mapi/lib/core/Model.class.php';
  1322. Model::$lib = dirname(__FILE__);
  1323. if (defined('GAME_DISTRIBUTION') && GAME_DISTRIBUTION) {
  1324. Model::build('game_distribution')->addLog($podcast_id, $video_id, 0, $total_ticket, '直播礼物分销',1);
  1325. }
  1326. if (defined('WEIXIN_DISTRIBUTION') && WEIXIN_DISTRIBUTION) {
  1327. Model::build('weixin_distribution_log')->addLog($podcast_id, $total_ticket, '直播礼物分销(微信)',1);
  1328. }
  1329. }
  1330. /**
  1331. * 分销抽成
  1332. * @param $user_id
  1333. * @param $total_ticket
  1334. */
  1335. private function distribution_calculate($user_id, $total_ticket)
  1336. {
  1337. $root = array();
  1338. $m_config = load_auto_cache("m_config");//初始化手机端配置
  1339. $table = DB_PREFIX . 'distribution_log';
  1340. fanwe_require(APP_ROOT_PATH . 'mapi/lib/redis/UserRedisService.php');
  1341. $user_redis = new UserRedisService();
  1342. $to_user_id = $user_redis->getOne_db($user_id, 'p_user_id');//用户总的:印票数
  1343. $ticket = 0;
  1344. $result = 0;
  1345. if (intval($to_user_id) > 0 && intval($m_config['distribution']) == 1 && $user_id > 0 && $total_ticket > 0) {
  1346. $ticket = round($m_config['distribution_rate'] * 0.01 * $total_ticket, 2);
  1347. $sql = "select id from " . $table . " where to_user_id = " . $to_user_id . " and from_user_id = " . $user_id;
  1348. $distribution_id = $GLOBALS['db']->getOne($sql);
  1349. if (intval($distribution_id) > 0) {
  1350. $sql = "update " . $table . " set ticket = ticket + " . $ticket . " where id = " . $distribution_id;
  1351. $GLOBALS['db']->query($sql);
  1352. $result = 1;
  1353. } else {
  1354. //插入:分销日志
  1355. $video_prop = array();
  1356. $video_prop['from_user_id'] = $user_id;
  1357. $video_prop['to_user_id'] = $to_user_id;
  1358. $video_prop['create_date'] = "'" . to_date(NOW_TIME, 'Y-m-d') . "'";
  1359. $video_prop['ticket'] = $ticket;
  1360. $video_prop['create_time'] = NOW_TIME;
  1361. $video_prop['create_ym'] = to_date($video_prop['create_time'], 'Ym');
  1362. $video_prop['create_d'] = to_date($video_prop['create_time'], 'd');
  1363. $video_prop['create_w'] = to_date($video_prop['create_time'], 'W');
  1364. //将日志写入mysql表中
  1365. $field_arr = array(
  1366. 'from_user_id',
  1367. 'to_user_id',
  1368. 'create_date',
  1369. 'ticket',
  1370. 'create_time',
  1371. 'create_ym',
  1372. 'create_d',
  1373. 'create_w'
  1374. );
  1375. $fields = implode(",", $field_arr);
  1376. $valus = implode(",", $video_prop);
  1377. $sql = "insert into " . $table . "(" . $fields . ") VALUES (" . $valus . ")";
  1378. $GLOBALS['db']->query($sql);
  1379. $result = $GLOBALS['db']->insert_id();
  1380. }
  1381. if (intval($result) > 0) {
  1382. $sql = "update " . DB_PREFIX . "user set ticket = ticket + " . $ticket . " where id = " . $to_user_id;
  1383. $GLOBALS['db']->query($sql);
  1384. }
  1385. }
  1386. }
  1387. //-------------无抽成模式,所得私信收益归公会长所有---------------
  1388. /**
  1389. * @param int $to_user_id 被送礼物用户ID
  1390. * @param int $total_ticket 获得的收益
  1391. * @param int $user_id 用户id
  1392. */
  1393. public function soceity_rake($to_user_id,$total_ticket,$user_id){
  1394. //判断是否有公会
  1395. $user_info = $GLOBALS['db']->getRow("select society_id,society_chieftain from ".DB_PREFIX."user where id=".$to_user_id);
  1396. $society_info = $GLOBALS['db']->getRow("select user_id,status from ".DB_PREFIX."society where id=".$user_info['society_id']);
  1397. if($user_info['society_id'] > 0 && $society_info['status'] == 1){
  1398. //主播的收益加到公会中
  1399. $pInTrans = $GLOBALS['db']->StartTrans();
  1400. try
  1401. {
  1402. //加入公会收益
  1403. $sql = "update ".DB_PREFIX."society set chairman_earnings=chairman_earnings+".$total_ticket." where user_id=".$society_info['user_id'];
  1404. $GLOBALS['db']->query($sql);
  1405. //排除公会长
  1406. if(!$user_info['society_chieftain']){
  1407. //将用户上交的公会的印票计入已提现
  1408. $sql = "update ".DB_PREFIX."user set ticket = ticket + ".$total_ticket.",refund_ticket = refund_ticket + ".$total_ticket.",society_ticket=society_ticket+".$total_ticket." where id = ".$to_user_id." and ticket >=refund_ticket";
  1409. $status = $GLOBALS['db']->query($sql);
  1410. //将收益汇入公会长
  1411. $sql_chieftain = "update ".DB_PREFIX."user set ticket = ticket + ".$total_ticket." where id = ".$society_info['user_id'];
  1412. $GLOBALS['db']->query($sql_chieftain);
  1413. user_deal_to_reids(array($society_info['user_id']));
  1414. //写入用户日志
  1415. earnings_log($total_ticket, $user_info['society_id'], 0, 10, '私信获取收益:'.$total_ticket.'印票', $to_user_id);
  1416. //写入会长日志
  1417. earnings_log($total_ticket, $user_info['society_id'], $to_user_id, 10, '主播'.$to_user_id.'私信获取收益,产生公会收益'.$total_ticket.'印票', $society_info['user_id']);
  1418. }else{
  1419. $sql = "update ".DB_PREFIX."user set ticket = ticket + ".$total_ticket.",society_ticket=society_ticket+".$total_ticket." where id = ".$to_user_id." and ticket >=refund_ticket";
  1420. $status = $GLOBALS['db']->query($sql);
  1421. //写入用户日志
  1422. earnings_log($total_ticket, $user_info['society_id'], $user_id, 10, '私信获取收益:'.$total_ticket.'印票', $society_info['user_id']);
  1423. }
  1424. //公会等级积分的写入
  1425. society_level_syn($total_ticket, 0, $user_info['society_id']);
  1426. //提交事务
  1427. $GLOBALS['db']->Commit($pInTrans);
  1428. }catch(Exception $e){
  1429. //异常回滚
  1430. $GLOBALS['db']->Rollback($pInTrans);
  1431. //写入会长日志
  1432. earnings_log($total_ticket, $user_info['society_id'], 0, 10, '公会成员'.$to_user_id.'贡献收益,'.$total_ticket.'印票'.'失败', $society_info['user_id']);
  1433. }
  1434. //-----事务结束-----
  1435. }
  1436. return ;
  1437. }
  1438. }