Party.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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("isOnlineList", $this->model->getIsOnlineList());
  27. $this->view->assign("statusList", $this->model->getStatusList());
  28. $this->view->assign("isRecommendList", $this->model->getIsRecommendList());
  29. $this->view->assign("isCloseList", $this->model->getIsCloseList());
  30. $this->view->assign("isScreenList", $this->model->getIsScreenList());
  31. $this->view->assign("onModelList", $this->model->getOnModelList());
  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(['u_id', 'nickname']);
  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. $roomTypeArr = [1=>"party",2=>"live"];
  93. $room_type = $row->room_type;
  94. $party_id = $row->id;
  95. if($party_id > 0) {
  96. // 存redis 房间信息
  97. $redis = new Redis();
  98. $redisconfig = config("redis");
  99. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  100. $partyInfo = $redis->get($roomTypeArr[$room_type]."_".$party_id);
  101. if($partyInfo) {
  102. $partyInfo = json_decode($partyInfo,true);
  103. $partyInfo = array_replace($partyInfo,$params);
  104. $redis->set($roomTypeArr[$room_type]."_".$party_id,json_encode($partyInfo));
  105. }
  106. }
  107. if($params["is_close"] == 1) {
  108. // 强制关闭需要退出正在房间的用户
  109. $this->outMemberFromRoom($ids);
  110. }
  111. //是否采用模型验证
  112. if ($this->modelValidate) {
  113. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  114. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  115. $row->validateFailException(true)->validate($validate);
  116. }
  117. $result = $row->allowField(true)->save($params);
  118. Db::commit();
  119. } catch (ValidateException $e) {
  120. Db::rollback();
  121. $this->error($e->getMessage());
  122. } catch (PDOException $e) {
  123. Db::rollback();
  124. $this->error($e->getMessage());
  125. } catch (Exception $e) {
  126. Db::rollback();
  127. $this->error($e->getMessage());
  128. }
  129. if ($result !== false) {
  130. $this->success();
  131. } else {
  132. $this->error(__('No rows were updated'));
  133. }
  134. }
  135. $this->error(__('Parameter %s can not be empty', ''));
  136. }
  137. $this->view->assign("row", $row);
  138. return $this->view->fetch();
  139. }
  140. /**
  141. * 踢出房间内所有用户
  142. */
  143. private function outMemberFromRoom($party_id) {
  144. if($party_id <= 0) return false;
  145. $message = [];
  146. $message["type"] = "91";
  147. $message["content"] = "房间已被管理员关闭!详情请联系客服!";
  148. $msgData = [];
  149. $msgData["version"] = "1.0";
  150. $msgData["action"] = 301;
  151. $msgData["command"] = "";
  152. $msgData["message"] = json_encode($message);
  153. $random = rand(10000000,99999999);
  154. $usersig = $this->usersig("administrator");
  155. // 获取配置信息
  156. $config = config("tencent_im");
  157. $url = "https://console.tim.qq.com/v4/group_open_http_svc/send_group_msg";
  158. $url .= "?sdkappid=".$config["sdkappid"];
  159. $url .= "&identifier=administrator";
  160. $url .= "&usersig=".$usersig;
  161. $url .= "&random=".$random;
  162. $url .= "&contenttype=json";
  163. $tencentObj = new tencentim($url);
  164. $data = [];
  165. $data["GroupId"] = $party_id;
  166. $data["Random"] = rand(1000000,9999999);
  167. $data["MsgBody"][] = [
  168. "MsgType" => "TIMCustomElem",
  169. "MsgContent" => [
  170. "Data"=> json_encode($msgData)
  171. ],
  172. ];
  173. $tencentObj->toSend($data);
  174. }
  175. /**
  176. * 获取usersig签名-具体操作
  177. */
  178. private function usersig($user_id) {
  179. // 获取配置信息
  180. $config = config("tencent_im");
  181. $usersigObj = new getusersig($config["sdkappid"],$config["key"]);
  182. $usersig = $usersigObj->genUserSig($user_id);
  183. return $usersig;
  184. }
  185. }