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