Party.php 105 KB

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