Party.php 103 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use Redis;
  5. use think\Db;
  6. use think\Exception;
  7. use think\exception\PDOException;
  8. use think\exception\ValidateException;
  9. use app\common\service\RoomService;
  10. use app\common\library\Easemob;
  11. require_once EXTEND_PATH . 'shengwang/src/RtcTokenBuilder2.php';
  12. require_once EXTEND_PATH . 'shengwang/src/RtmTokenBuilder2.php';
  13. /**
  14. *
  15. * 派对信息接口
  16. */
  17. class Party extends Api
  18. {
  19. protected $noNeedLogin = ['test_party_attr','updatePartyInfo','getPatyType','shengwang_token','addUserPositionToParty', 'clearMoney','clearCharm','getPartyRankList','getDefaultBackground','getPartGifList','getPartHeadgifList','isNotalk','getMusicList','updateTops','handleParty'];
  20. protected $noNeedRight = ['*'];
  21. protected $redis;
  22. public function __construct()
  23. {
  24. $this->roomTypeArr = [1=>"party",2=>"live"];
  25. //redis
  26. $redis = new Redis();
  27. $redisconfig = config("redis");
  28. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  29. if ($redisconfig['redis_pwd']) {
  30. $redis->auth($redisconfig['redis_pwd']);
  31. }
  32. if($redisconfig['redis_selectdb'] > 0){
  33. $redis->select($redisconfig['redis_selectdb']);
  34. }
  35. $this->redis = $redis;
  36. parent::__construct();
  37. }
  38. /**
  39. * 创建/进入派对
  40. */
  41. public function createParty() {
  42. if(!$this->apiLimit()){
  43. $this->error('访问频繁,请稍后重试');
  44. }
  45. $party_logo = input('party_logo');
  46. $party_name = input('party_name');
  47. $seatnum = input('seatnum');
  48. $is_public = input('is_public',1); //是否公开:1=是,0=否
  49. //$room_type = input('room_type',2); // 房间类型:1=公会房,2=K歌房
  50. $room_type = 2; // 房间类型:1=公会房,2=K歌房
  51. $partyModel = new \app\common\model\Party();
  52. $user_id = $this->auth->id;
  53. // 开直播时,判断当前用户是否为主播
  54. $anchorInfo = [];
  55. if($room_type == 2) {
  56. /*$anchorInfo = \app\common\model\UserAnchor::where(["user_id"=>$user_id])->find();
  57. $anchorInfo || $this->error("您还未申请主播!");
  58. $anchorInfo->status == 0 && $this->error("您的主播申请信息还未受理,请耐心等待!");
  59. $anchorInfo->status == 2 && $this->error("您的主播申请未通过,请重新提交!");*/
  60. }
  61. // 1。实名认证
  62. $userAuthInfo = \app\common\model\UserAuth::where(["user_id"=>$user_id])->find();
  63. if($userAuthInfo) {
  64. if($userAuthInfo->status == 0) {
  65. $this->error("您的实名认证还在这审核中...,请耐心等待!");
  66. } elseif($userAuthInfo->status == 2) {
  67. $this->error("您的实名认证审核未通过,请重新审核!");
  68. }
  69. } else {
  70. $this->error("请先申请实名认证!");
  71. }
  72. if($room_type == 1) {
  73. // 2。开厅申请
  74. $authInfo = model('Guild')->where(["user_id"=>$user_id])->order('id desc')->find();
  75. if($authInfo) {
  76. if($authInfo->status == 0) {
  77. $this->error("您的公会申请还在这审核中...,请耐心等待!");
  78. } elseif($authInfo->status == -1) {
  79. $this->error("您的公会申请已解散,请重新申请!");
  80. }
  81. } else {
  82. $this->error("请先申请公会!");
  83. }
  84. }
  85. // 判断派对是否存在
  86. $where = [];
  87. $where["user_id"] = $user_id;
  88. $where["room_type"] = $room_type;
  89. $sqlPartyInfo = $partyModel->where($where)->find();
  90. if(!$sqlPartyInfo) { // sql中不存在派对信息
  91. $party_type = 8; // 派对类型(情感互动,心动点单 等)
  92. $party_notice = "请编辑房间公告"; // 派对公告
  93. $party_notice_detail = "请编辑房间公告内容!"; // 派对公告详情
  94. $party_ids = $partyModel->column("party_id");
  95. // 创建派对ID (临时ID四位,派对数不超过8999)
  96. $party_id = $this->auth->getUinqueId(4,$party_ids);
  97. if($party_id > 9999) {
  98. $this->error("房间超限,请联系客服");
  99. }
  100. if ($room_type == 1) {//公会房间获取家族信息
  101. $guildWhere['user_id'] = $user_id;
  102. $guildWhere['status'] = 1;
  103. $guild = model('Guild')->where($guildWhere)->order('id desc')->find();
  104. if (!empty($guild)) {
  105. $party_name = $guild['name'].'的房间';
  106. $party_logo = $guild['image'];
  107. $party_notice = $guild['desc'];
  108. $party_notice_detail = $guild['notice'];
  109. }
  110. $party_type = 7;
  111. }
  112. //创建房间,初始化环信
  113. $easemob_room_id = $this->easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type);
  114. //写入派对
  115. Db::startTrans();
  116. $data = [];
  117. $data["user_id"] = $this->auth->id;
  118. $data["room_type"] = $room_type;
  119. $data["party_id"] = $party_id;
  120. $data["party_hot"] = 0;
  121. $data["party_name"] = $party_name;
  122. $data["party_logo"] = $party_logo;
  123. $data["party_type"] = $party_type;
  124. $data["party_notice"] = $party_notice;
  125. $data["party_notice_detail"] = $party_notice_detail;
  126. $data["is_online"] = 1;
  127. $data["status"] = 1;//直接正常状态
  128. $data["is_recommend"] = 0;
  129. $data["createtime"] = time();
  130. $data["easemob_room_id"] = $easemob_room_id;
  131. if($room_type == 1){
  132. $data["platRate"] = config('site.party_party_platrate');
  133. $data["guilderRate"] = config('site.party_party_guilderrate');
  134. }else{
  135. $data["platRate"] = config('site.party_live_platrate');
  136. $data["guilderRate"] = config('site.party_live_guilderrate');
  137. }
  138. $id = $partyModel->insertGetId($data);
  139. if(!$id) {
  140. Db::rollback();
  141. $this->error("房间创建失败,请稍后重试!");
  142. }
  143. $data["id"] = $id;
  144. if($room_type == 2) {
  145. \app\common\model\User::update(["is_live"=>1],["id"=>$user_id]);
  146. }
  147. $partyInfo = $partyModel->where(["id"=>$id])->find();
  148. $partyInfo["is_new"] = 1;
  149. // 冠名
  150. $userInfo = [];
  151. if($partyInfo["naming"]) {
  152. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$partyInfo["naming"]])->find();
  153. }
  154. // 头像
  155. if($partyInfo["user_id"] > 0 && $room_type == 2) {
  156. $partyInfo["avatar"] = \app\common\model\User::where(["id"=>$partyInfo["user_id"]])->value("avatar");
  157. }
  158. // 派对类型
  159. $partyTypeName = "普通房";
  160. if($partyInfo["party_type"]) {
  161. $partyTypeName = \app\common\model\PartyType::where(["id"=>$partyInfo["party_type"]])->value("name");
  162. }
  163. $partyInfo["naming"] = $userInfo;
  164. $partyInfo["type_name"] = $partyTypeName;
  165. // 加入缓存排序,rediskey:派对热度
  166. $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  167. // 加入缓存,rediskey:派对详情
  168. $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($partyInfo));
  169. if($room_type != 2) {
  170. //更新家族 party_id
  171. $guildWhere['user_id'] = $user_id;
  172. $guildWhere['status'] = 1;
  173. $guild = model('Guild')->where($guildWhere)->order('id desc')->find();
  174. if (!empty($guild)) {
  175. $rs_guild = model('Guild')->where($guildWhere)->update(['party_id'=>$partyInfo['id']]);
  176. if($rs_guild === false){
  177. Db::rollback();
  178. $this->error('创建房间失败');
  179. }
  180. }
  181. }
  182. Db::commit();
  183. } else {
  184. if($sqlPartyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
  185. // 如果是房主并且房间处于离线状态下激活
  186. if ($sqlPartyInfo['user_id'] != $user_id) {
  187. $this->error("当前直播已结束!");
  188. }
  189. $partyInfo = $sqlPartyInfo;
  190. $partyInfo["is_new"] = 0;
  191. //naming avatar type_name
  192. if(empty($sqlPartyInfo['easemob_room_id'])){
  193. $easemob_room_id = $this->easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type);
  194. }else{
  195. $easemob_room_id = $sqlPartyInfo['easemob_room_id'];
  196. }
  197. //重置easemob_room_id
  198. }
  199. //声网三个token
  200. $partyInfo['shengwang_token'] = $this->shengwang_token_private($easemob_room_id);
  201. $this->success("获取成功!",$partyInfo);
  202. }
  203. //创建房间,初始化环信
  204. private function easemob_createroom($party_name,$party_logo,$seatnum,$is_public,$room_type){
  205. //[环信]创建聊天室
  206. $easemob = new Easemob();
  207. $easemob_room_id = $easemob->room_createRoom($party_name,$party_name,$this->auth->id);
  208. if(empty($easemob_room_id)){
  209. $this->error('创建房间失败');
  210. }
  211. //[环信]初始化房间,初始化自定义属性
  212. $matedata = [
  213. 'seatnum'=> $seatnum,//麦位数量
  214. 'waitsing_list' => json_encode([]),//已点歌曲列表
  215. 'party_name' => $party_name,//房名字
  216. 'party_logo' => $party_logo,//logo
  217. 'is_public' => $is_public,//是否公开
  218. 'room_type' => $room_type,//房间类型
  219. 'online_user_num' => 0,//当前房间在线的人
  220. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  221. 'background' => '', //房间背景
  222. ];
  223. $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$matedata);
  224. //[环信]初始化N个麦位
  225. $seatdata = [];
  226. for($i=0;$i<$seatnum;$i++){
  227. $seat = [
  228. 'charm' => 0, //红心,魅力值
  229. 'isMaster' => false, // 是否是房主
  230. 'headUrl' => '', // 头像
  231. 'userNo' => '', // 座位上用户no
  232. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  233. 'name' => '', // 座位上用户昵称
  234. 'seatIndex' => -1, // 座位编号
  235. 'chorusSongCode' => '', // 是否合唱
  236. 'isAudioMuted' => 1, // 是否静音
  237. 'isVideoMuted' => 0, // 是否开启视频
  238. 'checked' => false, // 用于送礼物选择用户
  239. 'isUsed' => true, // 用于送礼物选择用户
  240. 'gender' => 1, //性别
  241. ];
  242. //创建完房间就进入,所以房主直接在麦位0
  243. if($i == 0){
  244. $seat['isMaster'] = true;
  245. $seat['headUrl'] = localpath_to_netpath($this->auth->avatar);
  246. $seat['userNo'] = $this->auth->id;
  247. $seat['rtcUid'] = $this->auth->id;
  248. $seat['name'] = $this->auth->nickname;
  249. $seat['seatIndex'] = $i;
  250. }
  251. $seatdata['seat'.$i] = json_encode($seat);
  252. }
  253. $rs = $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$seatdata);
  254. return $easemob_room_id;
  255. }
  256. //声网3个token
  257. public function shengwang_token(){
  258. $party_id = input('party_id'); // 派对ID
  259. $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
  260. if(!$partyInfo) {
  261. $this->error(__('派对不存在!'));
  262. }
  263. $rs = $this->shengwang_token_private($partyInfo['easemob_room_id']);
  264. $this->success(1,$rs);
  265. }
  266. //声网3个token
  267. private function shengwang_token_private($easemob_room_id){
  268. $config = config('shengwang');
  269. $appId = $config['app_id'];
  270. $appCertificate = $config['app_zhengshu'];;
  271. $channelName = $easemob_room_id;
  272. $uid = 0; //任何人都可进入
  273. $uidStr = '0';//任何人都可进入
  274. $tokenExpirationInSeconds = 86400;
  275. $privilegeExpirationInSeconds = 86400;
  276. //rtc token
  277. $rtc_token = \RtcTokenBuilder2::buildTokenWithUid($appId, $appCertificate, $channelName, $uid, \RtcTokenBuilder2::ROLE_PUBLISHER, $tokenExpirationInSeconds, $privilegeExpirationInSeconds);
  278. //chorus token
  279. $chorus_token = \RtcTokenBuilder2::buildTokenWithUid($appId, $appCertificate, $channelName.'_ex', $uid, \RtcTokenBuilder2::ROLE_PUBLISHER, $tokenExpirationInSeconds, $privilegeExpirationInSeconds);
  280. //rtm token
  281. $user = '0';
  282. $expireTimeInSeconds = 86400;
  283. $rtm_token = \RtmTokenBuilder2::buildToken($appId, $appCertificate, $user, $expireTimeInSeconds);
  284. $result = [
  285. 'rtc_token' => $rtc_token,
  286. 'chorus_token' => $chorus_token,
  287. 'rtm_token' => $rtm_token,
  288. ];
  289. //dump($result);
  290. return $result;
  291. }
  292. /**
  293. * 解散房间
  294. */
  295. public function closeParty() {
  296. $party_id = input('party_id'); // 派对ID
  297. $room_type = input('room_type',1); // 房间类型:1=派对,2=直播
  298. if (!$party_id || !in_array($room_type,[1,2])) {
  299. $this->error(__('Invalid parameters'));
  300. }
  301. $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
  302. if(!$partyInfo) {
  303. $this->error(__('派对不存在!'));
  304. }
  305. if($partyInfo['user_id'] != $this->auth->id){
  306. $this->error('房主才能解散');
  307. }
  308. //用户魅力榜,清空
  309. $this->redis->zRemRangeByRank("hourCharm_".$party_id, 0,-1);
  310. //用户贡献榜,清空
  311. $this->redis->zRemRangeByRank("hourWealth_".$party_id, 0,-1);
  312. //贡献榜前三,设置为空
  313. $this->redis->hSet("user_jewel_top3",$party_id,json_encode([]));
  314. //排麦列表,清空
  315. $this->redis->hSet("party_lineup",$party_id,serialize([]));
  316. //清空房间
  317. $user_ids = $this->redis->hGetAll("online_".$party_id);
  318. if(!empty($user_ids)){
  319. foreach($user_ids as $key => $userId){
  320. $this->redis->HDel("online_" . $party_id, $userId);
  321. $this->redis->zRem("party_user_".$party_id,$userId); //新加的
  322. $this->redis->hDel("livingUser", $userId);
  323. }
  324. }
  325. //环信操作
  326. $easemob_room_id = $partyInfo['easemob_room_id'];
  327. if(!empty($easemob_room_id)){
  328. ///初始化房间信息,重置自定义属性
  329. $matedata = [
  330. 'waitsing_list' => json_encode([]), //清空房间待唱列表
  331. 'online_user_num' => 0, //当前房间在线的人
  332. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  333. ];
  334. $easemob = new Easemob();
  335. $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  336. //重置麦位
  337. $seatnum = $partyInfo['seatnum'];
  338. $seatdata = [];
  339. for($i=0;$i<$seatnum;$i++){
  340. $seat = [
  341. 'charm' => 0, //红心,魅力值
  342. 'isMaster' => false, // 是否是房主
  343. 'headUrl' => '', // 头像
  344. 'userNo' => '', // 座位上用户no
  345. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  346. 'name' => '', // 座位上用户昵称
  347. 'seatIndex' => -1, // 座位编号
  348. 'chorusSongCode' => '', // 是否合唱
  349. 'isAudioMuted' => 1, // 是否静音
  350. 'isVideoMuted' => 0, // 是否开启视频
  351. 'checked' => false, // 用于送礼物选择用户
  352. 'isUsed' => true, // 用于送礼物选择用户
  353. 'gender' => 1, //性别
  354. ];
  355. $seatdata['seat'.$i] = json_encode($seat);
  356. }
  357. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$this->auth->id,$seatdata);
  358. //清空房间人,聊天室
  359. //分页获取聊天室成员列表
  360. $usernames = $easemob->room_listRoomMembers($easemob_room_id,0);
  361. //批量移除聊天室成员
  362. $user_ids = [];
  363. if(!empty($usernames)){
  364. foreach($usernames as $username){
  365. foreach($username as $key => $val)
  366. {
  367. $user_ids[] = $val;
  368. }
  369. }
  370. }
  371. if(!empty($user_ids)){
  372. $easemob->room_removeRoomMembers($easemob_room_id,$user_ids);
  373. }
  374. }
  375. $this->success();
  376. }
  377. /**
  378. * 获取派对列表排序
  379. */
  380. public function getPartyList() {
  381. $order = input('orderby','new');
  382. $this->reset_all_party();
  383. //
  384. $orderby = 'party.party_hot desc, party.id desc';
  385. $where = [];
  386. if($order == 'recommend'){
  387. $where['party.is_recommend'] = 1;
  388. }
  389. if($order == 'follow'){
  390. $follow_party_ids = Db::name('party_follow')->where('user_id',$this->auth->id)->column('party_id');
  391. $where['party.id'] = ['IN',$follow_party_ids];
  392. }
  393. $where['party.status'] = 1; //正常
  394. //$where['party.is_online'] = 1; //在线
  395. $where['party.is_close'] = 0; //未关闭
  396. $where['party.is_public'] = 1; //是否公开:1=是,0=否
  397. $field = 'party.id,party.user_id,party.room_type,party.party_id,party.party_name,party.party_hot,
  398. party.party_logo,party.party_pass,party.party_type,party.easemob_room_id,type.name as type_name';
  399. $list = Db::name('party')->alias('party')
  400. ->join('party_type type','party.party_type = type.id','LEFT')
  401. ->field($field)->where($where)->order($orderby)->autopage()->select();
  402. $list = list_domain_image($list,['party_logo']);
  403. $resdata = [];
  404. if (!empty($list)) {
  405. $roomService = new RoomService();
  406. foreach($list as $k => $v) {
  407. $mod = isset($v["party_type"]) ? intval($v["party_type"])%5 : 1;
  408. //房间用户列表
  409. $userDataResult = $roomService->getPartyUserList(['party_id'=>$v['id']]);
  410. $userDataRes = isset($userDataResult['data']) ? $userDataResult['data'] : [];
  411. $partyUserList = [
  412. 'user_list' => isset($userDataRes['member_list']) ? $userDataRes['member_list'] : [],
  413. 'online_num' => isset($userDataRes['online_num']) ? $userDataRes['online_num'] : 0,
  414. ];
  415. $resdata[] = [
  416. "id" => $v["id"],
  417. "user_id" => $v["user_id"],
  418. "room_type" => $v['room_type'],
  419. "party_id" => $v["party_id"],
  420. "r_id" => $v["party_id"],
  421. "party_name" => $v["party_name"],
  422. "party_hot" => $v['party_hot'],
  423. "party_logo" => $v["party_logo"],
  424. "party_pass" => $v["party_pass"] ? $v["party_pass"]:"",
  425. "party_type" => $v['type_name'],
  426. "party_type_color" => $mod == 0?5:$mod,
  427. "easemob_room_id" => $v['easemob_room_id'],
  428. "party_user" => $partyUserList,
  429. ];
  430. }
  431. }
  432. $this->success("获取成功!",$resdata);
  433. }
  434. //把所有房间的信息,都放到redis里去
  435. private function reset_all_party(){
  436. //rediskey:派对热度
  437. $redisPartyRankList = $this->redis->zRevRange("partyRank",0,-1,true);
  438. //rediskey:派对热度
  439. $redisLiveRankList = $this->redis->zRevRange("liveRank",0,-1,true);
  440. $roomService = new RoomService();
  441. if(!$redisPartyRankList) {//party
  442. $roomParams = ['room_type' => 1];
  443. $roomPartyRes = $roomService->roomRedis($roomParams);
  444. $redisPartyRankList = $roomPartyRes['data'];
  445. }
  446. if(!$redisLiveRankList) {//live
  447. $roomParams = ['room_type' => 2];
  448. $roomLiveRes = $roomService->roomRedis($roomParams);
  449. $redisLiveRankList = $roomLiveRes['data'];
  450. }
  451. }
  452. /**
  453. * 获取派对用户排序,(日榜,周榜,月榜) X (魅力榜,财富榜)
  454. */
  455. /*public function getPartyUserRank_old() {
  456. $room_type = input('room_type',1); // 房间类型
  457. $party_id = input("party_id");// 派对ID
  458. if (!$party_id) $this->error(__('Invalid parameters'));
  459. $res = [];
  460. // 获取今天
  461. $day = date("Ymd");
  462. // 获取本周第一天
  463. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  464. // 获取本月第一天
  465. $monthday = date("Ym01");
  466. $userModel = new \app\common\model\User();
  467. // 获取条数
  468. $num = 50;
  469. // 获取50条魅力排行日记录
  470. $getday = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$day."d",0,$num-1,true);
  471. $res['getRankListDay'] = $userModel->rankList($getday);
  472. // 获取50条财富排行日记录
  473. $today = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$day."d",0,$num-1,true);
  474. $res['toRankListDay'] = $userModel->rankList($today);
  475. // 获取50条魅力排行周记录
  476. $getweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$weekday."w",0,$num-1,true);
  477. $res['getRankListWeek'] = $userModel->rankList($getweek);
  478. // 获取50条财富排行周记录
  479. $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$weekday."w",0,$num-1,true);
  480. $res['toRankListWeek'] = $userModel->rankList($toweek);
  481. // 获取50条魅力排行月记录
  482. $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_get_".$party_id.":".$monthday."m",0,$num-1,true);
  483. $res['getRankListMonth'] = $userModel->rankList($toweek);
  484. // 获取50条财富排行月记录
  485. $toweek = $this->redis->zRevRange($this->roomTypeArr[$room_type]."_jewel_to_".$party_id.":".$monthday."m",0,$num-1,true);
  486. $res['toRankListMonth'] = $userModel->rankList($toweek);
  487. return $this->success("获取成功!",$res);
  488. }*/
  489. //获取派对用户排序(魅力榜,财富榜),有数值的在上,0的在下
  490. public function getPartyUserRank() {
  491. $party_id = input("party_id");// 派对ID
  492. if (!$party_id) $this->error(__('Invalid parameters'));
  493. $userModel = new \app\common\model\User();
  494. // 获取所有魅力排行
  495. $getday = $this->redis->zRevRange("hourCharm_".$party_id,0,-1,true);
  496. $res['getRankListDay'] = $userModel->rankList($getday);
  497. // 获取所有财富排行
  498. $today = $this->redis->zRevRange("hourWealth_".$party_id,0,-1,true);
  499. $res['toRankListDay'] = $userModel->rankList($today);
  500. return $this->success("获取成功!",$res);
  501. }
  502. /**
  503. * 派对热度更新(已废弃)
  504. */
  505. /*public function changeUserPartyhot() {
  506. $party_id = input('party_id',0,"intval"); // 派对ID
  507. $room_type = input('room_type',1); // 房间类型
  508. $party_hot = input('party_hot'); // 房间热度(正数表示提高的热度值,负数表示降低的热度值)
  509. if (!$party_id || !$party_hot) {
  510. $this->error(__('Invalid parameters'));
  511. }
  512. // 更新热度
  513. $this->redis->zIncrBy($this->roomTypeArr[$room_type]."Rank", $party_hot, $party_id);
  514. return $this->success("更新成功!");
  515. }*/
  516. /**
  517. * 更新主持人和麦位前四位至首页房间排行
  518. */
  519. public function addUserPositionToParty() {
  520. $party_id = input('party_id',0,"intval"); // 直播间ID
  521. $user_id = input('user_id',0,"intval"); // 用户ID
  522. $upOrdown = input('upordown'); // 上下麦:1=上麦-1=下麦
  523. $room_type = input('room_type',1); // 房间类型
  524. $avatar = input('avatar'); // 用户头像 (upOrdown参数为-1时可以不传)
  525. $position = input('position'); // 麦位置:0=支持人,1=1号麦,2=2号麦,3=3号麦,4=4号麦
  526. if (!$party_id || !$user_id || !in_array($position,[0,1,2,3,4,5,6,7,8]) || !in_array($upOrdown,[1,-1])) {
  527. $this->error(__('Invalid parameters'));
  528. }
  529. if($upOrdown == 1) {
  530. // 先完成所有麦上记录
  531. \app\common\model\UserOnsiteTime::update(["status"=>2],["user_id"=>$user_id]);
  532. // 保存上麦记录
  533. $data = [];
  534. $data["user_id"] = $user_id;
  535. $data["party_id"] = $party_id;
  536. $data["onsite_time"] = time();
  537. \app\common\model\UserOnsiteTime::insert($data);
  538. }
  539. if($upOrdown == -1) {
  540. // 更新下麦时间
  541. $update = [];
  542. $update["offsite_time"] = time();
  543. $update["status"] = 2;
  544. \app\common\model\UserOnsiteTime::update($update,["user_id"=>$user_id,"status"=>1]);
  545. }
  546. if(in_array($position,[5,6,7,8])) return $this->success("设置成功!");
  547. $redisData = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  548. if($redisData) {
  549. $partyInfo = json_decode($redisData,true);
  550. if($upOrdown == 1) {
  551. // 删掉已有头像,防止重复
  552. $partyuser = isset($partyInfo["party_user"])?$partyInfo["party_user"]:"";
  553. if(is_array($partyuser)) foreach($partyuser as $k => $v) if($v === $avatar) unset($partyInfo["party_user"][$k]);
  554. $partyInfo["party_user"][$position] = $avatar;
  555. } else {
  556. if(isset($partyInfo["party_user"])) unset($partyInfo["party_user"][$position]);
  557. }
  558. // sort($partyInfo["party_user"]);
  559. $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  560. }
  561. return $this->success("设置成功!");
  562. }
  563. /**
  564. * 获取派对在线人数列表
  565. */
  566. public function getOnlieList() {
  567. $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID
  568. if (!$party_id) {
  569. $this->error(__('Invalid parameters'));
  570. }
  571. $res = $this->redis->hGetAll("online_".$party_id);
  572. $user_ids = [];$userList = [];
  573. if($res) $user_ids = array_values($res);
  574. // 获取用户列表信息
  575. if($user_ids){
  576. $userList = Db::name('user')->alias('user')->field("user.id,user.avatar,user.nickname,user.level,user.gender,user.charm_level,user.wealth_level,charm.image as charm_image,charm.color as charm_color,wealth.image as wealth_image,wealth.color as wealth_color")
  577. ->join('user_config_charm charm','user.charm_level = charm.level','LEFT')
  578. ->join('user_config_wealth wealth','user.wealth_level = wealth.level','LEFT')
  579. ->where(["user.id"=>["in",$user_ids],'user.is_stealth'=>0])
  580. ->autopage()->select();
  581. $userList = list_domain_image($userList,['avatar','charm_image','wealth_image']);
  582. }
  583. $this->success("获取成功!",$userList);
  584. }
  585. /** 连续进房提示
  586. * @param $user_id
  587. * @param $party_id
  588. * @return int
  589. */
  590. private function getUserJoinPartyDays($user_id, $party_id)
  591. {
  592. $days = 1;
  593. $joinPartyDays = $this->redis->hGet("joinParty_days",$user_id."-".$party_id);
  594. if ($joinPartyDays){
  595. $joinPartyDaysArr = explode('-',$joinPartyDays);
  596. $oldTime = (int)$joinPartyDaysArr[0];
  597. $todayStartTime = strtotime(date('Y-m-d'));//今天0点
  598. $yesterdayStartTime = strtotime(date("Y-m-d",strtotime("-1 day")));//昨天开始时间戳
  599. $todayEndTime = $todayStartTime+24 * 60 * 60-1;//今天结束时间戳
  600. //昨天登录过,+1
  601. if ($yesterdayStartTime <= $oldTime && $oldTime < $todayStartTime){
  602. $days = (int)$joinPartyDaysArr[1];
  603. $days++;
  604. $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-'. $days);
  605. }elseif($todayStartTime <= $oldTime && $oldTime <= $todayEndTime){
  606. //今天登录过 days
  607. $days = (int)$joinPartyDaysArr[1];
  608. } else {
  609. $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
  610. }
  611. }else{
  612. $this->redis->hSet("joinParty_days", $user_id . "-" . $party_id, time().'-1');
  613. }
  614. return $days;
  615. }
  616. /**
  617. * 加入派对
  618. */
  619. public function joinParty() {
  620. $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID
  621. $room_type = input("room_type",1);//
  622. $party_pass = input("party_pass");//
  623. if (!$party_id || !in_array($room_type,[1,2])) {
  624. $this->error(__('Invalid parameters'));
  625. }
  626. $user_id = $this->auth->id;
  627. // 判断派对密码
  628. $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  629. if($partyInfo) {
  630. $partyInfo = json_decode($partyInfo,true);
  631. if($partyInfo["is_close"] == 1) $this->error("该房间已被关闭!");
  632. //if($partyInfo["status"] != 1) $this->error("该房间为预创建房间,请联系管理员正式开通!");
  633. $partyManage = $this->redis->hGet("party_manage_".$party_id,$user_id."-1");
  634. if(isset($partyInfo["party_pass"]) && $partyInfo["party_pass"] && $partyInfo["user_id"] != $user_id && empty($partyManage)) {
  635. if(($party_pass != $partyInfo["party_pass"] || strlen($party_pass) != 4) && $this->auth->is_manager == 0) {
  636. $this->error("派对密码不正确!");
  637. }
  638. }
  639. // 公会管理员和公会长激活派对厅
  640. /* $guildInfo = \app\common\model\Guild::where(['user_id' => $partyInfo['user_id'], 'status' => 1])->find();
  641. if ($guildInfo) {
  642. // 获取公会管理员
  643. $guildMemberInfo = \app\common\model\GuildMember::where(["user_id" => $user_id, "guild_id" => $guildInfo['id'], 'status' => 1])->find();
  644. if ($guildMemberInfo && $guildMemberInfo['role'] == 0 && $partyInfo['is_online'] == 0) {
  645. //$this->error("当前直播已结束!");//这种情况理论上不存在
  646. }
  647. }*/
  648. // // 如果是房主自己进入房间,则更新用户 为在线状态
  649. // if ($partyInfo["user_id"] == $user_id) {
  650. // if ($room_type == 2)
  651. // \app\common\model\User::update(["is_live" => 1], ["id" => $partyInfo["user_id"]]);
  652. // }
  653. } else {
  654. $this->error("派对信息获取失败!");
  655. }
  656. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  657. $lsetList = [];
  658. for($i=1;$i<=4;$i++) {
  659. $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i);
  660. $hgetlist = unserialize($hgetlist);
  661. if($hgetlist){
  662. $restime = time() - intval($hgetlist["createtime"]);
  663. }else{
  664. $restime = 0;
  665. }
  666. // 房管
  667. if($hgetlist && $i == 1) {
  668. $lsetList["manage_restime"] = "";
  669. //重新设置成管理员
  670. $this->setGroupAdmin($partyInfo['easemob_room_id'],$user_id,1);
  671. }
  672. // 禁言
  673. if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) {
  674. $lsetList["manage_notalk"] = $restime;
  675. }
  676. // 拉黑,管理员不能被拉黑
  677. if($hgetlist && $i == 3 && $restime < $hgetlist["time"] && $this->auth->is_manager == 0) {
  678. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  679. $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
  680. break;
  681. }
  682. // 踢出,管理员不能被踢出
  683. if($hgetlist && $i == 4 && $restime < $hgetlist["time"] && $this->auth->is_manager == 0) {
  684. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  685. $this->error(__('您已被该房间踢出,解除时间:'.$restime));
  686. break;
  687. }
  688. }
  689. //下移
  690. $this->redis->zAdd("party_user_".$party_id,$user_id,$user_id);
  691. $this->redis->hSet("online_".$party_id,$user_id,$user_id); //新增的
  692. // 判断当前用户是否存在在直播间通过切换过来的
  693. $livingUserPartyId = $this->redis->hGet("livingUser", $user_id);
  694. if ($livingUserPartyId) {
  695. if($livingUserPartyId != $party_id){
  696. // 扣除房间在线人数
  697. $this->redis->HDel("online_" . $livingUserPartyId, $user_id);
  698. //首页用户头像
  699. $this->redis->zRem("party_user_".$livingUserPartyId, $user_id);
  700. // 扣除在线用户在房间情况
  701. $this->redis->hDel("livingUser", $user_id);
  702. }
  703. }
  704. // 记录在线用户在房间情况
  705. $this->redis->hSet("livingUser", $user_id, $party_id);
  706. //下移
  707. // 获取用户魅力值。大概率没用了
  708. // 获取用户排行榜前三名头像。大概率没用了
  709. // 获取是否被当前用户关注
  710. $lsetList["is_follow"] = 0;
  711. $where = [];
  712. $where["user_id"] = $user_id;
  713. $where["party_id"] = $party_id;
  714. if(Db::name('party_follow')->where($where)->find()) {
  715. $lsetList["is_follow"] = 1;
  716. }
  717. if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  718. // $partyInfo["type_name"] = $partyInfo["type_name"];
  719. } else {
  720. $partyInfo["type_name"] = "普通房";
  721. }
  722. $partyInfo["party_hot"] = changeW($partyInfo["party_hot"]);
  723. $partyInfo['is_new'] = $partyInfo["status"] == 0 ? 1 : 0;
  724. $partyInfo['join_days'] = $this->getUserJoinPartyDays($user_id,$party_id);
  725. $lsetList["partyInfo"] = $partyInfo;
  726. //声网三个token
  727. $lsetList['shengwang_token'] = $this->shengwang_token_private($partyInfo['easemob_room_id']);
  728. //[环信] 更新聊天室自定义属性 在线用户人数
  729. $easemob = new Easemob();
  730. $matedata = [
  731. 'online_user_num' => count($this->redis->hGetAll("online_".$party_id)),
  732. ];
  733. $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  734. return $this->success("加入成功!",$lsetList);
  735. }
  736. /**
  737. * 判断是否被禁言
  738. */
  739. public function isNotalk() {
  740. $party_id = input('party_id',0,"intval"); // 直播间ID 逻辑ID
  741. $user_id = input('user_id',0,"intval"); // 用户ID 逻辑ID
  742. if (!$user_id || !$party_id) {
  743. $this->error(__('Invalid parameters'));
  744. }
  745. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  746. $lsetList = [];
  747. $lsetList["is_notalk"] = 0;
  748. $lsetList["notalk_time"] = 0;
  749. $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-2");
  750. $hgetlist = unserialize($hgetlist);
  751. $restime = time() - intval($hgetlist["createtime"]);
  752. // 禁言
  753. if($hgetlist && $restime < $hgetlist["time"]) {
  754. $lsetList["is_notalk"] = 1;
  755. $lsetList["notalk_time"] = $restime;//已经禁了N秒
  756. }
  757. return $this->success("获取成功!",$lsetList);
  758. }
  759. /**
  760. * 退出派对
  761. */
  762. public function outParty() {
  763. $party_id = input('party_id',0,"intval"); // 直播间ID
  764. $room_type = input("room_type",1);//
  765. if (!$party_id) {
  766. $this->error(__('Invalid parameters'));
  767. }
  768. $userId = $this->auth->id;
  769. $this->redis->zRem("party_user_".$party_id,$userId);
  770. // 处理online_party_id
  771. $this->redis->HDel("online_" . $party_id, $this->auth->id);
  772. // 扣除在线用户在房间情况
  773. $this->redis->hDel("livingUser", $this->auth->id);
  774. // 更新房间在线状态
  775. $partyInfo = $this->redis->get($this->roomTypeArr[$room_type].'_'.$party_id);
  776. if ($partyInfo) {
  777. $partyInfo = json_decode($partyInfo, true);
  778. $memCount = count($this->redis->hGetAll("online_" . $party_id));
  779. if ($memCount <= 0) {
  780. $partyInfo["is_online"] = 0;
  781. $this->redis->set($this->roomTypeArr[$room_type].'_'.$party_id,json_encode($partyInfo));
  782. \app\common\model\Party::update(["is_online" => 0], ["id" => $party_id]);
  783. }
  784. //[环信] 更新聊天室自定义属性 在线用户人数
  785. $easemob = new Easemob();
  786. $matedata = [
  787. 'online_user_num' => count($this->redis->hGetAll("online_".$party_id)),
  788. ];
  789. $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  790. //清除这个人点的歌
  791. $easemob_room_id = $partyInfo['easemob_room_id'];
  792. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  793. $singlist = json_decode($singlist['waitsing_list'],true);
  794. if(!empty($singlist)){
  795. foreach($singlist as $key => $sing){
  796. if($sing['userNo'] == $userId){
  797. unset($singlist[$key]);
  798. }
  799. }
  800. //重新排序
  801. $singlist = $this->waitsing_array_column_sort($singlist);
  802. //重新设置
  803. $matedata = [
  804. 'waitsing_list' => json_encode($singlist),
  805. ];
  806. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$partyInfo['user_id'],$matedata);
  807. }
  808. }
  809. $this->success("退出成功!",[]);
  810. }
  811. /**
  812. * 派对内搜索用户
  813. */
  814. public function searchUserParty() {
  815. $u_id = input('u_id',0,"intval"); // 用户u_id
  816. $party_id = input('party_id',0,"intval"); // 直播间ID
  817. // $userids = $this->redis->zRange("party_user_".$party_id,0,-1,true);
  818. // $useridArr = [];
  819. // if($userids)foreach($userids as $k => $v) $useridArr[$v] = $k; // 因为array_slip 不能交换数字啊
  820. // $userid = isset($useridArr[$u_id])?$useridArr[$u_id]:0;
  821. $userModel = new \app\common\model\User();
  822. $where = [];
  823. $where["u_id"] = $u_id;
  824. $userInfo = $userModel->field("id,u_id,avatar,nickname,level,gender")->where($where)->select();
  825. return $this->success("查询成功!",$userInfo);
  826. }
  827. /**
  828. * 设置冠名
  829. */
  830. public function setNaming() {
  831. $party_id = input('party_id',0,"intval"); // 直播间ID
  832. $user_id = input('user_id',0,"intval"); // 用户ID
  833. $type = input('type',1); // 1:设置 0:取消设置
  834. if (!$party_id || !$user_id || ($type != 1 && $type != 0)) {
  835. $this->error(__('Invalid parameters'));
  836. }
  837. $partyModel = new \app\common\model\Party();
  838. $data = [];
  839. $where = [];
  840. $where["id"] = $party_id;
  841. $partyInfo = $partyModel->where(["id"=>$party_id])->find();
  842. $getredisPartyInfo = $this->redis->get($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id);
  843. $redisPartyInfo = json_decode($getredisPartyInfo,true);
  844. if($type == 1) {
  845. $data["naming"] = $user_id;
  846. // 冠名
  847. $userInfo = \app\common\model\User::field("id,nickname,avatar")->where(["id"=>$user_id])->find($user_id);
  848. $redisPartyInfo["naming"] = $userInfo;
  849. } else {
  850. $data["naming"] = 0;
  851. $redisPartyInfo["naming"] = [];
  852. }
  853. $this->redis->set($this->roomTypeArr[$partyInfo->room_type].'_'.$party_id,json_encode($redisPartyInfo));
  854. $res = $partyModel->update($data,$where);
  855. if($res !== false) {
  856. $this->success("操作成功!");
  857. } else {
  858. $this->error("网络错误,请稍后重试!");
  859. }
  860. }
  861. /**
  862. * 派对关注
  863. */
  864. public function followparty() {
  865. $party_id = input('party_id',0,"intval"); // 直播间ID
  866. if (!$party_id) {
  867. $this->error(__('Invalid parameters'));
  868. }
  869. $user_id = $this->auth->id;
  870. // 添加记录
  871. $data = [];
  872. $data["user_id"] = $user_id;
  873. $data["party_id"] = $party_id;
  874. if(Db::name('party_follow')->where($data)->find()) {
  875. $res = Db::name('party_follow')->where($data)->delete();
  876. $this->success("已取消关注");
  877. }
  878. $data["createtime"] = time();
  879. $res = Db::name('party_follow')->insert($data);
  880. $this->success("已关注");
  881. }
  882. /**
  883. * 派对收藏列表
  884. */
  885. public function cellectionPartyList() {
  886. $page = input('page',1); // 分页
  887. $pageNum = input('pageNum',10); // 分页
  888. // 分页搜索构建
  889. $pageStart = ($page-1)*$pageNum;
  890. $partycellectionModel = new \app\common\model\PartyCellection();
  891. $where = [];
  892. $where["a.user_id"] = $this->auth->id;
  893. $where["r.room_type"] = 1;
  894. $list = $partycellectionModel->alias("a")
  895. ->field("a.party_id,r.party_logo,r.party_hot,r.party_id as r_id,r.party_name,t.id as party_type_id,t.name as party_type")
  896. ->where($where)
  897. ->join("hx_party r","a.party_id = r.id")
  898. ->join("hx_party_type t","t.id = r.party_type","left")
  899. ->limit($pageStart,$pageNum)
  900. ->select();
  901. if($list) {
  902. $users = [];
  903. foreach($list as $k => $v) {
  904. $users[$v["party_id"]] = $v["party_hot"];
  905. }
  906. $partyModel = new \app\common\model\Party();
  907. $resultInfo = $partyModel->getPatyInfoByPartyId($users,"party",0,0,1,0,20,0);
  908. $this->success("获取成功!",$resultInfo);
  909. }
  910. return $this->success("获取成功!",$list);
  911. }
  912. /**
  913. * 获取派对信息
  914. */
  915. public function getPartyInfo() {
  916. $party_id = input('party_id',0,"intval"); // 直播间ID
  917. if (!$party_id) {
  918. $this->error(__('Invalid parameters'));
  919. }
  920. $user_id = $this->auth->id;
  921. $partyModel = new \app\common\model\Party();
  922. $userModel = new \app\common\model\User();
  923. // 获取主体信息
  924. $where = [];
  925. $where["a.id"] = $party_id;
  926. $partyInfo = $partyModel->alias("a")
  927. ->field("a.id,a.user_id,a.party_id,a.party_name,a.party_hot,a.party_logo,a.party_notice,a.party_notice_detail,a.on_model,a.background,a.is_public,
  928. rt.id as party_type,rt.name as type_name")
  929. ->join("hx_party_type rt","rt.id = a.party_type", "left")
  930. ->where($where)
  931. ->find();
  932. if($partyInfo) {
  933. $mod = isset($partyInfo["party_type"])?intval($partyInfo["party_type"])%5:1;
  934. $partyInfo["party_type_color"] = $mod == 0?5:$mod;
  935. if(isset($partyInfo["type_name"]) && $partyInfo["type_name"]) {
  936. // $partyInfo["type_name"] = $partyInfo["type_name"];
  937. } else {
  938. $partyInfo["type_name"] = "普通房";
  939. }
  940. // 获取是否被当前用户关注
  941. $partyInfo["is_follow"] = 0;
  942. $where = [];
  943. $where["user_id"] = $user_id;
  944. $where["party_id"] = $party_id;
  945. if(Db::name('party_follow')->where($where)->find()) {
  946. $partyInfo["is_follow"] = 1;
  947. }
  948. // 获取房主信息
  949. $where = [];
  950. $where["id"] = $partyInfo["user_id"];
  951. $userInfo = $userModel->field("avatar,nickname")->where($where)->find();
  952. // 获取技能信息
  953. $partyInfo["userInfo"] = $userInfo;
  954. $partyInfo["party_hot"] = changeW($partyInfo["party_hot"]);
  955. }
  956. $this->success("获取成功!",$partyInfo);
  957. }
  958. /**
  959. * 派对设置
  960. */
  961. public function setParty() {
  962. $party_id = input('party_id',0,"intval"); // 直播间ID
  963. $room_type = input('room_type',1); // 房间类型:1=工会,2=个人k歌
  964. if (!$party_id || !$room_type) {
  965. $this->error(__('Invalid parameters'));
  966. }
  967. // 派对名称
  968. // 派对logo/封面
  969. // 派对密码
  970. // 使用场景
  971. // 是否关闭公屏:1=是,0=否
  972. // 上麦模式:1=自由模式,2=麦序模式
  973. // 派对背景
  974. // 是否公开:1=是,0=否
  975. $field_array = ['party_name','party_logo','party_pass'/*,'party_type'*/,'is_screen','on_model','background','is_public'];
  976. $data = [];
  977. foreach($field_array as $key => $field){
  978. //前端传不了post,改了
  979. if(!request()->has($field,'post')){
  980. continue;
  981. }
  982. /*if(!input('?'.$field)){
  983. continue;
  984. }*/
  985. $newone = input($field);
  986. $data[$field] = $newone;
  987. }
  988. if(isset($data['party_pass'])){
  989. if(empty($data['party_pass'])){
  990. $data['party_pass'] = '';
  991. }
  992. if(!empty($data['party_pass']) && strlen($data['party_pass']) != 4) {
  993. $this->error("房间密码必须为四位!");
  994. }
  995. }
  996. $partyModel = new \app\common\model\Party();
  997. $where = [];
  998. $where["id"] = $party_id;
  999. $res = $partyModel->update($data,$where);
  1000. if($res) {
  1001. // 获取派对类型
  1002. //if($party_type) $data["type_name"] = \app\common\model\PartyType::where(["id"=>$party_type])->value("name");
  1003. // 存redis 房间信息
  1004. $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  1005. if($partyInfo) {
  1006. $partyInfo = json_decode($partyInfo,true);
  1007. $partyInfo = array_replace($partyInfo,$data);
  1008. $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  1009. }
  1010. //[环信]初始化房间,初始化自定义属性
  1011. $matedata = [];
  1012. if(isset($data['party_name'])){
  1013. $matedata['party_name'] = $data['party_name'];
  1014. }
  1015. if(isset($data['party_logo'])){
  1016. $matedata['party_logo'] = $data['party_logo'];
  1017. }
  1018. if(isset($data['is_public'])){
  1019. $matedata['is_public'] = $data['is_public'];
  1020. }
  1021. if(isset($data['background'])){
  1022. $matedata['background'] = $data['background'];
  1023. }
  1024. if(!empty($matedata)){
  1025. $easemob = new Easemob();
  1026. $rs = $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  1027. }
  1028. $this->success("房间设置成功!",$data);
  1029. } else {
  1030. $this->error("网络错误,请稍后重试!");
  1031. }
  1032. }
  1033. /**
  1034. * 获取派对背景
  1035. */
  1036. public function getDefaultBackground() {
  1037. $room_type = input('room_type',1); // 房间类型
  1038. if (!in_array($room_type,[1,2])) {
  1039. $this->error(__('Invalid parameters'));
  1040. }
  1041. $this->success("获取成功!",\app\common\model\PartyBackground::where(["room_type"=>$room_type])->select());
  1042. }
  1043. //环信聊天室设置管理员,type=1设置,type=0移除
  1044. private function setGroupAdmin($easemob_room_id,$user_id,$type = 1)
  1045. {
  1046. $easemob = new Easemob();
  1047. if($type == 1){
  1048. $easemob->room_promoteRoomAdmin($easemob_room_id,$user_id);
  1049. }else{
  1050. $easemob->room_demoteRoomAdmin($easemob_room_id,$user_id);
  1051. }
  1052. }
  1053. /**
  1054. * 派对管理设置
  1055. */
  1056. public function partyManageSet() {
  1057. $party_id = input('party_id',0,"intval"); // 直播间ID
  1058. $user_id = input('user_id',0,"intval"); // 用户ID
  1059. $item = input('item'); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  1060. $time = input('time'); // 限制时间(单位:秒):0=永久
  1061. if (!$party_id || !$user_id || !in_array($item,[1,2,3,4])) {
  1062. $this->error(__('Invalid parameters'));
  1063. }
  1064. //2=禁言,3=拉黑,4=踢出 ,都要有时间
  1065. if($item>1 && $time<=0) {
  1066. $this->error(__('时间设置有误'));
  1067. }
  1068. //不能对房主恶意操作
  1069. $partyinfo = \app\common\model\Party::where('id', $party_id)->field('user_id,easemob_room_id')->find();
  1070. if ($partyinfo['user_id'] == $user_id && in_array($item, [2, 3, 4])) {
  1071. $this->error("您当前无权限操作!");
  1072. }
  1073. //只有房主才能加管理员
  1074. if($item == 1 && $this->auth->id != $partyinfo['user_id']){
  1075. $this->error('只有房主才能设置管理员');
  1076. }
  1077. // 获取用户信息
  1078. $userInfo = \app\common\model\User::field("avatar,nickname,gender")->where(["id"=>$user_id])->find();
  1079. if(!$userInfo) $this->error("用户信息获取失败!");
  1080. $data = [];
  1081. $data["user_id"] = $user_id;
  1082. $data["avatar"] = $userInfo->avatar;
  1083. $data["nickname"] = $userInfo->nickname;
  1084. $data["gender"] = $userInfo->gender;
  1085. $data["item"] = $item;
  1086. $data["time"] = $time;
  1087. $data["createtime"] = time();
  1088. $res = $this->redis->hSet("party_manage_".$party_id,$user_id."-".$item,serialize($data));
  1089. if ($item==1) {
  1090. $this->setGroupAdmin($partyinfo['easemob_room_id'],$user_id,1);
  1091. }
  1092. if($res !== false) {
  1093. $this->success("设置成功!",$data);
  1094. } else {
  1095. $this->error("网络错误,请稍后重试!");
  1096. }
  1097. }
  1098. /**
  1099. * 派对管理设置列表
  1100. * 仅用来给前端展示,禁言的鉴权在isNotalk接口,拉黑与提出的鉴权在joinparty接口
  1101. */
  1102. public function partyManageSetList() {
  1103. $party_id = input('party_id',0,"intval"); // 直播间ID
  1104. $item = input('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  1105. if (!$party_id || !in_array($item,[1,2,3,4])) {
  1106. $this->error(__('Invalid parameters'));
  1107. }
  1108. $time = time();
  1109. $hget=$this->redis->hGetAll("party_manage_".$party_id);
  1110. $list=array();
  1111. foreach($hget as $key=>$val) {
  1112. if(substr($key,-2) == "-".$item){
  1113. array_push($list,unserialize($val));
  1114. }
  1115. }
  1116. if($list) {
  1117. foreach($list as $k => $v) if(bcadd($v["createtime"],$v["time"]) <= $time && $v["item"] > 1) unset($list[$k]);
  1118. if($list) $list = array_values($list);
  1119. }
  1120. $this->success("获取成功!",$list);
  1121. }
  1122. /**
  1123. * 派对管理设置移除
  1124. */
  1125. public function partyManageSetDel() {
  1126. $id = input('user_id',0,"intval"); // userid
  1127. $party_id = input('party_id',0,"intval"); // 房间ID
  1128. $item = input('item',1,"intval"); // 项目:1=房管,2=禁言,3=拉黑,4=踢出
  1129. if (!$id || !in_array($item,[1,2,3,4])) {
  1130. $this->error(__('Invalid parameters'));
  1131. }
  1132. $partyinfo = \app\common\model\Party::where('id', $party_id)->field('user_id,easemob_room_id')->find();
  1133. //只有房主才能加管理员
  1134. if($item == 1 && $this->auth->id != $partyinfo['user_id']){
  1135. $this->error('只有房主才能免除管理员');
  1136. }
  1137. $res = $this->redis->hDel("party_manage_".$party_id,$id."-".$item);
  1138. if ($item==1) {
  1139. $this->setGroupAdmin($partyinfo['easemob_room_id'],$id,0);
  1140. }
  1141. if($res !== false) {
  1142. $this->success("移除成功!",$res);
  1143. } else {
  1144. $this->error("网络错误,请稍后重试!");
  1145. }
  1146. }
  1147. /**
  1148. * 更新派对公告
  1149. */
  1150. public function savePartyNotice() {
  1151. $party_id = input('party_id',0,"intval"); // 直播间ID
  1152. $room_type = input('room_type',1); // 房间类型:1=派对2=直播
  1153. $party_notice = input('party_notice'); // 公告标题
  1154. $party_notice_detail = input('party_notice_detail'); // 公告内容
  1155. if (!$party_id || !$party_notice || !$party_notice_detail) {
  1156. $this->error(__('Invalid parameters'));
  1157. }
  1158. $partyModel = new \app\common\model\Party();
  1159. $where = [];
  1160. $where["id"] = $party_id;
  1161. $data = [];
  1162. $data["party_notice"] = $party_notice;
  1163. $data["party_notice_detail"] = $party_notice_detail;
  1164. $res = $partyModel->update($data,$where);
  1165. if($res) {
  1166. // 存redis 房间信息
  1167. $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  1168. if($partyInfo) {
  1169. $partyInfo = json_decode($partyInfo,true);
  1170. $partyInfo = array_replace($partyInfo,$data);
  1171. $this->redis->set($this->roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  1172. }
  1173. $this->success("更新成功!",$res);
  1174. } else {
  1175. $this->error("网络错误,请稍后重试!");
  1176. }
  1177. }
  1178. /**
  1179. * 开始排麦
  1180. */
  1181. public function addLineUp() {
  1182. $party_id = input('party_id',0,"intval"); // 直播间ID
  1183. $mai_id = input('mai_id',"1"); // 麦位置
  1184. if (!$party_id) {
  1185. $this->error(__('Invalid parameters'));
  1186. }
  1187. $userInfo = $this->auth->getUserinfo();
  1188. $userid = $userInfo['id'];
  1189. // 获取 房间信息
  1190. $partyUser = \app\common\model\Party::where(['id'=>$party_id])->value("user_id");
  1191. if(!$partyUser) $this->error(__('房间信息未找到!'));
  1192. // $is_home = $partyUser == $userInfo->id?1:0;
  1193. $data = unserialize($this->redis->hGet("party_lineup",$party_id));
  1194. $data[$userid]["user_id"] = $userInfo['id'];
  1195. $data[$userid]["avatar"] = $userInfo['avatar'];
  1196. $data[$userid]["mai_id"] = $mai_id;
  1197. $data[$userid]["nickname"] = $userInfo['nickname'];
  1198. // $data["is_home"] = $is_home;
  1199. // $data[$userid]["level"] = $userInfo['level'];
  1200. $data[$userid]["gender"] = $userInfo['gender'];
  1201. $res = false;
  1202. $data && $res = $this->redis->hSet("party_lineup",$party_id,serialize($data));
  1203. $data = array_values($data);
  1204. if($res !== false) {
  1205. $this->success("设置成功!",$data);
  1206. } else {
  1207. $this->error("网络错误,请稍后重试!");
  1208. }
  1209. }
  1210. /**
  1211. * 取消排麦
  1212. */
  1213. public function cancelLineUp() {
  1214. $party_id = input('party_id',0,"intval"); // 直播间ID
  1215. $user_id = input('user_id',0); // 直播间ID
  1216. $is_empty = input('is_empty',0,"intval"); // 是否清空排麦列表 1=清空,0=不清空
  1217. $is_baomai = input('is_baomai',0,'intval'); //是否抱麦
  1218. if (!$party_id) {
  1219. $this->error(__('Invalid parameters'));
  1220. }
  1221. $data=unserialize($this->redis->hGet("party_lineup",$party_id));
  1222. $baomai_user = [];
  1223. if($data && $is_empty != 1) {
  1224. foreach($data as $k => $v) {
  1225. if($v["user_id"] == $user_id) {
  1226. $baomai_user = $v;
  1227. unset($data[$k]);
  1228. break;
  1229. }
  1230. }
  1231. }
  1232. // unset($data[$userid]);
  1233. if($is_empty == 1) $data = [];
  1234. $this->redis->hSet("party_lineup",$party_id,serialize($data));
  1235. //抱麦
  1236. if($is_empty != 1 && $is_baomai == 1 && !empty($baomai_user)){
  1237. $this->baomai($party_id,$user_id,$baomai_user);
  1238. }
  1239. //
  1240. $datas = [];
  1241. is_array($data) && $datas=array_values($data);
  1242. if($datas) {
  1243. $this->success("移除成功!",$datas);
  1244. } else {
  1245. $this->success("移除成功!",[]);
  1246. }
  1247. }
  1248. private function baomai($party_id,$user_id,$baomai_user){
  1249. $partyinfo = \app\common\model\Party::where('id', $party_id)->field('user_id,easemob_room_id')->find();
  1250. if(empty($partyinfo)){
  1251. return false;
  1252. }
  1253. //获取所有麦位,假设8个
  1254. $easemob = new Easemob();
  1255. $seatlist = $easemob->room_getRoomCustomAttribute($partyinfo['easemob_room_id'],['seat0','seat1','seat2','seat3','seat4','seat5','seat6','seat7']);
  1256. $newseat = [
  1257. 'charm' => 0, //红心,魅力值
  1258. 'isMaster' => false, // 是否是房主
  1259. 'headUrl' => $baomai_user['avatar'], // 头像
  1260. 'userNo' => $baomai_user['user_id'], // 座位上用户no
  1261. 'rtcUid' => $baomai_user['user_id'], // 座位上用户id,与rtc的userId一致
  1262. 'name' => $baomai_user['nickname'], // 座位上用户昵称
  1263. 'seatIndex' => -1, // 座位编号
  1264. 'chorusSongCode' => '', // 是否合唱
  1265. 'isAudioMuted' => 1, // 是否静音
  1266. 'isVideoMuted' => 0, // 是否开启视频
  1267. 'checked' => false, // 用于送礼物选择用户
  1268. 'isUsed' => true, // 用于送礼物选择用户
  1269. 'gender' => $baomai_user['gender'], //性别
  1270. ];
  1271. if(empty($seatlist)){
  1272. $newseat['seatIndex'] = 0;
  1273. $matedata['seat0'] = $newseat;
  1274. }else{
  1275. //第一遍轮询麦位,尝试找到这个人,如果找到在任何一个麦位,立刻return
  1276. foreach($seatlist as $k => $seat){
  1277. $seat = json_decode($seat,true);
  1278. //找到当前用户所在的麦位
  1279. if(isset($seat['rtcUid']) && !empty($seat['rtcUid']) && $seat['rtcUid'] == $user_id){
  1280. return true;
  1281. }
  1282. }
  1283. //第二遍轮询麦位,找一个空麦位,放进去
  1284. $matedata = [];
  1285. foreach($seatlist as $k => $seat){
  1286. $seat = json_decode($seat,true);
  1287. //找到当前用户所在的麦位
  1288. if(isset($seat['rtcUid']) && empty($seat['rtcUid'])){
  1289. //dump(substr($k,-1));
  1290. $newseat['seatIndex'] = substr($k,-1);
  1291. //初始化这个麦位
  1292. $matedata = [
  1293. $k => json_encode($newseat),
  1294. ];
  1295. }
  1296. }
  1297. }
  1298. //重置房间在线人数
  1299. if(!empty($matedata)){
  1300. $easemob->room_setRoomCustomAttributeForced($partyinfo['easemob_room_id'],$partyinfo['user_id'],$matedata);
  1301. }
  1302. return true;
  1303. }
  1304. /**
  1305. * 排麦列表
  1306. */
  1307. public function lineUpList() {
  1308. $party_id = input('party_id',0,"intval"); // 直播间ID
  1309. if (!$party_id) {
  1310. $this->error(__('Invalid parameters'));
  1311. }
  1312. $data = $this->redis->hGet("party_lineup",$party_id);
  1313. $dataArr = unserialize($data);
  1314. $datas = [];
  1315. is_array($dataArr) && $datas=array_values($dataArr);
  1316. if($datas) {
  1317. $this->success("获取成功!",$datas);
  1318. } else {
  1319. $this->success("获取成功!",[]);
  1320. }
  1321. }
  1322. /**
  1323. * 获取派对类型
  1324. */
  1325. public function getPatyType() {
  1326. $room_type = input("room_type",1);//
  1327. if (!in_array($room_type,[1,2])) $this->error(__('Invalid parameters'));
  1328. $partytypeModel = new \app\common\model\PartyType();
  1329. $partytypeList = $partytypeModel->where(["room_type"=>$room_type])->select();
  1330. $this->success("获取成功!",$partytypeList);
  1331. }
  1332. /**
  1333. * 判断派对是否设置过密码
  1334. */
  1335. public function getPatyIspass() {
  1336. $party_id = input("party_id");// 派对ID
  1337. $room_type = input("room_type",1);//
  1338. $is_miniprogram = input("is_miniprogram",0);//
  1339. if (!$party_id) $this->error(__('Invalid parameters'));
  1340. $user_id = $this->auth->id;
  1341. // 存redis 房间信息
  1342. $partyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$party_id);
  1343. if($partyInfo) {
  1344. $partyInfo = json_decode($partyInfo,true);
  1345. } else {
  1346. $partyModel = new \app\common\model\Party();
  1347. $partyInfo = $partyModel->field("id,user_id,party_pass")->where(["id"=>$party_id])->find();
  1348. if(!$partyInfo) {
  1349. $this->error(__('房间信息获取失败!'));
  1350. }
  1351. }
  1352. $data = [];
  1353. $data["status"] = 0;
  1354. $data["is_pass"] = $partyInfo["party_pass"]?1:0;
  1355. $partyManage = $this->redis->hGet("party_manage_".$party_id,$user_id."-1");
  1356. if($partyInfo["user_id"] == $user_id || $this->auth->is_manager == 1 || !empty($partyManage)){ $data["is_pass"] = 0;}
  1357. if($is_miniprogram == 1) { // 小程序单独处理
  1358. // 判断当前用户是否被该房间设置限制 //项目:1=房管,2=禁言,3=拉黑,4=踢出
  1359. $lsetList = [];
  1360. for($i=1;$i<=4;$i++) {
  1361. $hgetlist = $this->redis->hGet("party_manage_".$party_id,$user_id."-".$i);
  1362. $hgetlist = unserialize($hgetlist);
  1363. $restime = time() - intval($hgetlist["createtime"]);
  1364. // 房管
  1365. if($hgetlist && $i == 1) $lsetList["manage_restime"] = "";
  1366. // 禁言
  1367. if($hgetlist && $i == 2 && $restime < $hgetlist["time"]) $lsetList["manage_notalk"] = $restime;
  1368. // 拉黑
  1369. if($hgetlist && $i == 3 && $restime < $hgetlist["time"]) {
  1370. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  1371. $lsetList["manage_black"] = $restime;
  1372. $lsetList["tips"] = '您已被该房间拉黑,解除时间:'.$restime;
  1373. // $this->error(__('您已被该房间拉黑,解除时间:'.$restime));
  1374. break;
  1375. }
  1376. // 踢出
  1377. if($hgetlist && $i == 4 && $restime < $hgetlist["time"]) {
  1378. $restime = date("Y-m-d H:i:s",($hgetlist["time"]+$hgetlist["createtime"]));
  1379. $lsetList["manage_takeout"] = $restime;
  1380. $lsetList["tips"] = '您已被该房间踢出,解除时间:'.$restime;
  1381. // $this->error(__('您已被该房间踢出,解除时间:'.$restime));
  1382. break;
  1383. }
  1384. }
  1385. if($lsetList) {
  1386. $data["status"] = 1;
  1387. $data["hgetlist"] = $lsetList;
  1388. }
  1389. }
  1390. $this->success("获取成功!",$data);
  1391. }
  1392. /**
  1393. * 获取房间gif表情分类
  1394. */
  1395. public function getPartyGifType()
  1396. {
  1397. $list = \app\common\model\PartyGifType::field("id,name,image")->order("weigh", "asc")->select();
  1398. $this->success("获取成功!", $list);
  1399. }
  1400. /**
  1401. * 获取房间gif表情列表
  1402. */
  1403. public function getPartGifList() {
  1404. $this->success("获取成功!",\app\common\model\PartyGif::field("id,gif_image")->order("weight","asc")->select());
  1405. }
  1406. /**
  1407. * 获取房间头像gif表情列表
  1408. */
  1409. public function getPartHeadgifList() {
  1410. $this->success("获取成功!",\app\common\model\PartyHeadgif::field("id,name,gif_image")->order("weight","asc")->select());
  1411. }
  1412. /**
  1413. * 随机获取礼物盒礼物
  1414. */
  1415. private function getBoxGift($gift_box_type) {
  1416. // 查询本奖池内礼物是否还有可抽礼物
  1417. $where = [];
  1418. $where["Jackpot_id"] = $gift_box_type;
  1419. $where["is_use"] = 0;
  1420. $boxhasgift = \app\common\model\GiftBox::where($where)->select();
  1421. $giftcount = count($boxhasgift);
  1422. $giftArr = [];
  1423. foreach($boxhasgift as $k => $v) $giftArr[$v["id"]] = $v;
  1424. if($giftcount > 1) {
  1425. // 随机抽取$num个礼物
  1426. $giftids = array_rand($giftArr,1);
  1427. \app\common\model\GiftBox::update(["is_use"=>1],["id"=>$giftids]);
  1428. }
  1429. if($giftcount == 1) {
  1430. $giftids = $boxhasgift[0]["id"];
  1431. // 更新宝箱奖池全部礼物为未使用
  1432. \app\common\model\GiftBox::update(["is_use"=>0],["Jackpot_id"=>$gift_box_type]);
  1433. }
  1434. $giftInfo = $giftArr[$giftids];
  1435. return $giftInfo;
  1436. }
  1437. /**
  1438. * 全麦/单独赠送礼物
  1439. */
  1440. public function giveGiftToYou() {
  1441. // 是否背包赠送: 1=是,0=否
  1442. $is_back = input("is_back",0);
  1443. // 接口防并发
  1444. if($is_back){
  1445. if (!$this->apiLimit(50, 1000)) {
  1446. //1000毫秒50次
  1447. $this->error('休息一下吧');
  1448. }
  1449. }else{
  1450. if (!$this->apiLimit(10, 1000)) {
  1451. //1000毫秒10次
  1452. $this->error('休息一下吧');
  1453. }
  1454. }
  1455. //送礼物权限
  1456. /*if($this->auth->power['give_gift'] == 1){
  1457. $this->error('您已被限制:赠送礼物,请联系管理员');
  1458. }*/
  1459. //接收参数
  1460. $user_ids = input("user_id");// 赠送对象用户id
  1461. $seat_ids = input("seat_id");// 赠送对象麦位id
  1462. $gift_id = input("gift_id",0,'intval');// 礼物ID。如果是背包赠送,则是gift_back表的id,这样不合理,但是已经不好改了
  1463. $party_id = input("party_id",0,'intval');// 派对ID
  1464. $room_type = input('room_type',1,'intval'); // 房间类型
  1465. $number = input("number",1,'intval');// 赠送数量
  1466. if (!$user_ids || $seat_ids == '' || !in_array($is_back,[0,1]) || !$gift_id || !$number || !in_array($room_type,[1,2]))
  1467. {
  1468. $this->error(__('Invalid parameters'));
  1469. }
  1470. if(!$party_id){
  1471. $this->error('只有房间内能送礼物');
  1472. }
  1473. //处理参数
  1474. $user_id_arr = explode(",",$user_ids);
  1475. $seat_id_arr = explode(",",$seat_ids);
  1476. $userCount = count($user_id_arr);
  1477. $seatCount = count($seat_id_arr);
  1478. if($userCount != $seatCount){
  1479. $this->error();
  1480. }
  1481. $userauthid = $this->auth->id;
  1482. $money_to_jewel = config('site.money_to_jewel') ?: 10; //余额兑换金币
  1483. // 不可以赠送给自己
  1484. if(in_array($userauthid,$user_id_arr)) $this->error("不可以赠送给自己!");
  1485. $backGiftId = 0; //背包礼物表的 gift_id
  1486. $boxgiftInfo = [];
  1487. //每个人都能得到的礼物价值
  1488. $giftValue = 0;
  1489. //每个人都能得到的礼物价值
  1490. $getValue = 0;
  1491. //热度值
  1492. $hotValue = 0;
  1493. if($is_back == 1) {
  1494. // 获取背包礼物信息
  1495. $giftInfo = Db::name('gift_back')->field('gift_id')->where('id',$gift_id)->find();
  1496. if(!$giftInfo){
  1497. $this->error("背包礼物获取失败");
  1498. }
  1499. $backGiftId = $giftInfo['gift_id'];
  1500. // 随机获取一个礼物
  1501. $allCount = $number*$userCount;
  1502. $giftbackList = Db::name('gift_back')->field('id,value,name,image,gif_image,number')->where(["gift_id"=>$backGiftId,"user_id"=>$userauthid,'is_use'=>0])->limit($allCount)->order('id asc')->select();
  1503. $giftInfo = isset($giftbackList[0]) ? $giftbackList[0] : [];
  1504. $giftcount = 0;
  1505. $giftList = [];
  1506. if(!empty($giftbackList)) {
  1507. foreach($giftbackList as $k => $v) {
  1508. $giftList[$k] = $v;
  1509. $giftcount += $v["number"];
  1510. if($giftcount >= $allCount) {
  1511. break;
  1512. }
  1513. }
  1514. }
  1515. if($giftcount < $allCount)
  1516. {
  1517. $this->error("背包数量不足");
  1518. }
  1519. $giftValue = $giftInfo["value"] * $number;
  1520. $getValue = $giftValue;
  1521. $giftCountValue = $giftInfo["value"] * $number * $userCount;
  1522. } else {
  1523. // 获取礼物信息
  1524. $giftInfo = Db::name('gift')->field('id,value,name,image,special,is_baobi')->where('id',$gift_id)->find();
  1525. if (!$giftInfo) {
  1526. $this->error("请选择礼物!");
  1527. }
  1528. $giftValue = $giftInfo["value"] * $number;
  1529. $getValue = $giftValue;
  1530. // 判断当前用户余额,这里不需要锁表,钱包操作会锁
  1531. $giftCountValue = $giftInfo["value"] * $number * $userCount;
  1532. $user_jewel = model('wallet')->getWallet($this->auth->id,'jewel');
  1533. if($user_jewel < $giftCountValue){
  1534. $this->error("您的金币余额不足!");
  1535. }
  1536. }
  1537. //此时 $giftValue,$getValue,$hotValue是相等的(除了盒子礼物特殊)
  1538. $hotValue = $getValue;
  1539. //进行抽点设置
  1540. $partyInfo = null;
  1541. $partyInfo = \app\common\model\Party::field("id,room_type,party_name,party_hot,user_id,platRate,guilderRate,easemob_room_id")->where(["id"=>$party_id])->find();
  1542. // 获取系统配置信息
  1543. $platRate = $partyInfo->platRate; // 平台抽成百分比
  1544. $guilderRate = $partyInfo->guilderRate; // 房主抽成百分比,包括公会房主,普通房房主
  1545. //爆币礼物单独比例
  1546. if($is_back != 1){
  1547. if(isset($giftInfo['is_baobi']) && $giftInfo['is_baobi'] == 1){
  1548. $platRate = config('site.baobi_gift_platrate');
  1549. $guilderRate = config('site.baobi_gift_guilderrate');
  1550. }
  1551. }
  1552. //getValue削减
  1553. //
  1554. $returnData = [];
  1555. Db::startTrans();
  1556. try {
  1557. // 获取当天零点
  1558. $day = date("Ymd");
  1559. // 获取本周第一天
  1560. $weekday = $this->firstOfWeek(date("Y-m-d H:i:s"));
  1561. // 获取本月第一天
  1562. $monthday = date("Ym01");
  1563. $allVal = 0;
  1564. $i = 0;
  1565. //用户大循环
  1566. foreach($user_id_arr as $user_id_key => $user_id) {
  1567. if($is_back == 1) {
  1568. $b=0;
  1569. foreach($giftList as $k => $v) {
  1570. for($a=1;$a<=$v["number"];$a++) {
  1571. $b++;
  1572. $num = $v["number"] - $a;
  1573. if($num > 0) {
  1574. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->setDec("number");
  1575. } else {
  1576. $res1 = Db::name('gift_back')->where(["id"=>$v["id"]])->delete();
  1577. }
  1578. if($b == $number) break;
  1579. }
  1580. }
  1581. } else {
  1582. $res1 = true;
  1583. // 扣除当前用户金币余额
  1584. $rs_wallet = model('wallet')->lockChangeAccountRemain($userauthid, $giftValue, '-', 0, "赠送礼物:'" . $giftInfo["name"] . "',扣除" . $giftValue . "金币!", 3,'jewel');
  1585. if($rs_wallet['status'] == false){
  1586. Db::rollback();
  1587. $this->error($rs_wallet['msg']);
  1588. }
  1589. }
  1590. // 添加礼物赠送记录表
  1591. $data = [];
  1592. $data["user_id"] = $userauthid;
  1593. $data["user_to_id"] = $user_id;
  1594. $data["seat_num"] = $seat_id_arr[$user_id_key];
  1595. $data["party_id"] = $party_id;
  1596. $data["gift_id"] = $backGiftId > 0 ? $backGiftId : $gift_id;
  1597. $data["gift_give_type"] = $is_back ? 1 : 2;
  1598. $data["gift_name"] = $giftInfo["name"];
  1599. $data["gift_gif_image"] = $giftInfo["image"];
  1600. $data["number"] = $number;
  1601. $data["price"] = $giftInfo["value"];
  1602. $data["value"] = $giftValue;
  1603. //每个礼物都要计算平台抽成和房主抽成
  1604. $data['platvalue'] = bcmul($platRate/100 ,$data["value"],2);//平台抽成
  1605. $data['guildervalue'] = bcmul($guilderRate/100 ,$data["value"],2);// 房主抽成
  1606. $data['guildermoney'] = bcdiv($data['guildervalue'],$money_to_jewel,2);
  1607. $data['getvalue'] = bcsub(bcsub($data["value"] ,$data['platvalue'],2),$data['guildervalue'],2);//减去抽成剩余价值
  1608. $data['task_status'] = 0;
  1609. $data["createtime"] = time();
  1610. $res5 = Db::name('gift_user_party')->insertGetId($data);
  1611. //大礼物飘屏
  1612. //$this->bigGiftNotice($this->auth->nickname,$touserInfo['nickname'],$partyInfo,$data);
  1613. // 添加获赠用户余额
  1614. /*if($data['getvalue'] > 0){
  1615. $getMoney = bcdiv($data['getvalue'],$money_to_jewel,2);
  1616. if($getMoney > 0){
  1617. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$getMoney,'+',0,"{$this->auth->nickname}送你{$giftInfo['name']}x{$number}",101,'money');
  1618. if($rs_wallet['status'] == false){
  1619. Db::rollback();
  1620. $this->error($rs_wallet['msg']);
  1621. }
  1622. }
  1623. }*/
  1624. // 增加房主抽成
  1625. /*if ($partyInfo && $data['guildervalue'] > 0) {
  1626. $guilderMoney = bcdiv($data['guildervalue'],$money_to_jewel,2);
  1627. if($guilderMoney > 0){
  1628. $rs_wallet = model('wallet')->lockChangeAccountRemain($partyInfo->user_id,$guilderMoney,'+',0,"{$this->auth->nickname}送礼物{$giftInfo['name']}x{$number}给{$touserInfo['nickname']},房间礼物抽成",102,'money');
  1629. if($rs_wallet['status'] == false){
  1630. Db::rollback();
  1631. $this->error($rs_wallet['msg']);
  1632. }
  1633. }
  1634. }*/
  1635. if ($res1 && $res5) {
  1636. $i++;
  1637. if($party_id > 0) {
  1638. // 添加redis记录做魅力排行榜日榜用
  1639. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $day . "d", $hotValue, $user_id);
  1640. // 添加redis记录做魅力排行榜周榜用
  1641. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $weekday . "w", $hotValue, $user_id);
  1642. // 添加redis记录做魅力排行榜月榜用
  1643. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_get_" . $party_id . ":" . $monthday . "m", $hotValue, $user_id);
  1644. // 添加redis记录做财富排行榜日榜用
  1645. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $day . "d", $giftValue, $userauthid);
  1646. // 添加redis记录做财富排行榜周榜用
  1647. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $weekday . "w", $giftValue, $userauthid);
  1648. // 添加redis记录做财富排行榜月榜用
  1649. // $this->redis->zIncrBy($this->roomTypeArr[$room_type] . "_jewel_to_" . $party_id . ":" . $monthday . "m", $giftValue, $userauthid);
  1650. // tcp 更新用户魅力值
  1651. $this->redis->zIncrBy("hourCharm_".$party_id, $hotValue, $user_id);
  1652. // tcp 更新用户贡献值
  1653. $this->redis->zIncrBy("hourWealth_".$party_id, $hotValue, $user_id);
  1654. //更新麦位魅力值
  1655. //$this->updateSeatCharm($partyInfo['easemob_room_id'],$seat_id_arr[$user_id_key],$hotValue,$partyInfo);
  1656. // 如果是主播,则添加魅力值记录做榜单统计
  1657. /*if($room_type == 2) {
  1658. $data = [];
  1659. $data["user_id"] = $user_id;
  1660. $data["party_id"] = $party_id;
  1661. $data["charm"] = $hotValue;
  1662. $data["createtime"] = time();
  1663. \app\common\model\UserCharmRank::insert($data);
  1664. }*/
  1665. }
  1666. //用户经验升级
  1667. /*$getempirical = config("site.getempirical");
  1668. $getempirical = $getempirical * $hotValue;
  1669. // 获取用户贵族信息
  1670. $noble = \app\common\model\User::getUserNoble($this->auth->id);
  1671. if(isset($noble["noble_on"]) && $noble["noble_on"] == 1) {
  1672. $getempirical = $getempirical + $getempirical * ($noble["explain"]/100);
  1673. }
  1674. // 增加用户经验值
  1675. $res = \app\common\model\User::addEmpirical($this->auth->id,$getempirical);
  1676. if ($res){
  1677. $this->auth->level = $res->level;
  1678. }
  1679. //增加被送礼物用户的魅力等级
  1680. $res_charm = \app\common\model\User::add_charm_level($user_id,$giftValue);
  1681. // +exp
  1682. \app\common\model\TaskLog::tofinish($this->auth->id,"OBHqCX4g",$number);
  1683. */
  1684. // +message
  1685. //\app\common\model\Message::addMessage($user_id,"礼物通知","收到 ".$this->auth->nickname." 赠送的".$giftInfo["name"]." x".$number." 价值 ".$giftValue ." 金币");
  1686. $allVal = $allVal + $hotValue;
  1687. }
  1688. }
  1689. //爆币
  1690. $res_baobi = $this->baobi($this->auth->id,$data["gift_id"],$giftCountValue,$giftInfo["value"]);
  1691. if($res_baobi === false){
  1692. Db::rollback();
  1693. $this->error('爆币失败');
  1694. }elseif($res_baobi === true){
  1695. $returnData['baobi_total'] = 0;
  1696. $returnData['baobi_beilv'] = 0;
  1697. }else{
  1698. //爆币了
  1699. $returnData['baobi_total'] = $res_baobi['baobi_total'];
  1700. $returnData['baobi_beilv'] = $res_baobi['baobi_beilv'];
  1701. }
  1702. // 获取用户魅力值。大概率没用了
  1703. // tcp 更新房间热度
  1704. $partyHot = $this->updatePartyHot($partyInfo, $allVal, $room_type);
  1705. // 如果是派对,则添加派对热度值记录做榜单统计
  1706. //if($room_type == 1) {
  1707. $data = [];
  1708. $data["party_id"] = $party_id;
  1709. $data["hot"] = $allVal;
  1710. $data["createtime"] = time();
  1711. \app\common\model\PartyHot::insert($data);
  1712. //}
  1713. //增加送礼用户的财富等级
  1714. // $res_wealth = \app\common\model\User::add_wealth_level($this->auth->id,$allVal);
  1715. // tcp 获取房间用户周前三名
  1716. $this->getPartyUserTop($partyInfo, $room_type);
  1717. if($i == $userCount) {
  1718. if($is_back != 1) {
  1719. $returnData["image"] = $giftInfo["image"];
  1720. $returnData["gif_image"] = $giftInfo["special"];
  1721. } else {
  1722. $returnData["image"] = $giftInfo["image"];
  1723. $returnData["gif_image"] = $giftInfo["gif_image"];
  1724. }
  1725. // 增加抽点记录
  1726. Db::commit();
  1727. $this->success("赠送成功!",$returnData);
  1728. } else {
  1729. $this->success("赠送失败!");
  1730. }
  1731. } catch (ValidateException $e) {
  1732. Db::rollback();
  1733. $this->error($e->getMessage());
  1734. } catch (PDOException $e) {
  1735. Db::rollback();
  1736. $this->error($e->getMessage());
  1737. } catch (Exception $e) {
  1738. Db::rollback();
  1739. $this->error($e->getMessage());
  1740. }
  1741. }
  1742. //gift_id必须是爆币礼物才可以
  1743. public function baobi($user_id,$gift_id,$pay_total,$giftInfo_value){
  1744. //奖项参数
  1745. $conf_arr = Db::name('gift_baobi_config')->where('gift_id',$gift_id)->select();
  1746. if(empty($conf_arr)){
  1747. return true;
  1748. }
  1749. //用户今天的爆币情况,与消费情况,不用区分具体哪个爆币礼物
  1750. $today_start = strtotime(date('Y-m-d'));
  1751. $today_end = $today_start + 86399;
  1752. $map = [
  1753. 'user_id' => $user_id,
  1754. 'createtime' => ['BETWEEN',[$today_start,$today_end]],
  1755. ];
  1756. $today_data = Db::name('gift_baobi_log')->field('IFNULL(sum(pay_total),0) as today_pay_total,IFNULL(sum(baobi_total),0) as today_baobi_total')->where($map)->find();
  1757. //dump($today_data);
  1758. if($today_data['today_baobi_total'] >= $today_data['today_pay_total']){
  1759. //爆币比消费还高了,不用抽奖了,记录个日志,返回。这里的日志仅用来记录今日消费 sum(pay_total)
  1760. $data = [];
  1761. $data['user_id'] = $user_id;
  1762. $data['gift_id'] = $gift_id;
  1763. $data['baobi_id'] = 0;
  1764. $data['beilv'] = 0;
  1765. $data['rate'] = 0;
  1766. $data['pay_total'] = $pay_total;
  1767. $data['baobi_total'] = 0;
  1768. $data['createtime'] = time();
  1769. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  1770. if(!$log_id){
  1771. return false;
  1772. }
  1773. return true;
  1774. }
  1775. //概率新数组
  1776. $gailv = [];
  1777. $bei = 100; //小数变整数
  1778. foreach ($conf_arr as $key=>$value)
  1779. {
  1780. $gailv[$value['id']] = $value['rate']*$bei;
  1781. }
  1782. //dump($gailv);
  1783. //奖项新数组
  1784. $conf_column = [];
  1785. foreach ($conf_arr as $key=>$value)
  1786. {
  1787. $conf_column[$value['id']] = $value;
  1788. }
  1789. //dump($conf_column);
  1790. //抽
  1791. $rid = $this->getRand($gailv); //根据概率获取奖项id
  1792. //dump($rid);
  1793. //返回中奖结果
  1794. $result = $conf_column[$rid];
  1795. //爆币金额
  1796. $baobi_total = bcmul($giftInfo_value,$result['beilv'],0);
  1797. //写入爆币记录,不论有没有钱。全都写入其实是为了方便查日志,比在money_log里找范围更小,而且可删除
  1798. $data = [];
  1799. $data['user_id'] = $user_id;
  1800. $data['gift_id'] = $gift_id;
  1801. $data['baobi_id'] = $result['id'];
  1802. $data['beilv'] = $result['beilv'];
  1803. $data['rate'] = $result['rate'];
  1804. $data['pay_total'] = $pay_total;
  1805. $data['baobi_total'] = $baobi_total;
  1806. $data['createtime'] = time();
  1807. $log_id = Db::name('gift_baobi_log')->insertGetId($data);
  1808. if(!$log_id){
  1809. return false;
  1810. }
  1811. //直接给用户钱
  1812. if($baobi_total > 0){
  1813. $rs_wallet = model('wallet')->lockChangeAccountRemain($user_id,$baobi_total,'+',0,'送礼物爆币'.$result['beilv'].'倍',21,'jewel');
  1814. if($rs_wallet['status'] === false){
  1815. return false;
  1816. }
  1817. //返回爆币金额
  1818. return [
  1819. 'baobi_total' => $baobi_total,
  1820. 'baobi_beilv' => $result['beilv'],
  1821. ];
  1822. }
  1823. //默认成功
  1824. return true;
  1825. }
  1826. //概率获得算法
  1827. private function getRand($proArr) {
  1828. //概率数组的总概率精度
  1829. $proSum = array_sum($proArr);
  1830. $key = rand(1, $proSum);
  1831. // echo $key;
  1832. $result = 0;
  1833. $now = 0;
  1834. foreach ($proArr as $k=>$v)
  1835. {
  1836. $now = $now + $v;
  1837. if($key<=$now)
  1838. {
  1839. $result = $k;
  1840. break;
  1841. }
  1842. }
  1843. unset ($proArr);
  1844. return $result;
  1845. }
  1846. //某房间内礼物列表
  1847. public function getPartyGiftLog(){
  1848. $party_id = input('party_id',0);
  1849. $list = Db::name('gift_user_party')->alias('log')
  1850. ->field('log.gift_name,log.gift_gif_image,log.number,log.value,log.guildermoney,log.createtime,user.nickname as give_nickname,b.nickname as get_nickname')
  1851. ->join('user','log.user_id = user.id','LEFT')
  1852. ->join('user b','log.user_to_id = b.id','LEFT')
  1853. ->where('log.party_id',$party_id)->order('log.id desc')->autopage()->select();
  1854. $list = list_domain_image($list,['gift_gif_image']);
  1855. foreach($list as $key => &$val){
  1856. $val['createtime'] = get_last_time($val['createtime']);
  1857. }
  1858. $this->success('success',$list);
  1859. }
  1860. // /**
  1861. // * 播放动效
  1862. // */
  1863. // public function play($party_id,$type,$value,$number) {
  1864. // // 发送tcp
  1865. // $tcpArr = [];
  1866. // $tcpArr['type'] = "play";
  1867. // $tcpArr['data'] = [
  1868. // 'party_id' => $party_id,
  1869. // 'type' => $type,
  1870. // 'value' => $value,
  1871. // 'number' => $number,
  1872. // ];
  1873. // $tcpJson = json_encode($tcpArr);
  1874. // $client = stream_socket_client(config("tcp"));
  1875. // $buffer2 = base64_encode($tcpJson)."****";
  1876. // fwrite($client, $buffer2);
  1877. // }
  1878. /**
  1879. * 更新派对信息(热度等)
  1880. */
  1881. private function updatePartyHot($partyInfo,$hotValue,$room_type) {
  1882. // $partyInfo = \app\common\model\Party::where(['id'=>$party_id])->find();
  1883. if(!$partyInfo) return $hotValue;
  1884. $party_hot = $partyInfo->party_hot > 0 ? $partyInfo->party_hot:0;
  1885. $party_hot_value = $party_hot + $hotValue;
  1886. $party_hot_value = $party_hot_value > 0 ? $party_hot_value : 0;
  1887. if($party_hot_value != $party_hot) {
  1888. // 保存数据
  1889. $partyInfo->party_hot = $party_hot_value;
  1890. $partyInfo->save();
  1891. // 更新redis 加入缓存排序
  1892. $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $partyInfo['party_hot'], $partyInfo["id"]);
  1893. // 更新redis 加入缓存
  1894. $redPartyInfo = $this->redis->get($this->roomTypeArr[$room_type]."_".$partyInfo["id"]);
  1895. if($redPartyInfo) {
  1896. $redPartyInfo = json_decode($redPartyInfo,true);
  1897. $redPartyInfo["party_hot"] = $party_hot_value;
  1898. $this->redis->set($this->roomTypeArr[$room_type]."_".$partyInfo["id"],json_encode($redPartyInfo));
  1899. }
  1900. }
  1901. return $party_hot_value;
  1902. }
  1903. /**
  1904. * 用户赠送礼物后房间内用户排行,财富榜前三名
  1905. */
  1906. private function getPartyUserTop($party_info,$room_type) {
  1907. // 获取当天零点
  1908. $userModel = new \app\common\model\User();
  1909. // 获取条数
  1910. $num = 3;
  1911. // 获取3条财富排行记录
  1912. $getweek = $this->redis->zRevRange("hourWealth_".$party_info['id'],0,$num-1,true);
  1913. $userList = $userModel->rankList($getweek);
  1914. $avatarArr = [];
  1915. if($userList) {
  1916. foreach($userList as $k => $v) {
  1917. $v["jewel"] > 0 && $avatarArr[] = $v;
  1918. }
  1919. // 加入缓存做备份
  1920. $this->redis->hSet("user_jewel_top3",$party_info['id'],json_encode($avatarArr));
  1921. }
  1922. return $avatarArr;
  1923. //[环信]更新财富榜前3。放到异步了
  1924. }
  1925. /**
  1926. * 单个房间魅力值清零
  1927. */
  1928. public function partyClearCharm() {
  1929. $party_id = input("party_id");// 派对ID
  1930. $userId = input("user_id", 0);// 用户ID
  1931. if($party_id <= 0) {
  1932. $this->error("请输入派对ID");
  1933. }
  1934. if ($userId) {
  1935. $this->redis->zRem("hourCharm_" . $party_id, $userId);
  1936. } else {
  1937. $this->redis->del("hourCharm_" . $party_id);
  1938. }
  1939. $this->success("操作成功!");
  1940. }
  1941. //获取某房间内某个用户的魅力值
  1942. //上麦时候初始化小红心用的
  1943. public function getPartyUserCharm(){
  1944. $party_id = input("party_id");// 派对ID
  1945. $userId = input("user_id", 0);// 用户ID
  1946. $users = $this->redis->zRevRange("hourCharm_".$party_id,0,-1,true);
  1947. $charm = 0;
  1948. if($users) {
  1949. foreach($users as $k => $v){
  1950. if($userId == $k){
  1951. $charm = changeW($v);
  1952. }
  1953. }
  1954. }
  1955. $this->success(1,$charm);
  1956. }
  1957. /**
  1958. * 获取音乐列表
  1959. */
  1960. public function getMusicList() {
  1961. $this->success("获取成功!",\app\common\model\Music::select());
  1962. }
  1963. //派对举报
  1964. public function report_party() {
  1965. $type = input('type');// 类型描述
  1966. $content = input('content');// 内容
  1967. $images = input('images');// 图片
  1968. $party_id = input('party_id');//
  1969. if (!$type || !$content || !$images || !$party_id) {
  1970. $this->error("请完成举报内容!");
  1971. }
  1972. $data = [];
  1973. $data["user_id"] = $this->auth->id;
  1974. $data["party_id"] = $party_id;
  1975. $data["type"] = $type;
  1976. $data["content"] = $content;
  1977. $data["images"] = $images;
  1978. $data["createtime"] = time();
  1979. $res = Db::name('party_report')->insertGetId($data);
  1980. if ($res) {
  1981. $this->success("举报内容提交成功!");
  1982. } else {
  1983. $this->error("网络错误,请稍后重试!");
  1984. }
  1985. }
  1986. //=============待唱歌曲操作=================
  1987. //待唱,列表
  1988. public function waitsing_list(){
  1989. $party_id = input('party_id');
  1990. $party_info = Db::name('party')->where('id',$party_id)->find();
  1991. if(empty($party_info)){
  1992. $this->error();
  1993. }
  1994. $easemob_room_id = $party_info['easemob_room_id'];
  1995. $easemob = new Easemob();
  1996. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  1997. if(empty($singlist)){
  1998. //默认为空
  1999. $singlist['waitsing_list'] = '[]';
  2000. }
  2001. $singlist = json_decode($singlist['waitsing_list'],true);
  2002. //重新排序
  2003. $singlist = $this->waitsing_array_column_sort($singlist);
  2004. $this->success(1,$singlist);
  2005. }
  2006. //待唱,增加
  2007. public function waitsing_add(){
  2008. //查找派对
  2009. $party_id = input('party_id');
  2010. $party_info = Db::name('party')->where('id',$party_id)->find();
  2011. if(empty($party_info)){
  2012. $this->error();
  2013. }
  2014. $easemob_room_id = $party_info['easemob_room_id'];
  2015. //接收新歌
  2016. $sing_json = input('sing_json','','htmlspecialchars_decode');
  2017. $sing_json = json_decode($sing_json,true);
  2018. $sing_json['sing_no'] = createUniqueNo('s');//附加新参数:歌曲编号
  2019. $sing_json['sort'] = time();//附加新参数:排序
  2020. //获取已有列表
  2021. $easemob = new Easemob();
  2022. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  2023. if(empty($singlist)){
  2024. //默认为空
  2025. $singlist['waitsing_list'] = '[]';
  2026. }
  2027. $singlist = json_decode($singlist['waitsing_list'],true);
  2028. //加入新歌
  2029. $singlist[] = $sing_json;
  2030. //重新排序
  2031. $singlist = $this->waitsing_array_column_sort($singlist);
  2032. //重新设置
  2033. $matedata = [
  2034. 'waitsing_list' => json_encode($singlist),
  2035. ];
  2036. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
  2037. //返回最新歌曲列表
  2038. $this->success(1,$singlist);
  2039. }
  2040. //根据歌曲sort字段,正序排列
  2041. private function waitsing_array_column_sort($data){
  2042. if(empty($data)){
  2043. return $data;
  2044. }
  2045. //dump($data);
  2046. $sort = array_column($data,'sort');
  2047. $sing_no = array_column($data,'sing_no');
  2048. $a = array_multisort($sort,SORT_ASC,$sing_no,$data);
  2049. //dump($data);exit;
  2050. return $data;
  2051. }
  2052. //待唱,删除
  2053. public function waitsing_delete(){
  2054. //查找派对
  2055. $party_id = input('party_id');
  2056. $party_info = Db::name('party')->where('id',$party_id)->find();
  2057. if(empty($party_info)){
  2058. $this->error();
  2059. }
  2060. $easemob_room_id = $party_info['easemob_room_id'];
  2061. //需要删除的编号
  2062. $sing_no = input('sing_no','');
  2063. //获取已有列表
  2064. $easemob = new Easemob();
  2065. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  2066. if(empty($singlist)){
  2067. //默认为空
  2068. $singlist['waitsing_list'] = '[]';
  2069. }
  2070. $singlist = json_decode($singlist['waitsing_list'],true);
  2071. //轮询,找到,删除
  2072. foreach($singlist as $key => $sing){
  2073. if($sing['sing_no'] == $sing_no){
  2074. unset($singlist[$key]);
  2075. }
  2076. }
  2077. //重新排序
  2078. $singlist = $this->waitsing_array_column_sort($singlist);
  2079. //重新设置
  2080. $matedata = [
  2081. 'waitsing_list' => json_encode($singlist),
  2082. ];
  2083. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
  2084. //返回最新歌曲列表
  2085. $this->success(1,$singlist);
  2086. }
  2087. //待唱,置顶
  2088. public function waitsing_top(){
  2089. //查找派对
  2090. $party_id = input('party_id');
  2091. $party_info = Db::name('party')->where('id',$party_id)->find();
  2092. if(empty($party_info)){
  2093. $this->error();
  2094. }
  2095. $easemob_room_id = $party_info['easemob_room_id'];
  2096. //需要置顶的编号
  2097. $sing_no = input('sing_no','');
  2098. //获取已有列表
  2099. $easemob = new Easemob();
  2100. $singlist = $easemob->room_getRoomCustomAttribute($easemob_room_id,['waitsing_list']);
  2101. if(empty($singlist)){
  2102. //默认为空
  2103. $singlist['waitsing_list'] = '[]';
  2104. }
  2105. $singlist = json_decode($singlist['waitsing_list'],true);
  2106. //轮询,找到,改排序
  2107. if(!empty($singlist)){
  2108. //拿到所有排序字段,排列,取第0个
  2109. $sort = array_column($singlist,'sort');
  2110. ksort($sort);
  2111. foreach($singlist as $key => &$sing){
  2112. if($sing['sing_no'] == $sing_no){
  2113. $sing['sort'] = $sort[0] - 1;
  2114. }
  2115. }
  2116. }
  2117. //重新排序
  2118. $singlist = $this->waitsing_array_column_sort($singlist);
  2119. //重新设置
  2120. $matedata = [
  2121. 'waitsing_list' => json_encode($singlist),
  2122. ];
  2123. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$party_info['user_id'],$matedata);
  2124. //返回最新歌曲列表
  2125. $this->success(1,$singlist);
  2126. }
  2127. //轮询所有房间,挨个初始化自定义属性
  2128. public function room_task(){
  2129. $ids = Db::name('party')->select();
  2130. $easemob = new Easemob();
  2131. foreach($ids as $key => $party){
  2132. //[环信]初始化房间
  2133. $matedata = [
  2134. 'seatnum'=> $party['seatnum'],//麦位数量
  2135. 'waitsing_list' => json_encode([]),//已点歌曲列表
  2136. 'party_name' => $party['party_name'],//房名字
  2137. 'party_logo' => localpath_to_netpath($party['party_logo']),//logo
  2138. 'is_public' => $party['is_public'],//是否公开
  2139. 'room_type' => $party['room_type'],//房间类型
  2140. 'online_user_num' => 0,//当前房间在线的人
  2141. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  2142. 'background' => '',//背景图
  2143. ];
  2144. $rs = $easemob->room_setRoomCustomAttributeForced($party['easemob_room_id'],$party['user_id'],$matedata);
  2145. //[环信]初始化N个麦位
  2146. $seatdata = [];
  2147. for($i=0;$i<$party['seatnum'];$i++){
  2148. $seat = [
  2149. 'charm' => 0, //红心,魅力值
  2150. 'isMaster' => false, // 是否是房主
  2151. 'headUrl' => '', // 头像
  2152. 'userNo' => '', // 座位上用户no
  2153. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  2154. 'name' => '', // 座位上用户昵称
  2155. 'seatIndex' => -1, // 座位编号
  2156. 'chorusSongCode' => '', // 是否合唱
  2157. 'isAudioMuted' => 1, // 是否静音
  2158. 'isVideoMuted' => 0, // 是否开启视频
  2159. 'checked' => false, // 用于送礼物选择用户
  2160. 'isUsed' => true, // 用于送礼物选择用户
  2161. 'gender' => 1, //性别
  2162. ];
  2163. //创建完房间就进入,所以房主直接在麦位1
  2164. $owner = Db::name('user')->where('id',$party['user_id'])->find();
  2165. if($i == 0){
  2166. $seat['isMaster'] = true;
  2167. $seat['headUrl'] = localpath_to_netpath($owner['avatar']);
  2168. $seat['userNo'] = $owner['id'];
  2169. $seat['rtcUid'] = $owner['id'];
  2170. $seat['name'] = $owner['nickname'];
  2171. $seat['seatIndex']= $i;
  2172. }
  2173. $seatdata['seat'.$i] = json_encode($seat);
  2174. }
  2175. $rs = $easemob->room_setRoomCustomAttributeForced($party['easemob_room_id'],$party['user_id'],$seatdata);
  2176. }
  2177. }
  2178. //显示聊天室所有自定义属性,自用工具
  2179. public function test_party_attr(){
  2180. $party_id = input('party_id',0);
  2181. $party_info = Db::name('party')->where('id',$party_id)->find();
  2182. $easemob = new Easemob();
  2183. $field = [
  2184. 'seatnum',
  2185. 'waitsing_list',
  2186. 'party_name',
  2187. 'party_logo',
  2188. 'is_public',
  2189. 'room_type',
  2190. 'online_user_num',
  2191. 'wealth_top3_userlist',
  2192. 'background',
  2193. ];
  2194. for($i=0;$i<$party_info['seatnum'];$i++){
  2195. $field[] = 'seat'.$i;
  2196. }
  2197. $all_attr = $easemob->room_getRoomCustomAttribute($party_info['easemob_room_id'],$field);
  2198. dump($all_attr);
  2199. echo '<hr>';
  2200. foreach($all_attr as $key => $val){
  2201. echo $key;
  2202. echo '<br>';
  2203. $val = json_decode($val,true);
  2204. dump($val);
  2205. echo '<hr>';
  2206. }
  2207. //
  2208. /*$matedata = [
  2209. 'background' => '1.jpg',
  2210. ];
  2211. $rs = $easemob->room_setRoomCustomAttributeForced($party_info['easemob_room_id'],$party_info['user_id'],$matedata);
  2212. dump($rs);*/
  2213. }
  2214. //============================定时任务==========================//
  2215. /**
  2216. * redis清理排行榜 并数据库备份
  2217. */
  2218. /*public function updateTops() {
  2219. // 前一天日期
  2220. $yestaday = date("Ymd",bcsub(time(),86400));
  2221. // 上个周一
  2222. $preweek = $this->firstOfWeek(date("Y-m-d H:i:s",bcsub(time(),604800)));
  2223. // 上个月一号
  2224. $monty = bcsub(date("m"),1);
  2225. if($monty < 10) $monty = "0".$monty;
  2226. $premonth = date("Y").$monty."01";
  2227. // 获取所有派对ID
  2228. $party_ids = \app\common\model\Party::where(["status"=>1])->column("id");
  2229. // 获取数据
  2230. $time = time();
  2231. if($party_ids) foreach($party_ids as $k => $v) {
  2232. $keys = [
  2233. "party_jewel_get_".$v.":".$yestaday."d",
  2234. "party_jewel_to_".$v.":".$yestaday."d",
  2235. "party_jewel_get_".$v.":".$preweek."w",
  2236. "party_jewel_to_".$v.":".$preweek."w",
  2237. "party_jewel_get_".$v.":".$premonth."m",
  2238. "party_jewel_to_".$v.":".$premonth."m",
  2239. ];
  2240. foreach($keys as $key) {
  2241. // 备份数据
  2242. $redisData = $this->redis->zRevRange($key,0,-1,true);
  2243. if($redisData) {
  2244. $keyInfo = \app\common\model\RedisTops::where(["key"=>$key])->find();
  2245. $data = ["party_id"=>$v,"key"=>$key,"value"=>json_encode($redisData),"createtime" => $time];
  2246. if(!$keyInfo) {
  2247. $res = \app\common\model\RedisTops::insert($data);
  2248. // 清理数据
  2249. $res && $this->redis->zRemRangeByRank($key,0,-1);
  2250. }
  2251. }
  2252. }
  2253. }
  2254. }*/
  2255. public function handleParty() {
  2256. return false;
  2257. $partyList = \app\common\model\Party::where('is_online',1)->select();
  2258. // $this->redis->del("online_" . 3);die;
  2259. // $this->redis->hSet("online_" . 3, 4, 4);die;
  2260. foreach ($partyList as $party){
  2261. $userId = $party['user_id'];
  2262. $roomId = $party['id'];
  2263. $this->redis->HDel("online_" . $roomId, $userId);
  2264. $this->redis->zRem("party_user_".$roomId, $userId);
  2265. // 扣除在线用户在房间情况
  2266. $this->redis->hDel("livingUser",$userId);
  2267. // // 更新房间在线状态
  2268. $partyInfo = $this->redis->get("party_" . $roomId);
  2269. if ($partyInfo) {
  2270. $partyInfo = json_decode($partyInfo, true);
  2271. $memCount = count($this->redis->hGetAll("online_" . $roomId));
  2272. if ($memCount <= 0) {
  2273. dump($roomId.'--1---');
  2274. $partyInfo["is_online"] = 0;
  2275. $this->redis->set("party_" . $roomId, json_encode($partyInfo));
  2276. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  2277. }
  2278. }
  2279. $liveInfo = $this->redis->get("live_" . $roomId);
  2280. if ($liveInfo) {
  2281. $liveInfo = json_decode($liveInfo, true);
  2282. $memCount = count($this->redis->hGetAll("online_" . $roomId));
  2283. if ($memCount <= 0) {
  2284. dump($roomId.'---2--');
  2285. $liveInfo["is_online"] = 0;
  2286. $this->redis->set("live_" . $roomId, json_encode($liveInfo));
  2287. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  2288. }else{
  2289. dump($roomId.'---3--');
  2290. $this->redis->del("online_" . $roomId);
  2291. $liveInfo["is_online"] = 0;
  2292. $this->redis->set("live_" . $roomId, json_encode($liveInfo));
  2293. \app\common\model\Party::update(["is_online" => 0], ["id" => $roomId]);
  2294. }
  2295. }
  2296. }
  2297. $this->success("获取成功!");
  2298. }
  2299. }