RoomService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace app\common\service;
  3. use think\Db;
  4. use think\Exception;
  5. use Redis;
  6. use app\common\service\TenimService;
  7. class RoomService
  8. {
  9. private $model = '';
  10. private $redis = '';
  11. private $roomTypeArr = [];
  12. /**
  13. * 初始化方法
  14. */
  15. public function __construct()
  16. {
  17. $this->model = new \app\common\model\Party();
  18. $redis = new Redis();
  19. $redisconfig = config("redis");
  20. $redis->connect($redisconfig["host"], $redisconfig["port"]);
  21. if ($redisconfig['redis_pwd']) {
  22. $redis->auth($redisconfig['redis_pwd']);
  23. }
  24. if($redisconfig['redis_selectdb'] > 0){
  25. $redis->select($redisconfig['redis_selectdb']);
  26. }
  27. $this->redis = $redis;
  28. $this->roomTypeArr = [1=>"party",2=>"live"];
  29. }
  30. /**
  31. * 房间设置缓存
  32. * @return void
  33. */
  34. public function roomRedis($params=[])
  35. {
  36. $result = [
  37. 'status' => 1,
  38. 'msg' => '',
  39. 'data' => [],
  40. ];
  41. try {
  42. $room_type = isset($params['room_type']) ? $params['room_type'] : 0;
  43. if (!empty($room_type)) {
  44. $userModel = new \app\common\model\User();
  45. // 直接从数据库获取所有数据
  46. $where = [];
  47. $where["a.status"] = 1;
  48. $where["a.is_online"] = 1;
  49. $where["a.room_type"] = $room_type;
  50. $sqlPartyList = $this->model->alias("a")->field("a.*,b.name as type_name")
  51. ->join("hx_party_type b","a.party_type = b.id","left")->where($where)->select();
  52. $userList = $userModel->field("id,nickname,avatar")->where(["status"=>"normal"])->select();
  53. $userInfoArr = [];
  54. if($userList) foreach($userList as $k => $v) $userInfoArr[$v["id"]] = $v;
  55. if($sqlPartyList) {
  56. foreach($sqlPartyList as $k => $v) {
  57. // 加入缓存排序
  58. $this->redis->zAdd($this->roomTypeArr[$room_type]."Rank", $v['party_hot'], $v["id"]);
  59. // 设置冠名
  60. $sqlPartyList[$k]["naming"] = isset($userInfoArr[$v["naming"]])?$userInfoArr[$v["naming"]]:[];
  61. // 设置房主头像
  62. $sqlPartyList[$k]["avatar"] = isset($userInfoArr[$v["user_id"]])?$userInfoArr[$v["user_id"]]["avatar"]:[];
  63. // 加入缓存
  64. $this->redis->set($this->roomTypeArr[$room_type]."_".$v["id"],json_encode($v));
  65. }
  66. $redisPartyRankList = $this->redis->zRevRange($this->roomTypeArr[$room_type]."Rank",0,-1,true);
  67. $result['data'] = $redisPartyRankList;
  68. }
  69. }
  70. } catch (Exception $e) {
  71. $result['status'] = 0;
  72. $result['msg'] = $e->getMessage();
  73. }
  74. return $result;
  75. }
  76. /**
  77. * 获取房间信息
  78. */
  79. public function getRoomList($params=[])
  80. {
  81. $result = [
  82. 'status' => 1,
  83. 'msg' => '',
  84. 'data' => [],
  85. ];
  86. try {
  87. $partyIds = isset($params['ids']) ? $params['ids'] : [];
  88. $typeId = isset($params['type_id']) ? $params['type_id'] : 0;
  89. $isRecommend = isset($params['is_recommend']) ? $params['is_recommend'] : 0;
  90. $all = isset($params['is_recommend']) ? $params['all'] : 0;
  91. $start = isset($params['start']) ? $params['start'] : 1;
  92. $end = isset($params['end']) ? $params['end'] : 1;
  93. $index = isset($params['index']) ? $params['index'] : 0;
  94. if(!$partyIds) throw new Exception('未获取到房间ID');;
  95. $redisPartyInfo = $resdata = [];
  96. $where['status'] = 1;
  97. //$where['is_online'] = 1;
  98. $where['is_close'] = 0;
  99. $where['id'] = ['in',$partyIds];
  100. $field = 'id,room_type,party_hot';
  101. $partyData = $this->model->field($field)->where($where)->order('party_hot desc')->select();
  102. if (!empty($partyData)) {
  103. //获取房间用户信息
  104. /*$tenimService = new TenimService();
  105. $userParams = ['party_ids' => $partyIds];
  106. $userImRes = $tenimService->getRoomUser($userParams);*/
  107. foreach($partyData as $k => $v) {
  108. $roomType = $v['room_type'];
  109. $roomTypeStr = isset($this->roomTypeArr[$roomType]) ? $this->roomTypeArr[$roomType] : '';
  110. $getredisPartyInfo = $this->redis->get($roomTypeStr.'_'.$v['id']);
  111. $foreachData = json_decode($getredisPartyInfo,true);
  112. $isClose = isset($foreachData["is_close"]) ? $foreachData["is_close"] : 0;
  113. $partyPass = isset($foreachData["party_pass"]) ? $foreachData["party_pass"] : '';
  114. if(!$foreachData || $isClose == 1) continue;
  115. //if(!$foreachData || $partyPass != '') continue;//设置密码的也显示
  116. $mod = isset($foreachData["party_type"])?intval($foreachData["party_type"])%5:1;
  117. if(isset($foreachData["type_name"]) && $foreachData["type_name"]) {
  118. $type_name = $foreachData["type_name"];
  119. } else {
  120. $type_name = "普通房";
  121. }
  122. $hasdata = false;
  123. if($all == 1) {
  124. if($isRecommend == 1) {
  125. if($foreachData["is_recommend"] == 1) $hasdata = true;
  126. } else {
  127. $hasdata = true;
  128. }
  129. } elseif($typeId > 0) {
  130. if($typeId == $foreachData["party_type"]) $hasdata = true;
  131. }
  132. if($foreachData["status"] != 1) $hasdata = false;
  133. $foreachData = info_domain_image($foreachData,['party_logo']);
  134. //$userIds = $this->redis->zRange("party_user_".$foreachData["id"],0,-1);
  135. if ($hasdata) {
  136. /*$field = 'user_id,avatar';
  137. $fieldArr = explode(',',$field);*/
  138. /*$userWhere['id'] = ['in', $userIds];
  139. $userModel = new \app\common\model\User();
  140. $userData = $userModel->field($field)->where($userWhere)->select();*/
  141. $userDataResult = $this->getPartyUserList(['party_id'=>$v['id']]);
  142. $userDataRes = isset($userDataResult['data']) ? $userDataResult['data'] : [];
  143. $userData = isset($userDataRes['member_list']) ? $userDataRes['member_list'] : [];
  144. $userDatas = [];
  145. if (!empty($userData)) {
  146. $userDatas = $userData;
  147. $userDatas = list_domain_image($userDatas,['avatar']);
  148. }
  149. $partyUserList = [
  150. 'user_list' => $userDatas,
  151. 'online_num' => count($userData),
  152. ];
  153. $redisPartyInfo[] = [
  154. "id" => $foreachData["id"],
  155. "room_type" => $v['room_type'],
  156. "party_id" => $foreachData["party_id"],
  157. "r_id" => $foreachData["party_id"],
  158. "party_name" => $foreachData["party_name"],
  159. "party_pass" => $foreachData["party_pass"] ? $foreachData["party_pass"]:"",
  160. "party_hot" => $v['party_hot'],
  161. "party_logo" => $foreachData["party_logo"],
  162. "party_type" => $type_name,
  163. "avatar" => $roomType == 2 ? $foreachData["avatar"]:"",
  164. "party_type_color" => $mod == 0?5:$mod,
  165. "party_user" => $partyUserList,
  166. ];
  167. }
  168. }
  169. if($index == 1) {
  170. $num = $end - $start + 1;
  171. $countData = count($redisPartyInfo);
  172. if($num > $countData) $num = $countData;
  173. if($redisPartyInfo) {
  174. $dataRand = array_rand($redisPartyInfo,$num);
  175. $dataRand = is_array($dataRand)?$dataRand:[$dataRand];
  176. if($redisPartyInfo) {
  177. foreach($redisPartyInfo as $k => $v) {
  178. if(in_array($k,$dataRand)) $resdata[] = $v;
  179. }
  180. }
  181. }
  182. } else {
  183. if($redisPartyInfo) {
  184. foreach($redisPartyInfo as $k => $v){
  185. if($k >= $start && $k <= $end) $resdata[] = $v;
  186. }
  187. }
  188. }
  189. }
  190. $result['data'] = $resdata;
  191. } catch (Exception $e) {
  192. $result['status'] = 0;
  193. $result['msg'] = $e->getMessage();
  194. }
  195. return $result;
  196. }
  197. /**
  198. * 房间用户信息
  199. * @return void
  200. */
  201. public function getPartyUserList($params=[])
  202. {
  203. $result = [
  204. 'status' => 1,
  205. 'msg' => '',
  206. 'data' => [],
  207. ];
  208. try {
  209. $partyId = isset($params['party_id']) ? $params['party_id'] : 0;
  210. $userIds = $this->redis->zRange("party_user_".$partyId,0,-1);
  211. $field = 'id,avatar,nickname';
  212. $userWhere['id'] = ['in', $userIds];
  213. $userWhere['is_stealth'] = 0;
  214. $userModel = new \app\common\model\User();
  215. $userData = $userModel->field($field)->where($userWhere)->select();
  216. $userDatas = [];
  217. if (!empty($userData)) {
  218. foreach($userData as $key => $value) {
  219. $userDatas[$key] = $value;
  220. }
  221. $userDatas = list_domain_image($userDatas,['avatar']);
  222. }
  223. $partyUserList = [
  224. 'member_list' => $userDatas,
  225. 'online_num' => !empty($userDatas) ? count($userDatas) : 0,
  226. ];
  227. $result['data'] = $partyUserList;
  228. } catch (Exception $e) {
  229. $result['status'] = 0;
  230. $result['msg'] = $e->getMessage();
  231. }
  232. return $result;
  233. }
  234. }