Party.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace app\admin\controller\party;
  3. use app\common\controller\Backend;
  4. use tencentim\tencentim;
  5. use getusersig\getusersig;
  6. use think\Db;
  7. use Redis;
  8. /**
  9. * 派对管理
  10. *
  11. * @icon fa fa-circle-o
  12. */
  13. class Party extends Backend
  14. {
  15. protected $searchFields = 'user.u_id,user.nickname,party_id,party_name';
  16. /**
  17. * Party模型对象
  18. * @var \app\admin\model\party\Party
  19. */
  20. protected $model = null;
  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. }
  35. public function import()
  36. {
  37. parent::import();
  38. }
  39. /**
  40. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  41. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  42. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  43. */
  44. /**
  45. * 查看
  46. */
  47. public function index()
  48. {
  49. //当前是否为关联查询
  50. $this->relationSearch = true;
  51. //设置过滤方法
  52. $this->request->filter(['strip_tags', 'trim']);
  53. if ($this->request->isAjax()) {
  54. //如果发送的来源是Selectpage,则转发到Selectpage
  55. if ($this->request->request('keyField')) {
  56. return $this->selectpage();
  57. }
  58. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  59. $list = $this->model
  60. ->with(['user'])
  61. ->where($where)
  62. ->order($sort, $order)
  63. ->paginate($limit);
  64. foreach ($list as $row) {
  65. $row->getRelation('user')->visible(['username']);
  66. }
  67. $result = array("total" => $list->total(), "rows" => $list->items());
  68. return json($result);
  69. }
  70. return $this->view->fetch();
  71. }
  72. /**
  73. * 编辑
  74. */
  75. public function edit($ids = null)
  76. {
  77. $row = $this->model->get($ids);
  78. if (!$row) {
  79. $this->error(__('No Results were found'));
  80. }
  81. $adminIds = $this->getDataLimitAdminIds();
  82. if (is_array($adminIds)) {
  83. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  84. $this->error(__('You have no permission'));
  85. }
  86. }
  87. if ($this->request->isPost()) {
  88. $params = $this->request->post("row/a");
  89. if ($params) {
  90. $params = $this->preExcludeFields($params);
  91. $result = false;
  92. Db::startTrans();
  93. try {
  94. //判断房间id去重
  95. $check = Db::name('party')->where('party_id',$params['party_id'])->where('id','neq',$ids)->find();
  96. if($check){
  97. Db::rollback();
  98. $this->error('重复的派对id');
  99. }
  100. $roomTypeArr = [1=>"party",2=>"live"];
  101. $room_type = $row->room_type;
  102. $party_id = $row->id;
  103. if($party_id > 0) {
  104. // 存redis 房间信息
  105. $redis = new Redis();
  106. $redisconfig = config("redis");
  107. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  108. if ($redisconfig['redis_pwd']) {
  109. $redis->auth($redisconfig['redis_pwd']);
  110. }
  111. if($redisconfig['redis_selectdb'] > 0){
  112. $redis->select($redisconfig['redis_selectdb']);
  113. }
  114. $partyInfo = $redis->get($roomTypeArr[$room_type]."_".$party_id);
  115. if($partyInfo) {
  116. $partyInfo = json_decode($partyInfo,true);
  117. $partyInfo = array_replace($partyInfo,$params);
  118. $redis->set($roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  119. }
  120. }
  121. if($params["is_close"] == 1) {
  122. // 强制关闭需要退出正在房间的用户
  123. $this->outMemberFromRoom($ids);
  124. }
  125. //是否采用模型验证
  126. if ($this->modelValidate) {
  127. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  128. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  129. $row->validateFailException(true)->validate($validate);
  130. }
  131. $result = $row->allowField(true)->save($params);
  132. Db::commit();
  133. } catch (ValidateException $e) {
  134. Db::rollback();
  135. $this->error($e->getMessage());
  136. } catch (PDOException $e) {
  137. Db::rollback();
  138. $this->error($e->getMessage());
  139. } catch (Exception $e) {
  140. Db::rollback();
  141. $this->error($e->getMessage());
  142. }
  143. if ($result !== false) {
  144. $this->success();
  145. } else {
  146. $this->error(__('No rows were updated'));
  147. }
  148. }
  149. $this->error(__('Parameter %s can not be empty', ''));
  150. }
  151. $this->view->assign("row", $row);
  152. return $this->view->fetch();
  153. }
  154. /**
  155. * 踢出房间内所有用户
  156. */
  157. private function outMemberFromRoom($party_id) {
  158. if($party_id <= 0) return false;
  159. $message = [];
  160. $message["type"] = "91";
  161. $message["content"] = "房间已被管理员关闭!详情请联系客服!";
  162. $msgData = [];
  163. $msgData["version"] = "1.0";
  164. $msgData["action"] = 301;
  165. $msgData["command"] = "";
  166. $msgData["message"] = json_encode($message);
  167. $random = rand(10000000,99999999);
  168. $usersig = $this->usersig("administrator");
  169. // 获取配置信息
  170. $config = config("tencent_im");
  171. $url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";
  172. $url .= "?sdkappid=".$config["sdkappid"];
  173. $url .= "&identifier=administrator";
  174. $url .= "&usersig=".$usersig;
  175. $url .= "&random=".$random;
  176. $url .= "&contenttype=json";
  177. $tencentObj = new tencentim($url);
  178. $data = [];
  179. $data["GroupId"] = $party_id;
  180. $data["Random"] = rand(1000000,9999999);
  181. $data["MsgBody"][] = [
  182. "MsgType" => "TIMCustomElem",
  183. "MsgContent" => [
  184. "Data"=> json_encode($msgData)
  185. ],
  186. ];
  187. $tencentObj->toSend($data);
  188. }
  189. /**
  190. * 获取usersig签名-具体操作
  191. */
  192. private function usersig($user_id) {
  193. // 获取配置信息
  194. $config = config("tencent_im");
  195. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  196. $usersig = $usersigObj->genUserSig($user_id);
  197. return $usersig;
  198. }
  199. }