Party.php 9.0 KB

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