Party.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. <?php
  2. namespace app\admin\controller\party;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Redis;
  6. use app\common\library\Easemob;
  7. /**
  8. * 派对管理
  9. *
  10. * @icon fa fa-circle-o
  11. */
  12. class Party extends Backend
  13. {
  14. protected $searchFields = 'user.u_id,user.nickname,party_id,party_name';
  15. /**
  16. * Party模型对象
  17. * @var \app\admin\model\party\Party
  18. */
  19. protected $model = null;
  20. protected $redis;
  21. public function _initialize()
  22. {
  23. parent::_initialize();
  24. $this->model = new \app\admin\model\party\Party;
  25. $this->view->assign("roomTypeList", $this->model->getRoomTypeList());
  26. $this->view->assign("isCoolList", $this->model->getIsCoolList());
  27. $this->view->assign("isOnlineList", $this->model->getIsOnlineList());
  28. $this->view->assign("statusList", $this->model->getStatusList());
  29. $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
  30. $this->view->assign("isCloseList", $this->model->getIsCloseList());
  31. $this->view->assign("isScreenList", $this->model->getIsScreenList());
  32. $this->view->assign("onModelList", $this->model->getOnModelList());
  33. $this->view->assign("isPublicList", $this->model->getIsPublicList());
  34. //redis
  35. $redis = new Redis();
  36. $redisconfig = config("redis");
  37. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  38. if ($redisconfig['redis_pwd']) {
  39. $redis->auth($redisconfig['redis_pwd']);
  40. }
  41. if($redisconfig['redis_selectdb'] > 0){
  42. $redis->select($redisconfig['redis_selectdb']);
  43. }
  44. $this->redis = $redis;
  45. }
  46. public function import()
  47. {
  48. parent::import();
  49. }
  50. /**
  51. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  52. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  53. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  54. */
  55. /**
  56. * 查看
  57. */
  58. public function index()
  59. {
  60. //当前是否为关联查询
  61. $this->relationSearch = true;
  62. //设置过滤方法
  63. $this->request->filter(['strip_tags', 'trim']);
  64. if ($this->request->isAjax()) {
  65. //如果发送的来源是Selectpage,则转发到Selectpage
  66. if ($this->request->request('keyField')) {
  67. return $this->selectpage();
  68. }
  69. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  70. $list = $this->model
  71. ->with(['user'])
  72. ->where($where)
  73. ->order($sort, $order)
  74. ->paginate($limit);
  75. foreach ($list as $row) {
  76. $row->getRelation('user')->visible(['username']);
  77. }
  78. $result = array("total" => $list->total(), "rows" => $list->items());
  79. return json($result);
  80. }
  81. return $this->view->fetch();
  82. }
  83. /**
  84. * 编辑
  85. */
  86. public function edit($ids = null)
  87. {
  88. $row = $this->model->get($ids);
  89. if (!$row) {
  90. $this->error(__('No Results were found'));
  91. }
  92. $adminIds = $this->getDataLimitAdminIds();
  93. if (is_array($adminIds)) {
  94. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  95. $this->error(__('You have no permission'));
  96. }
  97. }
  98. if ($this->request->isPost()) {
  99. $params = $this->request->post("row/a");
  100. if ($params) {
  101. $params = $this->preExcludeFields($params);
  102. $result = false;
  103. Db::startTrans();
  104. try {
  105. //判断房间id去重
  106. /*$check = Db::name('party')->where('party_id',$params['party_id'])->where('id','neq',$ids)->find();
  107. if($check){
  108. Db::rollback();
  109. $this->error('重复的派对id');
  110. }*/
  111. $roomTypeArr = [1=>"party",2=>"live"];
  112. $room_type = $row->room_type;
  113. $party_id = $row->id;
  114. if($party_id > 0) {
  115. // 存redis 房间信息
  116. $partyInfo = $this->redis->get($roomTypeArr[$room_type]."_".$party_id);
  117. if($partyInfo) {
  118. $partyInfo = json_decode($partyInfo,true);
  119. $partyInfo = array_replace($partyInfo,$params);
  120. $this->redis->set($roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  121. }
  122. }
  123. //是否采用模型验证
  124. if ($this->modelValidate) {
  125. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  126. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  127. $row->validateFailException(true)->validate($validate);
  128. }
  129. $result = $row->allowField(true)->save($params);
  130. Db::commit();
  131. if($params["status"] == 3) {
  132. //解散房间
  133. $this->closeParty($party_id,$room_type);
  134. }
  135. } catch (ValidateException $e) {
  136. Db::rollback();
  137. $this->error($e->getMessage());
  138. } catch (PDOException $e) {
  139. Db::rollback();
  140. $this->error($e->getMessage());
  141. } catch (Exception $e) {
  142. Db::rollback();
  143. $this->error($e->getMessage());
  144. }
  145. if ($result !== false) {
  146. $this->success();
  147. } else {
  148. $this->error(__('No rows were updated'));
  149. }
  150. }
  151. $this->error(__('Parameter %s can not be empty', ''));
  152. }
  153. $this->view->assign("row", $row);
  154. return $this->view->fetch();
  155. }
  156. /**
  157. * 解散房间
  158. */
  159. public function closeParty($party_id,$room_type) {
  160. $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
  161. if(!$partyInfo) {
  162. $this->error(__('派对不存在!'));
  163. }
  164. //房间从首页列表消失
  165. //用户魅力榜,清空
  166. $this->redis->zRemRangeByRank("hourCharm_".$party_id, 0,-1);
  167. //用户贡献榜,清空
  168. $this->redis->zRemRangeByRank("hourWealth_".$party_id, 0,-1);
  169. //贡献榜前三,设置为空
  170. $this->redis->hSet("user_jewel_top3",$party_id,json_encode([]));
  171. //排麦列表,清空
  172. $this->redis->hSet("party_lineup",$party_id,serialize([]));
  173. //清空房间
  174. $user_ids = $this->redis->hGetAll("online_".$party_id);
  175. if(!empty($user_ids)){
  176. foreach($user_ids as $key => $userId){
  177. $this->redis->HDel("online_" . $party_id, $userId);
  178. $this->redis->zRem("party_user_".$party_id,$userId); //新加的
  179. $this->redis->hDel("livingUser", $userId);
  180. }
  181. }
  182. //环信操作
  183. $easemob_room_id = $partyInfo['easemob_room_id'];
  184. if(!empty($easemob_room_id)){
  185. ///初始化房间信息,重置自定义属性
  186. $matedata = [
  187. 'waitsing_list' => json_encode([]), //清空房间待唱列表
  188. 'online_user_num' => 0, //当前房间在线的人
  189. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  190. ];
  191. $easemob = new Easemob();
  192. $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  193. //重置麦位
  194. $seatnum = $partyInfo['seatnum'];
  195. $seatdata = [];
  196. for($i=0;$i<$seatnum;$i++){
  197. $seat = [
  198. 'charm' => 0, //红心,魅力值
  199. 'isMaster' => false, // 是否是房主
  200. 'headUrl' => '', // 头像
  201. 'userNo' => '', // 座位上用户no
  202. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  203. 'name' => '', // 座位上用户昵称
  204. 'seatIndex' => -1, // 座位编号
  205. 'chorusSongCode' => '', // 是否合唱
  206. 'isAudioMuted' => 1, // 是否静音
  207. 'isVideoMuted' => 0, // 是否开启视频
  208. 'checked' => false, // 用于送礼物选择用户
  209. 'isUsed' => true, // 用于送礼物选择用户
  210. 'gender' => 1, //性别
  211. ];
  212. $seatdata['seat'.$i] = json_encode($seat);
  213. }
  214. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$partyInfo['user_id'],$seatdata);
  215. //清空房间人,聊天室
  216. //分页获取聊天室成员列表
  217. $usernames = $easemob->room_listRoomMembers($easemob_room_id,0);
  218. //批量移除聊天室成员
  219. $user_ids = [];
  220. if(!empty($usernames)){
  221. foreach($usernames as $username){
  222. foreach($username as $key => $val)
  223. {
  224. $user_ids[] = $val;
  225. }
  226. }
  227. }
  228. if(!empty($user_ids)){
  229. $easemob->room_removeRoomMembers($easemob_room_id,$user_ids);
  230. }
  231. }
  232. return true;
  233. }
  234. /**
  235. * 踢出房间内所有用户
  236. */
  237. private function outMemberFromRoom($easemob_room_id) {
  238. //$easemob = new Easemob();
  239. //获取所有成员
  240. //$rs = $easemob->room_listRoomMembers($easemob_room_id,0);
  241. //移除所有成员
  242. //$rs = $easemob->room_removeRoomMembers($easemob_room_id,$usernames);
  243. }
  244. }