Party.php 103 KB

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