Party.php 98 KB

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