Party.php 106 KB

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