123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- <?php
- namespace app\admin\controller\party;
- use app\common\controller\Backend;
- use think\Db;
- use Redis;
- use app\common\library\Easemob;
- /**
- * 派对管理
- *
- * @icon fa fa-circle-o
- */
- class Party extends Backend
- {
- protected $searchFields = 'user.u_id,user.nickname,party_id,party_name';
- /**
- * Party模型对象
- * @var \app\admin\model\party\Party
- */
- protected $model = null;
- protected $redis;
- public function _initialize()
- {
- parent::_initialize();
- $this->model = new \app\admin\model\party\Party;
- $this->view->assign("roomTypeList", $this->model->getRoomTypeList());
- $this->view->assign("isCoolList", $this->model->getIsCoolList());
- $this->view->assign("isOnlineList", $this->model->getIsOnlineList());
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
- $this->view->assign("isCloseList", $this->model->getIsCloseList());
- $this->view->assign("isScreenList", $this->model->getIsScreenList());
- $this->view->assign("onModelList", $this->model->getOnModelList());
- $this->view->assign("isPublicList", $this->model->getIsPublicList());
- //redis
- $redis = new Redis();
- $redisconfig = config("redis");
- $redis->connect($redisconfig["host"], $redisconfig["port"]);
- if ($redisconfig['redis_pwd']) {
- $redis->auth($redisconfig['redis_pwd']);
- }
- if($redisconfig['redis_selectdb'] > 0){
- $redis->select($redisconfig['redis_selectdb']);
- }
- $this->redis = $redis;
- }
- public function import()
- {
- parent::import();
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
-
- /**
- * 查看
- */
- public function index()
- {
- //当前是否为关联查询
- $this->relationSearch = true;
- //设置过滤方法
- $this->request->filter(['strip_tags', 'trim']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $list = $this->model
- ->with(['user'])
- ->where($where)
- ->order($sort, $order)
- ->paginate($limit);
- foreach ($list as $row) {
-
- $row->getRelation('user')->visible(['username']);
- }
- $result = array("total" => $list->total(), "rows" => $list->items());
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 编辑
- */
- public function edit($ids = null)
- {
- $row = $this->model->get($ids);
- if (!$row) {
- $this->error(__('No Results were found'));
- }
- $adminIds = $this->getDataLimitAdminIds();
- if (is_array($adminIds)) {
- if (!in_array($row[$this->dataLimitField], $adminIds)) {
- $this->error(__('You have no permission'));
- }
- }
- if ($this->request->isPost()) {
- $params = $this->request->post("row/a");
- if ($params) {
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- //判断房间id去重
- /*$check = Db::name('party')->where('party_id',$params['party_id'])->where('id','neq',$ids)->find();
- if($check){
- Db::rollback();
- $this->error('重复的派对id');
- }*/
- $roomTypeArr = [1=>"party",2=>"live"];
- $room_type = $row->room_type;
- $party_id = $row->id;
- if($party_id > 0) {
- // 存redis 房间信息
- $partyInfo = $this->redis->get($roomTypeArr[$room_type]."_".$party_id);
- if($partyInfo) {
- $partyInfo = json_decode($partyInfo,true);
- $partyInfo = array_replace($partyInfo,$params);
- $this->redis->set($roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
- }
- }
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- $result = $row->allowField(true)->save($params);
- Db::commit();
- if($params["status"] == 3) {
- //解散房间
- $this->closeParty($party_id,$room_type);
- }
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign("row", $row);
- return $this->view->fetch();
- }
- /**
- * 解散房间
- */
- public function closeParty($party_id,$room_type) {
- $partyInfo = \app\common\model\Party::where(["id"=>$party_id])->find();
- if(!$partyInfo) {
- $this->error(__('派对不存在!'));
- }
- //房间从首页列表消失
- //用户魅力榜,清空
- $this->redis->zRemRangeByRank("hourCharm_".$party_id, 0,-1);
- //用户贡献榜,清空
- $this->redis->zRemRangeByRank("hourWealth_".$party_id, 0,-1);
- //贡献榜前三,设置为空
- $this->redis->hSet("user_jewel_top3",$party_id,json_encode([]));
- //排麦列表,清空
- $this->redis->hSet("party_lineup",$party_id,serialize([]));
- //清空房间
- $user_ids = $this->redis->hGetAll("online_".$party_id);
- if(!empty($user_ids)){
- foreach($user_ids as $key => $userId){
- $this->redis->HDel("online_" . $party_id, $userId);
- $this->redis->zRem("party_user_".$party_id,$userId); //新加的
- $this->redis->hDel("livingUser", $userId);
- }
- }
- //环信操作
- $easemob_room_id = $partyInfo['easemob_room_id'];
- if(!empty($easemob_room_id)){
- ///初始化房间信息,重置自定义属性
- $matedata = [
- 'waitsing_list' => json_encode([]), //清空房间待唱列表
- 'online_user_num' => 0, //当前房间在线的人
- 'wealth_top3_userlist' => json_encode([]),//财富榜前3个人列表
- ];
- $easemob = new Easemob();
- $easemob->room_setRoomCustomAttributeForced($partyInfo['easemob_room_id'],$partyInfo['user_id'],$matedata);
- //重置麦位
- $seatnum = $partyInfo['seatnum'];
- $seatdata = [];
- for($i=0;$i<$seatnum;$i++){
- $seat = [
- 'charm' => 0, //红心,魅力值
- 'isMaster' => false, // 是否是房主
- 'headUrl' => '', // 头像
- 'userNo' => '', // 座位上用户no
- 'rtcUid' => '', // 座位上用户id,与rtc的userId一致
- 'name' => '', // 座位上用户昵称
- 'seatIndex' => -1, // 座位编号
- 'chorusSongCode' => '', // 是否合唱
- 'isAudioMuted' => 1, // 是否静音
- 'isVideoMuted' => 0, // 是否开启视频
- 'checked' => false, // 用于送礼物选择用户
- 'isUsed' => true, // 用于送礼物选择用户
- 'gender' => 1, //性别
- ];
- $seatdata['seat'.$i] = json_encode($seat);
- }
- $easemob->room_setRoomCustomAttributeForced($easemob_room_id,$partyInfo['user_id'],$seatdata);
- //清空房间人,聊天室
- //分页获取聊天室成员列表
- $usernames = $easemob->room_listRoomMembers($easemob_room_id,0);
- //批量移除聊天室成员
- $user_ids = [];
- if(!empty($usernames)){
- foreach($usernames as $username){
- foreach($username as $key => $val)
- {
- $user_ids[] = $val;
- }
- }
- }
- if(!empty($user_ids)){
- $easemob->room_removeRoomMembers($easemob_room_id,$user_ids);
- }
- }
- return true;
- }
- /**
- * 踢出房间内所有用户
- */
- private function outMemberFromRoom($easemob_room_id) {
- //$easemob = new Easemob();
- //获取所有成员
- //$rs = $easemob->room_listRoomMembers($easemob_room_id,0);
- //移除所有成员
- //$rs = $easemob->room_removeRoomMembers($easemob_room_id,$usernames);
- }
- }
|