Party.php 107 KB

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