Party.php 99 KB

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