Party.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. namespace app\admin\controller\party;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use Redis;
  6. /**
  7. * 派对管理
  8. *
  9. * @icon fa fa-circle-o
  10. */
  11. class Party extends Backend
  12. {
  13. protected $searchFields = 'user.u_id,user.nickname,party_id,party_name';
  14. /**
  15. * Party模型对象
  16. * @var \app\admin\model\party\Party
  17. */
  18. protected $model = null;
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\party\Party;
  23. $this->view->assign("roomTypeList", $this->model->getRoomTypeList());
  24. $this->view->assign("isCoolList", $this->model->getIsCoolList());
  25. $this->view->assign("isOnlineList", $this->model->getIsOnlineList());
  26. $this->view->assign("statusList", $this->model->getStatusList());
  27. $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
  28. $this->view->assign("isCloseList", $this->model->getIsCloseList());
  29. $this->view->assign("isScreenList", $this->model->getIsScreenList());
  30. $this->view->assign("onModelList", $this->model->getOnModelList());
  31. $this->view->assign("isPublicList", $this->model->getIsPublicList());
  32. }
  33. public function import()
  34. {
  35. parent::import();
  36. }
  37. /**
  38. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  39. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  40. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  41. */
  42. /**
  43. * 查看
  44. */
  45. public function index()
  46. {
  47. //当前是否为关联查询
  48. $this->relationSearch = true;
  49. //设置过滤方法
  50. $this->request->filter(['strip_tags', 'trim']);
  51. if ($this->request->isAjax()) {
  52. //如果发送的来源是Selectpage,则转发到Selectpage
  53. if ($this->request->request('keyField')) {
  54. return $this->selectpage();
  55. }
  56. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  57. $list = $this->model
  58. ->with(['user'])
  59. ->where($where)
  60. ->order($sort, $order)
  61. ->paginate($limit);
  62. foreach ($list as $row) {
  63. $row->getRelation('user')->visible(['username']);
  64. }
  65. $result = array("total" => $list->total(), "rows" => $list->items());
  66. return json($result);
  67. }
  68. return $this->view->fetch();
  69. }
  70. /**
  71. * 编辑
  72. */
  73. public function edit($ids = null)
  74. {
  75. $row = $this->model->get($ids);
  76. if (!$row) {
  77. $this->error(__('No Results were found'));
  78. }
  79. $adminIds = $this->getDataLimitAdminIds();
  80. if (is_array($adminIds)) {
  81. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  82. $this->error(__('You have no permission'));
  83. }
  84. }
  85. if ($this->request->isPost()) {
  86. $params = $this->request->post("row/a");
  87. if ($params) {
  88. $params = $this->preExcludeFields($params);
  89. $result = false;
  90. Db::startTrans();
  91. try {
  92. //判断房间id去重
  93. $check = Db::name('party')->where('party_id',$params['party_id'])->where('id','neq',$ids)->find();
  94. if($check){
  95. Db::rollback();
  96. $this->error('重复的派对id');
  97. }
  98. $roomTypeArr = [1=>"party",2=>"live"];
  99. $room_type = $row->room_type;
  100. $party_id = $row->id;
  101. if($party_id > 0) {
  102. // 存redis 房间信息
  103. $redis = new Redis();
  104. $redisconfig = config("redis");
  105. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  106. if ($redisconfig['redis_pwd']) {
  107. $redis->auth($redisconfig['redis_pwd']);
  108. }
  109. if($redisconfig['redis_selectdb'] > 0){
  110. $redis->select($redisconfig['redis_selectdb']);
  111. }
  112. $partyInfo = $redis->get($roomTypeArr[$room_type]."_".$party_id);
  113. if($partyInfo) {
  114. $partyInfo = json_decode($partyInfo,true);
  115. $partyInfo = array_replace($partyInfo,$params);
  116. $redis->set($roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  117. }
  118. }
  119. if($params["is_close"] == 1) {
  120. // 强制关闭需要退出正在房间的用户
  121. $this->outMemberFromRoom($ids);
  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. } catch (ValidateException $e) {
  132. Db::rollback();
  133. $this->error($e->getMessage());
  134. } catch (PDOException $e) {
  135. Db::rollback();
  136. $this->error($e->getMessage());
  137. } catch (Exception $e) {
  138. Db::rollback();
  139. $this->error($e->getMessage());
  140. }
  141. if ($result !== false) {
  142. $this->success();
  143. } else {
  144. $this->error(__('No rows were updated'));
  145. }
  146. }
  147. $this->error(__('Parameter %s can not be empty', ''));
  148. }
  149. $this->view->assign("row", $row);
  150. return $this->view->fetch();
  151. }
  152. /**
  153. * 踢出房间内所有用户
  154. */
  155. private function outMemberFromRoom($party_id) {
  156. }
  157. }