Party.php 9.1 KB

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