Party.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. //[环信]初始化房间,初始化自定义属性
  132. $matedata = [];
  133. $data = $params;
  134. if(isset($data['party_name'])){
  135. $matedata['party_name'] = $data['party_name'];
  136. }
  137. if(isset($data['party_logo'])){
  138. $matedata['party_logo'] = localpath_to_netpath($data['party_logo']);
  139. }
  140. if(isset($data['is_public'])){
  141. $matedata['is_public'] = $data['is_public'];
  142. }
  143. if(isset($data['background'])){
  144. $matedata['background'] = localpath_to_netpath($data['background']);
  145. }
  146. if(isset($data['party_notice'])){
  147. $matedata['party_notice'] = $data['party_notice'];
  148. }
  149. if(isset($data['party_notice_detail'])){
  150. $matedata['party_notice_detail'] = $data['party_notice_detail'];
  151. }
  152. if(!empty($matedata)){
  153. $easemob = new Easemob();
  154. $rs = $easemob->room_setRoomCustomAttributeForced($row->easemob_room_id,$row->user_id,$matedata);
  155. }
  156. //解散房间
  157. if($params["status"] == 3) {
  158. $this->closeParty($party_id,$room_type);
  159. }
  160. } catch (ValidateException $e) {
  161. Db::rollback();
  162. $this->error($e->getMessage());
  163. } catch (PDOException $e) {
  164. Db::rollback();
  165. $this->error($e->getMessage());
  166. } catch (Exception $e) {
  167. Db::rollback();
  168. $this->error($e->getMessage());
  169. }
  170. if ($result !== false) {
  171. $this->success();
  172. } else {
  173. $this->error(__('No rows were updated'));
  174. }
  175. }
  176. $this->error(__('Parameter %s can not be empty', ''));
  177. }
  178. $this->view->assign("row", $row);
  179. return $this->view->fetch();
  180. }
  181. /**
  182. * 解散房间
  183. */
  184. public function closeParty($party_id,$room_type) {
  185. $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
  186. if(!$partyInfo) {
  187. $this->error(__('派对不存在!'));
  188. }
  189. //房间从首页列表消失
  190. //用户魅力榜,清空
  191. $this->redis->zRemRangeByRank("hourCharm_".$party_id, 0,-1);
  192. //用户贡献榜,清空
  193. $this->redis->zRemRangeByRank("hourWealth_".$party_id, 0,-1);
  194. //贡献榜前三,设置为空
  195. $this->redis->hSet("user_jewel_top3",$party_id,json_encode([]));
  196. //排麦列表,清空
  197. $this->redis->hSet("party_lineup",$party_id,serialize([]));
  198. //清空房间
  199. $user_ids = $this->redis->hGetAll("online_".$party_id);
  200. if(!empty($user_ids)){
  201. foreach($user_ids as $key => $userId){
  202. $this->redis->HDel("online_" . $party_id, $userId);
  203. $this->redis->zRem("party_user_".$party_id,$userId); //新加的
  204. $this->redis->hDel("livingUser", $userId);
  205. }
  206. }
  207. //全部下麦
  208. $key = "party_seat_".$party_id;
  209. $seats = $this->redis->hGetAll($key);
  210. foreach($seats as $seat_num => $seatuserid){
  211. $this->redis->HDel($key, $seat_num);
  212. }
  213. //环信操作
  214. $easemob_room_id = $partyInfo['easemob_room_id'];
  215. if(!empty($easemob_room_id)){
  216. ///初始化房间信息,重置自定义属性
  217. $matedata = [
  218. 'waitsing_list' => json_encode([]), //清空房间待唱列表
  219. 'online_user_num' => 0, //当前房间在线的人
  220. 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
  221. ];
  222. $easemob = new Easemob();
  223. $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
  224. //重置麦位
  225. $seatnum = $partyInfo['seatnum'];
  226. $seatdata = [];
  227. for($i=0;$i<$seatnum;$i++){
  228. $seat = [
  229. 'charm' => 0, //红心,魅力值
  230. 'isMaster' => false, // 是否是房主
  231. 'headUrl' => '', // 头像
  232. 'userNo' => '', // 座位上用户no
  233. 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
  234. 'name' => '', // 座位上用户昵称
  235. 'seatIndex' => -1, // 座位编号
  236. 'chorusSongCode' => '', // 是否合唱
  237. 'isAudioMuted' => 1, // 是否静音
  238. 'isVideoMuted' => 0, // 是否开启视频
  239. 'checked' => false, // 用于送礼物选择用户
  240. 'isUsed' => true, // 用于送礼物选择用户
  241. 'gender' => 1, //性别
  242. ];
  243. $seatdata['seat'.$i] = json_encode($seat);
  244. }
  245. $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$partyInfo['user_id'],$seatdata);
  246. //清空房间人,聊天室
  247. //分页获取聊天室成员列表
  248. $usernames = $easemob->room_listRoomMembers($easemob_room_id,0);
  249. //批量移除聊天室成员
  250. $user_ids = [];
  251. if(!empty($usernames)){
  252. foreach($usernames as $username){
  253. foreach($username as $key => $val)
  254. {
  255. $user_ids[] = $val;
  256. }
  257. }
  258. }
  259. if(!empty($user_ids)){
  260. $easemob->room_removeRoomMembers($easemob_room_id,$user_ids);
  261. }
  262. }
  263. return true;
  264. }
  265. }